model_parser_test.go 3.8 KB

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