forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.go
82 lines (77 loc) · 2.06 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
76
77
78
79
80
81
82
package kapacitor
import (
"errors"
"log"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/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, l *log.Logger) (wn *WhereNode, err error) {
wn = &WhereNode{
node: node{Node: n, et: et, logger: l},
w: n,
expressions: make(map[models.GroupID]*tick.StatefulExpr),
}
wn.runF = wn.runWhere
if n.Expression == nil {
return nil, errors.New("nil expression passed to WhereNode")
}
return
}
func (w *WhereNode) runWhere(snapshot []byte) 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.Time, 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 := 0; i < len(b.Points); {
p := b.Points[i]
if pass, err := EvalPredicate(expr, p.Time, 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:]...)
} else {
i++
}
}
if len(b.Points) > 0 {
for _, child := range w.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
}
return nil
}