-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (93 loc) · 2.91 KB
/
main.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
// -----------------------------------------------------------------------------
// Imports
// -----------------------------------------------------------------------------
import (
"flag"
"log"
"os"
"path/filepath"
"time"
)
// -----------------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------------
type Report map[int]ReportRow
type ReportRow map[string]int
type Transaction struct {
UserId int `json:"user_id"`
Amount int `json:"amount"`
Category string `json:"category"`
}
func (self Report) Update(transaction Transaction) {
if self[transaction.UserId] == nil {
self[transaction.UserId] = make(ReportRow)
}
self[transaction.UserId]["sum"] += transaction.Amount
self[transaction.UserId]["user_id"] = transaction.UserId
self[transaction.UserId]["category_"+transaction.Category] += transaction.Amount
}
// -----------------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------------
var start = time.Now()
var helpArg = flag.Bool("h", false, "Print help and exit")
var algoArg = flag.String("a", "naive", "Algorithm to use: naive, stream")
var inputArg = flag.String("i", "input/10K_transactions.json", "Path to the input file")
var outputArg = flag.String("o", "output/report.json", "Path to the output file")
var formatArg = flag.String("f", "json", "Report format: json, csv, sqlite")
var quietArg = flag.Bool("q", false, "Disable logs output")
func Check(err error) {
if err != nil {
log.Fatal(err)
}
}
func Checkpoint(message string) {
if *quietArg == false {
log.Println(time.Since(start).String() + " " + message)
}
}
func ResolvePath(path string) (string, error) {
if filepath.IsAbs(path) {
return path, nil
}
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Join(cwd, path), nil
}
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
func main() {
flag.Parse()
if (*inputArg == "") || (*outputArg == "") || (*formatArg == "") || (*helpArg == true) {
flag.PrintDefaults()
if *helpArg == true {
os.Exit(0)
} else {
os.Exit(1)
}
}
Checkpoint("program started")
inputPath, err := ResolvePath(*inputArg)
Check(err)
inputFile, err := os.Open(inputPath)
Check(err)
defer inputFile.Close()
Checkpoint("input opened")
generator, err := CreateReportGenerator(*algoArg)
Check(err)
report, err := generator.Generate(inputFile)
Check(err)
Checkpoint("report generated")
writer, err := CreateReportWriter(*formatArg)
Check(err)
outputPath, err := ResolvePath(*outputArg)
Check(err)
Check(os.MkdirAll(filepath.Dir(outputPath), 0755))
Check(writer.Write(report, outputPath))
Checkpoint("report persisted")
Checkpoint("program finished")
}