dashboard_parser_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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": "Alerting Panel Title alert",
  36. "description": "description",
  37. "critical": {
  38. "level": 20,
  39. "op": ">"
  40. },
  41. "frequency": 10,
  42. "query": {
  43. "from": "5m",
  44. "refId": "A",
  45. "to": "now"
  46. },
  47. "transform": {
  48. "method": "avg",
  49. "name": "aggregation"
  50. },
  51. "warning": {
  52. "level": 10,
  53. "op": ">"
  54. }
  55. }
  56. },
  57. {
  58. "title": "Active mobile users",
  59. "id": 4,
  60. "targets": [
  61. {
  62. "refId": "A",
  63. "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
  64. }
  65. ],
  66. "datasource": "graphite2",
  67. "alerting": {
  68. "name": "Alerting Panel Title alert",
  69. "description": "description",
  70. "critical": {
  71. "level": 20,
  72. "op": ">"
  73. },
  74. "frequency": 10,
  75. "query": {
  76. "from": "5m",
  77. "refId": "A",
  78. "to": "now"
  79. },
  80. "transform": {
  81. "method": "avg",
  82. "name": "aggregation"
  83. },
  84. "warning": {
  85. "level": 10,
  86. "op": ">"
  87. }
  88. }
  89. }
  90. ],
  91. "title": "Row"
  92. },
  93. {
  94. "collapse": false,
  95. "editable": true,
  96. "height": "250px",
  97. "panels": [
  98. {
  99. "datasource": "InfluxDB",
  100. "id": 2,
  101. "targets": [
  102. {
  103. "dsType": "influxdb",
  104. "groupBy": [
  105. {
  106. "params": [
  107. "$interval"
  108. ],
  109. "type": "time"
  110. },
  111. {
  112. "params": [
  113. "null"
  114. ],
  115. "type": "fill"
  116. }
  117. ],
  118. "measurement": "cpu",
  119. "policy": "default",
  120. "query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
  121. "refId": "A",
  122. "resultFormat": "table",
  123. "select": [
  124. [
  125. {
  126. "params": [
  127. "value"
  128. ],
  129. "type": "field"
  130. },
  131. {
  132. "params": [],
  133. "type": "mean"
  134. }
  135. ]
  136. ],
  137. "tags": [],
  138. "target": ""
  139. }
  140. ],
  141. "title": "Broken influxdb panel",
  142. "transform": "table",
  143. "type": "table"
  144. }
  145. ],
  146. "title": "New row"
  147. }
  148. ]
  149. }`
  150. dashboardJSON, _ := simplejson.NewJson([]byte(json))
  151. cmd := &m.SaveDashboardCommand{
  152. Dashboard: dashboardJSON,
  153. UserId: 1,
  154. OrgId: 1,
  155. Overwrite: true,
  156. Result: &m.Dashboard{
  157. Id: 1,
  158. },
  159. }
  160. InitTestDB(t)
  161. AddDataSource(&m.AddDataSourceCommand{
  162. Name: "graphite2",
  163. OrgId: 1,
  164. Type: m.DS_INFLUXDB,
  165. Access: m.DS_ACCESS_DIRECT,
  166. Url: "http://test",
  167. IsDefault: false,
  168. Database: "site",
  169. })
  170. AddDataSource(&m.AddDataSourceCommand{
  171. Name: "InfluxDB",
  172. OrgId: 1,
  173. Type: m.DS_GRAPHITE,
  174. Access: m.DS_ACCESS_DIRECT,
  175. Url: "http://test",
  176. IsDefault: true,
  177. })
  178. alerts := alerting.ParseAlertsFromDashboard(cmd)
  179. Convey("all properties have been set", func() {
  180. So(alerts, ShouldNotBeEmpty)
  181. So(len(alerts), ShouldEqual, 2)
  182. for _, v := range alerts {
  183. So(v.DashboardId, ShouldEqual, 1)
  184. So(v.PanelId, ShouldNotEqual, 0)
  185. So(v.Name, ShouldNotBeEmpty)
  186. So(v.Description, ShouldNotBeEmpty)
  187. }
  188. })
  189. })
  190. })
  191. }