forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
influxql.go
500 lines (464 loc) · 17.5 KB
/
influxql.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
package pipeline
import (
"time"
"github.com/influxdata/influxdb/influxql"
)
// tmpl -- go get github.com/benbjohnson/tmpl
//go:generate tmpl -data=@../tmpldata.json influxql.gen.go.tmpl
// An InfluxQLNode performs the available function from the InfluxQL language.
// These function can be performed on a stream or batch edge.
// The resulting edge is dependent on the function.
// For a stream edge, all points with the same time are accumulated into the function.
// For a batch edge, all points in the batch are accumulated into the function.
//
//
// Example:
// stream
// |window()
// .period(10s)
// .every(10s)
// // Sum the values for each 10s window of data.
// |sum('value')
//
//
// Note: Derivative has its own implementation as a DerivativeNode instead of as part of the
// InfluxQL functions.
type InfluxQLNode struct {
chainnode
// tick:ignore
Method string
// tick:ignore
Field string
// The name of the field, defaults to the name of
// function used (i.e. .mean -> 'mean')
As string
// tick:ignore
ReduceCreater ReduceCreater
// tick:ignore
PointTimes bool `tick:"UsePointTimes"`
}
func newInfluxQLNode(method, field string, wants, provides EdgeType, reducer ReduceCreater) *InfluxQLNode {
return &InfluxQLNode{
chainnode: newBasicChainNode(method, wants, provides),
Method: method,
Field: field,
As: method,
ReduceCreater: reducer,
}
}
// Use the time of the selected point instead of the time of the batch.
//
// Only applies to selector functions like first, last, top, bottom, etc.
// Aggregation functions always use the batch time.
// tick:property
func (n *InfluxQLNode) UsePointTimes() *InfluxQLNode {
n.PointTimes = true
return n
}
//------------------------------------
// Aggregation Functions
//
// Count the number of points.
func (n *chainnode) Count(field string) *InfluxQLNode {
i := newInfluxQLNode("count", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatIntegerReducer: func() (influxql.FloatPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewFloatFuncIntegerReducer(influxql.FloatCountReduce, &influxql.IntegerPoint{Value: 0})
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerCountReduce, &influxql.IntegerPoint{Value: 0})
return fn, fn
},
CreateStringIntegerReducer: func() (influxql.StringPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewStringFuncIntegerReducer(influxql.StringCountReduce, &influxql.IntegerPoint{Value: 0})
return fn, fn
},
CreateBooleanIntegerReducer: func() (influxql.BooleanPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewBooleanFuncIntegerReducer(influxql.BooleanCountReduce, &influxql.IntegerPoint{Value: 0})
return fn, fn
},
IsEmptyOK: true,
})
n.linkChild(i)
return i
}
// Produce batch of only the distinct points.
func (n *chainnode) Distinct(field string) *InfluxQLNode {
i := newInfluxQLNode("distinct", field, n.Provides(), BatchEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatDistinctReducer()
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerDistinctReducer()
return fn, fn
},
CreateStringReducer: func() (influxql.StringPointAggregator, influxql.StringPointEmitter) {
fn := influxql.NewStringDistinctReducer()
return fn, fn
},
CreateBooleanReducer: func() (influxql.BooleanPointAggregator, influxql.BooleanPointEmitter) {
fn := influxql.NewBooleanDistinctReducer()
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the mean of the data.
func (n *chainnode) Mean(field string) *InfluxQLNode {
i := newInfluxQLNode("mean", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatMeanReducer()
return fn, fn
},
CreateIntegerFloatReducer: func() (influxql.IntegerPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewIntegerMeanReducer()
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the median of the data. Note, this method is not a selector,
// if you want the median point use `.percentile(field, 50.0)`.
func (n *chainnode) Median(field string) *InfluxQLNode {
i := newInfluxQLNode("median", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.FloatMedianReduceSlice)
return fn, fn
},
CreateIntegerBulkFloatReducer: func() (IntegerBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewIntegerSliceFuncFloatReducer(influxql.IntegerMedianReduceSlice)
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the mode of the data.
func (n *chainnode) Mode(field string) *InfluxQLNode {
i := newInfluxQLNode("mode", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.FloatModeReduceSlice)
return fn, fn
},
CreateIntegerBulkReducer: func() (IntegerBulkPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerSliceFuncReducer(influxql.IntegerModeReduceSlice)
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the difference between `min` and `max` points.
func (n *chainnode) Spread(field string) *InfluxQLNode {
i := newInfluxQLNode("spread", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.FloatSpreadReduceSlice)
return fn, fn
},
CreateIntegerBulkReducer: func() (IntegerBulkPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerSliceFuncReducer(influxql.IntegerSpreadReduceSlice)
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the sum of all values.
func (n *chainnode) Sum(field string) *InfluxQLNode {
i := newInfluxQLNode("sum", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatFuncReducer(influxql.FloatSumReduce, &influxql.FloatPoint{Value: 0})
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerSumReduce, &influxql.IntegerPoint{Value: 0})
return fn, fn
},
IsEmptyOK: true,
})
n.linkChild(i)
return i
}
//------------------------------------
// Selection Functions
//
// Select the first point.
func (n *chainnode) First(field string) *InfluxQLNode {
i := newInfluxQLNode("first", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatFuncReducer(influxql.FloatFirstReduce, nil)
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerFirstReduce, nil)
return fn, fn
},
CreateStringReducer: func() (influxql.StringPointAggregator, influxql.StringPointEmitter) {
fn := influxql.NewStringFuncReducer(influxql.StringFirstReduce, nil)
return fn, fn
},
CreateBooleanReducer: func() (influxql.BooleanPointAggregator, influxql.BooleanPointEmitter) {
fn := influxql.NewBooleanFuncReducer(influxql.BooleanFirstReduce, nil)
return fn, fn
},
IsSimpleSelector: true,
})
n.linkChild(i)
return i
}
// Select the last point.
func (n *chainnode) Last(field string) *InfluxQLNode {
i := newInfluxQLNode("last", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatFuncReducer(influxql.FloatLastReduce, nil)
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerLastReduce, nil)
return fn, fn
},
CreateStringReducer: func() (influxql.StringPointAggregator, influxql.StringPointEmitter) {
fn := influxql.NewStringFuncReducer(influxql.StringLastReduce, nil)
return fn, fn
},
CreateBooleanReducer: func() (influxql.BooleanPointAggregator, influxql.BooleanPointEmitter) {
fn := influxql.NewBooleanFuncReducer(influxql.BooleanLastReduce, nil)
return fn, fn
},
IsSimpleSelector: true,
})
n.linkChild(i)
return i
}
// Select the minimum point.
func (n *chainnode) Min(field string) *InfluxQLNode {
i := newInfluxQLNode("min", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatFuncReducer(influxql.FloatMinReduce, nil)
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerMinReduce, nil)
return fn, fn
},
IsSimpleSelector: true,
})
n.linkChild(i)
return i
}
// Select the maximum point.
func (n *chainnode) Max(field string) *InfluxQLNode {
i := newInfluxQLNode("max", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatFuncReducer(influxql.FloatMaxReduce, nil)
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerFuncReducer(influxql.IntegerMaxReduce, nil)
return fn, fn
},
IsSimpleSelector: true,
})
n.linkChild(i)
return i
}
// Select a point at the given percentile. This is a selector function, no interpolation between points is performed.
func (n *chainnode) Percentile(field string, percentile float64) *InfluxQLNode {
i := newInfluxQLNode("percentile", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.NewFloatPercentileReduceSliceFunc(percentile))
return fn, fn
},
CreateIntegerBulkReducer: func() (IntegerBulkPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerSliceFuncReducer(influxql.NewIntegerPercentileReduceSliceFunc(percentile))
return fn, fn
},
IsSimpleSelector: true,
})
n.linkChild(i)
return i
}
//tick:ignore
type TopBottomCallInfo struct {
FieldsAndTags []string
}
// Select the top `num` points for `field` and sort by any extra tags or fields.
func (n *chainnode) Top(num int64, field string, fieldsAndTags ...string) *InfluxQLNode {
tags := make([]int, len(fieldsAndTags))
for i := range fieldsAndTags {
tags[i] = i
}
i := newInfluxQLNode("top", field, n.Provides(), BatchEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.NewFloatTopReduceSliceFunc(
int(num),
tags,
influxql.Interval{},
))
return fn, fn
},
CreateIntegerBulkReducer: func() (IntegerBulkPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerSliceFuncReducer(influxql.NewIntegerTopReduceSliceFunc(
int(num),
tags,
influxql.Interval{},
))
return fn, fn
},
TopBottomCallInfo: &TopBottomCallInfo{
FieldsAndTags: fieldsAndTags,
},
})
n.linkChild(i)
return i
}
// Select the bottom `num` points for `field` and sort by any extra tags or fields.
func (n *chainnode) Bottom(num int64, field string, fieldsAndTags ...string) *InfluxQLNode {
tags := make([]int, len(fieldsAndTags))
for i := range fieldsAndTags {
tags[i] = i
}
i := newInfluxQLNode("bottom", field, n.Provides(), BatchEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.NewFloatBottomReduceSliceFunc(
int(num),
tags,
influxql.Interval{},
))
return fn, fn
},
CreateIntegerBulkReducer: func() (IntegerBulkPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerSliceFuncReducer(influxql.NewIntegerBottomReduceSliceFunc(
int(num),
tags,
influxql.Interval{},
))
return fn, fn
},
TopBottomCallInfo: &TopBottomCallInfo{
FieldsAndTags: fieldsAndTags,
},
})
n.linkChild(i)
return i
}
//------------------------------------
// Transformation Functions
//
// Compute the standard deviation.
func (n *chainnode) Stddev(field string) *InfluxQLNode {
i := newInfluxQLNode("stddev", field, n.Provides(), StreamEdge, ReduceCreater{
CreateFloatBulkReducer: func() (FloatBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatSliceFuncReducer(influxql.FloatStddevReduceSlice)
return fn, fn
},
CreateIntegerBulkFloatReducer: func() (IntegerBulkPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewIntegerSliceFuncFloatReducer(influxql.IntegerStddevReduceSlice)
return fn, fn
},
})
n.linkChild(i)
return i
}
// Compute the elapsed time between points
func (n *chainnode) Elapsed(field string, unit time.Duration) *InfluxQLNode {
i := newInfluxQLNode("elapsed", field, n.Provides(), n.Provides(), ReduceCreater{
CreateFloatIntegerReducer: func() (influxql.FloatPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewFloatElapsedReducer(influxql.Interval{Duration: unit})
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerElapsedReducer(influxql.Interval{Duration: unit})
return fn, fn
},
CreateStringIntegerReducer: func() (influxql.StringPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewStringElapsedReducer(influxql.Interval{Duration: unit})
return fn, fn
},
CreateBooleanIntegerReducer: func() (influxql.BooleanPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewBooleanElapsedReducer(influxql.Interval{Duration: unit})
return fn, fn
},
IsStreamTransformation: true,
})
n.linkChild(i)
return i
}
// Compute the difference between points independent of elapsed time.
func (n *chainnode) Difference(field string) *InfluxQLNode {
i := newInfluxQLNode("difference", field, n.Provides(), n.Provides(), ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatDifferenceReducer()
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerDifferenceReducer()
return fn, fn
},
IsStreamTransformation: true,
})
n.linkChild(i)
return i
}
// Compute a moving average of the last window points.
// No points are emitted until the window is full.
func (n *chainnode) MovingAverage(field string, window int64) *InfluxQLNode {
i := newInfluxQLNode("movingAverage", field, n.Provides(), n.Provides(), ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatMovingAverageReducer(int(window))
return fn, fn
},
CreateIntegerFloatReducer: func() (influxql.IntegerPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewIntegerMovingAverageReducer(int(window))
return fn, fn
},
IsStreamTransformation: true,
})
n.linkChild(i)
return i
}
// Compute the holt-winters (https://docs.influxdata.com/influxdb/latest/query_language/functions/#holt-winters) forecast of a data set.
func (n *chainnode) HoltWinters(field string, h, m int64, interval time.Duration) *InfluxQLNode {
return n.holtWinters(field, h, m, interval, false)
}
// Compute the holt-winters (https://docs.influxdata.com/influxdb/latest/query_language/functions/#holt-winters) forecast of a data set.
// This method also outputs all the points used to fit the data in addition to the forecasted data.
func (n *chainnode) HoltWintersWithFit(field string, h, m int64, interval time.Duration) *InfluxQLNode {
return n.holtWinters(field, h, m, interval, true)
}
func (n *chainnode) holtWinters(field string, h, m int64, interval time.Duration, includeFitData bool) *InfluxQLNode {
i := newInfluxQLNode("holtWinters", field, n.Provides(), BatchEdge, ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatHoltWintersReducer(int(h), int(m), includeFitData, interval)
return fn, fn
},
CreateIntegerFloatReducer: func() (influxql.IntegerPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatHoltWintersReducer(int(h), int(m), includeFitData, interval)
return fn, fn
},
})
// Always use point times for Holt Winters
i.PointTimes = true
n.linkChild(i)
return i
}
// Compute a cumulative sum of each point that is received.
// A point is emitted for every point collected.
func (n *chainnode) CumulativeSum(field string) *InfluxQLNode {
i := newInfluxQLNode("cumulativeSum", field, n.Provides(), n.Provides(), ReduceCreater{
CreateFloatReducer: func() (influxql.FloatPointAggregator, influxql.FloatPointEmitter) {
fn := influxql.NewFloatCumulativeSumReducer()
return fn, fn
},
CreateIntegerReducer: func() (influxql.IntegerPointAggregator, influxql.IntegerPointEmitter) {
fn := influxql.NewIntegerCumulativeSumReducer()
return fn, fn
},
IsStreamTransformation: true,
})
n.linkChild(i)
return i
}