Files
grafana/pkg/services/alerting/test_rule.go
Selene 2c90dcf3c0 Dashboard Alert Extractor: Create service for dashboard extractor and remove bus (#45518)
* Create DashAlertService service

* Remove no used dashboard service from plugin's manager that generates dependency cycle in Enterprise

* Remove bus for dashboard permissions

* Remove bus from dashboard extractor service

* Add missing argument

* Fix wire

* Fix lint

* More goimports

* Use datasource service instead sql calls

* Fix integration test
2022-02-28 09:54:56 +01:00

46 lines
1.1 KiB
Go

package alerting
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
)
// AlertTest makes a test alert.
func (e *AlertEngine) AlertTest(orgID int64, dashboard *simplejson.Json, panelID int64, user *models.SignedInUser) (*EvalContext, error) {
dash := models.NewDashboardFromJson(dashboard)
dashInfo := DashAlertInfo{
User: user,
Dash: dash,
OrgID: orgID,
}
alerts, err := e.dashAlertExtractor.GetAlerts(context.Background(), dashInfo)
if err != nil {
return nil, err
}
for _, alert := range alerts {
if alert.PanelId != panelID {
continue
}
rule, err := NewRuleFromDBAlert(context.Background(), alert, true)
if err != nil {
return nil, err
}
handler := NewEvalHandler(e.DataService)
context := NewEvalContext(context.Background(), rule, fakeRequestValidator{}, nil)
context.IsTestRun = true
context.IsDebug = true
handler.Eval(context)
context.Rule.State = context.GetNewState()
return context, nil
}
return nil, fmt.Errorf("could not find alert with panel ID %d", panelID)
}