forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
35 lines (31 loc) · 831 Bytes
/
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
package kapacitor
import (
"fmt"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/tick"
)
// Evaluate a given expression as a boolean predicate against a set of fields and tags
func EvalPredicate(se *tick.StatefulExpr, fields models.Fields, tags map[string]string) (bool, error) {
vars, err := mergeFieldsAndTags(fields, tags)
if err != nil {
return false, err
}
b, err := se.EvalBool(vars)
if err != nil {
return false, err
}
return b, nil
}
func mergeFieldsAndTags(fields models.Fields, tags map[string]string) (*tick.Scope, error) {
scope := tick.NewScope()
for k, v := range fields {
if _, ok := tags[k]; ok {
return nil, fmt.Errorf("cannot have field and tags with same name %q", k)
}
scope.Set(k, v)
}
for k, v := range tags {
scope.Set(k, v)
}
return scope, nil
}