dashboard_parser_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/services/alerting"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertModel(t *testing.T) {
  10. Convey("Parsing alerts from dashboard", t, func() {
  11. json := `{
  12. "id": 57,
  13. "title": "Graphite 4",
  14. "originalTitle": "Graphite 4",
  15. "tags": [
  16. "graphite"
  17. ],
  18. "style": "dark",
  19. "timezone": "browser",
  20. "editable": true,
  21. "hideControls": false,
  22. "sharedCrosshair": false,
  23. "rows": [
  24. {
  25. "collapse": false,
  26. "editable": true,
  27. "height": "250px",
  28. "panels": [
  29. {
  30. "title": "Active desktop users",
  31. "error": false,
  32. "span": 6,
  33. "editable": true,
  34. "type": "graph",
  35. "isNew": true,
  36. "id": 3,
  37. "targets": [
  38. {
  39. "refId": "A",
  40. "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
  41. }
  42. ],
  43. "datasource": null,
  44. "renderer": "flot",
  45. "yaxes": [
  46. {
  47. "label": null,
  48. "show": true,
  49. "logBase": 1,
  50. "min": null,
  51. "max": null,
  52. "format": "short"
  53. },
  54. {
  55. "label": null,
  56. "show": true,
  57. "logBase": 1,
  58. "min": null,
  59. "max": null,
  60. "format": "short"
  61. }
  62. ],
  63. "xaxis": {
  64. "show": true
  65. },
  66. "grid": {
  67. "threshold1": null,
  68. "threshold2": null,
  69. "threshold1Color": "rgba(216, 200, 27, 0.27)",
  70. "threshold2Color": "rgba(234, 112, 112, 0.22)"
  71. },
  72. "lines": true,
  73. "fill": 1,
  74. "linewidth": 2,
  75. "points": false,
  76. "pointradius": 5,
  77. "bars": false,
  78. "stack": false,
  79. "percentage": false,
  80. "legend": {
  81. "show": true,
  82. "values": false,
  83. "min": false,
  84. "max": false,
  85. "current": false,
  86. "total": false,
  87. "avg": false
  88. },
  89. "nullPointMode": "connected",
  90. "steppedLine": false,
  91. "tooltip": {
  92. "value_type": "cumulative",
  93. "shared": true,
  94. "msResolution": false
  95. },
  96. "timeFrom": null,
  97. "timeShift": null,
  98. "aliasColors": {},
  99. "seriesOverrides": [],
  100. "alerting": {
  101. "frequency": 10,
  102. "warning": {
  103. "op": ">",
  104. "level": 10
  105. },
  106. "critical": {
  107. "op": ">",
  108. "level": 20
  109. },
  110. "function": "static",
  111. "valueQuery": {
  112. "queryRefId": "A",
  113. "from": "5m",
  114. "to": "now",
  115. "agg": "avg",
  116. "params": [
  117. "#A",
  118. "5m",
  119. "now",
  120. "avg"
  121. ]
  122. },
  123. "evalQuery": {
  124. "queryRefId": "A",
  125. "from": "5m",
  126. "to": "now",
  127. "agg": "avg",
  128. "params": [
  129. "#A",
  130. "5m",
  131. "now",
  132. "avg"
  133. ]
  134. },
  135. "evalStringParam1": "",
  136. "name": "Alerting Panel Title alert"
  137. },
  138. "links": []
  139. },
  140. {
  141. "title": "Active mobile users",
  142. "error": false,
  143. "span": 6,
  144. "editable": true,
  145. "type": "graph",
  146. "isNew": true,
  147. "id": 4,
  148. "targets": [
  149. {
  150. "refId": "A",
  151. "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
  152. }
  153. ],
  154. "datasource": "graphite2",
  155. "renderer": "flot",
  156. "yaxes": [
  157. {
  158. "label": null,
  159. "show": true,
  160. "logBase": 1,
  161. "min": null,
  162. "max": null,
  163. "format": "short"
  164. },
  165. {
  166. "label": null,
  167. "show": true,
  168. "logBase": 1,
  169. "min": null,
  170. "max": null,
  171. "format": "short"
  172. }
  173. ],
  174. "xaxis": {
  175. "show": true
  176. },
  177. "grid": {
  178. "threshold1": null,
  179. "threshold2": null,
  180. "threshold1Color": "rgba(216, 200, 27, 0.27)",
  181. "threshold2Color": "rgba(234, 112, 112, 0.22)"
  182. },
  183. "lines": true,
  184. "fill": 1,
  185. "linewidth": 2,
  186. "points": false,
  187. "pointradius": 5,
  188. "bars": false,
  189. "stack": false,
  190. "percentage": false,
  191. "legend": {
  192. "show": true,
  193. "values": false,
  194. "min": false,
  195. "max": false,
  196. "current": false,
  197. "total": false,
  198. "avg": false
  199. },
  200. "nullPointMode": "connected",
  201. "steppedLine": false,
  202. "tooltip": {
  203. "value_type": "cumulative",
  204. "shared": true,
  205. "msResolution": false
  206. },
  207. "timeFrom": null,
  208. "timeShift": null,
  209. "aliasColors": {
  210. "mobile": "#EAB839"
  211. },
  212. "seriesOverrides": [],
  213. "alerting": {
  214. "frequency": 10,
  215. "warning": {
  216. "op": ">",
  217. "level": 10
  218. },
  219. "critical": {
  220. "op": ">",
  221. "level": 20
  222. },
  223. "function": "static",
  224. "valueQuery": {
  225. "queryRefId": "A",
  226. "from": "5m",
  227. "to": "now",
  228. "agg": "avg",
  229. "params": [
  230. "#A",
  231. "5m",
  232. "now",
  233. "avg"
  234. ]
  235. },
  236. "evalQuery": {
  237. "queryRefId": "A",
  238. "from": "5m",
  239. "to": "now",
  240. "agg": "avg",
  241. "params": [
  242. "#A",
  243. "5m",
  244. "now",
  245. "avg"
  246. ]
  247. },
  248. "evalStringParam1": "",
  249. "name": "Alerting Panel Title alert"
  250. },
  251. "links": []
  252. }
  253. ],
  254. "title": "Row"
  255. },
  256. {
  257. "collapse": false,
  258. "editable": true,
  259. "height": "250px",
  260. "panels": [
  261. {
  262. "columns": [],
  263. "datasource": "InfluxDB",
  264. "editable": true,
  265. "error": false,
  266. "fontSize": "100%",
  267. "id": 2,
  268. "isNew": true,
  269. "pageSize": null,
  270. "scroll": true,
  271. "showHeader": true,
  272. "sort": {
  273. "col": 0,
  274. "desc": true
  275. },
  276. "span": 6,
  277. "styles": [
  278. {
  279. "dateFormat": "YYYY-MM-DD HH:mm:ss",
  280. "pattern": "Time",
  281. "type": "date"
  282. },
  283. {
  284. "colorMode": null,
  285. "colors": [
  286. "rgba(245, 54, 54, 0.9)",
  287. "rgba(237, 129, 40, 0.89)",
  288. "rgba(50, 172, 45, 0.97)"
  289. ],
  290. "decimals": 2,
  291. "pattern": "/.*/",
  292. "thresholds": [],
  293. "type": "number",
  294. "unit": "short"
  295. }
  296. ],
  297. "targets": [
  298. {
  299. "dsType": "influxdb",
  300. "groupBy": [
  301. {
  302. "params": [
  303. "$interval"
  304. ],
  305. "type": "time"
  306. },
  307. {
  308. "params": [
  309. "null"
  310. ],
  311. "type": "fill"
  312. }
  313. ],
  314. "measurement": "cpu",
  315. "policy": "default",
  316. "query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
  317. "refId": "A",
  318. "resultFormat": "table",
  319. "select": [
  320. [
  321. {
  322. "params": [
  323. "value"
  324. ],
  325. "type": "field"
  326. },
  327. {
  328. "params": [],
  329. "type": "mean"
  330. }
  331. ]
  332. ],
  333. "tags": [],
  334. "target": ""
  335. }
  336. ],
  337. "title": "Broken influxdb panel",
  338. "transform": "table",
  339. "type": "table",
  340. "links": []
  341. }
  342. ],
  343. "title": "New row"
  344. }
  345. ],
  346. "time": {
  347. "from": "now-1h",
  348. "to": "now"
  349. },
  350. "timepicker": {
  351. "now": true,
  352. "nowDelay": "5m",
  353. "refresh_intervals": [
  354. "5s",
  355. "10s",
  356. "30s",
  357. "1m",
  358. "5m",
  359. "15m",
  360. "30m",
  361. "1h",
  362. "2h",
  363. "1d",
  364. "7d"
  365. ],
  366. "time_options": [
  367. "5m",
  368. "15m",
  369. "1h",
  370. "6h",
  371. "12h",
  372. "24h",
  373. "2d",
  374. "7d",
  375. "30d"
  376. ]
  377. },
  378. "templating": {
  379. "list": []
  380. },
  381. "annotations": {
  382. "list": []
  383. },
  384. "schemaVersion": 12,
  385. "version": 16,
  386. "links": []
  387. }`
  388. dashboardJson, _ := simplejson.NewJson([]byte(json))
  389. cmd := &m.SaveDashboardCommand{
  390. Dashboard: dashboardJson,
  391. UserId: 1,
  392. OrgId: 1,
  393. Overwrite: true,
  394. Result: &m.Dashboard{
  395. Id: 1,
  396. },
  397. }
  398. InitTestDB(t)
  399. AddDataSource(&m.AddDataSourceCommand{
  400. Name: "graphite2",
  401. OrgId: 1,
  402. Type: m.DS_INFLUXDB,
  403. Access: m.DS_ACCESS_DIRECT,
  404. Url: "http://test",
  405. IsDefault: false,
  406. Database: "site",
  407. })
  408. AddDataSource(&m.AddDataSourceCommand{
  409. Name: "InfluxDB",
  410. OrgId: 1,
  411. Type: m.DS_GRAPHITE,
  412. Access: m.DS_ACCESS_DIRECT,
  413. Url: "http://test",
  414. IsDefault: true,
  415. })
  416. alerts := alerting.ParseAlertsFromDashboard(cmd)
  417. Convey("all properties have been set", func() {
  418. So(alerts, ShouldNotBeEmpty)
  419. So(len(alerts), ShouldEqual, 2)
  420. for _, v := range alerts {
  421. So(v.DashboardId, ShouldEqual, 1)
  422. So(v.PanelId, ShouldNotEqual, 0)
  423. So(v.Name, ShouldNotBeEmpty)
  424. So(v.Description, ShouldNotBeEmpty)
  425. expr := simplejson.NewFromAny(v.Expression)
  426. So(expr.Get("valueQuery").Get("query").MustString(), ShouldNotEqual, "")
  427. So(expr.Get("valueQuery").Get("datsourceId").MustInt64(), ShouldNotEqual, 0)
  428. }
  429. })
  430. })
  431. }