This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_filterseries.go
102 lines (86 loc) · 2.49 KB
/
func_filterseries.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package expr
import (
"math"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/errors"
)
type FuncFilterSeries struct {
in GraphiteFunc
fn string
operator string
threshold float64
}
func NewFilterSeries() GraphiteFunc {
return &FuncFilterSeries{}
}
func NewFilterSeriesConstructor(fn string, operator string) func() GraphiteFunc {
return func() GraphiteFunc {
return &FuncFilterSeries{fn: fn, operator: operator}
}
}
func (s *FuncFilterSeries) Signature() ([]Arg, []Arg) {
if s.fn != "" && s.operator != "" {
return []Arg{
ArgSeriesList{val: &s.in},
ArgFloat{key: "threshold", val: &s.threshold},
}, []Arg{ArgSeriesList{}}
}
return []Arg{
ArgSeriesList{val: &s.in},
ArgString{key: "func", val: &s.fn, validator: []Validator{IsConsolFunc}},
ArgString{key: "operator", val: &s.operator, validator: []Validator{IsOperator}},
ArgFloat{key: "threshold", val: &s.threshold},
}, []Arg{ArgSeriesList{}}
}
func (s *FuncFilterSeries) Context(context Context) Context {
return context
}
func getOperatorFunc(operator string) (func(float64, float64) bool, error) {
switch operator {
case "=":
return func(val, threshold float64) bool {
return val == threshold
}, nil
case "!=":
return func(val, threshold float64) bool {
return val != threshold
}, nil
case ">":
return func(val, threshold float64) bool {
return val > threshold
}, nil
case ">=":
return func(val, threshold float64) bool {
return val >= threshold
}, nil
case "<":
return func(val, threshold float64) bool {
return math.IsNaN(val) || val < threshold
}, nil
case "<=":
return func(val, threshold float64) bool {
return math.IsNaN(val) || val <= threshold
}, nil
}
return func(v1, v2 float64) bool { return false }, errors.NewBadRequest("Unsupported operator: " + operator)
}
func (s *FuncFilterSeries) Exec(dataMap DataMap) ([]models.Series, error) {
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
// note that s.fn has already been validated at series construction time using consolidation.IsConsolFunc
consolidationFunc := consolidation.GetAggFunc(consolidation.FromConsolidateBy(s.fn))
operatorFunc, err := getOperatorFunc(s.operator)
if err != nil {
return nil, err
}
out := make([]models.Series, 0, len(series))
for _, serie := range series {
if operatorFunc(consolidationFunc(serie.Datapoints), s.threshold) {
out = append(out, serie)
}
}
return out, nil
}