2016-07-20 09:13:36 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2016-07-20 09:13:36 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-07-20 09:13:36 -05:00
|
|
|
)
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
// 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)
|
2022-02-28 02:54:56 -06:00
|
|
|
dashInfo := DashAlertInfo{
|
|
|
|
User: user,
|
|
|
|
Dash: dash,
|
|
|
|
OrgID: orgID,
|
|
|
|
}
|
|
|
|
alerts, err := e.dashAlertExtractor.GetAlerts(context.Background(), dashInfo)
|
2016-07-20 09:13:36 -05:00
|
|
|
if err != nil {
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, err
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
for _, alert := range alerts {
|
2021-03-08 00:02:49 -06:00
|
|
|
if alert.PanelId != panelID {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-08 07:30:25 -05:00
|
|
|
rule, err := NewRuleFromDBAlert(context.Background(), e.sqlStore, alert, true)
|
2021-03-08 00:02:49 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
handler := NewEvalHandler(e.DataService)
|
2016-07-20 09:13:36 -05:00
|
|
|
|
2022-05-19 09:13:02 -05:00
|
|
|
context := NewEvalContext(context.Background(), rule, fakeRequestValidator{}, e.sqlStore, nil)
|
2021-03-08 00:02:49 -06:00
|
|
|
context.IsTestRun = true
|
|
|
|
context.IsDebug = true
|
2016-07-20 09:13:36 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
handler.Eval(context)
|
|
|
|
context.Rule.State = context.GetNewState()
|
|
|
|
return context, nil
|
|
|
|
}
|
2016-07-21 06:09:12 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, fmt.Errorf("could not find alert with panel ID %d", panelID)
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|