alerts_test.go 9.4 KB

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