1
1
package expr
2
2
3
3
import (
4
+ "errors"
4
5
"math"
5
6
6
7
"github.com/grafana/metrictank/api/models"
@@ -31,37 +32,37 @@ func (s *FuncFilterSeries) Context(context Context) Context {
31
32
return context
32
33
}
33
34
34
- func ( s * FuncFilterSeries ) getOperatorFunc () func (float64 ) bool {
35
- switch s . operator {
35
+ func getOperatorFunc ( operator string ) ( func (float64 , float64 ) bool , error ) {
36
+ switch operator {
36
37
case "=" :
37
- return func (val float64 ) bool {
38
- return val == s . threshold
39
- }
38
+ return func (val , threshold float64 ) bool {
39
+ return val == threshold
40
+ }, nil
40
41
41
42
case "!=" :
42
- return func (val float64 ) bool {
43
- return val != s . threshold
44
- }
43
+ return func (val , threshold float64 ) bool {
44
+ return val != threshold
45
+ }, nil
45
46
46
47
case ">" :
47
- return func (val float64 ) bool {
48
- return val > s . threshold
49
- }
48
+ return func (val , threshold float64 ) bool {
49
+ return val > threshold
50
+ }, nil
50
51
case ">=" :
51
- return func (val float64 ) bool {
52
- return val >= s . threshold
53
- }
52
+ return func (val , threshold float64 ) bool {
53
+ return val >= threshold
54
+ }, nil
54
55
55
56
case "<" :
56
- return func (val float64 ) bool {
57
- return math .IsNaN (val ) || val < s . threshold
58
- }
57
+ return func (val , threshold float64 ) bool {
58
+ return math .IsNaN (val ) || val < threshold
59
+ }, nil
59
60
case "<=" :
60
- return func (val float64 ) bool {
61
- return math .IsNaN (val ) || val <= s . threshold
62
- }
61
+ return func (val , threshold float64 ) bool {
62
+ return math .IsNaN (val ) || val <= threshold
63
+ }, nil
63
64
}
64
- return func (val float64 ) bool { return false } // should never happen
65
+ return func (v1 , v2 float64 ) bool { return false }, errors . New ( "Unsupported operator: " + operator )
65
66
}
66
67
67
68
func (s * FuncFilterSeries ) Exec (cache map [Req ][]models.Series ) ([]models.Series , error ) {
@@ -71,11 +72,14 @@ func (s *FuncFilterSeries) Exec(cache map[Req][]models.Series) ([]models.Series,
71
72
}
72
73
73
74
consolidationFunc := consolidation .GetAggFunc (consolidation .FromConsolidateBy (s .fn ))
74
- operatorFunc := s .getOperatorFunc ()
75
+ operatorFunc , err := getOperatorFunc (s .operator )
76
+ if err != nil {
77
+ return nil , err
78
+ }
75
79
76
80
out := make ([]models.Series , 0 , len (series ))
77
81
for _ , serie := range series {
78
- if operatorFunc (consolidationFunc (serie .Datapoints )) {
82
+ if operatorFunc (consolidationFunc (serie .Datapoints ), s . threshold ) {
79
83
out = append (out , serie )
80
84
}
81
85
}
0 commit comments