-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.go
161 lines (141 loc) · 4.27 KB
/
statistics.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package buna
import (
"context"
"errors"
"fmt"
)
func getAverageBrewingRating(ctx context.Context, db DB) error {
fmt.Println("Getting average brewing rating (Enter # to quit):")
fmt.Print("Add filters (true or false): ")
showOptionalOptions, quit := validateBoolInput(quitStr, true)
if quit {
fmt.Println(quitMsg)
return nil
}
var (
brewingMethodName, v60FilterType, coffeeName, coffeeRoaster, grinderName string
err error
)
if showOptionalOptions {
brewingMethodName, quit, err = getBrewingMethodNameWithSuggestions(ctx, db, quitStr, true)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get brewing method name: %w", err)
}
if quit {
fmt.Println(quitMsg)
return nil
}
if brewingMethodName == "v60" || brewingMethodName == "V60" {
v60FilterType, quit = getV60FilterTypeWithSuggestions(quitStr)
if quit {
fmt.Println(quitMsg)
return nil
}
}
coffeeName, quit, err = getCoffeeNameWithSuggestions(ctx, db, quitStr, true)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get coffee name: %w", err)
}
if quit {
fmt.Println(quitMsg)
return nil
}
if coffeeName != "" {
coffeeRoaster, quit, err = getCoffeeRoasterWithSuggestions(ctx, db, quitStr, coffeeName)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get coffee roaster: %w", err)
}
if quit {
fmt.Println(quitMsg)
return nil
}
}
grinderName, quit, err = getCoffeeGrinderNameWithSuggestions(ctx, db, quitStr, true)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get coffee grinder name: %w", err)
}
if quit {
fmt.Println(quitMsg)
return nil
}
}
brewingFilter := brewing{
date: "",
coffeeName: coffeeName,
coffeeRoaster: coffeeRoaster,
brewingMethodName: brewingMethodName,
roastDate: "",
grinderName: grinderName,
grindSetting: 0,
totalBrewingTimeSec: 0,
coffeeGrams: 0,
waterGrams: 0,
v60FilterType: v60FilterType,
rating: 0,
recommendedGrindSettingAdjustment: "",
recommendedCoffeeWeightAdjustmentGrams: 0,
notes: "",
}
averageRating, err := db.getAverageBrewingRating(ctx, brewingFilter)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get the average brewing rating: %w", err)
}
if averageRating == 0 {
fmt.Println("No brewings exist")
return nil
}
fmt.Printf("The average brewing rating is %.1f/10\n", averageRating)
return nil
}
func getTotalCountInDB(ctx context.Context, db DB) error {
options := map[int]string{
0: "Total brewings count",
1: "Total coffees count",
2: "Total cuppings count",
3: "Total coffee purchases count",
4: "Total brewing methods count",
5: "Total coffee grinders count",
}
fmt.Println("Getting total count (Enter # to quit):")
if err := displayIntOptions(options); err != nil {
return fmt.Errorf("buna: statistics: failed to display int options: %w", err)
}
selection, quit, err := getIntSelection(options, quitStr)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get int selection: %w", err)
}
if quit {
fmt.Println(quitMsg)
return nil
}
entity, err := getDBEntityFromSelection(ctx, selection)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get the dbEntity selection: %w", err)
}
count, err := db.getTotalCount(ctx, entity)
if err != nil {
return fmt.Errorf("buna: statistics: failed to get the total count: %w", err)
}
fmt.Println("There are", count, dbEntityToName[entity], "in total")
return nil
}
func getDBEntityFromSelection(ctx context.Context, selection int) (dbEntity, error) {
var entity dbEntity
switch selection {
case 0:
entity = brewings
case 1:
entity = coffees
case 2:
entity = cuppings
case 3:
entity = coffeePurchases
case 4:
entity = brewingMethods
case 5:
entity = grinders
default:
return 0, errors.New("buna: statistics: invalid dbEntity selection")
}
return entity, nil
}