File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed
pkg/services/alerting/conditions Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -27,13 +27,17 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
27
27
28
28
switch s .Type {
29
29
case "avg" :
30
+ validPointsCount := 0
30
31
for _ , point := range series .Points {
31
32
if point [0 ].Valid {
32
33
value += point [0 ].Float64
34
+ validPointsCount += 1
33
35
allNull = false
34
36
}
35
37
}
36
- value = value / float64 (len (series .Points ))
38
+ if validPointsCount > 0 {
39
+ value = value / float64 (validPointsCount )
40
+ }
37
41
case "sum" :
38
42
for _ , point := range series .Points {
39
43
if point [0 ].Valid {
@@ -90,7 +94,6 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
90
94
value = (values [(length / 2 )- 1 ] + values [length / 2 ]) / 2
91
95
}
92
96
}
93
-
94
97
}
95
98
96
99
if allNull {
Original file line number Diff line number Diff line change @@ -16,6 +16,20 @@ func TestSimpleReducer(t *testing.T) {
16
16
So (result , ShouldEqual , float64 (2 ))
17
17
})
18
18
19
+ Convey ("avg of none null data" , func () {
20
+ reducer := NewSimpleReducer ("avg" )
21
+ series := & tsdb.TimeSeries {
22
+ Name : "test time serie" ,
23
+ }
24
+
25
+ series .Points = append (series .Points , tsdb .NewTimePoint (null .FloatFrom (3 ), 1 ))
26
+ series .Points = append (series .Points , tsdb .NewTimePoint (null .FloatFromPtr (nil ), 2 ))
27
+ series .Points = append (series .Points , tsdb .NewTimePoint (null .FloatFromPtr (nil ), 3 ))
28
+ series .Points = append (series .Points , tsdb .NewTimePoint (null .FloatFrom (3 ), 4 ))
29
+
30
+ So (reducer .Reduce (series ).Float64 , ShouldEqual , float64 (3 ))
31
+ })
32
+
19
33
Convey ("sum" , func () {
20
34
result := testReducer ("sum" , 1 , 2 , 3 )
21
35
So (result , ShouldEqual , float64 (6 ))
You can’t perform that action at this time.
0 commit comments