model_parser_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package influxdb
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestInfluxdbQueryParser(t *testing.T) {
  9. Convey("Influxdb query parser", t, func() {
  10. parser := &InfluxdbQueryParser{}
  11. dsInfo := &models.DataSource{
  12. JsonData: simplejson.New(),
  13. }
  14. Convey("can parse influxdb json model", func() {
  15. json := `
  16. {
  17. "dsType": "influxdb",
  18. "groupBy": [
  19. {
  20. "params": [
  21. "$interval"
  22. ],
  23. "type": "time"
  24. },
  25. {
  26. "params": [
  27. "datacenter"
  28. ],
  29. "type": "tag"
  30. },
  31. {
  32. "params": [
  33. "none"
  34. ],
  35. "type": "fill"
  36. }
  37. ],
  38. "measurement": "logins.count",
  39. "policy": "default",
  40. "refId": "B",
  41. "resultFormat": "time_series",
  42. "select": [
  43. [
  44. {
  45. "type": "field",
  46. "params": [
  47. "value"
  48. ]
  49. },
  50. {
  51. "type": "count",
  52. "params": []
  53. }
  54. ],
  55. [
  56. {
  57. "type": "field",
  58. "params": [
  59. "value"
  60. ]
  61. },
  62. {
  63. "type": "bottom",
  64. "params": [
  65. 3
  66. ]
  67. }
  68. ],
  69. [
  70. {
  71. "type": "field",
  72. "params": [
  73. "value"
  74. ]
  75. },
  76. {
  77. "type": "mean",
  78. "params": []
  79. },
  80. {
  81. "type": "math",
  82. "params": [
  83. " / 100"
  84. ]
  85. }
  86. ]
  87. ],
  88. "alias": "serie alias",
  89. "tags": [
  90. {
  91. "key": "datacenter",
  92. "operator": "=",
  93. "value": "America"
  94. },
  95. {
  96. "condition": "OR",
  97. "key": "hostname",
  98. "operator": "=",
  99. "value": "server1"
  100. }
  101. ]
  102. }
  103. `
  104. dsInfo.JsonData.Set("timeInterval", ">20s")
  105. modelJson, err := simplejson.NewJson([]byte(json))
  106. So(err, ShouldBeNil)
  107. res, err := parser.Parse(modelJson, dsInfo)
  108. So(err, ShouldBeNil)
  109. So(len(res.GroupBy), ShouldEqual, 3)
  110. So(len(res.Selects), ShouldEqual, 3)
  111. So(len(res.Tags), ShouldEqual, 2)
  112. So(res.Interval, ShouldEqual, ">20s")
  113. So(res.Alias, ShouldEqual, "serie alias")
  114. })
  115. Convey("can part raw query json model", func() {
  116. json := `
  117. {
  118. "dsType": "influxdb",
  119. "groupBy": [
  120. {
  121. "params": [
  122. "$interval"
  123. ],
  124. "type": "time"
  125. },
  126. {
  127. "params": [
  128. "null"
  129. ],
  130. "type": "fill"
  131. }
  132. ],
  133. "interval": ">10s",
  134. "policy": "default",
  135. "query": "RawDummieQuery",
  136. "rawQuery": true,
  137. "refId": "A",
  138. "resultFormat": "time_series",
  139. "select": [
  140. [
  141. {
  142. "params": [
  143. "value"
  144. ],
  145. "type": "field"
  146. },
  147. {
  148. "params": [
  149. ],
  150. "type": "mean"
  151. }
  152. ]
  153. ],
  154. "tags": [
  155. ]
  156. }
  157. `
  158. modelJson, err := simplejson.NewJson([]byte(json))
  159. So(err, ShouldBeNil)
  160. res, err := parser.Parse(modelJson, dsInfo)
  161. So(err, ShouldBeNil)
  162. So(res.RawQuery, ShouldEqual, "RawDummieQuery")
  163. So(len(res.GroupBy), ShouldEqual, 2)
  164. So(len(res.Selects), ShouldEqual, 1)
  165. So(len(res.Tags), ShouldEqual, 0)
  166. So(res.Interval, ShouldEqual, ">10s")
  167. })
  168. })
  169. }