-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregator.go
More file actions
171 lines (146 loc) · 2.78 KB
/
aggregator.go
File metadata and controls
171 lines (146 loc) · 2.78 KB
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
package gofeat
// Aggregator computes a value from a sequence of inputs.
type Aggregator interface {
Add(e Event)
Result() any
}
// AggregatorFactory creates new Aggregator instances.
type AggregatorFactory = func() Aggregator
// Count counts the number of events.
func Count() Aggregator { return &countAgg{} }
type countAgg struct{ n int }
func (a *countAgg) Add(Event) { a.n++ }
func (a *countAgg) Result() any { return a.n }
// Sum computes the sum of float64 values.
func Sum(field string) AggregatorFactory {
return func() Aggregator {
return &sumAgg{field: field}
}
}
type sumAgg struct {
sum float64
field string
}
func (a *sumAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
if f, okF := toFloat64(v); okF {
a.sum += f
}
}
func (a *sumAgg) Result() any { return a.sum }
// Min computes the minimum float64 value.
func Min(field string) AggregatorFactory {
return func() Aggregator {
return &minAgg{field: field}
}
}
type minAgg struct {
min float64
valid bool
field string
}
func (a *minAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
f, ok := toFloat64(v)
if !ok {
return
}
if !a.valid || f < a.min {
a.min = f
a.valid = true
}
}
func (a *minAgg) Result() any {
if !a.valid {
return 0.0
}
return a.min
}
// Max computes the maximum float64 value.
func Max(field string) AggregatorFactory {
return func() Aggregator {
return &maxAgg{field: field}
}
}
type maxAgg struct {
max float64
valid bool
field string
}
func (a *maxAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
f, ok := toFloat64(v)
if !ok {
return
}
if !a.valid || f > a.max {
a.max = f
a.valid = true
}
}
func (a *maxAgg) Result() any {
if !a.valid {
return 0.0
}
return a.max
}
// Last returns the last non-nil value.
func Last(field string) AggregatorFactory {
return func() Aggregator {
return &lastAgg{field: field}
}
}
type lastAgg struct {
last any
field string
}
func (a *lastAgg) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
a.last = v
}
func (a *lastAgg) Result() any { return a.last }
// DistinctCount counts unique values.
func DistinctCount(field string) AggregatorFactory {
return func() Aggregator {
return &distinctCount{field: field, seen: make(map[any]struct{})}
}
}
type distinctCount struct {
seen map[any]struct{}
field string
}
func (a *distinctCount) Add(e Event) {
v, ok := e.Data[a.field]
if !ok {
return
}
a.seen[v] = struct{}{}
}
func (a *distinctCount) Result() any { return len(a.seen) }
func toFloat64(v any) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case float32:
return float64(n), true
case int:
return float64(n), true
case int64:
return float64(n), true
case int32:
return float64(n), true
}
return 0, false
}