-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (72 loc) · 2 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
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
"time"
"github.com/jszwec/csvutil"
)
type Order struct {
Date OrderDate `csv:"date"`
Cutlery int `csv:"cutlery"`
Tips int `csv:"tips"`
Price int `csv:"order_price"`
UserID int `csv:"uid"`
ID int `csv:"order_id"`
}
type OrderDate struct {
time.Time
}
const format = "2006-01-02 15:04:05"
func (t *OrderDate) UnmarshalCSV(data []byte) error {
tt, err := time.Parse(format, string(data))
if err != nil {
return err
}
*t = OrderDate{Time: tt}
return nil
}
const (
shmyaSourceCSV = "shmya_final_version.csv"
shmyaOutputCSV = "shmya_users_segment.csv"
)
func main() {
res, err := os.ReadFile(shmyaSourceCSV)
if err != nil {
log.Fatalf("failed to read csv source file, %v", err)
}
var orders []Order
if err := csvutil.Unmarshal(res, &orders); err != nil {
log.Fatalf("failed to unmarshal orders to struct, %v", err)
}
if isGreater := tipsHypothesisForHolidays(orders); isGreater {
log.Print("users with more cutlery (>2) leave more tips on holidays\n\n")
} else {
log.Print("users with fewer cutlery (<=2) leave more tips on holidays\n\n")
}
if isGreater := tipsHypothesisForAnotherDays(orders); isGreater {
log.Print("users with more cutlery (>2) leave more tips on another days\n\n")
} else {
log.Print("users with fewer cutlery (<=2) leave more tips on another days\n\n")
}
usersIDs := usersSegment(orders)
f, err := os.Create(shmyaOutputCSV)
if err != nil {
log.Fatalf("failed to create output file, %v", err)
}
w := csv.NewWriter(f)
w.Write([]string{"uid"})
for _, userID := range usersIDs {
line := []string{strconv.Itoa(userID)}
if err := w.Write(line); err != nil {
log.Printf("can't write line to output file, userID = %d\n", userID)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatalf("failed to write buffered data, %v", err)
}
log.Printf("users segment saved to %s\n", shmyaOutputCSV)
log.Printf("users segment count = %d", len(usersIDs))
}