summaryrefslogtreecommitdiff
path: root/main.go
blob: 027867a33faacc3a7caf3616f8cd56dd1484fa1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
	"bufio"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"os"
	"time"
	//"strings"
)

const (
	DATABASE_USER       = "flowcleaner"
	DATABASE_PASS       = "nil"
	DATABASE_CONNECTION = "" //e.g. "tcp(localhost:55555)
	DATABASE_NAME       = "pmacct"
)

func main() {
	conf, err := readConfig()
	if err != nil {
		log.Println("Could not read config")
		return
	}

	/*
		stdin := readFromStdin()
		for line := range stdin {
			strs := strings.Split(line, ",")
			for _, str := range strs {
				log.Println(str)
			}
		}
	*/
	starttime := time.Now()
	numOfRowsNotCleaned, err := cleanData(conf, DATABASE_USER, DATABASE_PASS, DATABASE_CONNECTION, DATABASE_NAME)
	if err != nil {
		log.Println(err)
	}

	// If  either all rows are processed or if there is no limit for the processing
	// we can safely add noise to the cleaned data
	if numOfRowsNotCleaned == 0 || conf.Limit == 0 {
		db, err := sql.Open("mysql", DATABASE_USER+":"+DATABASE_PASS+"@"+DATABASE_CONNECTION+"/"+DATABASE_NAME)
		if err != nil {
			log.Println("Failed to connect to db")
			return
		}
		defer db.Close()
		ival, err := conf.getInterval()
		if err != nil {
			log.Println("erronous interval in conf prevents the privatization of data")
			return
		}
		privatizeCleaned(db, starttime.Add(-2*ival), conf)
	}
}

//Starts a process that reads from stdin and
//puts the strings read on the returned channel
func readFromStdin() <-chan string {

	out := make(chan string)
	go func() {
		scanner := bufio.NewScanner(os.Stdin)
		for scanner.Scan() {
			out <- scanner.Text()
		}
		close(out)
	}()
	return out
}