mirror of
https://github.com/grafana/grafana.git
synced 2024-12-28 18:01:40 -06:00
refactor dashboard alert extractor
This commit is contained in:
parent
c5ce8536d4
commit
9e0da02b6a
@ -134,12 +134,16 @@ func AlertTest(c *m.ReqContext, dto dtos.AlertTestCommand) Response {
|
||||
OrgId: c.OrgId,
|
||||
Dashboard: dto.Dashboard,
|
||||
PanelId: dto.PanelId,
|
||||
User: c.SignedInUser,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&backendCmd); err != nil {
|
||||
if validationErr, ok := err.(alerting.ValidationError); ok {
|
||||
return Error(422, validationErr.Error(), nil)
|
||||
}
|
||||
if err == m.ErrDataSourceAccessDenied {
|
||||
return Error(403, "Access denied to datasource", err)
|
||||
}
|
||||
return Error(500, "Failed to test rule", err)
|
||||
}
|
||||
|
||||
|
@ -215,13 +215,14 @@ type AlertStateInfoDTO struct {
|
||||
// "Internal" commands
|
||||
|
||||
type UpdateDashboardAlertsCommand struct {
|
||||
UserId int64
|
||||
OrgId int64
|
||||
Dashboard *Dashboard
|
||||
User *SignedInUser
|
||||
}
|
||||
|
||||
type ValidateDashboardAlertsCommand struct {
|
||||
UserId int64
|
||||
OrgId int64
|
||||
Dashboard *Dashboard
|
||||
User *SignedInUser
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ func init() {
|
||||
}
|
||||
|
||||
func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
|
||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId)
|
||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId, cmd.User)
|
||||
|
||||
return extractor.ValidateAlerts()
|
||||
}
|
||||
@ -19,11 +19,11 @@ func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
|
||||
func updateDashboardAlerts(cmd *m.UpdateDashboardAlertsCommand) error {
|
||||
saveAlerts := m.SaveAlertsCommand{
|
||||
OrgId: cmd.OrgId,
|
||||
UserId: cmd.UserId,
|
||||
UserId: cmd.User.UserId,
|
||||
DashboardId: cmd.Dashboard.Id,
|
||||
}
|
||||
|
||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId)
|
||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId, cmd.User)
|
||||
|
||||
alerts, err := extractor.GetAlerts()
|
||||
if err != nil {
|
||||
|
@ -13,14 +13,16 @@ import (
|
||||
|
||||
// DashAlertExtractor extracts alerts from the dashboard json
|
||||
type DashAlertExtractor struct {
|
||||
User *m.SignedInUser
|
||||
Dash *m.Dashboard
|
||||
OrgID int64
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
// NewDashAlertExtractor returns a new DashAlertExtractor
|
||||
func NewDashAlertExtractor(dash *m.Dashboard, orgID int64) *DashAlertExtractor {
|
||||
func NewDashAlertExtractor(dash *m.Dashboard, orgID int64, user *m.SignedInUser) *DashAlertExtractor {
|
||||
return &DashAlertExtractor{
|
||||
User: user,
|
||||
Dash: dash,
|
||||
OrgID: orgID,
|
||||
log: log.New("alerting.extractor"),
|
||||
@ -149,6 +151,21 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
|
||||
return nil, ValidationError{Reason: fmt.Sprintf("Data source used by alert rule not found, alertName=%v, datasource=%s", alert.Name, dsName)}
|
||||
}
|
||||
|
||||
dsFilterQuery := m.DatasourcesPermissionFilterQuery{
|
||||
User: e.User,
|
||||
Datasources: []*m.DataSource{datasource},
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&dsFilterQuery); err != nil {
|
||||
if err != bus.ErrHandlerNotFound {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if len(dsFilterQuery.Result) == 0 {
|
||||
return nil, m.ErrDataSourceAccessDenied
|
||||
}
|
||||
}
|
||||
|
||||
jsonQuery.SetPath([]string{"datasourceId"}, datasource.Id)
|
||||
|
||||
if interval, err := panel.Get("interval").String(); err == nil {
|
||||
|
@ -69,7 +69,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
So(getTarget(dashJson), ShouldEqual, "")
|
||||
})
|
||||
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
_, _ = extractor.GetAlerts()
|
||||
|
||||
Convey("Dashboard json should not be updated after extracting rules", func() {
|
||||
@ -83,7 +83,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
alerts, err := extractor.GetAlerts()
|
||||
|
||||
@ -146,7 +146,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
dashJson, err := simplejson.NewJson(panelWithoutId)
|
||||
So(err, ShouldBeNil)
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
_, err = extractor.GetAlerts()
|
||||
|
||||
@ -162,7 +162,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
dashJson, err := simplejson.NewJson(panelWithIdZero)
|
||||
So(err, ShouldBeNil)
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
_, err = extractor.GetAlerts()
|
||||
|
||||
@ -178,7 +178,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
dashJson, err := simplejson.NewJson(json)
|
||||
So(err, ShouldBeNil)
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
alerts, err := extractor.GetAlerts()
|
||||
|
||||
@ -198,7 +198,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
dashJson, err := simplejson.NewJson(json)
|
||||
So(err, ShouldBeNil)
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
alerts, err := extractor.GetAlerts()
|
||||
|
||||
@ -228,7 +228,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
dash := m.NewDashboardFromJson(dashJson)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
alerts, err := extractor.GetAlerts()
|
||||
|
||||
@ -248,7 +248,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
||||
dashJSON, err := simplejson.NewJson(json)
|
||||
So(err, ShouldBeNil)
|
||||
dash := m.NewDashboardFromJson(dashJSON)
|
||||
extractor := NewDashAlertExtractor(dash, 1)
|
||||
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||
|
||||
err = extractor.ValidateAlerts()
|
||||
|
||||
|
@ -13,6 +13,7 @@ type AlertTestCommand struct {
|
||||
Dashboard *simplejson.Json
|
||||
PanelId int64
|
||||
OrgId int64
|
||||
User *m.SignedInUser
|
||||
|
||||
Result *EvalContext
|
||||
}
|
||||
@ -25,7 +26,7 @@ func handleAlertTestCommand(cmd *AlertTestCommand) error {
|
||||
|
||||
dash := m.NewDashboardFromJson(cmd.Dashboard)
|
||||
|
||||
extractor := NewDashAlertExtractor(dash, cmd.OrgId)
|
||||
extractor := NewDashAlertExtractor(dash, cmd.OrgId, cmd.User)
|
||||
alerts, err := extractor.GetAlerts()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -90,6 +90,7 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
|
||||
validateAlertsCmd := models.ValidateDashboardAlertsCommand{
|
||||
OrgId: dto.OrgId,
|
||||
Dashboard: dash,
|
||||
User: dto.User,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&validateAlertsCmd); err != nil {
|
||||
@ -159,8 +160,8 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
|
||||
func (dr *dashboardServiceImpl) updateAlerting(cmd *models.SaveDashboardCommand, dto *SaveDashboardDTO) error {
|
||||
alertCmd := models.UpdateDashboardAlertsCommand{
|
||||
OrgId: dto.OrgId,
|
||||
UserId: dto.User.UserId,
|
||||
Dashboard: cmd.Result,
|
||||
User: dto.User,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&alertCmd); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user