ソースを参照

feat(alerting): add parameteters for filtering alerts

bergquist 9 年 前
コミット
1bb8bc58b8

+ 4 - 33
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)
-}

+ 0 - 5
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

+ 4 - 15
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
-}

+ 10 - 31
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
-}

+ 2 - 2
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)

+ 23 - 25
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)