summaryrefslogtreecommitdiff
path: root/flow-cleaner.go
blob: 71c11966df33296f0a92358292fb35eb8937212a (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
74
75
76
77
78
package main

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

var (
	logger *log.Logger
)

func init() {
	logger = log.New(os.Stdout, "[ Main ]", log.LstdFlags)
}

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

	switch cfg.DataSource {
	case "stdin":
		processFromStdin(cfg)
	case "mysq":
		processFromDB(cfg)
	default:
		logger.Println("Invalid dataSource in config. Needs to be either 'stdin' or 'mysql'.")
	}

	logger.Println("Finished processing, now exiting")
}

func processFromStdin(cfg *Config) {
	logger.Println("Starting to process from stdin...")
	input := readFromStdin()
	rDatChan := parseRawData(input, cfg)
	cleanFromStdin(rDatChan, cfg)
}

func processFromDB(cfg *Config) {
	logger.Print("Cleaning data...")
	starttime := time.Now()
	numOfRowsNotCleaned, err := cleanFromDB(cfg)
	if err != nil {
		logger.Println(err)
		logger.Println("Exiting...")
		return
	}
	logger.Println("Done!")

	// 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 || cfg.Limit == 0) && cfg.Epsilon >= 0 {
		logger.Println("Adding differential privacy noise to processed data...")
		db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
		if err != nil {
			logger.Println("Failed to connect to db:", err)
			return
		}
		defer db.Close()

		ival, err := cfg.getInterval()
		if err != nil {
			logger.Println("erronous interval in conf prevents the privatization of data:", err)
			return
		}

		err = privatizeCleaned(db, starttime.Add(-2*ival), cfg)
		if err != nil {
			logger.Println("Failed to privatize data:", err)
		}
		logger.Println("Done!")
	}
}