diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 6eaa21b958e..0a61c13fa12 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -141,6 +141,16 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { break } } + case "count_non_null": + for _, v := range series.Points { + if v[0].Valid { + value++ + } + } + + if value > 0 { + allNull = false + } } if allNull { diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index f0147e9021a..866b574f59f 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/pkg/services/alerting/conditions/reducer_test.go @@ -67,6 +67,35 @@ func TestSimpleReducer(t *testing.T) { So(reducer.Reduce(series).Valid, ShouldEqual, false) }) + Convey("count_non_null", func() { + Convey("with null values and real values", func() { + reducer := NewSimpleReducer("count_non_null") + series := &tsdb.TimeSeries{ + Name: "test time serie", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2)) + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 3)) + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 4)) + + So(reducer.Reduce(series).Valid, ShouldEqual, true) + So(reducer.Reduce(series).Float64, ShouldEqual, 2) + }) + + Convey("with null values", func() { + reducer := NewSimpleReducer("count_non_null") + series := &tsdb.TimeSeries{ + Name: "test time serie", + } + + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1)) + series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2)) + + So(reducer.Reduce(series).Valid, ShouldEqual, false) + }) + }) + Convey("avg of number values and null values should ignore nulls", func() { reducer := NewSimpleReducer("avg") series := &tsdb.TimeSeries{ diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index c86f0dee775..84481b60ce0 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -51,6 +51,7 @@ var reducerTypes = [ {text: 'median()', value: 'median'}, {text: 'diff()', value: 'diff'}, {text: 'percent_diff()', value: 'percent_diff'}, + {text: 'count_non_null()', value: 'count_non_null'}, ]; var noDataModes = [ diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index ce737df84d9..9b5a535c727 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -42,7 +42,7 @@ WHEN
- + OF