grafana/pkg/services/alerting/test_rule.go
Arve Knudsen b79e61656a
Introduce TSDB service (#31520)
* Introduce TSDB service

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
Co-authored-by: Will Browne <will.browne@grafana.com>
Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
2021-03-08 07:02:49 +01:00

43 lines
1016 B
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)
extractor := NewDashAlertExtractor(dash, orgID, user)
alerts, err := extractor.GetAlerts()
if err != nil {
return nil, err
}
for _, alert := range alerts {
if alert.PanelId != panelID {
continue
}
rule, err := NewRuleFromDBAlert(alert, true)
if err != nil {
return nil, err
}
handler := NewEvalHandler(e.DataService)
context := NewEvalContext(context.Background(), rule, fakeRequestValidator{})
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)
}