mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): limit alerts to one per panel
This commit is contained in:
@@ -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() {
|
alerting := panel.Get("alerting")
|
||||||
alertDef := simplejson.NewFromAny(alertObj)
|
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(),
|
||||||
|
}
|
||||||
|
|
||||||
alert := Alert{
|
for _, targetsObj := range panel.Get("targets").MustArray() {
|
||||||
DashboardId: dash.Id,
|
target := simplejson.NewFromAny(targetsObj)
|
||||||
PanelId: panel.Get("id").MustInt64(),
|
|
||||||
Id: alertDef.Get("id").MustInt64(),
|
if target.Get("refId").MustString() == alert.QueryRefId {
|
||||||
Query: alertDef.Get("query").MustString(),
|
alert.Query = target.Get("target").MustString()
|
||||||
QueryRefId: alertDef.Get("query_ref").MustString(),
|
continue
|
||||||
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 alert.Query != "" {
|
||||||
alerts = append(alerts, alert)
|
alerts = append(alerts, alert)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,28 +66,16 @@ func TestAlertModel(t *testing.T) {
|
|||||||
"span": 12,
|
"span": 12,
|
||||||
"stack": false,
|
"stack": false,
|
||||||
"steppedLine": false,
|
"steppedLine": false,
|
||||||
"alerts": [
|
"alerting": {
|
||||||
{
|
"query_ref": "A",
|
||||||
"query_ref": "A",
|
"warn_level": 30,
|
||||||
"warn_level": 30,
|
"error_level": 50,
|
||||||
"error_level": 50,
|
"title": "desktop visiter alerts",
|
||||||
"title": "desktop visiter alerts",
|
"description": "Restart the webservers",
|
||||||
"description": "Restart the webservers",
|
"query_range": "5m",
|
||||||
"query_range": "5m",
|
"aggregator": "avg",
|
||||||
"aggregator": "avg",
|
"interval": 10
|
||||||
"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
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"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[0].Query, ShouldEqual, "statsd.fakesite.counters.session_start.desktop.count")
|
||||||
//So(alerts[1].Query, ShouldEqual, "statsd.fakesite.counters.session_start.mobile.count")
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user