From 75fa1510268ee38bfad654a9280d41ea0b4b8138 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 13 Dec 2017 13:37:10 +0100 Subject: [PATCH] fixes broken alert eval when first condition is using OR closes #9318 --- pkg/services/alerting/eval_handler.go | 5 +++ pkg/services/alerting/eval_handler_test.go | 37 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/services/alerting/eval_handler.go b/pkg/services/alerting/eval_handler.go index 5c2861b9154..001ebc17855 100644 --- a/pkg/services/alerting/eval_handler.go +++ b/pkg/services/alerting/eval_handler.go @@ -39,6 +39,11 @@ func (e *DefaultEvalHandler) Eval(context *EvalContext) { break } + if i == 0 { + firing = cr.Firing + noDataFound = cr.NoDataFound + } + // calculating Firing based on operator if cr.Operator == "or" { firing = firing || cr.Firing diff --git a/pkg/services/alerting/eval_handler_test.go b/pkg/services/alerting/eval_handler_test.go index cf6422a5250..c942e24818f 100644 --- a/pkg/services/alerting/eval_handler_test.go +++ b/pkg/services/alerting/eval_handler_test.go @@ -36,6 +36,16 @@ func TestAlertingEvaluationHandler(t *testing.T) { So(context.ConditionEvals, ShouldEqual, "true = true") }) + Convey("Show return triggered with single passing condition2", func() { + context := NewEvalContext(context.TODO(), &Rule{ + Conditions: []Condition{&conditionStub{firing: true, operator: "and"}}, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, true) + So(context.ConditionEvals, ShouldEqual, "true = true") + }) + Convey("Show return false with not passing asdf", func() { context := NewEvalContext(context.TODO(), &Rule{ Conditions: []Condition{ @@ -131,6 +141,33 @@ func TestAlertingEvaluationHandler(t *testing.T) { So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true") }) + Convey("Should return false if no condition is firing using OR operator", func() { + context := NewEvalContext(context.TODO(), &Rule{ + Conditions: []Condition{ + &conditionStub{firing: false, operator: "or"}, + &conditionStub{firing: false, operator: "or"}, + &conditionStub{firing: false, operator: "or"}, + }, + }) + + handler.Eval(context) + So(context.Firing, ShouldEqual, false) + So(context.ConditionEvals, ShouldEqual, "[[false OR false] OR false] = false") + }) + + Convey("Should retuasdfrn no data if one condition has nodata", func() { + context := NewEvalContext(context.TODO(), &Rule{ + Conditions: []Condition{ + &conditionStub{operator: "or", noData: false}, + &conditionStub{operator: "or", noData: false}, + &conditionStub{operator: "or", noData: false}, + }, + }) + + handler.Eval(context) + So(context.NoDataFound, ShouldBeFalse) + }) + Convey("Should return no data if one condition has nodata", func() { context := NewEvalContext(context.TODO(), &Rule{ Conditions: []Condition{