mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add support for deleting alert rules
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -84,6 +84,10 @@ type SaveAlertsCommand struct {
|
||||
Alerts *[]AlertRule
|
||||
}
|
||||
|
||||
type DeleteAlertCommand struct {
|
||||
AlertId int64
|
||||
}
|
||||
|
||||
//Queries
|
||||
type GetAlertsQuery struct {
|
||||
OrgId int64
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user