Files
grafana/pkg/services/alerting/test_rule.go
Joan López de la Franca Beltran 6415d2802e Plugins: Requests validator (#30445)
* Introduce PluginRequestValidator abstraction with a NoOp implementation

* Update PluginRequestValidator abstraction to use the dsURL instead

* Inject PluginRequestValidator into the HTTPServer and validate requests going through data source proxy

* Inject PluginRequestValidator into the BackendPluginManager and validate requests going through it

* Validate requests going through QueryMetrics & QueryMetricsV2

* Validate BackendPluginManager health requests

* Fix backend plugins manager tests

* Validate requests going through alerting service

* Fix tests

* fix tests

* goimports

Co-authored-by: Leonard Gram <leo@xlson.com>
2021-02-03 20:47:45 +01:00

63 lines
1.3 KiB
Go

package alerting
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
)
// AlertTestCommand initiates an test evaluation
// of an alert rule.
type AlertTestCommand struct {
Dashboard *simplejson.Json
PanelID int64
OrgID int64
User *models.SignedInUser
Result *EvalContext
}
func init() {
bus.AddHandler("alerting", handleAlertTestCommand)
}
func handleAlertTestCommand(cmd *AlertTestCommand) error {
dash := models.NewDashboardFromJson(cmd.Dashboard)
extractor := NewDashAlertExtractor(dash, cmd.OrgID, cmd.User)
alerts, err := extractor.GetAlerts()
if err != nil {
return err
}
for _, alert := range alerts {
if alert.PanelId == cmd.PanelID {
rule, err := NewRuleFromDBAlert(alert, true)
if err != nil {
return err
}
cmd.Result = testAlertRule(rule)
return nil
}
}
return fmt.Errorf("could not find alert with panel ID %d", cmd.PanelID)
}
func testAlertRule(rule *Rule) *EvalContext {
handler := NewEvalHandler()
context := NewEvalContext(context.Background(), rule, fakeRequestValidator{})
context.IsTestRun = true
context.IsDebug = true
handler.Eval(context)
context.Rule.State = context.GetNewState()
return context
}