macros_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package postgres
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/tsdb"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestMacroEngine(t *testing.T) {
  11. Convey("MacroEngine", t, func() {
  12. timescaledbEnabled := false
  13. engine := newPostgresMacroEngine(timescaledbEnabled)
  14. timescaledbEnabled = true
  15. engineTS := newPostgresMacroEngine(timescaledbEnabled)
  16. query := &tsdb.Query{}
  17. Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
  18. from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
  19. to := from.Add(5 * time.Minute)
  20. timeRange := tsdb.NewFakeTimeRange("5m", "now", to)
  21. Convey("interpolate __time function", func() {
  22. sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)")
  23. So(err, ShouldBeNil)
  24. So(sql, ShouldEqual, "select time_column AS \"time\"")
  25. })
  26. Convey("interpolate __time function wrapped in aggregation", func() {
  27. sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))")
  28. So(err, ShouldBeNil)
  29. So(sql, ShouldEqual, "select min(time_column AS \"time\")")
  30. })
  31. Convey("interpolate __timeFilter function", func() {
  32. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  33. So(err, ShouldBeNil)
  34. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  35. })
  36. Convey("interpolate __timeFrom function", func() {
  37. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom()")
  38. So(err, ShouldBeNil)
  39. So(sql, ShouldEqual, "select '2018-04-12T18:00:00Z'")
  40. })
  41. Convey("interpolate __timeTo function", func() {
  42. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo()")
  43. So(err, ShouldBeNil)
  44. So(sql, ShouldEqual, "select '2018-04-12T18:05:00Z'")
  45. })
  46. Convey("interpolate __timeGroup function pre 5.3 compatibility", func() {
  47. sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m'), value")
  48. So(err, ShouldBeNil)
  49. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 AS \"time\", value")
  50. sql, err = engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m') as time, value")
  51. So(err, ShouldBeNil)
  52. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 as time, value")
  53. })
  54. Convey("interpolate __timeGroup function", func() {
  55. sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m')")
  56. So(err, ShouldBeNil)
  57. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroupAlias(time_column,'5m')")
  58. So(err, ShouldBeNil)
  59. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300")
  60. So(sql2, ShouldEqual, sql+" AS \"time\"")
  61. })
  62. Convey("interpolate __timeGroup function with spaces between args", func() {
  63. sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column , '5m')")
  64. So(err, ShouldBeNil)
  65. sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column , '5m')")
  66. So(err, ShouldBeNil)
  67. So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
  68. So(sql2, ShouldEqual, sql+" AS \"time\"")
  69. })
  70. Convey("interpolate __timeGroup function with TimescaleDB enabled", func() {
  71. sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
  72. So(err, ShouldBeNil)
  73. So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
  74. })
  75. Convey("interpolate __timeGroup function with spaces between args and TimescaleDB enabled", func() {
  76. sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
  77. So(err, ShouldBeNil)
  78. So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
  79. })
  80. Convey("interpolate __unixEpochFilter function", func() {
  81. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  82. So(err, ShouldBeNil)
  83. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  84. })
  85. Convey("interpolate __unixEpochFilterNano function", func() {
  86. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilterNano(time)")
  87. So(err, ShouldBeNil)
  88. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  89. })
  90. Convey("interpolate __unixEpochGroup function", func() {
  91. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  92. So(err, ShouldBeNil)
  93. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  94. So(err, ShouldBeNil)
  95. So(sql, ShouldEqual, "SELECT floor(time_column/300)*300")
  96. So(sql2, ShouldEqual, sql+" AS \"time\"")
  97. })
  98. })
  99. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  100. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  101. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  102. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  103. Convey("interpolate __timeFilter function", func() {
  104. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  105. So(err, ShouldBeNil)
  106. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  107. })
  108. Convey("interpolate __unixEpochFilter function", func() {
  109. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  110. So(err, ShouldBeNil)
  111. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  112. })
  113. Convey("interpolate __unixEpochFilterNano function", func() {
  114. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilterNano(time)")
  115. So(err, ShouldBeNil)
  116. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  117. })
  118. })
  119. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  120. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  121. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  122. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  123. Convey("interpolate __timeFilter function", func() {
  124. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  125. So(err, ShouldBeNil)
  126. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  127. })
  128. Convey("interpolate __unixEpochFilter function", func() {
  129. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  130. So(err, ShouldBeNil)
  131. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  132. })
  133. Convey("interpolate __unixEpochFilterNano function", func() {
  134. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilterNano(time)")
  135. So(err, ShouldBeNil)
  136. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  137. })
  138. })
  139. })
  140. }