mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Improve validation of query and expressions on rule submit (#53258)
* Improve error messages of server-side expression * move validation of alert queries and a condition to eval package
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
models2 "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
@@ -276,3 +277,45 @@ func CopyRule(r *AlertRule) *AlertRule {
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
func CreateClassicConditionExpression(refID string, inputRefID string, reducer string, operation string, threshold int) AlertQuery {
|
||||
return AlertQuery{
|
||||
RefID: refID,
|
||||
QueryType: expr.DatasourceType,
|
||||
DatasourceUID: expr.OldDatasourceUID,
|
||||
// the format corresponds to model `ClassicConditionJSON` in /pkg/expr/classic/classic.go
|
||||
Model: json.RawMessage(fmt.Sprintf(`
|
||||
{
|
||||
"refId": "%[1]s",
|
||||
"hide": false,
|
||||
"type": "classic_conditions",
|
||||
"datasource": {
|
||||
"uid": "%[6]s",
|
||||
"type": "%[7]s"
|
||||
},
|
||||
"conditions": [
|
||||
{
|
||||
"type": "query",
|
||||
"evaluator": {
|
||||
"params": [
|
||||
%[4]d
|
||||
],
|
||||
"type": "%[3]s"
|
||||
},
|
||||
"operator": {
|
||||
"type": "and"
|
||||
},
|
||||
"query": {
|
||||
"params": [
|
||||
"%[2]s"
|
||||
]
|
||||
},
|
||||
"reducer": {
|
||||
"params": [],
|
||||
"type": "%[5]s"
|
||||
}
|
||||
}
|
||||
]
|
||||
}`, refID, inputRefID, operation, threshold, reducer, expr.OldDatasourceUID, expr.DatasourceType)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user