alerts_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. "alerting": {
  66. "query_ref": "A",
  67. "warn_level": 30,
  68. "error_level": 50,
  69. "title": "desktop visiter alerts",
  70. "description": "Restart the webservers",
  71. "query_range": "5m",
  72. "aggregator": "avg",
  73. "interval": 10
  74. },
  75. "targets": [
  76. {
  77. "hide": false,
  78. "refId": "A",
  79. "target": "statsd.fakesite.counters.session_start.desktop.count"
  80. },
  81. {
  82. "hide": false,
  83. "refId": "B",
  84. "target": "statsd.fakesite.counters.session_start.mobile.count"
  85. }
  86. ],
  87. "timeFrom": null,
  88. "timeShift": null,
  89. "title": "Panel Title",
  90. "tooltip": {
  91. "msResolution": false,
  92. "shared": true,
  93. "value_type": "cumulative"
  94. },
  95. "type": "graph",
  96. "xaxis": {
  97. "show": true
  98. },
  99. "yaxes": [
  100. {
  101. "format": "short",
  102. "logBase": 1,
  103. "max": null,
  104. "min": null,
  105. "show": true
  106. },
  107. {
  108. "format": "short",
  109. "logBase": 1,
  110. "max": null,
  111. "min": null,
  112. "show": true
  113. }
  114. ]
  115. }
  116. ],
  117. "title": "Row"
  118. },
  119. {
  120. "collapse": false,
  121. "editable": true,
  122. "height": "250px",
  123. "panels": [
  124. {
  125. "columns": [],
  126. "datasource": "InfluxDB",
  127. "editable": true,
  128. "error": false,
  129. "fontSize": "100%",
  130. "id": 2,
  131. "isNew": true,
  132. "pageSize": null,
  133. "scroll": true,
  134. "showHeader": true,
  135. "sort": {
  136. "col": 0,
  137. "desc": true
  138. },
  139. "span": 12,
  140. "styles": [
  141. {
  142. "dateFormat": "YYYY-MM-DD HH:mm:ss",
  143. "pattern": "Time",
  144. "type": "date"
  145. },
  146. {
  147. "colorMode": null,
  148. "colors": [
  149. "rgba(245, 54, 54, 0.9)",
  150. "rgba(237, 129, 40, 0.89)",
  151. "rgba(50, 172, 45, 0.97)"
  152. ],
  153. "decimals": 2,
  154. "pattern": "/.*/",
  155. "thresholds": [],
  156. "type": "number",
  157. "unit": "short"
  158. }
  159. ],
  160. "targets": [
  161. {
  162. "dsType": "influxdb",
  163. "groupBy": [
  164. {
  165. "params": [
  166. "$interval"
  167. ],
  168. "type": "time"
  169. },
  170. {
  171. "params": [
  172. "null"
  173. ],
  174. "type": "fill"
  175. }
  176. ],
  177. "measurement": "cpu",
  178. "policy": "default",
  179. "query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
  180. "refId": "A",
  181. "resultFormat": "table",
  182. "select": [
  183. [
  184. {
  185. "params": [
  186. "value"
  187. ],
  188. "type": "field"
  189. },
  190. {
  191. "params": [],
  192. "type": "mean"
  193. }
  194. ]
  195. ],
  196. "tags": [],
  197. "target": ""
  198. }
  199. ],
  200. "title": "Panel Title",
  201. "transform": "table",
  202. "type": "table"
  203. }
  204. ],
  205. "title": "New row"
  206. }
  207. ],
  208. "time": {
  209. "from": "now-1h",
  210. "to": "now"
  211. },
  212. "timepicker": {
  213. "now": true,
  214. "nowDelay": "5m",
  215. "refresh_intervals": [
  216. "5s",
  217. "10s",
  218. "30s",
  219. "1m",
  220. "5m",
  221. "15m",
  222. "30m",
  223. "1h",
  224. "2h",
  225. "1d",
  226. "7d"
  227. ],
  228. "time_options": [
  229. "5m",
  230. "15m",
  231. "1h",
  232. "6h",
  233. "12h",
  234. "24h",
  235. "2d",
  236. "7d",
  237. "30d"
  238. ]
  239. },
  240. "templating": {
  241. "list": []
  242. },
  243. "annotations": {
  244. "list": []
  245. },
  246. "schemaVersion": 12,
  247. "version": 20,
  248. "links": []
  249. }`
  250. dashboardJson, _ := simplejson.NewJson([]byte(json))
  251. cmd := &SaveDashboardCommand{
  252. Dashboard: dashboardJson,
  253. UserId: 1,
  254. OrgId: 1,
  255. Overwrite: true,
  256. Result: &Dashboard{
  257. Id: 1,
  258. },
  259. }
  260. alerts := *cmd.GetAlertModels()
  261. Convey("all properties have been set", func() {
  262. So(alerts, ShouldNotBeEmpty)
  263. So(len(alerts), ShouldEqual, 1)
  264. for _, v := range alerts {
  265. So(v.DashboardId, ShouldEqual, 1)
  266. So(v.PanelId, ShouldNotEqual, 0)
  267. So(v.WarnLevel, ShouldEqual, 30)
  268. So(v.ErrorLevel, ShouldEqual, 50)
  269. So(v.Aggregator, ShouldNotBeEmpty)
  270. So(v.Query, ShouldNotBeEmpty)
  271. So(v.QueryRefId, ShouldNotBeEmpty)
  272. So(v.QueryRange, ShouldNotBeEmpty)
  273. So(v.Title, ShouldNotBeEmpty)
  274. So(v.Description, ShouldNotBeEmpty)
  275. fmt.Println(v.Query)
  276. }
  277. So(alerts[0].Query, ShouldEqual, "statsd.fakesite.counters.session_start.desktop.count")
  278. })
  279. })
  280. }