query_parser_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "testing"
  17. "time"
  18. . "github.com/smartystreets/goconvey/convey"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  21. )
  22. func TestInfluxdbQueryParser(t *testing.T) {
  23. Convey("Influxdb query parser", t, func() {
  24. parser := &InfluxdbQueryParser{}
  25. Convey("can parse influxdb json model", func() {
  26. json := `
  27. {
  28. "group_by": [
  29. {
  30. "params": ["$interval"],
  31. "type": "time"
  32. },
  33. {
  34. "params": ["datacenter"],
  35. "type": "tag"
  36. },
  37. {
  38. "params": ["none"],
  39. "type": "fill"
  40. }
  41. ],
  42. "measurement": "logins.count",
  43. "tz": "Asia/Shanghai",
  44. "policy": "default",
  45. "refId": "B",
  46. "result_format": "time_series",
  47. "select": [
  48. [
  49. {
  50. "type": "field",
  51. "params": ["value"]
  52. },
  53. {
  54. "type": "count",
  55. "params": []
  56. }
  57. ],
  58. [
  59. {
  60. "type": "field",
  61. "params": ["value"]
  62. },
  63. {
  64. "type": "bottom",
  65. "params": ["3"]
  66. }
  67. ],
  68. [
  69. {
  70. "type": "field",
  71. "params": ["value"]
  72. },
  73. {
  74. "type": "mean",
  75. "params": []
  76. },
  77. {
  78. "type": "math",
  79. "params": [" / 100"]
  80. }
  81. ]
  82. ],
  83. "alias": "serie alias",
  84. "tags": [
  85. {"key": "datacenter", "operator": "=", "value": "America"},
  86. {"condition": "OR", "key": "hostname", "operator": "=", "value": "server1"}
  87. ]
  88. }
  89. `
  90. obj, err := jsonutils.Parse([]byte(json))
  91. So(err, ShouldBeNil)
  92. apiQuery := new(tsdb.Query)
  93. So(obj.Unmarshal(apiQuery), ShouldBeNil)
  94. dsInfo := &tsdb.DataSource{TimeInterval: ">20s"}
  95. res, err := parser.Parse(apiQuery, dsInfo)
  96. So(err, ShouldBeNil)
  97. So(len(res.GroupBy), ShouldEqual, 3)
  98. So(len(res.Selects), ShouldEqual, 3)
  99. So(len(res.Tags), ShouldEqual, 2)
  100. So(res.Tz, ShouldEqual, "Asia/Shanghai")
  101. So(res.Interval, ShouldEqual, time.Second*20)
  102. So(res.Alias, ShouldEqual, "serie alias")
  103. })
  104. })
  105. }