feat(alerting): add datasource name field

This commit is contained in:
bergquist
2016-04-27 09:06:19 +02:00
parent 973db1ac38
commit 7041169ffb
5 changed files with 210 additions and 118 deletions

View File

@@ -19,6 +19,7 @@ type AlertRule struct {
Description string
QueryRange string
Aggregator string
DatasourceName string
}
type AlertRuleChange struct {
@@ -42,6 +43,7 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
DashboardId: cmd.Result.Id,
OrgId: cmd.Result.OrgId,
PanelId: panel.Get("id").MustInt64(),
DatasourceName: panel.Get("datasource").MustString(),
Id: alerting.Get("id").MustInt64(),
QueryRefId: alerting.Get("queryRef").MustString(),
WarnLevel: alerting.Get("warnLevel").MustString(),

View File

@@ -3,7 +3,6 @@ package models
import (
"testing"
"fmt"
"github.com/grafana/grafana/pkg/components/simplejson"
. "github.com/smartystreets/goconvey/convey"
)
@@ -12,7 +11,7 @@ func TestAlertModel(t *testing.T) {
Convey("Parsing alerts from dashboard", t, func() {
json := `{
"id": 7,
"id": 57,
"title": "Graphite 4",
"originalTitle": "Graphite 4",
"tags": [
@@ -30,92 +29,172 @@ func TestAlertModel(t *testing.T) {
"height": "250px",
"panels": [
{
"aliasColors": {},
"bars": false,
"datasource": null,
"editable": true,
"title": "Active desktop users",
"error": false,
"fill": 1,
"span": 6,
"editable": true,
"type": "graph",
"isNew": true,
"id": 3,
"targets": [
{
"refId": "A",
"target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
}
],
"datasource": null,
"renderer": "flot",
"yaxes": [
{
"label": null,
"show": true,
"logBase": 1,
"min": null,
"max": null,
"format": "short"
},
{
"label": null,
"show": true,
"logBase": 1,
"min": null,
"max": null,
"format": "short"
}
],
"xaxis": {
"show": true
},
"grid": {
"threshold1": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2Color": "rgba(234, 112, 112, 0.22)"
},
"id": 1,
"isNew": true,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"fill": 1,
"linewidth": 2,
"nullPointMode": "connected",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"span": 12,
"pointradius": 5,
"bars": false,
"stack": false,
"percentage": false,
"legend": {
"show": true,
"values": false,
"min": false,
"max": false,
"current": false,
"total": false,
"avg": false
},
"nullPointMode": "connected",
"steppedLine": false,
"tooltip": {
"value_type": "cumulative",
"shared": true,
"msResolution": false
},
"timeFrom": null,
"timeShift": null,
"aliasColors": {},
"seriesOverrides": [],
"alerting": {
"queryRef": "A",
"warnLevel": "> 30",
"critLevel": "> 50",
"title": "desktop visiter alerts",
"description": "Restart the webservers",
"queryRange": "5m",
"aggregator": "avg",
"interval": "10"
"aggregator": "sum",
"queryRange": "10m",
"interval": "10s",
"title": "active desktop users",
"description": "restart webservers"
},
"links": []
},
{
"title": "Active mobile users",
"error": false,
"span": 6,
"editable": true,
"type": "graph",
"isNew": true,
"id": 4,
"targets": [
{
"hide": false,
"refId": "A",
"target": "statsd.fakesite.counters.session_start.desktop.count"
},
{
"hide": false,
"refId": "B",
"target": "statsd.fakesite.counters.session_start.mobile.count"
"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Panel Title",
"tooltip": {
"msResolution": false,
"shared": true,
"value_type": "cumulative"
"datasource": "graphite2",
"renderer": "flot",
"yaxes": [
{
"label": null,
"show": true,
"logBase": 1,
"min": null,
"max": null,
"format": "short"
},
"type": "graph",
{
"label": null,
"show": true,
"logBase": 1,
"min": null,
"max": null,
"format": "short"
}
],
"xaxis": {
"show": true
},
"yaxes": [
{
"format": "short",
"logBase": 1,
"max": null,
"min": null,
"show": true
"grid": {
"threshold1": null,
"threshold2": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2Color": "rgba(234, 112, 112, 0.22)"
},
{
"format": "short",
"logBase": 1,
"max": null,
"min": null,
"show": true
}
]
"lines": true,
"fill": 1,
"linewidth": 2,
"points": false,
"pointradius": 5,
"bars": false,
"stack": false,
"percentage": false,
"legend": {
"show": true,
"values": false,
"min": false,
"max": false,
"current": false,
"total": false,
"avg": false
},
"nullPointMode": "connected",
"steppedLine": false,
"tooltip": {
"value_type": "cumulative",
"shared": true,
"msResolution": false
},
"timeFrom": null,
"timeShift": null,
"aliasColors": {
"mobile": "#EAB839"
},
"seriesOverrides": [],
"alerting": {
"queryRef": "A",
"warnLevel": "> 300",
"critLevel": "> 500",
"aggregator": "avg",
"queryRange": "10m",
"interval": "10s",
"title": "active mobile users",
"description": "restart itunes"
},
"links": []
}
],
"title": "Row"
@@ -140,7 +219,7 @@ func TestAlertModel(t *testing.T) {
"col": 0,
"desc": true
},
"span": 12,
"span": 6,
"styles": [
{
"dateFormat": "YYYY-MM-DD HH:mm:ss",
@@ -201,9 +280,10 @@ func TestAlertModel(t *testing.T) {
"target": ""
}
],
"title": "Panel Title",
"title": "Broken influxdb panel",
"transform": "table",
"type": "table"
"type": "table",
"links": []
}
],
"title": "New row"
@@ -248,10 +328,9 @@ func TestAlertModel(t *testing.T) {
"list": []
},
"schemaVersion": 12,
"version": 20,
"version": 16,
"links": []
}`
dashboardJson, _ := simplejson.NewJson([]byte(json))
cmd := &SaveDashboardCommand{
Dashboard: dashboardJson,
@@ -267,14 +346,14 @@ func TestAlertModel(t *testing.T) {
Convey("all properties have been set", func() {
So(alerts, ShouldNotBeEmpty)
So(len(alerts), ShouldEqual, 1)
So(len(alerts), ShouldEqual, 2)
for _, v := range alerts {
So(v.DashboardId, ShouldEqual, 1)
So(v.PanelId, ShouldNotEqual, 0)
So(v.WarnLevel, ShouldEqual, "> 30")
So(v.CritLevel, ShouldEqual, "> 50")
So(v.WarnLevel, ShouldNotBeEmpty)
So(v.CritLevel, ShouldNotBeEmpty)
So(v.Aggregator, ShouldNotBeEmpty)
So(v.Query, ShouldNotBeEmpty)
@@ -282,12 +361,19 @@ func TestAlertModel(t *testing.T) {
So(v.QueryRange, ShouldNotBeEmpty)
So(v.Title, ShouldNotBeEmpty)
So(v.Description, ShouldNotBeEmpty)
So(v.Interval, ShouldEqual, "10")
fmt.Println(v.Query)
}
So(alerts[0].Query, ShouldEqual, "{\"hide\":false,\"refId\":\"A\",\"target\":\"statsd.fakesite.counters.session_start.desktop.count\"}")
So(alerts[0].WarnLevel, ShouldEqual, "> 30")
So(alerts[1].WarnLevel, ShouldEqual, "> 300")
So(alerts[0].CritLevel, ShouldEqual, "> 50")
So(alerts[1].CritLevel, ShouldEqual, "> 500")
So(alerts[0].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"}`)
So(alerts[1].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}`)
So(alerts[0].DatasourceName, ShouldEqual, "")
So(alerts[1].DatasourceName, ShouldEqual, "graphite2")
})
})
}

View File

@@ -68,6 +68,7 @@ func alertIsDifferent(rule1, rule2 m.AlertRule) bool {
result = result || rule1.Title != rule2.Title
result = result || rule1.Description != rule2.Description
result = result || rule1.QueryRange != rule2.QueryRange
result = result || rule1.DatasourceName != rule2.DatasourceName
return result
}

View File

@@ -28,6 +28,7 @@ func TestAlertingDataAccess(t *testing.T) {
Description: "Alerting description",
QueryRange: "5m",
Aggregator: "avg",
DatasourceName: "graphite",
},
}
@@ -62,6 +63,7 @@ func TestAlertingDataAccess(t *testing.T) {
So(alert.Description, ShouldEqual, "Alerting description")
So(alert.QueryRange, ShouldEqual, "5m")
So(alert.Aggregator, ShouldEqual, "avg")
So(alert.DatasourceName, ShouldEqual, "graphite")
})
Convey("Alerts with same dashboard id and panel id should update", func() {

View File

@@ -19,6 +19,7 @@ func addAlertMigrations(mg *Migrator) {
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "query_range", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "aggregator", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "datasource_name", Type: DB_NVarchar, Length: 255, Nullable: false},
},
}