dashboard_parser_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 TestAlertModelParsing(t *testing.T) {
  10. Convey("Parsing alert info from json", t, func() {
  11. Convey("Parsing and validating alerts from dashboards", func() {
  12. json := `{
  13. "id": 57,
  14. "title": "Graphite 4",
  15. "originalTitle": "Graphite 4",
  16. "tags": [
  17. "graphite"
  18. ],
  19. "rows": [
  20. {
  21. "panels": [
  22. {
  23. "title": "Active desktop users",
  24. "editable": true,
  25. "type": "graph",
  26. "id": 3,
  27. "targets": [
  28. {
  29. "refId": "A",
  30. "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
  31. }
  32. ],
  33. "datasource": null,
  34. "alerting": {
  35. "name": "alert name",
  36. "description": "description",
  37. "frequency": 10,
  38. "warning": {
  39. "op": ">",
  40. "level": 10
  41. },
  42. "critical": {
  43. "op": ">",
  44. "level": 20
  45. },
  46. "function": "static",
  47. "valueQuery": {
  48. "queryRefId": "A",
  49. "from": "5m",
  50. "to": "now",
  51. "agg": "avg",
  52. "params": [
  53. "#A",
  54. "5m",
  55. "now",
  56. "avg"
  57. ]
  58. },
  59. "evalQuery": {
  60. "queryRefId": "A",
  61. "from": "5m",
  62. "to": "now",
  63. "agg": "avg",
  64. "params": [
  65. "#A",
  66. "5m",
  67. "now",
  68. "avg"
  69. ]
  70. },
  71. "evalStringParam1": "",
  72. "name": "Alerting Panel Title alert"
  73. }
  74. },
  75. {
  76. "title": "Active mobile users",
  77. "id": 4,
  78. "targets": [
  79. {
  80. "refId": "A",
  81. "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
  82. }
  83. ],
  84. "datasource": "graphite2",
  85. "alerting": {
  86. "name": "alert name",
  87. "description": "description",
  88. "frequency": 10,
  89. "warning": {
  90. "op": ">",
  91. "level": 10
  92. },
  93. "critical": {
  94. "op": ">",
  95. "level": 20
  96. },
  97. "function": "static",
  98. "valueQuery": {
  99. "queryRefId": "A",
  100. "from": "5m",
  101. "to": "now",
  102. "agg": "avg",
  103. "params": [
  104. "#A",
  105. "5m",
  106. "now",
  107. "avg"
  108. ]
  109. },
  110. "evalQuery": {
  111. "queryRefId": "A",
  112. "from": "5m",
  113. "to": "now",
  114. "agg": "avg",
  115. "params": [
  116. "#A",
  117. "5m",
  118. "now",
  119. "avg"
  120. ]
  121. },
  122. "evalStringParam1": "",
  123. "name": "Alerting Panel Title alert"
  124. }
  125. }
  126. ],
  127. "title": "Row"
  128. },
  129. {
  130. "collapse": false,
  131. "editable": true,
  132. "height": "250px",
  133. "panels": [
  134. {
  135. "datasource": "InfluxDB",
  136. "id": 2,
  137. "targets": [
  138. {
  139. "dsType": "influxdb",
  140. "groupBy": [
  141. {
  142. "params": [
  143. "$interval"
  144. ],
  145. "type": "time"
  146. },
  147. {
  148. "params": [
  149. "null"
  150. ],
  151. "type": "fill"
  152. }
  153. ],
  154. "measurement": "cpu",
  155. "policy": "default",
  156. "query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
  157. "refId": "A",
  158. "resultFormat": "table",
  159. "select": [
  160. [
  161. {
  162. "params": [
  163. "value"
  164. ],
  165. "type": "field"
  166. },
  167. {
  168. "params": [],
  169. "type": "mean"
  170. }
  171. ]
  172. ],
  173. "tags": [],
  174. "target": ""
  175. }
  176. ],
  177. "title": "Broken influxdb panel",
  178. "transform": "table",
  179. "type": "table"
  180. }
  181. ],
  182. "title": "New row"
  183. }
  184. ]
  185. }`
  186. dashboardJSON, _ := simplejson.NewJson([]byte(json))
  187. cmd := &m.SaveDashboardCommand{
  188. Dashboard: dashboardJSON,
  189. UserId: 1,
  190. OrgId: 1,
  191. Overwrite: true,
  192. Result: &m.Dashboard{
  193. Id: 1,
  194. },
  195. }
  196. InitTestDB(t)
  197. AddDataSource(&m.AddDataSourceCommand{
  198. Name: "graphite2",
  199. OrgId: 1,
  200. Type: m.DS_INFLUXDB,
  201. Access: m.DS_ACCESS_DIRECT,
  202. Url: "http://test",
  203. IsDefault: false,
  204. Database: "site",
  205. })
  206. AddDataSource(&m.AddDataSourceCommand{
  207. Name: "InfluxDB",
  208. OrgId: 1,
  209. Type: m.DS_GRAPHITE,
  210. Access: m.DS_ACCESS_DIRECT,
  211. Url: "http://test",
  212. IsDefault: true,
  213. })
  214. alerts := alerting.ParseAlertsFromDashboard(cmd)
  215. Convey("all properties have been set", func() {
  216. So(alerts, ShouldNotBeEmpty)
  217. So(len(alerts), ShouldEqual, 2)
  218. for _, v := range alerts {
  219. So(v.DashboardId, ShouldEqual, 1)
  220. So(v.PanelId, ShouldNotEqual, 0)
  221. So(v.Name, ShouldNotBeEmpty)
  222. So(v.Description, ShouldNotBeEmpty)
  223. }
  224. })
  225. })
  226. })
  227. }