mirror of
https://github.com/grafana/grafana.git
synced 2024-12-29 10:21:41 -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,
|
OrgId: c.OrgId,
|
||||||
Dashboard: dto.Dashboard,
|
Dashboard: dto.Dashboard,
|
||||||
PanelId: dto.PanelId,
|
PanelId: dto.PanelId,
|
||||||
|
User: c.SignedInUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&backendCmd); err != nil {
|
if err := bus.Dispatch(&backendCmd); err != nil {
|
||||||
if validationErr, ok := err.(alerting.ValidationError); ok {
|
if validationErr, ok := err.(alerting.ValidationError); ok {
|
||||||
return Error(422, validationErr.Error(), nil)
|
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)
|
return Error(500, "Failed to test rule", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,13 +215,14 @@ type AlertStateInfoDTO struct {
|
|||||||
// "Internal" commands
|
// "Internal" commands
|
||||||
|
|
||||||
type UpdateDashboardAlertsCommand struct {
|
type UpdateDashboardAlertsCommand struct {
|
||||||
UserId int64
|
|
||||||
OrgId int64
|
OrgId int64
|
||||||
Dashboard *Dashboard
|
Dashboard *Dashboard
|
||||||
|
User *SignedInUser
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidateDashboardAlertsCommand struct {
|
type ValidateDashboardAlertsCommand struct {
|
||||||
UserId int64
|
UserId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
Dashboard *Dashboard
|
Dashboard *Dashboard
|
||||||
|
User *SignedInUser
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
|
func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
|
||||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId)
|
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId, cmd.User)
|
||||||
|
|
||||||
return extractor.ValidateAlerts()
|
return extractor.ValidateAlerts()
|
||||||
}
|
}
|
||||||
@ -19,11 +19,11 @@ func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
|
|||||||
func updateDashboardAlerts(cmd *m.UpdateDashboardAlertsCommand) error {
|
func updateDashboardAlerts(cmd *m.UpdateDashboardAlertsCommand) error {
|
||||||
saveAlerts := m.SaveAlertsCommand{
|
saveAlerts := m.SaveAlertsCommand{
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
UserId: cmd.UserId,
|
UserId: cmd.User.UserId,
|
||||||
DashboardId: cmd.Dashboard.Id,
|
DashboardId: cmd.Dashboard.Id,
|
||||||
}
|
}
|
||||||
|
|
||||||
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId)
|
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId, cmd.User)
|
||||||
|
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,14 +13,16 @@ import (
|
|||||||
|
|
||||||
// DashAlertExtractor extracts alerts from the dashboard json
|
// DashAlertExtractor extracts alerts from the dashboard json
|
||||||
type DashAlertExtractor struct {
|
type DashAlertExtractor struct {
|
||||||
|
User *m.SignedInUser
|
||||||
Dash *m.Dashboard
|
Dash *m.Dashboard
|
||||||
OrgID int64
|
OrgID int64
|
||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDashAlertExtractor returns a new DashAlertExtractor
|
// 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{
|
return &DashAlertExtractor{
|
||||||
|
User: user,
|
||||||
Dash: dash,
|
Dash: dash,
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
log: log.New("alerting.extractor"),
|
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)}
|
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)
|
jsonQuery.SetPath([]string{"datasourceId"}, datasource.Id)
|
||||||
|
|
||||||
if interval, err := panel.Get("interval").String(); err == nil {
|
if interval, err := panel.Get("interval").String(); err == nil {
|
||||||
|
@ -69,7 +69,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
So(getTarget(dashJson), ShouldEqual, "")
|
So(getTarget(dashJson), ShouldEqual, "")
|
||||||
})
|
})
|
||||||
|
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
_, _ = extractor.GetAlerts()
|
_, _ = extractor.GetAlerts()
|
||||||
|
|
||||||
Convey("Dashboard json should not be updated after extracting rules", func() {
|
Convey("Dashboard json should not be updated after extracting rules", func() {
|
||||||
@ -83,7 +83,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
dashJson, err := simplejson.NewJson(panelWithoutId)
|
dashJson, err := simplejson.NewJson(panelWithoutId)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
_, err = extractor.GetAlerts()
|
_, err = extractor.GetAlerts()
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
dashJson, err := simplejson.NewJson(panelWithIdZero)
|
dashJson, err := simplejson.NewJson(panelWithIdZero)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
_, err = extractor.GetAlerts()
|
_, err = extractor.GetAlerts()
|
||||||
|
|
||||||
@ -178,7 +178,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
dashJson, err := simplejson.NewJson(json)
|
dashJson, err := simplejson.NewJson(json)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
dashJson, err := simplejson.NewJson(json)
|
dashJson, err := simplejson.NewJson(json)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
|
|
||||||
@ -228,7 +228,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
dash := m.NewDashboardFromJson(dashJson)
|
dash := m.NewDashboardFromJson(dashJson)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
|
|
||||||
@ -248,7 +248,7 @@ func TestAlertRuleExtraction(t *testing.T) {
|
|||||||
dashJSON, err := simplejson.NewJson(json)
|
dashJSON, err := simplejson.NewJson(json)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
dash := m.NewDashboardFromJson(dashJSON)
|
dash := m.NewDashboardFromJson(dashJSON)
|
||||||
extractor := NewDashAlertExtractor(dash, 1)
|
extractor := NewDashAlertExtractor(dash, 1, nil)
|
||||||
|
|
||||||
err = extractor.ValidateAlerts()
|
err = extractor.ValidateAlerts()
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ type AlertTestCommand struct {
|
|||||||
Dashboard *simplejson.Json
|
Dashboard *simplejson.Json
|
||||||
PanelId int64
|
PanelId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
|
User *m.SignedInUser
|
||||||
|
|
||||||
Result *EvalContext
|
Result *EvalContext
|
||||||
}
|
}
|
||||||
@ -25,7 +26,7 @@ func handleAlertTestCommand(cmd *AlertTestCommand) error {
|
|||||||
|
|
||||||
dash := m.NewDashboardFromJson(cmd.Dashboard)
|
dash := m.NewDashboardFromJson(cmd.Dashboard)
|
||||||
|
|
||||||
extractor := NewDashAlertExtractor(dash, cmd.OrgId)
|
extractor := NewDashAlertExtractor(dash, cmd.OrgId, cmd.User)
|
||||||
alerts, err := extractor.GetAlerts()
|
alerts, err := extractor.GetAlerts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -90,6 +90,7 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
|
|||||||
validateAlertsCmd := models.ValidateDashboardAlertsCommand{
|
validateAlertsCmd := models.ValidateDashboardAlertsCommand{
|
||||||
OrgId: dto.OrgId,
|
OrgId: dto.OrgId,
|
||||||
Dashboard: dash,
|
Dashboard: dash,
|
||||||
|
User: dto.User,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&validateAlertsCmd); err != nil {
|
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 {
|
func (dr *dashboardServiceImpl) updateAlerting(cmd *models.SaveDashboardCommand, dto *SaveDashboardDTO) error {
|
||||||
alertCmd := models.UpdateDashboardAlertsCommand{
|
alertCmd := models.UpdateDashboardAlertsCommand{
|
||||||
OrgId: dto.OrgId,
|
OrgId: dto.OrgId,
|
||||||
UserId: dto.User.UserId,
|
|
||||||
Dashboard: cmd.Result,
|
Dashboard: cmd.Result,
|
||||||
|
User: dto.User,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&alertCmd); err != nil {
|
if err := bus.Dispatch(&alertCmd); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user