query_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package influxdb
  15. import (
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "time"
  20. . "github.com/smartystreets/goconvey/convey"
  21. api "yunion.io/x/onecloud/pkg/apis/monitor"
  22. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  23. )
  24. func TestInfluxdbQueryBuilder(t *testing.T) {
  25. Convey("Influxdb query builder", t, func() {
  26. qp1, _ := NewQueryPart("field", []string{"value"})
  27. qp2, _ := NewQueryPart("mean", []string{})
  28. qp3, _ := NewQueryPart("func_field", []string{"usage_active", "vm_name", "vm_ip"})
  29. qp4, _ := NewQueryPart("top", []string{"5"})
  30. mathPartDivideBy100, _ := NewQueryPart("math", []string{"/ 100"})
  31. mathPartDivideByIntervalMs, _ := NewQueryPart("math", []string{"/ $__interval_ms"})
  32. groupBy1, _ := NewQueryPart("time", []string{"$__interval"})
  33. groupBy2, _ := NewQueryPart("tag", []string{"datacenter"})
  34. groupBy3, _ := NewQueryPart("fill", []string{"null"})
  35. groupByOldInterval, _ := NewQueryPart("time", []string{"$interval"})
  36. tag1 := api.MetricQueryTag{Key: "hostname", Value: "server1", Operator: "="}
  37. tag2 := api.MetricQueryTag{Key: "hostname", Value: "server2", Operator: "=", Condition: "OR"}
  38. queryContext := &tsdb.TsdbQuery{
  39. TimeRange: tsdb.NewTimeRange("5m", "now"),
  40. }
  41. queryContext2 := &tsdb.TsdbQuery{
  42. TimeRange: tsdb.NewTimeRange("30m", "now"),
  43. }
  44. Convey("can build simple query", func() {
  45. query := &Query{
  46. Selects: []*Select{{*qp1, *qp2}},
  47. Measurement: "cpu",
  48. Policy: "policy",
  49. GroupBy: []*QueryPart{groupBy1, groupBy3},
  50. Interval: time.Second * 10,
  51. }
  52. rawQuery, err := query.Build(queryContext)
  53. So(err, ShouldBeNil)
  54. So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "policy"."cpu" WHERE time > now() - 5m GROUP BY time(10s) fill(null)`)
  55. })
  56. Convey("can build top query", func() {
  57. query := &Query{
  58. Selects: []*Select{{*qp3, *qp4}},
  59. Measurement: "vm_cpu",
  60. Policy: "30day_only",
  61. Interval: time.Second * 10,
  62. }
  63. rawQuery, err := query.Build(queryContext2)
  64. So(err, ShouldBeNil)
  65. So(rawQuery, ShouldEqual, `SELECT top("usage_active","vm_name","vm_ip", 5) FROM "30day_only"."vm_cpu" WHERE time > now() - 30m`)
  66. })
  67. Convey("can build query with tz", func() {
  68. query := &Query{
  69. Selects: []*Select{{*qp1, *qp2}},
  70. Measurement: "cpu",
  71. GroupBy: []*QueryPart{groupBy1},
  72. Tz: "Europe/Paris",
  73. Interval: time.Second * 5,
  74. }
  75. rawQuery, err := query.Build(queryContext)
  76. So(err, ShouldBeNil)
  77. So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE time > now() - 5m GROUP BY time(5s) tz('Europe/Paris')`)
  78. })
  79. Convey("can build query with group bys", func() {
  80. query := &Query{
  81. Selects: []*Select{{*qp1, *qp2}},
  82. Measurement: "cpu",
  83. GroupBy: []*QueryPart{groupBy1, groupBy2, groupBy3},
  84. Tags: []api.MetricQueryTag{tag1, tag2},
  85. Interval: time.Second * 5,
  86. }
  87. rawQuery, err := query.Build(queryContext)
  88. So(err, ShouldBeNil)
  89. So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE ("hostname" = 'server1' OR "hostname" = 'server2') AND time > now() - 5m GROUP BY time(5s), "datacenter" fill(null)`)
  90. })
  91. Convey("can build query with math part", func() {
  92. query := &Query{
  93. Selects: []*Select{{*qp1, *qp2, *mathPartDivideBy100}},
  94. Measurement: "cpu",
  95. Interval: time.Second * 5,
  96. }
  97. rawQuery, err := query.Build(queryContext)
  98. So(err, ShouldBeNil)
  99. So(rawQuery, ShouldEqual, `SELECT mean("value") / 100 FROM "cpu" WHERE time > now() - 5m`)
  100. })
  101. Convey("can build query with math part using $__interval_ms variable", func() {
  102. query := &Query{
  103. Selects: []*Select{{*qp1, *qp2, *mathPartDivideByIntervalMs}},
  104. Measurement: "cpu",
  105. Interval: time.Second * 5,
  106. }
  107. rawQuery, err := query.Build(queryContext)
  108. So(err, ShouldBeNil)
  109. So(rawQuery, ShouldEqual, `SELECT mean("value") / 5000 FROM "cpu" WHERE time > now() - 5m`)
  110. })
  111. Convey("can build query with old $interval variable", func() {
  112. query := &Query{
  113. Selects: []*Select{{*qp1, *qp2}},
  114. Measurement: "cpu",
  115. Policy: "",
  116. GroupBy: []*QueryPart{groupByOldInterval},
  117. }
  118. rawQuery, err := query.Build(queryContext)
  119. So(err, ShouldBeNil)
  120. So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE time > now() - 5m GROUP BY time(200ms)`)
  121. })
  122. Convey("can render time range", func() {
  123. query := Query{}
  124. Convey("render from: 2h to now-1h", func() {
  125. query := Query{}
  126. queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("2h", "now-1h")}
  127. So(query.renderTimeFilter(queryContext), ShouldEqual, "time > now() - 2h and time < now() - 1h")
  128. })
  129. Convey("render from: 10m", func() {
  130. queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("10m", "now")}
  131. So(query.renderTimeFilter(queryContext), ShouldEqual, "time > now() - 10m")
  132. })
  133. Convey("render from: 1701957983540 to 1701961583540", func() {
  134. query := Query{}
  135. start := "1701957983540"
  136. end := "1701961583540"
  137. queryContext := &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange(start, end)}
  138. So(query.renderTimeFilter(queryContext), ShouldEqual, fmt.Sprintf("time > %sms and time < %sms", start, end))
  139. })
  140. })
  141. Convey("can render normal tags without operator", func() {
  142. query := &Query{Tags: []api.MetricQueryTag{{Operator: "", Value: `value`, Key: "key"}}}
  143. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'value'`)
  144. })
  145. Convey("can render regex tags without operator", func() {
  146. query := &Query{Tags: []api.MetricQueryTag{{Operator: "", Value: `/value/`, Key: "key"}}}
  147. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" =~ /value/`)
  148. })
  149. Convey("can render regex tags", func() {
  150. query := &Query{Tags: []api.MetricQueryTag{{Operator: "=~", Value: `/value/`, Key: "key"}}}
  151. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" =~ /value/`)
  152. })
  153. Convey("can render number tags", func() {
  154. query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: "10001", Key: "key"}}}
  155. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = '10001'`)
  156. })
  157. Convey("can render numbers less then condition tags", func() {
  158. query := &Query{Tags: []api.MetricQueryTag{{Operator: "<", Value: "10001", Key: "key"}}}
  159. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" < 10001`)
  160. })
  161. Convey("can render number greater then condition tags", func() {
  162. query := &Query{Tags: []api.MetricQueryTag{{Operator: ">", Value: "10001", Key: "key"}}}
  163. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" > 10001`)
  164. })
  165. Convey("can render string tags", func() {
  166. query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: "value", Key: "key"}}}
  167. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'value'`)
  168. })
  169. Convey("can escape backslashes when rendering string tags", func() {
  170. query := &Query{Tags: []api.MetricQueryTag{{Operator: "=", Value: `C:\test\`, Key: "key"}}}
  171. So(strings.Join(query.renderTags(), ""), ShouldEqual, `"key" = 'C:\\test\\'`)
  172. })
  173. Convey("can render regular measurement", func() {
  174. query := &Query{Measurement: `apa`, Policy: "policy"}
  175. So(query.renderMeasurement(), ShouldEqual, ` FROM "policy"."apa"`)
  176. })
  177. Convey("can render regexp measurement", func() {
  178. query := &Query{Measurement: `/apa/`, Policy: "policy"}
  179. So(query.renderMeasurement(), ShouldEqual, ` FROM "policy"./apa/`)
  180. })
  181. })
  182. }