SSE/Alerting: First pass at query/condition translation (#31693)

- Takes the conditions property from the settings column of an alert from alerts table and turns into an ng alerting condition with the queries and classic condition.
- Has temp API rest endpoint that will take the dashboard conditions json, translate it to SEE queries + classic condition, and execute it (only enabled in dev mode).
- Changes expressions to catch query responses with a non-nil error property
- Adds two new states for an NG instance result (NoData, Error) and updates evaluation to match those states
- Changes the AsDataFrame (for frontend) from Bool to string to represent additional states
- Fix bug in condition model to accept first Operator as empty string.
- In ngalert, adds GetQueryDataRequest, which was part of execute and is still called from there. But this allows me to get the Expression request from a condition to make the "pipeline" can be built.
- Update AsDataFrame for evalresult to be row based so it displays a little better for now
This commit is contained in:
Kyle Brandt
2021-03-23 12:11:15 -04:00
committed by GitHub
parent 24cb059a6b
commit 7bb79158ed
9 changed files with 1095 additions and 24 deletions
+6 -7
View File
@@ -15,10 +15,10 @@ type ConditionsCmd struct {
refID string
}
// classicConditionJSON is the JSON model for a single condition.
// ClassicConditionJSON is the JSON model for a single condition.
// It is based on services/alerting/conditions/query.go's newQueryCondition().
type classicConditionJSON struct {
Evaluator conditionEvalJSON `json:"evaluator"`
type ClassicConditionJSON struct {
Evaluator ConditionEvalJSON `json:"evaluator"`
Operator struct {
Type string `json:"type"`
@@ -34,10 +34,9 @@ type classicConditionJSON struct {
} `json:"reducer"`
}
type conditionEvalJSON struct {
type ConditionEvalJSON struct {
Params []float64 `json:"params"`
Type string `json:"type"` // e.g. "gt"
}
// condition is a single condition within the ConditionsCmd.
@@ -120,7 +119,7 @@ func UnmarshalConditionsCmd(rawQuery map[string]interface{}, refID string) (*Con
if err != nil {
return nil, fmt.Errorf("failed to remarshal classic condition body: %w", err)
}
var ccj []classicConditionJSON
var ccj []ClassicConditionJSON
if err = json.Unmarshal(jsonFromM, &ccj); err != nil {
return nil, fmt.Errorf("failed to unmarshal remarshaled classic condition body: %w", err)
}
@@ -132,7 +131,7 @@ func UnmarshalConditionsCmd(rawQuery map[string]interface{}, refID string) (*Con
for i, cj := range ccj {
cond := condition{}
if cj.Operator.Type != "and" && cj.Operator.Type != "or" {
if i > 0 && cj.Operator.Type != "and" && cj.Operator.Type != "or" {
return nil, fmt.Errorf("classic condition %v operator must be `and` or `or`", i+1)
}
cond.Operator = cj.Operator.Type
+3 -3
View File
@@ -25,7 +25,7 @@ type rangedEvaluator struct {
// newAlertEvaluator is a factory function for returning
// an AlertEvaluator depending on evaluation operator.
func newAlertEvaluator(model conditionEvalJSON) (evaluator, error) {
func newAlertEvaluator(model ConditionEvalJSON) (evaluator, error) {
switch model.Type {
case "gt", "lt":
return newThresholdEvaluator(model)
@@ -54,7 +54,7 @@ func (e *thresholdEvaluator) Eval(reducedValue mathexp.Number) bool {
return false
}
func newThresholdEvaluator(model conditionEvalJSON) (*thresholdEvaluator, error) {
func newThresholdEvaluator(model ConditionEvalJSON) (*thresholdEvaluator, error) {
if len(model.Params) == 0 {
return nil, fmt.Errorf("evaluator '%v' is missing the threshold parameter", model.Type)
}
@@ -69,7 +69,7 @@ func (e *noValueEvaluator) Eval(reducedValue mathexp.Number) bool {
return reducedValue.GetFloat64Value() == nil
}
func newRangedEvaluator(model conditionEvalJSON) (*rangedEvaluator, error) {
func newRangedEvaluator(model ConditionEvalJSON) (*rangedEvaluator, error) {
if len(model.Params) != 2 {
return nil, fmt.Errorf("ranged evaluator requires 2 parameters")
}