mirror of
https://github.com/grafana/grafana.git
synced 2024-12-29 10:21:41 -06:00
7bb79158ed
- 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
99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
package classic
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana/pkg/expr/mathexp"
|
|
)
|
|
|
|
type evaluator interface {
|
|
Eval(mathexp.Number) bool
|
|
}
|
|
|
|
type noValueEvaluator struct{}
|
|
|
|
type thresholdEvaluator struct {
|
|
Type string
|
|
Threshold float64
|
|
}
|
|
|
|
type rangedEvaluator struct {
|
|
Type string
|
|
Lower float64
|
|
Upper float64
|
|
}
|
|
|
|
// newAlertEvaluator is a factory function for returning
|
|
// an AlertEvaluator depending on evaluation operator.
|
|
func newAlertEvaluator(model ConditionEvalJSON) (evaluator, error) {
|
|
switch model.Type {
|
|
case "gt", "lt":
|
|
return newThresholdEvaluator(model)
|
|
case "within_range", "outside_range":
|
|
return newRangedEvaluator(model)
|
|
case "no_value":
|
|
return &noValueEvaluator{}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("evaluator invalid evaluator type: %s", model.Type)
|
|
}
|
|
|
|
func (e *thresholdEvaluator) Eval(reducedValue mathexp.Number) bool {
|
|
fv := reducedValue.GetFloat64Value()
|
|
if fv == nil {
|
|
return false
|
|
}
|
|
|
|
switch e.Type {
|
|
case "gt":
|
|
return *fv > e.Threshold
|
|
case "lt":
|
|
return *fv < e.Threshold
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func newThresholdEvaluator(model ConditionEvalJSON) (*thresholdEvaluator, error) {
|
|
if len(model.Params) == 0 {
|
|
return nil, fmt.Errorf("evaluator '%v' is missing the threshold parameter", model.Type)
|
|
}
|
|
|
|
return &thresholdEvaluator{
|
|
Type: model.Type,
|
|
Threshold: model.Params[0],
|
|
}, nil
|
|
}
|
|
|
|
func (e *noValueEvaluator) Eval(reducedValue mathexp.Number) bool {
|
|
return reducedValue.GetFloat64Value() == nil
|
|
}
|
|
|
|
func newRangedEvaluator(model ConditionEvalJSON) (*rangedEvaluator, error) {
|
|
if len(model.Params) != 2 {
|
|
return nil, fmt.Errorf("ranged evaluator requires 2 parameters")
|
|
}
|
|
|
|
return &rangedEvaluator{
|
|
Type: model.Type,
|
|
Lower: model.Params[0],
|
|
Upper: model.Params[1],
|
|
}, nil
|
|
}
|
|
|
|
func (e *rangedEvaluator) Eval(reducedValue mathexp.Number) bool {
|
|
fv := reducedValue.GetFloat64Value()
|
|
if fv == nil {
|
|
return false
|
|
}
|
|
|
|
switch e.Type {
|
|
case "within_range":
|
|
return (e.Lower < *fv && e.Upper > *fv) || (e.Upper < *fv && e.Lower > *fv)
|
|
case "outside_range":
|
|
return (e.Upper < *fv && e.Lower < *fv) || (e.Upper > *fv && e.Lower > *fv)
|
|
}
|
|
|
|
return false
|
|
}
|