From 1bb8bc58b89d1f29b8c73cf0450bc2fc542d55f9 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 10 May 2016 09:45:56 +0200 Subject: [PATCH] feat(alerting): add parameteters for filtering alerts --- pkg/api/alerting.go | 37 ++------------ pkg/api/api.go | 5 -- pkg/models/alerts.go | 19 ++------ pkg/services/sqlstore/alert_rule.go | 41 ++++------------ .../sqlstore/alert_rule_changes_test.go | 4 +- pkg/services/sqlstore/alert_rule_test.go | 48 +++++++++---------- 6 files changed, 43 insertions(+), 111 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 8a4ee443e98..39c3fe3f54e 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -46,8 +46,10 @@ func GetAlertChanges(c *middleware.Context) Response { // GET /api/alerts func GetAlerts(c *middleware.Context) Response { query := models.GetAlertsQuery{ - OrgId: c.OrgId, - State: c.QueryStrings("state"), + OrgId: c.OrgId, + State: c.QueryStrings("state"), + DashboardId: c.QueryInt64("dashboardId"), + PanelId: c.QueryInt64("panelId"), } if err := bus.Dispatch(&query); err != nil { @@ -161,34 +163,3 @@ 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 3d216fd7587..d4b450720c2 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -252,11 +252,6 @@ func Register(r *macaron.Macaron) { r.Get("/changes", wrap(GetAlertChanges)) }) - r.Get("/alert-changes", wrap(GetAlertChanges)) - - 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 82edfdbefeb..94bf430659c 100644 --- a/pkg/models/alerts.go +++ b/pkg/models/alerts.go @@ -95,8 +95,10 @@ type DeleteAlertCommand struct { //Queries type GetAlertsQuery struct { - OrgId int64 - State []string + OrgId int64 + State []string + DashboardId int64 + PanelId int64 Result []AlertRule } @@ -114,16 +116,3 @@ 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/services/sqlstore/alert_rule.go b/pkg/services/sqlstore/alert_rule.go index 00bb2aa9964..c3157a7f617 100644 --- a/pkg/services/sqlstore/alert_rule.go +++ b/pkg/services/sqlstore/alert_rule.go @@ -13,8 +13,6 @@ func init() { bus.AddHandler("sql", SaveAlerts) bus.AddHandler("sql", HandleAlertsQuery) bus.AddHandler("sql", GetAlertById) - bus.AddHandler("sql", GetAlertsByDashboardId) - bus.AddHandler("sql", GetAlertsByDashboardAndPanelId) bus.AddHandler("sql", DeleteAlertById) } @@ -54,8 +52,17 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { sql.WriteString(`WHERE org_id = ?`) params = append(params, query.OrgId) - if len(query.State) > 0 { + if query.DashboardId != 0 { + sql.WriteString(` AND dashboard_id = ?`) + params = append(params, query.DashboardId) + } + if query.PanelId != 0 { + sql.WriteString(` AND panel_id = ?`) + params = append(params, query.PanelId) + } + + if len(query.State) > 0 { sql.WriteString(` AND (`) for i, v := range query.State { if i > 0 { @@ -65,7 +72,6 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { params = append(params, strings.ToUpper(v)) } sql.WriteString(")") - } alerts := make([]m.AlertRule, 0) @@ -201,30 +207,3 @@ func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]m.AlertRu return alerts, nil } - -func GetAlertsByDashboardId(cmd *m.GetAlertsForDashboardQuery) error { - alerts := make([]m.AlertRule, 0) - err := x.Where("dashboard_id = ?", cmd.DashboardId).Find(&alerts) - - if err != nil { - return err - } - - cmd.Result = alerts - return nil -} - -func GetAlertsByDashboardAndPanelId(cmd *m.GetAlertForPanelQuery) error { - alerts := make([]m.AlertRule, 0) - err := x.Where("dashboard_id = ? and panel_id = ?", cmd.DashboardId, cmd.PanelId).Find(&alerts) - - if err != nil { - return err - } - - if len(alerts) != 1 { - return err - } - 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 dce2e3b262d..ed939497250 100644 --- a/pkg/services/sqlstore/alert_rule_changes_test.go +++ b/pkg/services/sqlstore/alert_rule_changes_test.go @@ -61,8 +61,8 @@ func TestAlertRuleChangesDataAccess(t *testing.T) { So(err, ShouldBeNil) Convey("Alerts should be removed", func() { - query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} - err2 := GetAlertsByDashboardId(&query) + query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} + err2 := HandleAlertsQuery(&query) So(testDash.Id, ShouldEqual, 1) So(err2, ShouldBeNil) diff --git a/pkg/services/sqlstore/alert_rule_test.go b/pkg/services/sqlstore/alert_rule_test.go index 480b9f42326..becffcfa1ba 100644 --- a/pkg/services/sqlstore/alert_rule_test.go +++ b/pkg/services/sqlstore/alert_rule_test.go @@ -52,25 +52,23 @@ func TestAlertingDataAccess(t *testing.T) { }) Convey("Can read properties", func() { - query := m.GetAlertForPanelQuery{ - DashboardId: testDash.Id, - PanelId: 1, - } - err2 := GetAlertsByDashboardAndPanelId(&query) + alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1} + err2 := HandleAlertsQuery(&alertQuery) + alert := alertQuery.Result[0] So(err2, ShouldBeNil) - So(query.Result.Interval, ShouldEqual, "10") - So(query.Result.WarnLevel, ShouldEqual, 30) - So(query.Result.CritLevel, ShouldEqual, 50) - So(query.Result.WarnOperator, ShouldEqual, ">") - So(query.Result.CritOperator, ShouldEqual, ">") - 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") + So(alert.Interval, ShouldEqual, "10") + So(alert.WarnLevel, ShouldEqual, 30) + So(alert.CritLevel, ShouldEqual, 50) + So(alert.WarnOperator, ShouldEqual, ">") + So(alert.CritOperator, ShouldEqual, ">") + 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") }) Convey("Alerts with same dashboard id and panel id should update", func() { @@ -92,8 +90,8 @@ func TestAlertingDataAccess(t *testing.T) { }) Convey("Alerts should be updated", func() { - query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} - err2 := GetAlertsByDashboardId(&query) + query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} + err2 := HandleAlertsQuery(&query) So(err2, ShouldBeNil) So(len(query.Result), ShouldEqual, 1) @@ -143,8 +141,8 @@ func TestAlertingDataAccess(t *testing.T) { Convey("Should save 3 dashboards", func() { So(err, ShouldBeNil) - queryForDashboard := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} - err2 := GetAlertsByDashboardId(&queryForDashboard) + queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} + err2 := HandleAlertsQuery(&queryForDashboard) So(err2, ShouldBeNil) So(len(queryForDashboard.Result), ShouldEqual, 3) @@ -162,8 +160,8 @@ func TestAlertingDataAccess(t *testing.T) { err = SaveAlerts(&cmd) Convey("should delete the missing alert", func() { - query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} - err2 := GetAlertsByDashboardId(&query) + query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} + err2 := HandleAlertsQuery(&query) So(err2, ShouldBeNil) So(len(query.Result), ShouldEqual, 2) }) @@ -213,8 +211,8 @@ func TestAlertingDataAccess(t *testing.T) { So(err, ShouldBeNil) Convey("Alerts should be removed", func() { - query := m.GetAlertsForDashboardQuery{DashboardId: testDash.Id} - err2 := GetAlertsByDashboardId(&query) + query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} + err2 := HandleAlertsQuery(&query) So(testDash.Id, ShouldEqual, 1) So(err2, ShouldBeNil)