forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
64 lines (53 loc) · 1.69 KB
/
expr.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
package kapacitor
import (
"fmt"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/kapacitor/tick/stateful"
)
// EvalPredicate - Evaluate a given expression as a boolean predicate against a set of fields and tags
func EvalPredicate(se stateful.Expression, scopePool stateful.ScopePool, p edge.FieldsTagsTimeGetter) (bool, error) {
vars := scopePool.Get()
defer scopePool.Put(vars)
err := fillScope(vars, scopePool.ReferenceVariables(), p)
if err != nil {
return false, err
}
// for function signature check
if _, err := se.Type(vars); err != nil {
return false, err
}
return se.EvalBool(vars)
}
// fillScope - given a scope and reference variables, we fill the exact variables from the now, fields and tags.
func fillScope(vars *stateful.Scope, referenceVariables []string, p edge.FieldsTagsTimeGetter) error {
now := p.Time()
fields := p.Fields()
tags := p.Tags()
for _, refVariableName := range referenceVariables {
if refVariableName == "time" {
vars.Set("time", now.Local())
continue
}
// Support the error with tags/fields collision
var fieldValue interface{}
var isFieldExists bool
var tagValue interface{}
var isTagExists bool
if fieldValue, isFieldExists = fields[refVariableName]; isFieldExists {
vars.Set(refVariableName, fieldValue)
}
if tagValue, isTagExists = tags[refVariableName]; isTagExists {
if isFieldExists {
return fmt.Errorf("cannot have field and tags with same name %q", refVariableName)
}
vars.Set(refVariableName, tagValue)
}
if !isFieldExists && !isTagExists {
if !vars.Has(refVariableName) {
vars.Set(refVariableName, ast.MissingValue)
}
}
}
return nil
}