The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/models/alerts_test.go

294 lines
6.7 KiB

package models
import (
"testing"
"fmt"
"github.com/grafana/grafana/pkg/components/simplejson"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertModel(t *testing.T) {
Convey("Parsing alerts from dashboard", t, func() {
json := `{
"id": 7,
"title": "Graphite 4",
"originalTitle": "Graphite 4",
"tags": [
"graphite"
],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"sharedCrosshair": false,
"rows": [
{
"collapse": false,
"editable": true,
"height": "250px",
"panels": [
{
"aliasColors": {},
"bars": false,
"datasource": null,
"editable": true,
"error": false,
"fill": 1,
"grid": {
"threshold1": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2": null,
"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,
"linewidth": 2,
"nullPointMode": "connected",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"span": 12,
"stack": false,
"steppedLine": false,
"alerting": {
"queryRef": "A",
"warnLevel": "> 30",
"critLevel": "> 50",
"title": "desktop visiter alerts",
"description": "Restart the webservers",
"queryRange": "5m",
"aggregator": "avg",
"interval": "10"
},
"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"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Panel Title",
"tooltip": {
"msResolution": false,
"shared": true,
"value_type": "cumulative"
},
"type": "graph",
"xaxis": {
"show": true
},
"yaxes": [
{
"format": "short",
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"logBase": 1,
"max": null,
"min": null,
"show": true
}
]
}
],
"title": "Row"
},
{
"collapse": false,
"editable": true,
"height": "250px",
"panels": [
{
"columns": [],
"datasource": "InfluxDB",
"editable": true,
"error": false,
"fontSize": "100%",
"id": 2,
"isNew": true,
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": {
"col": 0,
"desc": true
},
"span": 12,
"styles": [
{
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"pattern": "Time",
"type": "date"
},
{
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"decimals": 2,
"pattern": "/.*/",
"thresholds": [],
"type": "number",
"unit": "short"
}
],
"targets": [
{
"dsType": "influxdb",
"groupBy": [
{
"params": [
"$interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "cpu",
"policy": "default",
"query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
"refId": "A",
"resultFormat": "table",
"select": [
[
{
"params": [
"value"
],
"type": "field"
},
{
"params": [],
"type": "mean"
}
]
],
"tags": [],
"target": ""
}
],
"title": "Panel Title",
"transform": "table",
"type": "table"
}
],
"title": "New row"
}
],
"time": {
"from": "now-1h",
"to": "now"
},
"timepicker": {
"now": true,
"nowDelay": "5m",
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d",
"7d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"templating": {
"list": []
},
"annotations": {
"list": []
},
"schemaVersion": 12,
"version": 20,
"links": []
}`
dashboardJson, _ := simplejson.NewJson([]byte(json))
cmd := &SaveDashboardCommand{
Dashboard: dashboardJson,
UserId: 1,
OrgId: 1,
Overwrite: true,
Result: &Dashboard{
Id: 1,
},
}
alerts := *cmd.GetAlertModels()
Convey("all properties have been set", func() {
So(alerts, ShouldNotBeEmpty)
So(len(alerts), ShouldEqual, 1)
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.Aggregator, ShouldNotBeEmpty)
So(v.Query, ShouldNotBeEmpty)
So(v.QueryRefId, ShouldNotBeEmpty)
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\"}")
})
})
}