model_parser_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "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, time.Second*20)
  113. So(res.Alias, ShouldEqual, "serie alias")
  114. })
  115. Convey("can part raw query json model", func() {
  116. json := `
  117. {
  118. "groupBy": [
  119. {
  120. "params": [
  121. "$interval"
  122. ],
  123. "type": "time"
  124. },
  125. {
  126. "params": [
  127. "null"
  128. ],
  129. "type": "fill"
  130. }
  131. ],
  132. "interval": ">10s",
  133. "policy": "default",
  134. "query": "RawDummieQuery",
  135. "rawQuery": true,
  136. "refId": "A",
  137. "resultFormat": "time_series",
  138. "select": [
  139. [
  140. {
  141. "params": [
  142. "value"
  143. ],
  144. "type": "field"
  145. },
  146. {
  147. "params": [
  148. ],
  149. "type": "mean"
  150. }
  151. ]
  152. ],
  153. "tags": [
  154. ]
  155. }
  156. `
  157. modelJson, err := simplejson.NewJson([]byte(json))
  158. So(err, ShouldBeNil)
  159. res, err := parser.Parse(modelJson, dsInfo)
  160. So(err, ShouldBeNil)
  161. So(res.RawQuery, ShouldEqual, "RawDummieQuery")
  162. So(len(res.GroupBy), ShouldEqual, 2)
  163. So(len(res.Selects), ShouldEqual, 1)
  164. So(len(res.Tags), ShouldEqual, 0)
  165. So(res.Interval, ShouldEqual, time.Second*10)
  166. })
  167. })
  168. }