Procházet zdrojové kódy

fix(query): requires all that all series are empty to set NoDataFound

bergquist před 9 roky
rodič
revize
7b9099ef93

+ 3 - 1
pkg/services/alerting/conditions/query.go

@@ -38,6 +38,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) {
 		return
 	}
 
+	emptySerieCount := 0
 	for _, series := range seriesList {
 		reducedValue := c.Reducer.Reduce(series)
 		evalMatch := c.Evaluator.Eval(reducedValue)
@@ -57,10 +58,11 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) {
 
 		// handle no data scenario
 		if reducedValue == nil {
-			context.NoDataFound = true
+			emptySerieCount++
 		}
 	}
 
+	context.NoDataFound = emptySerieCount == len(seriesList)
 	context.Firing = len(context.EvalMatches) > 0
 }
 

+ 26 - 0
pkg/services/alerting/conditions/query_test.go

@@ -72,6 +72,32 @@ func TestQueryCondition(t *testing.T) {
 				So(ctx.result.Error, ShouldBeNil)
 				So(ctx.result.Firing, ShouldBeTrue)
 			})
+
+			Convey("Empty series", func() {
+				Convey("Should set NoDataFound both series are empty", func() {
+					ctx.series = tsdb.TimeSeriesSlice{
+						tsdb.NewTimeSeries("test1", [][2]*float64{}),
+						tsdb.NewTimeSeries("test2", [][2]*float64{}),
+					}
+					ctx.exec()
+
+					So(ctx.result.Error, ShouldBeNil)
+					So(ctx.result.NoDataFound, ShouldBeTrue)
+				})
+
+				Convey("Should not set NoDataFound if one serie is empty", func() {
+					one := float64(120)
+					two := float64(0)
+					ctx.series = tsdb.TimeSeriesSlice{
+						tsdb.NewTimeSeries("test1", [][2]*float64{}),
+						tsdb.NewTimeSeries("test2", [][2]*float64{{&one, &two}}),
+					}
+					ctx.exec()
+
+					So(ctx.result.Error, ShouldBeNil)
+					So(ctx.result.NoDataFound, ShouldBeFalse)
+				})
+			})
 		})
 	})
 }