-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
189 lines (183 loc) · 4.2 KB
/
db.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
package main
import (
"fmt"
"math/big"
)
type DataPoints map[uint64]interface{}
func (dp DataPoints) ToInt() (map[uint64]*big.Int, error) {
dataPoints := make(map[uint64]*big.Int)
for k, v := range dp {
bigIntVal, ok := v.(*big.Int)
if !ok {
return nil, fmt.Errorf("invalid data for bigInt: %v", v)
}
dataPoints[k] = bigIntVal
}
return dataPoints, nil
}
func (dp DataPoints) ToUint64() (map[uint64]uint64, error) {
dataPoints := make(map[uint64]uint64)
for k, v := range dp {
uint64Val, ok := v.(uint64)
if !ok {
return nil, fmt.Errorf("invalid data for uint64: %v", v)
}
dataPoints[k] = uint64Val
}
return dataPoints, nil
}
func (dp DataPoints) AggregateUint64(af AggregateFunction, aggregateFuncValue InputValue) (uint64, error) {
dataPoints, err := dp.ToUint64()
if err != nil {
return 0, err
}
aggregatedValue := uint64(0)
switch af {
case Count:
for _, v := range dataPoints {
if v > 0 {
aggregatedValue++
}
}
case CountEqual:
aggregateFuncValue, err := aggregateFuncValue.ToUint64()
if err != nil {
return 0, err
}
for _, v := range dataPoints {
if v == aggregateFuncValue {
aggregatedValue++
}
}
case CountUnequal:
aggregateFuncValue, err := aggregateFuncValue.ToUint64()
if err != nil {
return 0, err
}
for _, v := range dataPoints {
if v != aggregateFuncValue {
aggregatedValue++
}
}
case Percentage:
total := uint64(0)
for _, v := range dataPoints {
total++
if v > 0 {
aggregatedValue++
}
}
aggregatedValue = (aggregatedValue * 100) / total
case Average:
count := uint64(0)
for _, v := range dataPoints {
aggregatedValue += v
count++
}
if count > 0 {
aggregatedValue = aggregatedValue / count
}
case Sum:
for _, v := range dataPoints {
aggregatedValue += v
}
case Max:
for _, v := range dataPoints {
if v > aggregatedValue {
aggregatedValue = v
}
}
case Min:
aggregatedValue = uint64(0)
firstVal := true
for _, v := range dataPoints {
if v < aggregatedValue || firstVal {
aggregatedValue = v
firstVal = false
}
}
default:
return aggregatedValue, fmt.Errorf("invalid aggregate function for uint64: %s", af)
}
return aggregatedValue, nil
}
func (dp DataPoints) AggregateBigInt(af AggregateFunction, aggregateFuncValue InputValue) (*big.Int, error) {
dataPoints, err := dp.ToInt()
if err != nil {
return nil, err
}
aggregatedValue := big.NewInt(0)
switch af {
case Count:
for _, v := range dataPoints {
if v.Cmp(big.NewInt(0)) > 0 {
aggregatedValue = aggregatedValue.Add(aggregatedValue, big.NewInt(1))
}
}
case CountEqual:
aggregateFuncValue, err := aggregateFuncValue.ToBigInt()
if err != nil {
return nil, err
}
for _, v := range dataPoints {
if v.Cmp(aggregateFuncValue) == 0 {
aggregatedValue = aggregatedValue.Add(aggregatedValue, big.NewInt(1))
}
}
case CountUnequal:
aggregateFuncValue, err := aggregateFuncValue.ToBigInt()
if err != nil {
return nil, err
}
for _, v := range dataPoints {
if v.Cmp(aggregateFuncValue) != 0 {
aggregatedValue = aggregatedValue.Add(aggregatedValue, big.NewInt(1))
}
}
case Percentage:
total := int64(0)
aggregatedInt := int64(0)
for _, v := range dataPoints {
total++
if v.Cmp(big.NewInt(0)) != 0 {
aggregatedInt++
}
}
aggregatedValue = big.NewInt(100 * aggregatedInt)
aggregatedValue = aggregatedValue.Div(aggregatedValue, big.NewInt(total))
case Average:
count := int64(0)
for _, v := range dataPoints {
aggregatedValue = aggregatedValue.Add(aggregatedValue, v)
count++
}
if count > 0 {
aggregatedValue = aggregatedValue.Div(aggregatedValue, big.NewInt(count))
}
case Sum:
for _, v := range dataPoints {
aggregatedValue = aggregatedValue.Add(aggregatedValue, v)
}
case Min:
firstVal := true
for _, v := range dataPoints {
v := v
if firstVal || aggregatedValue.Cmp(v) > 0 {
aggregatedValue = v
firstVal = false
}
}
case Max:
firstVal := true
for _, v := range dataPoints {
v := v
if firstVal || aggregatedValue.Cmp(v) < 0 {
aggregatedValue = v
firstVal = false
}
}
default:
return nil, fmt.Errorf("invalid aggregate function for bigInt: %s", af)
}
return aggregatedValue, nil
}