Alerting: Fix mathexp.NoData for ConditionsCmd (#56816)

This commit is contained in:
George Robinson 2022-10-12 17:34:28 +01:00 committed by GitHub
parent 61fd369b3f
commit 004bb7689d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -82,6 +82,11 @@ func (cmd *ConditionsCmd) Execute(_ context.Context, vars mathexp.Vars) (mathexp
var reducedNum mathexp.Number
var name string
switch v := val.(type) {
case mathexp.NoData:
// To keep this code as simple as possible we translate mathexp.NoData into a
// mathexp.Number with a nil value so number.GetFloat64Value() returns nil
reducedNum = mathexp.NewNumber("no data", nil)
reducedNum.SetValue(nil)
case mathexp.Series:
reducedNum = c.Reducer.Reduce(v)
name = v.GetName()

View File

@ -326,6 +326,29 @@ func TestConditionsCmdExecute(t *testing.T) {
},
{
name: "single query with no data",
vars: mathexp.Vars{
"A": mathexp.Results{
Values: []mathexp.Value{mathexp.NoData{}.New()},
},
},
conditionsCmd: &ConditionsCmd{
Conditions: []condition{
{
InputRefID: "A",
Reducer: reducer("avg"),
Operator: "and",
Evaluator: &thresholdEvaluator{"gt", 1},
},
},
},
resultNumber: func() mathexp.Number {
v := valBasedNumber(nil)
v.SetMeta([]EvalMatch{{Metric: "NoData"}})
return v
},
},
{
name: "single query with no values",
vars: mathexp.Vars{
"A": mathexp.Results{
Values: []mathexp.Value{},