Browse Source

feat(alerting): add paused api endpoint

bergquist 9 years ago
parent
commit
264590a9c2
5 changed files with 58 additions and 0 deletions
  1. 21 0
      pkg/api/alerting.go
  2. 1 0
      pkg/api/api.go
  3. 6 0
      pkg/api/dtos/alerting.go
  4. 6 0
      pkg/models/alert.go
  5. 24 0
      pkg/services/sqlstore/alert.go

+ 21 - 0
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")

+ 1 - 0
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))

+ 6 - 0
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"`
+}

+ 6 - 0
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

+ 24 - 0
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,