2016-07-20 16:13:36 +02:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
|
|
import (
|
2016-10-03 09:38:03 +02:00
|
|
|
"context"
|
2016-07-20 16:13:36 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2022-09-19 10:54:37 +03:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
|
2023-01-16 16:33:55 +01:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-08-10 11:56:48 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2016-07-20 16:13:36 +02:00
|
|
|
)
|
|
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
// AlertTest makes a test alert.
|
2022-08-10 11:56:48 +02:00
|
|
|
func (e *AlertEngine) AlertTest(orgID int64, dashboard *simplejson.Json, panelID int64, user *user.SignedInUser) (*EvalContext, error) {
|
2023-01-16 16:33:55 +01:00
|
|
|
dash := dashboards.NewDashboardFromJson(dashboard)
|
2022-02-28 09:54:56 +01:00
|
|
|
dashInfo := DashAlertInfo{
|
|
|
|
|
User: user,
|
|
|
|
|
Dash: dash,
|
|
|
|
|
OrgID: orgID,
|
|
|
|
|
}
|
|
|
|
|
alerts, err := e.dashAlertExtractor.GetAlerts(context.Background(), dashInfo)
|
2016-07-20 16:13:36 +02:00
|
|
|
if err != nil {
|
2021-03-08 07:02:49 +01:00
|
|
|
return nil, err
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
for _, alert := range alerts {
|
2023-02-02 17:22:43 +01:00
|
|
|
if alert.PanelID != panelID {
|
2021-03-08 07:02:49 +01:00
|
|
|
continue
|
|
|
|
|
}
|
2022-08-03 11:17:26 -04:00
|
|
|
rule, err := NewRuleFromDBAlert(context.Background(), e.AlertStore, alert, true)
|
2021-03-08 07:02:49 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
handler := NewEvalHandler(e.DataService)
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2022-09-19 10:54:37 +03:00
|
|
|
context := NewEvalContext(context.Background(), rule, fakeRequestValidator{}, e.AlertStore, nil, e.datasourceService, annotationstest.NewFakeAnnotationsRepo())
|
2021-03-08 07:02:49 +01:00
|
|
|
context.IsTestRun = true
|
|
|
|
|
context.IsDebug = true
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
handler.Eval(context)
|
|
|
|
|
context.Rule.State = context.GetNewState()
|
|
|
|
|
return context, nil
|
|
|
|
|
}
|
2016-07-21 13:09:12 +02:00
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
return nil, fmt.Errorf("could not find alert with panel ID %d", panelID)
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|