forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivative.go
63 lines (55 loc) · 1.57 KB
/
derivative.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
package pipeline
import (
"time"
)
// Compute the derivative of a stream or batch.
// The derivative is computed on a single field
// and behaves similarly to the InfluxQL derivative
// function. Deriviative is not a MapReduce function
// and as a result is not part of the normal influxql functions.
//
// Example:
// stream
// |from()
// .measurement('net_rx_packets')
// |derivative('value')
// .unit(1s) // default
// .nonNegative()
// ...
//
// Computes the derivative via:
// (current - previous ) / ( time_difference / unit)
//
// For batch edges the derivative is computed for each
// point in the batch and because of boundary conditions
// the number of points is reduced by one.
type DerivativeNode struct {
chainnode
// The field to use when calculating the derivative
// tick:ignore
Field string
// The new name of the derivative field.
// Default is the name of the field used
// when calculating the derivative.
As string
// The time unit of the resulting derivative value.
// Default: 1s
Unit time.Duration
// Where negative values are acceptable.
// tick:ignore
NonNegativeFlag bool `tick:"NonNegative"`
}
func newDerivativeNode(wants EdgeType, field string) *DerivativeNode {
return &DerivativeNode{
chainnode: newBasicChainNode("derivative", wants, wants),
Unit: time.Second,
Field: field,
As: field,
}
}
// If called the derivative will skip negative results.
// tick:property
func (d *DerivativeNode) NonNegative() *DerivativeNode {
d.NonNegativeFlag = true
return d
}