forked from bigcommerce/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_tracking.go
189 lines (159 loc) · 4.42 KB
/
state_tracking.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 kapacitor
import (
"fmt"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/kapacitor/tick/stateful"
)
type stateTracker interface {
track(t time.Time, inState bool) interface{}
reset()
}
type stateTrackingGroup struct {
n *StateTrackingNode
stateful.Expression
tracker stateTracker
}
type StateTrackingNode struct {
node
as string
expr stateful.Expression
scopePool stateful.ScopePool
newTracker func() stateTracker
}
func (n *StateTrackingNode) runStateTracking(_ []byte) error {
consumer := edge.NewGroupedConsumer(
n.ins[0],
n,
)
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar())
return consumer.Consume()
}
func (n *StateTrackingNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) {
return edge.NewReceiverFromForwardReceiverWithStats(
n.outs,
edge.NewTimedForwardReceiver(n.timer, n.newGroup()),
), nil
}
func (n *StateTrackingNode) newGroup() *stateTrackingGroup {
// Create a new tracking group
g := &stateTrackingGroup{
n: n,
}
g.Expression = n.expr.CopyReset()
g.tracker = n.newTracker()
return g
}
func (g *stateTrackingGroup) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) {
g.tracker.reset()
return begin, nil
}
func (g *stateTrackingGroup) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) {
bp = bp.ShallowCopy()
err := g.track(bp)
if err != nil {
g.n.diag.Error("error while evaluating expression", err)
return nil, nil
}
return bp, nil
}
func (g *stateTrackingGroup) EndBatch(end edge.EndBatchMessage) (edge.Message, error) {
return end, nil
}
func (g *stateTrackingGroup) Point(p edge.PointMessage) (edge.Message, error) {
p = p.ShallowCopy()
err := g.track(p)
if err != nil {
g.n.diag.Error("error while evaluating expression", err)
return nil, nil
}
return p, nil
}
func (g *stateTrackingGroup) track(p edge.FieldsTagsTimeSetter) error {
pass, err := EvalPredicate(g.Expression, g.n.scopePool, p)
if err != nil {
return err
}
fields := p.Fields().Copy()
fields[g.n.as] = g.tracker.track(p.Time(), pass)
p.SetFields(fields)
return nil
}
func (g *stateTrackingGroup) Barrier(b edge.BarrierMessage) (edge.Message, error) {
return b, nil
}
func (g *stateTrackingGroup) DeleteGroup(d edge.DeleteGroupMessage) (edge.Message, error) {
return d, nil
}
func (g *stateTrackingGroup) Done() {}
type stateDurationTracker struct {
sd *pipeline.StateDurationNode
startTime time.Time
}
func (sdt *stateDurationTracker) reset() {
sdt.startTime = time.Time{}
}
func (sdt *stateDurationTracker) track(t time.Time, inState bool) interface{} {
if !inState {
sdt.startTime = time.Time{}
return float64(-1)
}
if sdt.startTime.IsZero() {
sdt.startTime = t
}
return float64(t.Sub(sdt.startTime)) / float64(sdt.sd.Unit)
}
func newStateDurationNode(et *ExecutingTask, sd *pipeline.StateDurationNode, d NodeDiagnostic) (*StateTrackingNode, error) {
if sd.Lambda == nil {
return nil, fmt.Errorf("nil expression passed to StateDurationNode")
}
// Validate lambda expression
expr, err := stateful.NewExpression(sd.Lambda.Expression)
if err != nil {
return nil, err
}
n := &StateTrackingNode{
node: node{Node: sd, et: et, diag: d},
as: sd.As,
newTracker: func() stateTracker { return &stateDurationTracker{sd: sd} },
expr: expr,
scopePool: stateful.NewScopePool(ast.FindReferenceVariables(sd.Lambda.Expression)),
}
n.node.runF = n.runStateTracking
return n, nil
}
type stateCountTracker struct {
count int64
}
func (sct *stateCountTracker) reset() {
sct.count = 0
}
func (sct *stateCountTracker) track(t time.Time, inState bool) interface{} {
if !inState {
sct.count = 0
return int64(-1)
}
sct.count++
return sct.count
}
func newStateCountNode(et *ExecutingTask, sc *pipeline.StateCountNode, d NodeDiagnostic) (*StateTrackingNode, error) {
if sc.Lambda == nil {
return nil, fmt.Errorf("nil expression passed to StateCountNode")
}
// Validate lambda expression
expr, err := stateful.NewExpression(sc.Lambda.Expression)
if err != nil {
return nil, err
}
n := &StateTrackingNode{
node: node{Node: sc, et: et, diag: d},
as: sc.As,
newTracker: func() stateTracker { return &stateCountTracker{} },
expr: expr,
scopePool: stateful.NewScopePool(ast.FindReferenceVariables(sc.Lambda.Expression)),
}
n.node.runF = n.runStateTracking
return n, nil
}