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

Commit 5cf5a0e

Browse files
committed
pr changes
1 parent 7573c23 commit 5cf5a0e

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

expr/func_filterseries.go

+30-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
)
99

1010
type FuncFilterSeries struct {
11-
in GraphiteFunc
12-
fn string
13-
operator string
14-
threshhold float64
11+
in GraphiteFunc
12+
fn string
13+
operator string
14+
threshold float64
1515
}
1616

1717
func NewFilterSeries() GraphiteFunc {
@@ -23,33 +23,45 @@ func (s *FuncFilterSeries) Signature() ([]Arg, []Arg) {
2323
ArgSeriesList{val: &s.in},
2424
ArgString{key: "func", val: &s.fn, validator: []Validator{IsConsolFunc}},
2525
ArgString{key: "operator", val: &s.operator, validator: []Validator{IsOperator}},
26-
ArgFloat{key: "threshhold", val: &s.threshhold},
26+
ArgFloat{key: "threshold", val: &s.threshold},
2727
}, []Arg{ArgSeriesList{}}
2828
}
2929

3030
func (s *FuncFilterSeries) Context(context Context) Context {
3131
return context
3232
}
3333

34-
func (s *FuncFilterSeries) operatorFunc(val float64) bool {
35-
if math.IsNaN(val) {
36-
return true
37-
}
34+
func (s *FuncFilterSeries) getOperatorFunc() func(float64) bool {
3835
switch s.operator {
3936
case "=":
40-
return val == s.threshhold
37+
return func(val float64) bool {
38+
return val == s.threshold
39+
}
40+
4141
case "!=":
42-
return val != s.threshhold
42+
return func(val float64) bool {
43+
return val != s.threshold
44+
}
45+
4346
case ">":
44-
return val > s.threshhold
47+
return func(val float64) bool {
48+
return val > s.threshold
49+
}
4550
case ">=":
46-
return val >= s.threshhold
51+
return func(val float64) bool {
52+
return val >= s.threshold
53+
}
54+
4755
case "<":
48-
return val < s.threshhold
56+
return func(val float64) bool {
57+
return math.IsNaN(val) || val < s.threshold
58+
}
4959
case "<=":
50-
return val <= s.threshhold
60+
return func(val float64) bool {
61+
return math.IsNaN(val) || val <= s.threshold
62+
}
5163
}
52-
return false // should never happen
64+
return func(val float64) bool { return false } // should never happen
5365
}
5466

5567
func (s *FuncFilterSeries) Exec(cache map[Req][]models.Series) ([]models.Series, error) {
@@ -59,10 +71,11 @@ func (s *FuncFilterSeries) Exec(cache map[Req][]models.Series) ([]models.Series,
5971
}
6072

6173
consolidationFunc := consolidation.GetAggFunc(consolidation.FromConsolidateBy(s.fn))
74+
operatorFunc := s.getOperatorFunc()
6275

6376
out := make([]models.Series, 0, len(series))
6477
for _, serie := range series {
65-
if s.operatorFunc(consolidationFunc(serie.Datapoints)) {
78+
if operatorFunc(consolidationFunc(serie.Datapoints)) {
6679
out = append(out, serie)
6780
}
6881
}

expr/func_filterseries_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -286,33 +286,33 @@ func TestFilterSeriesMoreThanOrEqual(t *testing.T) {
286286
)
287287
}
288288

289-
func testFilterSeries(name string, fn string, operator string, threshhold float64, in []models.Series, out []models.Series, t *testing.T) {
289+
func testFilterSeries(name string, fn string, operator string, threshold float64, in []models.Series, out []models.Series, t *testing.T) {
290290
f := NewFilterSeries()
291291
f.(*FuncFilterSeries).in = NewMock(in)
292292
f.(*FuncFilterSeries).fn = fn
293293
f.(*FuncFilterSeries).operator = operator
294-
f.(*FuncFilterSeries).threshhold = threshhold
294+
f.(*FuncFilterSeries).threshold = threshold
295295
gots, err := f.Exec(make(map[Req][]models.Series))
296296
if err != nil {
297-
t.Fatalf("case %q (%s, %s, %f): err should be nil. got %q", name, fn, operator, threshhold, err)
297+
t.Fatalf("case %q (%s, %s, %f): err should be nil. got %q", name, fn, operator, threshold, err)
298298
}
299299
if len(gots) != len(out) {
300-
t.Fatalf("case %q (%s, %s, %f): isNonNull len output expected %d, got %d", name, fn, operator, threshhold, len(out), len(gots))
300+
t.Fatalf("case %q (%s, %s, %f): isNonNull len output expected %d, got %d", name, fn, operator, threshold, len(out), len(gots))
301301
}
302302
for i, g := range gots {
303303
exp := out[i]
304304
if g.Target != exp.Target {
305-
t.Fatalf("case %q (%s, %s, %f): expected target %q, got %q", name, fn, operator, threshhold, exp.Target, g.Target)
305+
t.Fatalf("case %q (%s, %s, %f): expected target %q, got %q", name, fn, operator, threshold, exp.Target, g.Target)
306306
}
307307
if len(g.Datapoints) != len(exp.Datapoints) {
308-
t.Fatalf("case %q (%s, %s, %f) len output expected %d, got %d", name, fn, operator, threshhold, len(exp.Datapoints), len(g.Datapoints))
308+
t.Fatalf("case %q (%s, %s, %f) len output expected %d, got %d", name, fn, operator, threshold, len(exp.Datapoints), len(g.Datapoints))
309309
}
310310
for j, p := range g.Datapoints {
311311
bothNaN := math.IsNaN(p.Val) && math.IsNaN(exp.Datapoints[j].Val)
312312
if (bothNaN || p.Val == exp.Datapoints[j].Val) && p.Ts == exp.Datapoints[j].Ts {
313313
continue
314314
}
315-
t.Fatalf("case %q (%s, %s, %f): output point %d - expected %v got %v", name, fn, operator, threshhold, j, exp.Datapoints[j], p)
315+
t.Fatalf("case %q (%s, %s, %f): output point %d - expected %v got %v", name, fn, operator, threshold, j, exp.Datapoints[j], p)
316316
}
317317
}
318318
}
@@ -375,7 +375,7 @@ func benchmarkFilterSeries(b *testing.B, numSeries int, fn0, fn1 func() []schema
375375
f.(*FuncFilterSeries).in = NewMock(input)
376376
f.(*FuncFilterSeries).fn = "sum"
377377
f.(*FuncFilterSeries).operator = ">"
378-
f.(*FuncFilterSeries).threshhold = rand.Float64()
378+
f.(*FuncFilterSeries).threshold = rand.Float64()
379379
got, err := f.Exec(make(map[Req][]models.Series))
380380
if err != nil {
381381
b.Fatalf("%s", err)

0 commit comments

Comments
 (0)