model_parser_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package influxdb
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/tsdb"
  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 := &tsdb.DataSourceInfo{
  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. "tags": [
  89. {
  90. "key": "datacenter",
  91. "operator": "=",
  92. "value": "America"
  93. },
  94. {
  95. "condition": "OR",
  96. "key": "hostname",
  97. "operator": "=",
  98. "value": "server1"
  99. }
  100. ]
  101. }
  102. `
  103. dsInfo.JsonData.Set("timeInterval", ">20s")
  104. modelJson, err := simplejson.NewJson([]byte(json))
  105. So(err, ShouldBeNil)
  106. res, err := parser.Parse(modelJson, dsInfo)
  107. So(err, ShouldBeNil)
  108. So(len(res.GroupBy), ShouldEqual, 3)
  109. So(len(res.Selects), ShouldEqual, 3)
  110. So(len(res.Tags), ShouldEqual, 2)
  111. So(res.Interval, ShouldEqual, ">20s")
  112. })
  113. Convey("can part raw query json model", func() {
  114. json := `
  115. {
  116. "dsType": "influxdb",
  117. "groupBy": [
  118. {
  119. "params": [
  120. "$interval"
  121. ],
  122. "type": "time"
  123. },
  124. {
  125. "params": [
  126. "null"
  127. ],
  128. "type": "fill"
  129. }
  130. ],
  131. "interval": ">10s",
  132. "policy": "default",
  133. "query": "RawDummieQuery",
  134. "rawQuery": true,
  135. "refId": "A",
  136. "resultFormat": "time_series",
  137. "select": [
  138. [
  139. {
  140. "params": [
  141. "value"
  142. ],
  143. "type": "field"
  144. },
  145. {
  146. "params": [
  147. ],
  148. "type": "mean"
  149. }
  150. ]
  151. ],
  152. "tags": [
  153. ]
  154. }
  155. `
  156. modelJson, err := simplejson.NewJson([]byte(json))
  157. So(err, ShouldBeNil)
  158. res, err := parser.Parse(modelJson, dsInfo)
  159. So(err, ShouldBeNil)
  160. So(res.RawQuery, ShouldEqual, "RawDummieQuery")
  161. So(len(res.GroupBy), ShouldEqual, 2)
  162. So(len(res.Selects), ShouldEqual, 1)
  163. So(len(res.Tags), ShouldEqual, 0)
  164. So(res.Interval, ShouldEqual, ">10s")
  165. })
  166. })
  167. }