From 743a6fa37c4c5a800f34d8a4ed3c14551cbff8d3 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 2 May 2016 16:07:19 +0200 Subject: [PATCH] feat(alerting): add support for deleting alert rules --- pkg/api/alerting.go | 20 +++++++++++++++++++- pkg/api/api.go | 3 ++- pkg/models/alerts.go | 4 ++++ pkg/services/sqlstore/alert_rule.go | 11 +++++++++++ public/app/features/alerts/alerts_ctrl.ts | 10 ++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 8efe60374e2..b8f8a010f8e 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -8,7 +8,7 @@ import ( ) func ValidateOrgAlert(c *middleware.Context) { - id := c.ParamsInt64(":id") + id := c.ParamsInt64(":alertId") query := models.GetAlertByIdQuery{Id: id} if err := bus.Dispatch(&query); err != nil { @@ -98,6 +98,24 @@ func GetAlert(c *middleware.Context) Response { return Json(200, &query.Result) } +// DEL /api/alerts/:id +func DelAlert(c *middleware.Context) Response { + alertId := c.ParamsInt64(":alertId") + + if alertId == 0 { + return ApiError(401, "Failed to parse alertid", nil) + } + + cmd := models.DeleteAlertCommand{AlertId: alertId} + + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "Failed to delete alert", err) + } + + var resp = map[string]interface{}{"alertId": alertId} + return Json(200, resp) +} + // GET /api/alerts/state/:id func GetAlertState(c *middleware.Context) Response { alertId := c.ParamsInt64(":alertId") diff --git a/pkg/api/api.go b/pkg/api/api.go index dde47a0e865..fa3717e165e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -243,7 +243,8 @@ func Register(r *macaron.Macaron) { r.Put("/events/:alertId", bind(m.UpdateAlertStateCommand{}), wrap(PutAlertState)) r.Get("/changes", wrap(GetAlertChanges)) r.Get("/", wrap(GetAlerts)) - r.Get("/:id", ValidateOrgAlert, wrap(GetAlert)) + r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert)) + r.Delete("/:alertId", ValidateOrgAlert, wrap(DelAlert)) }) r.Get("/alerts-dashboard/:dashboardId", wrap(GetAlertsForDashboard)) diff --git a/pkg/models/alerts.go b/pkg/models/alerts.go index df89ad25869..159ea02f328 100644 --- a/pkg/models/alerts.go +++ b/pkg/models/alerts.go @@ -84,6 +84,10 @@ type SaveAlertsCommand struct { Alerts *[]AlertRule } +type DeleteAlertCommand struct { + AlertId int64 +} + //Queries type GetAlertsQuery struct { OrgId int64 diff --git a/pkg/services/sqlstore/alert_rule.go b/pkg/services/sqlstore/alert_rule.go index 64479da3dbe..86743f5894b 100644 --- a/pkg/services/sqlstore/alert_rule.go +++ b/pkg/services/sqlstore/alert_rule.go @@ -13,6 +13,7 @@ func init() { bus.AddHandler("sql", GetAlertById) bus.AddHandler("sql", GetAlertsByDashboardId) bus.AddHandler("sql", GetAlertsByDashboardAndPanelId) + bus.AddHandler("sql", DeleteAlertById) } func GetAlertById(query *m.GetAlertByIdQuery) error { @@ -30,6 +31,16 @@ func GetAlertById(query *m.GetAlertByIdQuery) error { return nil } +func DeleteAlertById(cmd *m.DeleteAlertCommand) error { + return inTransaction(func(sess *xorm.Session) error { + if _, err := sess.Exec("DELETE FROM alert_rule WHERE id = ?", cmd.AlertId); err != nil { + return err + } + + return nil + }) +} + func GetAllAlertsForOrg(query *m.GetAlertsQuery) error { alerts := make([]m.AlertRule, 0) if err := x.Where("org_id = ?", query.OrgId).Find(&alerts); err != nil { diff --git a/public/app/features/alerts/alerts_ctrl.ts b/public/app/features/alerts/alerts_ctrl.ts index dc89746f370..7b07b18ec18 100644 --- a/public/app/features/alerts/alerts_ctrl.ts +++ b/public/app/features/alerts/alerts_ctrl.ts @@ -23,6 +23,16 @@ export class AlertPageCtrl { }); }); } + + deleteAlert(alert) { + this.backendSrv.delete('/api/alerts/' + alert.id).then(result => { + if (result.alertId) { + this.alerts = this.alerts.filter(alert => { + return alert.id !== result.alertId; + }); + } + }); + } } coreModule.controller('AlertPageCtrl', AlertPageCtrl);