Explorar o código

feat(alerting): limit alerts to one per panel

bergquist %!s(int64=9) %!d(string=hai) anos
pai
achega
832e38af34
Modificáronse 2 ficheiros con 35 adicións e 41 borrados
  1. 22 15
      pkg/models/alerts.go
  2. 13 26
      pkg/models/alerts_test.go

+ 22 - 15
pkg/models/alerts.go

@@ -29,24 +29,31 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]Alert {
 		for _, panelObj := range row.Get("panels").MustArray() {
 		for _, panelObj := range row.Get("panels").MustArray() {
 			panel := simplejson.NewFromAny(panelObj)
 			panel := simplejson.NewFromAny(panelObj)
 
 
-			for _, alertObj := range panel.Get("alerts").MustArray() {
-				alertDef := simplejson.NewFromAny(alertObj)
+			alerting := panel.Get("alerting")
+			alert := Alert{
+				DashboardId: dash.Id,
+				PanelId:     panel.Get("id").MustInt64(),
+				Id:          alerting.Get("id").MustInt64(),
+				QueryRefId:  alerting.Get("query_ref").MustString(),
+				WarnLevel:   alerting.Get("warn_level").MustInt64(),
+				ErrorLevel:  alerting.Get("error_level").MustInt64(),
+				Interval:    alerting.Get("interval").MustInt64(),
+				Title:       alerting.Get("title").MustString(),
+				Description: alerting.Get("description").MustString(),
+				QueryRange:  alerting.Get("query_range").MustString(),
+				Aggregator:  alerting.Get("aggregator").MustString(),
+			}
+
+			for _, targetsObj := range panel.Get("targets").MustArray() {
+				target := simplejson.NewFromAny(targetsObj)
 
 
-				alert := Alert{
-					DashboardId: dash.Id,
-					PanelId:     panel.Get("id").MustInt64(),
-					Id:          alertDef.Get("id").MustInt64(),
-					Query:       alertDef.Get("query").MustString(),
-					QueryRefId:  alertDef.Get("query_ref").MustString(),
-					WarnLevel:   alertDef.Get("warn_level").MustInt64(),
-					ErrorLevel:  alertDef.Get("error_level").MustInt64(),
-					Interval:    alertDef.Get("interval").MustInt64(),
-					Title:       alertDef.Get("title").MustString(),
-					Description: alertDef.Get("description").MustString(),
-					QueryRange:  alertDef.Get("query_range").MustString(),
-					Aggregator:  alertDef.Get("aggregator").MustString(),
+				if target.Get("refId").MustString() == alert.QueryRefId {
+					alert.Query = target.Get("target").MustString()
+					continue
 				}
 				}
+			}
 
 
+			if alert.Query != "" {
 				alerts = append(alerts, alert)
 				alerts = append(alerts, alert)
 			}
 			}
 		}
 		}

+ 13 - 26
pkg/models/alerts_test.go

@@ -66,28 +66,16 @@ func TestAlertModel(t *testing.T) {
           "span": 12,
           "span": 12,
           "stack": false,
           "stack": false,
           "steppedLine": false,
           "steppedLine": false,
-          "alerts": [
-            {
-              "query_ref": "A",
-              "warn_level": 30,
-              "error_level": 50,
-              "title": "desktop visiter alerts",
-              "description": "Restart the webservers",
-              "query_range": "5m",
-              "aggregator": "avg",
-              "interval": 10
-            },
-            {
-              "query_ref": "B",
-              "warn_level": 30,
-              "error_level": 50,
-              "title": "mobile visiter alerts",
-              "description": "Restart the webservers",
-              "query_range": "5m",
-              "aggregator": "avg",
-              "interval": 10
-            }
-          ],
+          "alerting": {
+						"query_ref": "A",
+						"warn_level": 30,
+						"error_level": 50,
+						"title": "desktop visiter alerts",
+						"description": "Restart the webservers",
+						"query_range": "5m",
+						"aggregator": "avg",
+						"interval": 10
+          },
           "targets": [
           "targets": [
             {
             {
               "hide": false,
               "hide": false,
@@ -276,7 +264,7 @@ func TestAlertModel(t *testing.T) {
 
 
 		Convey("all properties have been set", func() {
 		Convey("all properties have been set", func() {
 			So(alerts, ShouldNotBeEmpty)
 			So(alerts, ShouldNotBeEmpty)
-			So(len(alerts), ShouldEqual, 2)
+			So(len(alerts), ShouldEqual, 1)
 
 
 			for _, v := range alerts {
 			for _, v := range alerts {
 				So(v.DashboardId, ShouldNotEqual, 0)
 				So(v.DashboardId, ShouldNotEqual, 0)
@@ -286,7 +274,7 @@ func TestAlertModel(t *testing.T) {
 				So(v.ErrorLevel, ShouldEqual, 50)
 				So(v.ErrorLevel, ShouldEqual, 50)
 
 
 				So(v.Aggregator, ShouldNotBeEmpty)
 				So(v.Aggregator, ShouldNotBeEmpty)
-				//So(v.Query, ShouldNotBeEmpty)
+				So(v.Query, ShouldNotBeEmpty)
 				So(v.QueryRefId, ShouldNotBeEmpty)
 				So(v.QueryRefId, ShouldNotBeEmpty)
 				So(v.QueryRange, ShouldNotBeEmpty)
 				So(v.QueryRange, ShouldNotBeEmpty)
 				So(v.Title, ShouldNotBeEmpty)
 				So(v.Title, ShouldNotBeEmpty)
@@ -295,8 +283,7 @@ func TestAlertModel(t *testing.T) {
 				fmt.Println(v.Query)
 				fmt.Println(v.Query)
 			}
 			}
 
 
-			//So(alerts[0].Query, ShouldEqual, "statsd.fakesite.counters.session_start.desktop.count")
-			//So(alerts[1].Query, ShouldEqual, "statsd.fakesite.counters.session_start.mobile.count")
+			So(alerts[0].Query, ShouldEqual, "statsd.fakesite.counters.session_start.desktop.count")
 		})
 		})
 	})
 	})
 }
 }