forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
139 lines (131 loc) · 3.21 KB
/
eval.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
package kapacitor
import (
"errors"
"log"
"time"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick"
)
const (
statsEvalErrors = "eval_errors"
)
type EvalNode struct {
node
e *pipeline.EvalNode
expressions []*tick.StatefulExpr
evalErrors *expvar.Int
}
// Create a new EvalNode which applies a transformation func to each point in a stream and returns a single point.
func newEvalNode(et *ExecutingTask, n *pipeline.EvalNode, l *log.Logger) (*EvalNode, error) {
if len(n.AsList) != len(n.Expressions) {
return nil, errors.New("must provide one name per expression via the 'As' property")
}
en := &EvalNode{
node: node{Node: n, et: et, logger: l},
e: n,
}
// Create stateful expressions
en.expressions = make([]*tick.StatefulExpr, len(n.Expressions))
for i, expr := range n.Expressions {
en.expressions[i] = tick.NewStatefulExpr(expr)
}
en.node.runF = en.runEval
return en, nil
}
func (e *EvalNode) runEval(snapshot []byte) error {
e.evalErrors = &expvar.Int{}
e.statMap.Set(statsEvalErrors, e.evalErrors)
switch e.Provides() {
case pipeline.StreamEdge:
for p, ok := e.ins[0].NextPoint(); ok; p, ok = e.ins[0].NextPoint() {
e.timer.Start()
p.Fields = e.eval(p.Time, p.Fields, p.Tags)
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
case pipeline.BatchEdge:
for b, ok := e.ins[0].NextBatch(); ok; b, ok = e.ins[0].NextBatch() {
e.timer.Start()
for i, p := range b.Points {
b.Points[i].Fields = e.eval(p.Time, p.Fields, p.Tags)
}
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}
func (e *EvalNode) eval(now time.Time, fields models.Fields, tags map[string]string) models.Fields {
vars, err := mergeFieldsAndTags(now, fields, tags)
if err != nil {
e.logger.Println("E!", err)
return nil
}
for i, expr := range e.expressions {
if v, err := expr.EvalNum(vars); err == nil {
name := e.e.AsList[i]
vars.Set(name, v)
} else {
e.evalErrors.Add(1)
if !e.e.QuiteFlag {
e.logger.Println("E!", err)
}
}
}
var newFields models.Fields
if e.e.KeepFlag {
if l := len(e.e.KeepList); l != 0 {
newFields = make(models.Fields, l)
for _, f := range e.e.KeepList {
if v, err := vars.Get(f); err == nil {
newFields[f] = v
} else {
e.evalErrors.Add(1)
if !e.e.QuiteFlag {
e.logger.Println("E!", err)
}
}
}
} else {
newFields = make(models.Fields, len(fields)+len(e.e.AsList))
for f, v := range fields {
newFields[f] = v
}
for _, f := range e.e.AsList {
if v, err := vars.Get(f); err == nil {
newFields[f] = v
} else {
e.evalErrors.Add(1)
if !e.e.QuiteFlag {
e.logger.Println("E!", err)
}
}
}
}
} else {
newFields = make(models.Fields, len(e.e.AsList))
for _, f := range e.e.AsList {
if v, err := vars.Get(f); err == nil {
newFields[f] = v
} else {
e.evalErrors.Add(1)
if !e.e.QuiteFlag {
e.logger.Println("E!", err)
}
}
}
}
return newFields
}