dashboard_parser_test.go 10 KB

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