alerts_test.go 7.1 KB

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