forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
59 lines (50 loc) · 1.65 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
package kapacitor
import (
"fmt"
"time"
"github.com/influxdata/kapacitor/models"
"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, now time.Time, fields models.Fields, tags models.Tags) (bool, error) {
vars := scopePool.Get()
defer scopePool.Put(vars)
err := fillScope(vars, scopePool.ReferenceVariables(), now, fields, tags)
if err != nil {
return false, err
}
b, err := se.EvalBool(vars)
if err != nil {
return false, err
}
return b, nil
}
// 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, now time.Time, fields models.Fields, tags models.Tags) error {
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) {
return fmt.Errorf("no field or tag exists for %s", refVariableName)
}
}
}
return nil
}