| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- package sqlstore
- import (
- "testing"
- "github.com/grafana/grafana/pkg/components/simplejson"
- m "github.com/grafana/grafana/pkg/models"
- "github.com/grafana/grafana/pkg/services/alerting"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestAlertModel(t *testing.T) {
- Convey("Parsing alerts from dashboard", t, func() {
- json := `{
- "id": 57,
- "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": [
- {
- "title": "Active desktop users",
- "error": false,
- "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,
- "threshold2": null,
- "threshold1Color": "rgba(216, 200, 27, 0.27)",
- "threshold2Color": "rgba(234, 112, 112, 0.22)"
- },
- "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": {},
- "seriesOverrides": [],
-
-
- "alerting": {
- "frequency": 10,
- "warning": {
- "op": ">",
- "level": 10
- },
- "critical": {
- "op": ">",
- "level": 20
- },
- "function": "static",
- "valueQuery": {
- "queryRefId": "A",
- "from": "5m",
- "to": "now",
- "agg": "avg",
- "params": [
- "#A",
- "5m",
- "now",
- "avg"
- ]
- },
- "evalQuery": {
- "queryRefId": "A",
- "from": "5m",
- "to": "now",
- "agg": "avg",
- "params": [
- "#A",
- "5m",
- "now",
- "avg"
- ]
- },
- "evalStringParam1": "",
- "name": "Alerting Panel Title alert"
- },
- "links": []
- },
- {
- "title": "Active mobile users",
- "error": false,
- "span": 6,
- "editable": true,
- "type": "graph",
- "isNew": true,
- "id": 4,
- "targets": [
- {
- "refId": "A",
- "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
- }
- ],
- "datasource": "graphite2",
- "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,
- "threshold2": null,
- "threshold1Color": "rgba(216, 200, 27, 0.27)",
- "threshold2Color": "rgba(234, 112, 112, 0.22)"
- },
- "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": {
- "frequency": 10,
- "warning": {
- "op": ">",
- "level": 10
- },
- "critical": {
- "op": ">",
- "level": 20
- },
- "function": "static",
- "valueQuery": {
- "queryRefId": "A",
- "from": "5m",
- "to": "now",
- "agg": "avg",
- "params": [
- "#A",
- "5m",
- "now",
- "avg"
- ]
- },
- "evalQuery": {
- "queryRefId": "A",
- "from": "5m",
- "to": "now",
- "agg": "avg",
- "params": [
- "#A",
- "5m",
- "now",
- "avg"
- ]
- },
- "evalStringParam1": "",
- "name": "Alerting Panel Title alert"
- },
- "links": []
- }
- ],
- "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": 6,
- "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": "Broken influxdb panel",
- "transform": "table",
- "type": "table",
- "links": []
- }
- ],
- "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": 16,
- "links": []
- }`
- dashboardJson, _ := simplejson.NewJson([]byte(json))
- cmd := &m.SaveDashboardCommand{
- Dashboard: dashboardJson,
- UserId: 1,
- OrgId: 1,
- Overwrite: true,
- Result: &m.Dashboard{
- Id: 1,
- },
- }
- InitTestDB(t)
- AddDataSource(&m.AddDataSourceCommand{
- Name: "graphite2",
- OrgId: 1,
- Type: m.DS_INFLUXDB,
- Access: m.DS_ACCESS_DIRECT,
- Url: "http://test",
- IsDefault: false,
- Database: "site",
- })
- AddDataSource(&m.AddDataSourceCommand{
- Name: "InfluxDB",
- OrgId: 1,
- Type: m.DS_GRAPHITE,
- Access: m.DS_ACCESS_DIRECT,
- Url: "http://test",
- IsDefault: true,
- })
- alerts := alerting.ParseAlertsFromDashboard(cmd)
- Convey("all properties have been set", func() {
- So(alerts, ShouldNotBeEmpty)
- So(len(alerts), ShouldEqual, 2)
- for _, v := range alerts {
- So(v.DashboardId, ShouldEqual, 1)
- So(v.PanelId, ShouldNotEqual, 0)
- So(v.Name, ShouldNotBeEmpty)
- So(v.Description, ShouldNotBeEmpty)
- expr := simplejson.NewFromAny(v.Expression)
- So(expr.Get("valueQuery").Get("query").MustString(), ShouldNotEqual, "")
- So(expr.Get("valueQuery").Get("datsourceId").MustInt64(), ShouldNotEqual, 0)
- }
- })
- })
- }
|