alerts_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. "aggregator": "sum",
  103. "queryRange": "10m",
  104. "interval": "10s",
  105. "title": "active desktop users",
  106. "description": "restart webservers"
  107. },
  108. "links": []
  109. },
  110. {
  111. "title": "Active mobile users",
  112. "error": false,
  113. "span": 6,
  114. "editable": true,
  115. "type": "graph",
  116. "isNew": true,
  117. "id": 4,
  118. "targets": [
  119. {
  120. "refId": "A",
  121. "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
  122. }
  123. ],
  124. "datasource": "graphite2",
  125. "renderer": "flot",
  126. "yaxes": [
  127. {
  128. "label": null,
  129. "show": true,
  130. "logBase": 1,
  131. "min": null,
  132. "max": null,
  133. "format": "short"
  134. },
  135. {
  136. "label": null,
  137. "show": true,
  138. "logBase": 1,
  139. "min": null,
  140. "max": null,
  141. "format": "short"
  142. }
  143. ],
  144. "xaxis": {
  145. "show": true
  146. },
  147. "grid": {
  148. "threshold1": null,
  149. "threshold2": null,
  150. "threshold1Color": "rgba(216, 200, 27, 0.27)",
  151. "threshold2Color": "rgba(234, 112, 112, 0.22)"
  152. },
  153. "lines": true,
  154. "fill": 1,
  155. "linewidth": 2,
  156. "points": false,
  157. "pointradius": 5,
  158. "bars": false,
  159. "stack": false,
  160. "percentage": false,
  161. "legend": {
  162. "show": true,
  163. "values": false,
  164. "min": false,
  165. "max": false,
  166. "current": false,
  167. "total": false,
  168. "avg": false
  169. },
  170. "nullPointMode": "connected",
  171. "steppedLine": false,
  172. "tooltip": {
  173. "value_type": "cumulative",
  174. "shared": true,
  175. "msResolution": false
  176. },
  177. "timeFrom": null,
  178. "timeShift": null,
  179. "aliasColors": {
  180. "mobile": "#EAB839"
  181. },
  182. "seriesOverrides": [],
  183. "alerting": {
  184. "queryRef": "A",
  185. "warnLevel": "> 300",
  186. "critLevel": "> 500",
  187. "aggregator": "avg",
  188. "queryRange": "10m",
  189. "interval": "10s",
  190. "title": "active mobile users",
  191. "description": "restart itunes"
  192. },
  193. "links": []
  194. }
  195. ],
  196. "title": "Row"
  197. },
  198. {
  199. "collapse": false,
  200. "editable": true,
  201. "height": "250px",
  202. "panels": [
  203. {
  204. "columns": [],
  205. "datasource": "InfluxDB",
  206. "editable": true,
  207. "error": false,
  208. "fontSize": "100%",
  209. "id": 2,
  210. "isNew": true,
  211. "pageSize": null,
  212. "scroll": true,
  213. "showHeader": true,
  214. "sort": {
  215. "col": 0,
  216. "desc": true
  217. },
  218. "span": 6,
  219. "styles": [
  220. {
  221. "dateFormat": "YYYY-MM-DD HH:mm:ss",
  222. "pattern": "Time",
  223. "type": "date"
  224. },
  225. {
  226. "colorMode": null,
  227. "colors": [
  228. "rgba(245, 54, 54, 0.9)",
  229. "rgba(237, 129, 40, 0.89)",
  230. "rgba(50, 172, 45, 0.97)"
  231. ],
  232. "decimals": 2,
  233. "pattern": "/.*/",
  234. "thresholds": [],
  235. "type": "number",
  236. "unit": "short"
  237. }
  238. ],
  239. "targets": [
  240. {
  241. "dsType": "influxdb",
  242. "groupBy": [
  243. {
  244. "params": [
  245. "$interval"
  246. ],
  247. "type": "time"
  248. },
  249. {
  250. "params": [
  251. "null"
  252. ],
  253. "type": "fill"
  254. }
  255. ],
  256. "measurement": "cpu",
  257. "policy": "default",
  258. "query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
  259. "refId": "A",
  260. "resultFormat": "table",
  261. "select": [
  262. [
  263. {
  264. "params": [
  265. "value"
  266. ],
  267. "type": "field"
  268. },
  269. {
  270. "params": [],
  271. "type": "mean"
  272. }
  273. ]
  274. ],
  275. "tags": [],
  276. "target": ""
  277. }
  278. ],
  279. "title": "Broken influxdb panel",
  280. "transform": "table",
  281. "type": "table",
  282. "links": []
  283. }
  284. ],
  285. "title": "New row"
  286. }
  287. ],
  288. "time": {
  289. "from": "now-1h",
  290. "to": "now"
  291. },
  292. "timepicker": {
  293. "now": true,
  294. "nowDelay": "5m",
  295. "refresh_intervals": [
  296. "5s",
  297. "10s",
  298. "30s",
  299. "1m",
  300. "5m",
  301. "15m",
  302. "30m",
  303. "1h",
  304. "2h",
  305. "1d",
  306. "7d"
  307. ],
  308. "time_options": [
  309. "5m",
  310. "15m",
  311. "1h",
  312. "6h",
  313. "12h",
  314. "24h",
  315. "2d",
  316. "7d",
  317. "30d"
  318. ]
  319. },
  320. "templating": {
  321. "list": []
  322. },
  323. "annotations": {
  324. "list": []
  325. },
  326. "schemaVersion": 12,
  327. "version": 16,
  328. "links": []
  329. }`
  330. dashboardJson, _ := simplejson.NewJson([]byte(json))
  331. cmd := &SaveDashboardCommand{
  332. Dashboard: dashboardJson,
  333. UserId: 1,
  334. OrgId: 1,
  335. Overwrite: true,
  336. Result: &Dashboard{
  337. Id: 1,
  338. },
  339. }
  340. alerts := *cmd.GetAlertModels()
  341. Convey("all properties have been set", func() {
  342. So(alerts, ShouldNotBeEmpty)
  343. So(len(alerts), ShouldEqual, 2)
  344. for _, v := range alerts {
  345. So(v.DashboardId, ShouldEqual, 1)
  346. So(v.PanelId, ShouldNotEqual, 0)
  347. So(v.WarnLevel, ShouldNotBeEmpty)
  348. So(v.CritLevel, ShouldNotBeEmpty)
  349. So(v.Aggregator, ShouldNotBeEmpty)
  350. So(v.Query, ShouldNotBeEmpty)
  351. So(v.QueryRefId, ShouldNotBeEmpty)
  352. So(v.QueryRange, ShouldNotBeEmpty)
  353. So(v.Title, ShouldNotBeEmpty)
  354. So(v.Description, ShouldNotBeEmpty)
  355. }
  356. So(alerts[0].WarnLevel, ShouldEqual, "> 30")
  357. So(alerts[1].WarnLevel, ShouldEqual, "> 300")
  358. So(alerts[0].CritLevel, ShouldEqual, "> 50")
  359. So(alerts[1].CritLevel, ShouldEqual, "> 500")
  360. So(alerts[0].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"}`)
  361. So(alerts[1].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}`)
  362. })
  363. })
  364. }