Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 9ce7278

Browse files
committed
added error check and free function
1 parent 5cf5a0e commit 9ce7278

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

expr/func_filterseries.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expr
22

33
import (
4+
"errors"
45
"math"
56

67
"github.com/grafana/metrictank/api/models"
@@ -31,37 +32,37 @@ func (s *FuncFilterSeries) Context(context Context) Context {
3132
return context
3233
}
3334

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 {
3637
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
4041

4142
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
4546

4647
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
5051
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
5455

5556
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
5960
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
6364
}
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)
6566
}
6667

6768
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,
7172
}
7273

7374
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+
}
7579

7680
out := make([]models.Series, 0, len(series))
7781
for _, serie := range series {
78-
if operatorFunc(consolidationFunc(serie.Datapoints)) {
82+
if operatorFunc(consolidationFunc(serie.Datapoints), s.threshold) {
7983
out = append(out, serie)
8084
}
8185
}

0 commit comments

Comments
 (0)