forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.go
75 lines (70 loc) · 1.84 KB
/
where.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
package kapacitor
import (
"errors"
"github.com/influxdb/kapacitor/models"
"github.com/influxdb/kapacitor/pipeline"
"github.com/influxdb/kapacitor/tick"
)
type WhereNode struct {
node
w *pipeline.WhereNode
endpoint string
expressions map[models.GroupID]*tick.StatefulExpr
}
// Create a new WhereNode which filters down the batch or stream by a condition
func newWhereNode(et *ExecutingTask, n *pipeline.WhereNode) (wn *WhereNode, err error) {
wn = &WhereNode{
node: node{Node: n, et: et},
w: n,
}
wn.runF = wn.runWhere
if n.Expression == nil {
return nil, errors.New("nil expression passed to WhereNode")
}
return
}
func (w *WhereNode) runWhere() error {
switch w.Wants() {
case pipeline.StreamEdge:
for p, ok := w.ins[0].NextPoint(); ok; p, ok = w.ins[0].NextPoint() {
expr := w.expressions[p.Group]
if expr == nil {
expr = tick.NewStatefulExpr(w.w.Expression)
w.expressions[p.Group] = expr
}
if pass, err := EvalPredicate(expr, p.Fields, p.Tags); pass {
for _, child := range w.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
} else if err != nil {
w.logger.Println("E! error while evaluating expression:", err)
}
}
case pipeline.BatchEdge:
for b, ok := w.ins[0].NextBatch(); ok; b, ok = w.ins[0].NextBatch() {
expr := w.expressions[b.Group]
if expr == nil {
expr = tick.NewStatefulExpr(w.w.Expression)
w.expressions[b.Group] = expr
}
for i, p := range b.Points {
if pass, err := EvalPredicate(expr, p.Fields, p.Tags); !pass {
if err != nil {
w.logger.Println("E! error while evaluating WHERE expression:", err)
}
b.Points = append(b.Points[:i], b.Points[i+1:]...)
}
}
for _, child := range w.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}