-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
functions.go
583 lines (522 loc) · 12.4 KB
/
functions.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package influxql
// All aggregate and query functions are defined in this file along with any intermediate data objects they need to process.
// Query functions are represented as two discreet functions: Map and Reduce. These roughly follow the MapReduce
// paradigm popularized by Google and Hadoop.
//
// When adding an aggregate function, define a mapper, a reducer, and add them in the switch statement in the MapReduceFuncs function
import (
"encoding/json"
"fmt"
"math"
"sort"
"strings"
)
// Iterator represents a forward-only iterator over a set of points.
// These are used by the MapFunctions in this file
type Iterator interface {
Next() (seriesID uint64, timestamp int64, value interface{})
}
// MapFunc represents a function used for mapping over a sequential series of data.
// The iterator represents a single group by interval
type MapFunc func(Iterator) interface{}
// ReduceFunc represents a function used for reducing mapper output.
type ReduceFunc func([]interface{}) interface{}
// UnmarshalFunc represents a function that can take bytes from a mapper from remote
// server and marshal it into an interface the reduer can use
type UnmarshalFunc func([]byte) (interface{}, error)
// InitializeMapFunc takes an aggregate call from the query and returns the MapFunc
func InitializeMapFunc(c *Call) (MapFunc, error) {
// see if it's a query for raw data
if c == nil {
return MapRawQuery, nil
}
// Ensure that there is either a single argument or if for percentile, two
if c.Name == "percentile" {
if len(c.Args) != 2 {
return nil, fmt.Errorf("expected two arguments for percentile()")
}
} else if len(c.Args) != 1 {
return nil, fmt.Errorf("expected one argument for %s()", c.Name)
}
// Ensure the argument is a variable reference.
_, ok := c.Args[0].(*VarRef)
if !ok {
return nil, fmt.Errorf("expected field argument in %s()", c.Name)
}
// Retrieve map function by name.
switch strings.ToLower(c.Name) {
case "count":
return MapCount, nil
case "sum":
return MapSum, nil
case "mean":
return MapMean, nil
case "min":
return MapMin, nil
case "max":
return MapMax, nil
case "spread":
return MapSpread, nil
case "stddev":
return MapStddev, nil
case "first":
return MapFirst, nil
case "last":
return MapLast, nil
case "percentile":
_, ok := c.Args[1].(*NumberLiteral)
if !ok {
return nil, fmt.Errorf("expected float argument in percentile()")
}
return MapEcho, nil
default:
return nil, fmt.Errorf("function not found: %q", c.Name)
}
}
// InitializeReduceFunc takes an aggregate call from the query and returns the ReduceFunc
func InitializeReduceFunc(c *Call) (ReduceFunc, error) {
// Retrieve reduce function by name.
switch strings.ToLower(c.Name) {
case "count":
return ReduceSum, nil
case "sum":
return ReduceSum, nil
case "mean":
return ReduceMean, nil
case "min":
return ReduceMin, nil
case "max":
return ReduceMax, nil
case "spread":
return ReduceSpread, nil
case "stddev":
return ReduceStddev, nil
case "first":
return ReduceFirst, nil
case "last":
return ReduceLast, nil
case "percentile":
lit, ok := c.Args[1].(*NumberLiteral)
if !ok {
return nil, fmt.Errorf("expected float argument in percentile()")
}
return ReducePercentile(lit.Val), nil
default:
return nil, fmt.Errorf("function not found: %q", c.Name)
}
}
func InitializeUnmarshaller(c *Call) (UnmarshalFunc, error) {
// if c is nil it's a raw data query
if c == nil {
return func(b []byte) (interface{}, error) {
warn("MARSHAL OUTPUT: ", string(b))
a := make([]*rawQueryMapOutput, 0)
err := json.Unmarshal(b, &a)
return a, err
}, nil
}
// Retrieve marshal function by name
switch strings.ToLower(c.Name) {
case "mean":
return func(b []byte) (interface{}, error) {
var o meanMapOutput
err := json.Unmarshal(b, &o)
return &o, err
}, nil
case "spread":
return func(b []byte) (interface{}, error) {
var o spreadMapOutput
err := json.Unmarshal(b, &o)
return &o, err
}, nil
case "first":
return func(b []byte) (interface{}, error) {
var o firstLastMapOutput
err := json.Unmarshal(b, &o)
return &o, err
}, nil
case "last":
return func(b []byte) (interface{}, error) {
var o firstLastMapOutput
err := json.Unmarshal(b, &o)
return &o, err
}, nil
default:
return func(b []byte) (interface{}, error) {
var val interface{}
err := json.Unmarshal(b, &val)
return val, err
}, nil
}
}
// MapCount computes the number of values in an iterator.
func MapCount(itr Iterator) interface{} {
n := 0
for _, k, _ := itr.Next(); k != 0; _, k, _ = itr.Next() {
n++
}
return float64(n)
}
// MapSum computes the summation of values in an iterator.
func MapSum(itr Iterator) interface{} {
n := float64(0)
count := 0
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
count++
n += v.(float64)
}
if count > 0 {
return n
}
return nil
}
// ReduceSum computes the sum of values for each key.
func ReduceSum(values []interface{}) interface{} {
var n float64
count := 0
for _, v := range values {
if v == nil {
continue
}
count++
n += v.(float64)
}
if count > 0 {
return n
}
return nil
}
// MapMean computes the count and sum of values in an iterator to be combined by the reducer.
func MapMean(itr Iterator) interface{} {
out := &meanMapOutput{}
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
out.Count++
out.Sum += v.(float64)
}
return out
}
type meanMapOutput struct {
Count int
Sum float64
}
// ReduceMean computes the mean of values for each key.
func ReduceMean(values []interface{}) interface{} {
out := &meanMapOutput{}
for _, v := range values {
if v == nil {
continue
}
val := v.(*meanMapOutput)
out.Count += val.Count
out.Sum += val.Sum
}
if out.Count > 0 {
return out.Sum / float64(out.Count)
}
return nil
}
// MapMin collects the values to pass to the reducer
func MapMin(itr Iterator) interface{} {
var min float64
pointsYielded := false
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
val := v.(float64)
// Initialize min
if !pointsYielded {
min = val
pointsYielded = true
}
min = math.Min(min, val)
}
if pointsYielded {
return min
}
return nil
}
// ReduceMin computes the min of value.
func ReduceMin(values []interface{}) interface{} {
var min float64
pointsYielded := false
for _, v := range values {
if v == nil {
continue
}
val := v.(float64)
// Initialize min
if !pointsYielded {
min = val
pointsYielded = true
}
m := math.Min(min, val)
min = m
}
if pointsYielded {
return min
}
return nil
}
// MapMax collects the values to pass to the reducer
func MapMax(itr Iterator) interface{} {
var max float64
pointsYielded := false
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
val := v.(float64)
// Initialize max
if !pointsYielded {
max = val
pointsYielded = true
}
max = math.Max(max, val)
}
if pointsYielded {
return max
}
return nil
}
// ReduceMax computes the max of value.
func ReduceMax(values []interface{}) interface{} {
var max float64
pointsYielded := false
for _, v := range values {
if v == nil {
continue
}
val := v.(float64)
// Initialize max
if !pointsYielded {
max = val
pointsYielded = true
}
max = math.Max(max, val)
}
if pointsYielded {
return max
}
return nil
}
type spreadMapOutput struct {
Min, Max float64
}
// MapSpread collects the values to pass to the reducer
func MapSpread(itr Iterator) interface{} {
var out spreadMapOutput
pointsYielded := false
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
val := v.(float64)
// Initialize
if !pointsYielded {
out.Max = val
out.Min = val
pointsYielded = true
}
out.Max = math.Max(out.Max, val)
out.Min = math.Min(out.Min, val)
}
if pointsYielded {
return out
}
return nil
}
// ReduceSpread computes the spread of values.
func ReduceSpread(values []interface{}) interface{} {
var result spreadMapOutput
pointsYielded := false
for _, v := range values {
if v == nil {
continue
}
val := v.(spreadMapOutput)
// Initialize
if !pointsYielded {
result.Max = val.Max
result.Min = val.Min
pointsYielded = true
}
result.Max = math.Max(result.Max, val.Max)
result.Min = math.Min(result.Min, val.Min)
}
if pointsYielded {
return result.Max - result.Min
}
return nil
}
// MapStddev collects the values to pass to the reducer
func MapStddev(itr Iterator) interface{} {
var values []float64
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
values = append(values, v.(float64))
}
return nil
}
// ReduceStddev computes the stddev of values.
func ReduceStddev(values []interface{}) interface{} {
var data []float64
// Collect all the data points
for _, value := range values {
if value == nil {
continue
}
data = append(data, value.([]float64)...)
}
// If no data or we only have one point, it's nil or undefined
if len(data) < 2 {
return nil
}
// Get the sum
var sum float64
for _, v := range data {
sum += v
}
// Get the mean
mean := sum / float64(len(data))
// Get the variance
var variance float64
for _, v := range data {
dif := v - mean
sq := math.Pow(dif, 2)
variance += sq
}
variance = variance / float64(len(data)-1)
stddev := math.Sqrt(variance)
return stddev
}
type firstLastMapOutput struct {
Time int64
Val interface{}
}
// MapFirst collects the values to pass to the reducer
func MapFirst(itr Iterator) interface{} {
out := firstLastMapOutput{}
pointsYielded := false
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
// Initialize first
if !pointsYielded {
out.Time = k
out.Val = v
pointsYielded = true
}
if k < out.Time {
out.Time = k
out.Val = v
}
}
if pointsYielded {
return out
}
return nil
}
// ReduceFirst computes the first of value.
func ReduceFirst(values []interface{}) interface{} {
out := firstLastMapOutput{}
pointsYielded := false
for _, v := range values {
if v == nil {
continue
}
val := v.(firstLastMapOutput)
// Initialize first
if !pointsYielded {
out.Time = val.Time
out.Val = val.Val
pointsYielded = true
}
if val.Time < out.Time {
out.Time = val.Time
out.Val = val.Val
}
}
if pointsYielded {
return out.Val
}
return nil
}
// MapLast collects the values to pass to the reducer
func MapLast(itr Iterator) interface{} {
out := firstLastMapOutput{}
pointsYielded := false
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
// Initialize last
if !pointsYielded {
out.Time = k
out.Val = v
pointsYielded = true
}
if k > out.Time {
out.Time = k
out.Val = v
}
}
if pointsYielded {
return out
}
return nil
}
// ReduceLast computes the last of value.
func ReduceLast(values []interface{}) interface{} {
out := firstLastMapOutput{}
pointsYielded := false
for _, v := range values {
if v == nil {
continue
}
val := v.(firstLastMapOutput)
// Initialize last
if !pointsYielded {
out.Time = val.Time
out.Val = val.Val
pointsYielded = true
}
if val.Time > out.Time {
out.Time = val.Time
out.Val = val.Val
}
}
if pointsYielded {
return out.Val
}
return nil
}
// MapEcho emits the data points for each group by interval
func MapEcho(itr Iterator) interface{} {
var values []interface{}
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
values = append(values, v)
}
return values
}
// ReducePercentile computes the percentile of values for each key.
func ReducePercentile(percentile float64) ReduceFunc {
return func(values []interface{}) interface{} {
var allValues []float64
for _, v := range values {
vals := v.([]interface{})
for _, v := range vals {
allValues = append(allValues, v.(float64))
}
}
sort.Float64s(allValues)
length := len(allValues)
index := int(math.Floor(float64(length)*percentile/100.0+0.5)) - 1
if index < 0 || index >= len(allValues) {
return nil
}
return allValues[index]
}
}
// MapRawQuery is for queries without aggregates
func MapRawQuery(itr Iterator) interface{} {
var values []*rawQueryMapOutput
for _, k, v := itr.Next(); k != 0; _, k, v = itr.Next() {
val := &rawQueryMapOutput{k, v}
values = append(values, val)
}
return values
}
type rawQueryMapOutput struct {
Timestamp int64
Values interface{}
}
type rawOutputs []*rawQueryMapOutput
func (a rawOutputs) Len() int { return len(a) }
func (a rawOutputs) Less(i, j int) bool { return a[i].Timestamp < a[j].Timestamp }
func (a rawOutputs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }