forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.go
65 lines (60 loc) · 1.57 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
package kapacitor
import (
"errors"
"github.com/influxdb/kapacitor/pipeline"
"github.com/influxdb/kapacitor/tick"
)
type WhereNode struct {
node
w *pipeline.WhereNode
endpoint string
expression *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")
}
wn.expression = tick.NewStatefulExpr(n.Expression)
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() {
if pass, err := EvalPredicate(w.expression, 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() {
for i, p := range b.Points {
if pass, err := EvalPredicate(w.expression, p.Fields, b.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
}