macros_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.RFC3339Nano), to.Format(time.RFC3339Nano)))
  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 __unixEpochNanoFilter function", func() {
  86. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(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 __unixEpochNanoFrom function", func() {
  91. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFrom()")
  92. So(err, ShouldBeNil)
  93. So(sql, ShouldEqual, fmt.Sprintf("select '%d'", from.UnixNano()))
  94. })
  95. Convey("interpolate __unixEpochNanoTo function", func() {
  96. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoTo()")
  97. So(err, ShouldBeNil)
  98. So(sql, ShouldEqual, fmt.Sprintf("select '%d'", to.UnixNano()))
  99. })
  100. Convey("interpolate __unixEpochGroup function", func() {
  101. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  102. So(err, ShouldBeNil)
  103. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  104. So(err, ShouldBeNil)
  105. So(sql, ShouldEqual, "SELECT floor(time_column/300)*300")
  106. So(sql2, ShouldEqual, sql+" AS \"time\"")
  107. })
  108. })
  109. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  110. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  111. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  112. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  113. Convey("interpolate __timeFilter function", func() {
  114. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  115. So(err, ShouldBeNil)
  116. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339Nano), to.Format(time.RFC3339Nano)))
  117. })
  118. Convey("interpolate __unixEpochFilter function", func() {
  119. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  120. So(err, ShouldBeNil)
  121. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  122. })
  123. Convey("interpolate __unixEpochNanoFilter function", func() {
  124. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time)")
  125. So(err, ShouldBeNil)
  126. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  127. })
  128. })
  129. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  130. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  131. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  132. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  133. Convey("interpolate __timeFilter function", func() {
  134. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  135. So(err, ShouldBeNil)
  136. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339Nano), to.Format(time.RFC3339Nano)))
  137. })
  138. Convey("interpolate __unixEpochFilter function", func() {
  139. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  140. So(err, ShouldBeNil)
  141. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  142. })
  143. Convey("interpolate __unixEpochNanoFilter function", func() {
  144. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time)")
  145. So(err, ShouldBeNil)
  146. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  147. })
  148. })
  149. Convey("Given a time range between 1960-02-01 07:00:00.5 and 1980-02-03 08:00:00.5", func() {
  150. from := time.Date(1960, 2, 1, 7, 0, 0, 500e6, time.UTC)
  151. to := time.Date(1980, 2, 3, 8, 0, 0, 500e6, time.UTC)
  152. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  153. So(from.Format(time.RFC3339Nano), ShouldEqual, "1960-02-01T07:00:00.5Z")
  154. So(to.Format(time.RFC3339Nano), ShouldEqual, "1980-02-03T08:00:00.5Z")
  155. Convey("interpolate __timeFilter function", func() {
  156. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  157. So(err, ShouldBeNil)
  158. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339Nano), to.Format(time.RFC3339Nano)))
  159. })
  160. })
  161. })
  162. }