From f442adca47bd1065321f13a2fe7113139310b827 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 28 Apr 2016 15:13:42 +0200 Subject: [PATCH] feat(alerting): adds api endpoints for alerts per dashboard and panel --- pkg/api/alerting.go | 31 +++++++++++ pkg/api/api.go | 3 ++ pkg/models/alerts.go | 13 +++++ .../{alerting_state.go => alerts_state.go} | 0 pkg/services/sqlstore/alert_rule.go | 23 +++++---- .../sqlstore/alert_rule_changes_test.go | 5 +- pkg/services/sqlstore/alert_rule_test.go | 51 +++++++++++-------- public/app/features/alerts/alert_log_ctrl.ts | 1 + .../features/alerts/partials/alert_log.html | 2 +- 9 files changed, 95 insertions(+), 34 deletions(-) rename pkg/models/{alerting_state.go => alerts_state.go} (100%) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index c45cb120f3b..8efe60374e2 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -136,3 +136,34 @@ func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Re return Json(200, cmd.Result) } + +// GET /api/alerts-dashboard/:dashboardId +func GetAlertsForDashboard(c *middleware.Context) Response { + dashboardId := c.ParamsInt64(":dashboardId") + query := &models.GetAlertsForDashboardQuery{ + DashboardId: dashboardId, + } + + if err := bus.Dispatch(&query); err != nil { + return ApiError(500, "Failed get alert ", err) + } + + return Json(200, query.Result) +} + +// GET /api/alerts-dashboard/:dashboardId/:panelId +func GetAlertsForPanel(c *middleware.Context) Response { + dashboardId := c.ParamsInt64(":dashboardId") + panelId := c.ParamsInt64(":panelId") + + query := &models.GetAlertForPanelQuery{ + DashboardId: dashboardId, + PanelId: panelId, + } + + if err := bus.Dispatch(&query); err != nil { + return ApiError(500, "Failed get alert ", err) + } + + return Json(200, query.Result) +} diff --git a/pkg/api/api.go b/pkg/api/api.go index f6e321b1b21..dde47a0e865 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -246,6 +246,9 @@ func Register(r *macaron.Macaron) { r.Get("/:id", ValidateOrgAlert, wrap(GetAlert)) }) + r.Get("/alerts-dashboard/:dashboardId", wrap(GetAlertsForDashboard)) + r.Get("/alerts-dashboard/:dashboardId/:panelId", wrap(GetAlertsForPanel)) + }, reqSignedIn) // admin api diff --git a/pkg/models/alerts.go b/pkg/models/alerts.go index f9c8a151bc2..df89ad25869 100644 --- a/pkg/models/alerts.go +++ b/pkg/models/alerts.go @@ -102,3 +102,16 @@ type GetAlertChangesQuery struct { Result []AlertRuleChange } + +type GetAlertsForDashboardQuery struct { + DashboardId int64 + + Result []AlertRule +} + +type GetAlertForPanelQuery struct { + DashboardId int64 + PanelId int64 + + Result AlertRule +} diff --git a/pkg/models/alerting_state.go b/pkg/models/alerts_state.go similarity index 100% rename from pkg/models/alerting_state.go rename to pkg/models/alerts_state.go diff --git a/pkg/services/sqlstore/alert_rule.go b/pkg/services/sqlstore/alert_rule.go index 523705ad6d9..64479da3dbe 100644 --- a/pkg/services/sqlstore/alert_rule.go +++ b/pkg/services/sqlstore/alert_rule.go @@ -11,6 +11,8 @@ func init() { bus.AddHandler("sql", SaveAlerts) bus.AddHandler("sql", GetAllAlertsForOrg) bus.AddHandler("sql", GetAlertById) + bus.AddHandler("sql", GetAlertsByDashboardId) + bus.AddHandler("sql", GetAlertsByDashboardAndPanelId) } func GetAlertById(query *m.GetAlertByIdQuery) error { @@ -160,28 +162,29 @@ func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]m.AlertRu return alerts, nil } -func GetAlertsByDashboardId(dashboardId int64) ([]m.AlertRule, error) { +func GetAlertsByDashboardId(cmd *m.GetAlertsForDashboardQuery) error { alerts := make([]m.AlertRule, 0) - err := x.Where("dashboard_id = ?", dashboardId).Find(&alerts) + err := x.Where("dashboard_id = ?", cmd.DashboardId).Find(&alerts) if err != nil { - return []m.AlertRule{}, err + return err } - return alerts, nil + cmd.Result = alerts + return nil } -func GetAlertsByDashboardAndPanelId(dashboardId, panelId int64) (m.AlertRule, error) { +func GetAlertsByDashboardAndPanelId(cmd *m.GetAlertForPanelQuery) error { alerts := make([]m.AlertRule, 0) - err := x.Where("dashboard_id = ? and panel_id = ?", dashboardId, panelId).Find(&alerts) + err := x.Where("dashboard_id = ? and panel_id = ?", cmd.DashboardId, cmd.PanelId).Find(&alerts) if err != nil { - return m.AlertRule{}, err + return err } if len(alerts) != 1 { - return m.AlertRule{}, err + return err } - - return alerts[0], nil + cmd.Result = alerts[0] + return nil } diff --git a/pkg/services/sqlstore/alert_rule_changes_test.go b/pkg/services/sqlstore/alert_rule_changes_test.go index 78e218533bd..3605789b3b8 100644 --- a/pkg/services/sqlstore/alert_rule_changes_test.go +++ b/pkg/services/sqlstore/alert_rule_changes_test.go @@ -59,11 +59,12 @@ func TestAlertRuleChangesDataAccess(t *testing.T) { So(err, ShouldBeNil) Convey("Alerts should be removed", func() { - alerts, err2 := GetAlertsByDashboardId(testDash.Id) + query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} + err2 := GetAlertsByDashboardId(&query) So(testDash.Id, ShouldEqual, 1) So(err2, ShouldBeNil) - So(len(alerts), ShouldEqual, 0) + So(len(query.Result), ShouldEqual, 0) }) Convey("should add one more alert_rule_change", func() { diff --git a/pkg/services/sqlstore/alert_rule_test.go b/pkg/services/sqlstore/alert_rule_test.go index e920d05a2c4..5b41da11aac 100644 --- a/pkg/services/sqlstore/alert_rule_test.go +++ b/pkg/services/sqlstore/alert_rule_test.go @@ -51,19 +51,23 @@ func TestAlertingDataAccess(t *testing.T) { }) Convey("Can read properties", func() { - alert, err2 := GetAlertsByDashboardAndPanelId(testDash.Id, 1) + query := m.GetAlertForPanelQuery{ + DashboardId: testDash.Id, + PanelId: 1, + } + err2 := GetAlertsByDashboardAndPanelId(&query) So(err2, ShouldBeNil) - So(alert.Interval, ShouldEqual, "10") - So(alert.WarnLevel, ShouldEqual, "> 30") - So(alert.CritLevel, ShouldEqual, "> 50") - So(alert.Query, ShouldEqual, "Query") - So(alert.QueryRefId, ShouldEqual, "A") - So(alert.Title, ShouldEqual, "Alerting title") - So(alert.Description, ShouldEqual, "Alerting description") - So(alert.QueryRange, ShouldEqual, "5m") - So(alert.Aggregator, ShouldEqual, "avg") - So(alert.State, ShouldEqual, "OK") + So(query.Result.Interval, ShouldEqual, "10") + So(query.Result.WarnLevel, ShouldEqual, "> 30") + So(query.Result.CritLevel, ShouldEqual, "> 50") + So(query.Result.Query, ShouldEqual, "Query") + So(query.Result.QueryRefId, ShouldEqual, "A") + So(query.Result.Title, ShouldEqual, "Alerting title") + So(query.Result.Description, ShouldEqual, "Alerting description") + So(query.Result.QueryRange, ShouldEqual, "5m") + So(query.Result.Aggregator, ShouldEqual, "avg") + So(query.Result.State, ShouldEqual, "OK") }) Convey("Alerts with same dashboard id and panel id should update", func() { @@ -85,14 +89,15 @@ func TestAlertingDataAccess(t *testing.T) { }) Convey("Alerts should be updated", func() { - alerts, err2 := GetAlertsByDashboardId(testDash.Id) + query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} + err2 := GetAlertsByDashboardId(&query) So(err2, ShouldBeNil) - So(len(alerts), ShouldEqual, 1) - So(alerts[0].Query, ShouldEqual, "Updated Query") + So(len(query.Result), ShouldEqual, 1) + So(query.Result[0].Query, ShouldEqual, "Updated Query") Convey("Alert state should not be updated", func() { - So(alerts[0].State, ShouldEqual, "OK") + So(query.Result[0].State, ShouldEqual, "OK") }) }) @@ -135,9 +140,11 @@ func TestAlertingDataAccess(t *testing.T) { Convey("Should save 3 dashboards", func() { So(err, ShouldBeNil) - alerts, err2 := GetAlertsByDashboardId(testDash.Id) + queryForDashboard := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} + err2 := GetAlertsByDashboardId(&queryForDashboard) + So(err2, ShouldBeNil) - So(len(alerts), ShouldEqual, 3) + So(len(queryForDashboard.Result), ShouldEqual, 3) query := &m.GetAlertChangesQuery{OrgId: 1} er := GetAlertRuleChanges(query) @@ -152,9 +159,10 @@ func TestAlertingDataAccess(t *testing.T) { err = SaveAlerts(&cmd) Convey("should delete the missing alert", func() { - alerts, err2 := GetAlertsByDashboardId(testDash.Id) + query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} + err2 := GetAlertsByDashboardId(&query) So(err2, ShouldBeNil) - So(len(alerts), ShouldEqual, 2) + So(len(query.Result), ShouldEqual, 2) }) Convey("should add one more alert_rule_change", func() { @@ -200,11 +208,12 @@ func TestAlertingDataAccess(t *testing.T) { So(err, ShouldBeNil) Convey("Alerts should be removed", func() { - alerts, err2 := GetAlertsByDashboardId(testDash.Id) + query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} + err2 := GetAlertsByDashboardId(&query) So(testDash.Id, ShouldEqual, 1) So(err2, ShouldBeNil) - So(len(alerts), ShouldEqual, 0) + So(len(query.Result), ShouldEqual, 0) }) }) }) diff --git a/public/app/features/alerts/alert_log_ctrl.ts b/public/app/features/alerts/alert_log_ctrl.ts index 191f6337e99..045e6a878b5 100644 --- a/public/app/features/alerts/alert_log_ctrl.ts +++ b/public/app/features/alerts/alert_log_ctrl.ts @@ -21,6 +21,7 @@ export class AlertLogCtrl { loadAlertLogs() { this.backendSrv.get('/api/alerts/events/' + this.alertId).then(result => { + console.log(result); this.alertLogs = result; }); diff --git a/public/app/features/alerts/partials/alert_log.html b/public/app/features/alerts/partials/alert_log.html index 3fb4a805854..028b9bfc638 100644 --- a/public/app/features/alerts/partials/alert_log.html +++ b/public/app/features/alerts/partials/alert_log.html @@ -8,7 +8,7 @@ - +
Status Time Description