mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add paused api endpoint
This commit is contained in:
parent
19427b59f9
commit
264590a9c2
@ -252,6 +252,27 @@ func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) R
|
|||||||
return ApiSuccess("Test notification sent")
|
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) {
|
func getAlertIdForRequest(c *middleware.Context) (int64, error) {
|
||||||
alertId := c.QueryInt64("alertId")
|
alertId := c.QueryInt64("alertId")
|
||||||
panelId := c.QueryInt64("panelId")
|
panelId := c.QueryInt64("panelId")
|
||||||
|
@ -252,6 +252,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
|
|
||||||
r.Group("/alerts", func() {
|
r.Group("/alerts", func() {
|
||||||
r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
|
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("/:alertId", ValidateOrgAlert, wrap(GetAlert))
|
||||||
r.Get("/", wrap(GetAlerts))
|
r.Get("/", wrap(GetAlerts))
|
||||||
r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
|
r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
|
||||||
|
@ -58,3 +58,9 @@ type NotificationTestCommand struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Settings *simplejson.Json `json:"settings"`
|
Settings *simplejson.Json `json:"settings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PauseAlertCommand struct {
|
||||||
|
AlertId int64 `json:"alertId"`
|
||||||
|
DashboardId int64 `json:"dashboardId"`
|
||||||
|
PanelId int64 `json:"panelId"`
|
||||||
|
}
|
||||||
|
@ -101,6 +101,12 @@ type SaveAlertsCommand struct {
|
|||||||
Alerts []*Alert
|
Alerts []*Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PauseAlertCommand struct {
|
||||||
|
OrgId int64
|
||||||
|
AlertId int64 `json:"alertId"`
|
||||||
|
Paused bool `json:"bool"`
|
||||||
|
}
|
||||||
|
|
||||||
type SetAlertStateCommand struct {
|
type SetAlertStateCommand struct {
|
||||||
AlertId int64
|
AlertId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
|
@ -18,6 +18,7 @@ func init() {
|
|||||||
bus.AddHandler("sql", GetAllAlertQueryHandler)
|
bus.AddHandler("sql", GetAllAlertQueryHandler)
|
||||||
bus.AddHandler("sql", SetAlertState)
|
bus.AddHandler("sql", SetAlertState)
|
||||||
bus.AddHandler("sql", GetAlertStatesForDashboard)
|
bus.AddHandler("sql", GetAlertStatesForDashboard)
|
||||||
|
bus.AddHandler("sql", PauseAlertRule)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAlertById(query *m.GetAlertByIdQuery) error {
|
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 {
|
func GetAlertStatesForDashboard(query *m.GetAlertStatesForDashboardQuery) error {
|
||||||
var rawSql = `SELECT
|
var rawSql = `SELECT
|
||||||
id,
|
id,
|
||||||
|
Loading…
Reference in New Issue
Block a user