helper_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "github.com/smartystreets/goconvey/convey"
  18. monitor2 "yunion.io/x/onecloud/pkg/apis/monitor"
  19. "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  20. )
  21. func TestAlertQuery(t *testing.T) {
  22. convey.Convey("Alert query test", t, func() {
  23. parser := new(InfluxdbQueryParser)
  24. q := monitor.NewAlertQuery("telegraf", "diskio").From("5m").To("now")
  25. q.Selects().Select("await").MEAN()
  26. q.Where().Equal("hostname", "host1").Equal("provider", "kvm")
  27. q.GroupBy().TAG("*").FILL_NULL()
  28. qCtx := q.ToTsdbQuery()
  29. influxdbQ, err := parser.Parse(qCtx.Queries[0], nil)
  30. convey.So(err, convey.ShouldBeNil)
  31. rawQuery, err := influxdbQ.Build(qCtx)
  32. convey.So(err, convey.ShouldBeNil)
  33. convey.So(rawQuery, convey.ShouldEqual, `SELECT mean("await") FROM "diskio" WHERE ("hostname" = 'host1' AND "provider" = 'kvm') AND time > now() - 5m GROUP BY * fill(null)`)
  34. })
  35. convey.Convey("Alert last query", t, func() {
  36. q := monitor.NewAlertQuery("telegraf", "diskio").From("5m").To("now")
  37. q.Selects().Select("*").LAST()
  38. q.Where().AddTag(&monitor2.MetricQueryTag{
  39. Operator: "=~",
  40. Key: "project_id",
  41. Value: "/xxx/",
  42. })
  43. tq := q.ToTsdbQuery()
  44. parser := new(InfluxdbQueryParser)
  45. influxQ, err := parser.Parse(tq.Queries[0], nil)
  46. convey.So(err, convey.ShouldBeNil)
  47. rawQ, err := influxQ.Build(tq)
  48. convey.So(err, convey.ShouldBeNil)
  49. convey.So(rawQ, convey.ShouldEqual, `SELECT last(*) FROM "diskio" WHERE "project_id" =~ /xxx/ AND time > now() - 5m`)
  50. })
  51. }