diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 460ad56e1ab..aaa5752c446 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -252,6 +252,27 @@ func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) R return ApiSuccess("Test notification sent") } +//POST /api/alerts/:alertId/pause +func PauseAlert(c *middleware.Context, cmd models.PauseAlertCommand) Response { + cmd.OrgId = c.OrgId + cmd.AlertId = c.ParamsInt64(":alertId") + + if cmd.AlertId == 0 { + return ApiError(400, "Missing alert id", nil) + } + + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "", err) + } + + response := "un paused" + if cmd.Paused { + response = "paused" + } + + return ApiSuccess("Alert " + response) +} + func getAlertIdForRequest(c *middleware.Context) (int64, error) { alertId := c.QueryInt64("alertId") panelId := c.QueryInt64("panelId") diff --git a/pkg/api/api.go b/pkg/api/api.go index deb29d730eb..bf9e427214a 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -252,6 +252,7 @@ func Register(r *macaron.Macaron) { r.Group("/alerts", func() { r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest)) + r.Post("/:alertId/pause", ValidateOrgAlert, bind(m.PauseAlertCommand{}), wrap(PauseAlert)) r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert)) r.Get("/", wrap(GetAlerts)) r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard)) diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index e024768cd5e..a0aa632abd9 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -58,3 +58,9 @@ type NotificationTestCommand struct { Type string `json:"type"` Settings *simplejson.Json `json:"settings"` } + +type PauseAlertCommand struct { + AlertId int64 `json:"alertId"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` +} diff --git a/pkg/models/alert.go b/pkg/models/alert.go index 938be8ddbd4..419e832ae39 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -101,6 +101,12 @@ type SaveAlertsCommand struct { Alerts []*Alert } +type PauseAlertCommand struct { + OrgId int64 + AlertId int64 `json:"alertId"` + Paused bool `json:"bool"` +} + type SetAlertStateCommand struct { AlertId int64 OrgId int64 diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 61397017943..76ea268bc04 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -18,6 +18,7 @@ func init() { bus.AddHandler("sql", GetAllAlertQueryHandler) bus.AddHandler("sql", SetAlertState) bus.AddHandler("sql", GetAlertStatesForDashboard) + bus.AddHandler("sql", PauseAlertRule) } func GetAlertById(query *m.GetAlertByIdQuery) error { @@ -243,6 +244,29 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error { }) } +func PauseAlertRule(cmd *m.PauseAlertCommand) error { + return inTransaction(func(sess *xorm.Session) error { + alert := m.Alert{} + + if has, err := sess.Id(cmd.AlertId).Get(&alert); err != nil { + return err + } else if !has { + return fmt.Errorf("Could not find alert") + } + + var newState m.AlertStateType + if cmd.Paused { + newState = m.AlertStatePaused + } else { + newState = m.AlertStateNoData + } + alert.State = newState + + sess.Id(alert.Id).Update(&alert) + return nil + }) +} + func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error { var rawSql = `SELECT id,