diff --git a/pkg/models/alert.go b/pkg/models/alert.go index 452e0fa44c6..013abe82932 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -28,6 +28,7 @@ const ( ) const ( + ExecutionErrorSetOk ExecutionErrorOption = "ok" ExecutionErrorSetAlerting ExecutionErrorOption = "alerting" ExecutionErrorKeepState ExecutionErrorOption = "keep_state" ) @@ -55,7 +56,7 @@ func (s NoDataOption) ToAlertState() AlertStateType { } func (s ExecutionErrorOption) IsValid() bool { - return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState + return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState || s == ExecutionErrorSetOk } func (s ExecutionErrorOption) ToAlertState() AlertStateType { diff --git a/pkg/services/ngalert/api/tooling/definitions/cortex-ruler.go b/pkg/services/ngalert/api/tooling/definitions/cortex-ruler.go index 5707b4d01f2..451c5456fee 100644 --- a/pkg/services/ngalert/api/tooling/definitions/cortex-ruler.go +++ b/pkg/services/ngalert/api/tooling/definitions/cortex-ruler.go @@ -354,6 +354,7 @@ const ( type ExecutionErrorState string const ( + OkErrState ExecutionErrorState = "OK" AlertingErrState ExecutionErrorState = "Alerting" ErrorErrState ExecutionErrorState = "Error" ) diff --git a/pkg/services/ngalert/api/tooling/post.json b/pkg/services/ngalert/api/tooling/post.json index 641089a1746..9a875b709ea 100644 --- a/pkg/services/ngalert/api/tooling/post.json +++ b/pkg/services/ngalert/api/tooling/post.json @@ -874,11 +874,12 @@ }, "exec_err_state": { "enum": [ + "OK", "Alerting", "Error" ], "type": "string", - "x-go-enum-desc": "Alerting AlertingErrState\nError ErrorErrState", + "x-go-enum-desc": "OK OkErrState\nAlerting AlertingErrState\nError ErrorErrState", "x-go-name": "ExecErrState" }, "id": { @@ -1836,11 +1837,12 @@ }, "exec_err_state": { "enum": [ + "OK", "Alerting", "Error" ], "type": "string", - "x-go-enum-desc": "Alerting AlertingErrState\nError ErrorErrState", + "x-go-enum-desc": "OK OkErrState\nAlerting AlertingErrState\nError ErrorErrState", "x-go-name": "ExecErrState" }, "no_data_state": { diff --git a/pkg/services/ngalert/api/tooling/spec.json b/pkg/services/ngalert/api/tooling/spec.json index 811d0e52854..b99f7224990 100644 --- a/pkg/services/ngalert/api/tooling/spec.json +++ b/pkg/services/ngalert/api/tooling/spec.json @@ -2627,10 +2627,11 @@ "exec_err_state": { "type": "string", "enum": [ + "OK", "Alerting", "Error" ], - "x-go-enum-desc": "Alerting AlertingErrState\nError ErrorErrState", + "x-go-enum-desc": "OK OkErrState\nAlerting AlertingErrState\nError ErrorErrState", "x-go-name": "ExecErrState" }, "id": { @@ -3590,10 +3591,11 @@ "exec_err_state": { "type": "string", "enum": [ + "OK", "Alerting", "Error" ], - "x-go-enum-desc": "Alerting AlertingErrState\nError ErrorErrState", + "x-go-enum-desc": "OK OkErrState\nAlerting AlertingErrState\nError ErrorErrState", "x-go-name": "ExecErrState" }, "no_data_state": { diff --git a/pkg/services/sqlstore/migrations/ualert/alert_rule.go b/pkg/services/sqlstore/migrations/ualert/alert_rule.go index 57aeefeb2db..9846d6630d7 100644 --- a/pkg/services/sqlstore/migrations/ualert/alert_rule.go +++ b/pkg/services/sqlstore/migrations/ualert/alert_rule.go @@ -6,6 +6,7 @@ import ( "time" "github.com/grafana/grafana/pkg/expr" + legacymodels "github.com/grafana/grafana/pkg/models" ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/tsdb/graphite" "github.com/grafana/grafana/pkg/util" @@ -241,29 +242,29 @@ func ruleAdjustInterval(freq int64) int64 { } func transNoData(s string) (string, error) { - switch s { - case "ok": - return "OK", nil // values from ngalert/models/rule - case "", "no_data": - return "NoData", nil - case "alerting": - return "Alerting", nil - case "keep_state": - return "NoData", nil // "keep last state" translates to no data because we now emit a special alert when the state is "noData". The result is that the evaluation will not return firing and instead we'll raise the special alert. + switch legacymodels.NoDataOption(s) { + case legacymodels.NoDataSetOK: + return string(ngmodels.OK), nil // values from ngalert/models/rule + case "", legacymodels.NoDataSetNoData: + return string(ngmodels.NoData), nil + case legacymodels.NoDataSetAlerting: + return string(ngmodels.Alerting), nil + case legacymodels.NoDataKeepState: + return string(ngmodels.NoData), nil // "keep last state" translates to no data because we now emit a special alert when the state is "noData". The result is that the evaluation will not return firing and instead we'll raise the special alert. } return "", fmt.Errorf("unrecognized No Data setting %v", s) } func transExecErr(s string) (string, error) { - switch s { - case "", "alerting": - return "Alerting", nil - case "keep_state": + switch legacymodels.ExecutionErrorOption(s) { + case "", legacymodels.ExecutionErrorSetAlerting: + return string(ngmodels.AlertingErrState), nil + case legacymodels.ExecutionErrorKeepState: // Keep last state is translated to error as we now emit a // DatasourceError alert when the state is error - return "Error", nil - case "ok": - return "OK", nil + return string(ngmodels.ErrorErrState), nil + case legacymodels.ExecutionErrorSetOk: + return string(ngmodels.OkErrState), nil } return "", fmt.Errorf("unrecognized Execution Error setting %v", s) }