/
stats.go
270 lines (239 loc) · 5.2 KB
/
stats.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package dbutils
import (
"context"
"fmt"
"time"
"github.com/tierklinik-dobersberg/cis/internal/stats"
"github.com/tierklinik-dobersberg/cis/pkg/httperr"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var DateMongoFormat = map[string]string{
"hour": "%Y-%m-%d %H",
"day": "%Y-%m-%d",
"month": "%Y-%m",
"week": "%V",
"year": "%Y",
}
var DateTimeFormat = map[string]string{
"hour": "2006-01-02 15",
"day": "2006-01-02",
"month": "2006-01",
"week": "",
"year": "2006",
}
type Stats struct {
ValidGroupBys map[string]bson.M
ValidTimeKeys map[string]struct{}
ValidCounts map[string]bson.M
Collection *mongo.Collection
}
func (s *Stats) SimpleCount(ctx context.Context, counterKey string) (int, error) {
counter, err := s.getCounter(counterKey)
if err != nil {
return 0, err
}
projection := bson.M{
"count": bson.M{
"$cond": bson.M{
"if": bson.M{"$isArray": "$count"},
"then": bson.M{"$size": "$count"},
"else": "$count",
},
},
}
res, err := s.Collection.Aggregate(ctx, mongo.Pipeline{
// group stage
bson.D{
{Key: "$group", Value: bson.M{
"_id": nil,
"count": counter,
}},
},
// projection state
bson.D{
{Key: "$project", Value: projection},
},
})
if err != nil {
return 0, err
}
var r []struct {
Count int `bson:"count"`
}
if err := res.All(ctx, &r); err != nil {
return 0, err
}
if len(r) != 1 {
return 0, fmt.Errorf("expected 1 result but got %d", len(r))
}
return r[0].Count, nil
}
func (s *Stats) SimpleGroupByOverTime(ctx context.Context, from, to time.Time, timeRange string, groupByKey, timeKey, counterKey string) ([]stats.TimeSeries, error) {
if _, found := s.ValidTimeKeys[timeKey]; !found {
return nil, httperr.BadRequest("invalid time key")
}
counter, err := s.getCounter(counterKey)
if err != nil {
return nil, err
}
mongoFormat, ok := DateMongoFormat[timeRange]
if !ok {
return nil, httperr.BadRequest("timeRange must be daily, monthly or yearly")
}
goFormat, _ := DateTimeFormat[timeRange]
groupByFields := bson.M{
"date": bson.M{
"$dateToString": bson.M{
"date": "$" + timeKey,
"format": mongoFormat,
},
},
}
if groupByKey != "" {
grpBy, err := s.getGroupBy(groupByKey)
if err != nil {
return nil, err
}
groupByFields["group"] = grpBy
}
groupByClause := bson.M{
"_id": groupByFields,
"count": counter,
}
projection := bson.M{
"count": bson.M{
"$cond": bson.M{
"if": bson.M{"$isArray": "$count"},
"then": bson.M{"$size": "$count"},
"else": "$count",
},
},
}
res, err := s.Collection.Aggregate(ctx, mongo.Pipeline{
// matching stage
bson.D{
{Key: "$match", Value: bson.M{
timeKey: bson.M{
"$gte": from,
"$lte": to,
},
}},
},
// group stage
bson.D{
{Key: "$group", Value: groupByClause},
},
// projection state
bson.D{
{Key: "$project", Value: projection},
},
// sort stage
bson.D{
{Key: "$sort", Value: bson.D{
{Key: "_id.date", Value: 1},
}},
},
})
if err != nil {
return nil, err
}
var r []struct {
ID struct {
Date string `bson:"date"`
Group string `bson:"group"`
} `bson:"_id"`
Count int `bson:"count"`
}
if err := res.All(ctx, &r); err != nil {
return nil, err
}
lm := make(map[string][]stats.TimedValue)
for _, rec := range r {
d, _ := time.ParseInLocation(goFormat, rec.ID.Date, time.UTC)
lm[rec.ID.Group] = append(lm[rec.ID.Group], stats.TimedValue{
Value: rec.Count,
Time: d,
Name: rec.ID.Date,
})
}
var result []stats.TimeSeries
for id, val := range lm {
result = append(result, stats.TimeSeries{
ID: id,
Label: id,
Data: val,
})
}
return result, nil
}
func (s *Stats) SimpleGroupBy(ctx context.Context, groupByKey string, counterKey string) ([]stats.Group, error) {
grpBy, err := s.getGroupBy(groupByKey)
if err != nil {
return nil, err
}
counter, err := s.getCounter(counterKey)
if err != nil {
return nil, err
}
res, err := s.Collection.Aggregate(ctx, mongo.Pipeline{
// group stage
bson.D{
{Key: "$group", Value: bson.M{
"_id": grpBy,
"count": counter,
}},
},
// sort stage by _id
// FIXME(ppacher): whis will likely fail on _ids that are
// objects.
bson.D{
{Key: "$sort", Value: bson.D{
{Key: "_id", Value: 1},
}},
},
})
if err != nil {
return nil, err
}
var result []struct {
ID string `bson:"_id"`
Count int `bson:"count"`
}
if err := res.All(ctx, &result); err != nil {
return nil, err
}
var groups []stats.Group
for _, r := range result {
groups = append(groups, stats.Group{
ID: r.ID,
Label: r.ID,
Count: r.Count,
})
}
return groups, nil
}
func (s *Stats) getGroupBy(groupByKey string) (bson.M, error) {
grpBy, found := s.ValidGroupBys[groupByKey]
if !found {
return nil, httperr.InvalidParameter("group-by", groupByKey)
}
if grpBy == nil {
grpBy = bson.M{
"$toString": "$" + groupByKey,
}
}
return grpBy, nil
}
func (s *Stats) getCounter(counterKey string) (bson.M, error) {
if counterKey == "" || counterKey == "null" {
return bson.M{
"$sum": 1,
}, nil
}
b, ok := s.ValidCounts[counterKey]
if !ok {
return nil, httperr.InvalidParameter("counter", counterKey)
}
return b, nil
}