index_pattern_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package es
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestIndexPattern(t *testing.T) {
  10. Convey("Static index patterns", t, func() {
  11. indexPatternScenario(noInterval, "data-*", nil, func(indices []string) {
  12. So(indices, ShouldHaveLength, 1)
  13. So(indices[0], ShouldEqual, "data-*")
  14. })
  15. indexPatternScenario(noInterval, "es-index-name", nil, func(indices []string) {
  16. So(indices, ShouldHaveLength, 1)
  17. So(indices[0], ShouldEqual, "es-index-name")
  18. })
  19. })
  20. Convey("Dynamic index patterns", t, func() {
  21. from := fmt.Sprintf("%d", time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
  22. to := fmt.Sprintf("%d", time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
  23. indexPatternScenario(intervalHourly, "[data-]YYYY.MM.DD.HH", tsdb.NewTimeRange(from, to), func(indices []string) {
  24. So(indices, ShouldHaveLength, 1)
  25. So(indices[0], ShouldEqual, "data-2018.05.15.17")
  26. })
  27. indexPatternScenario(intervalHourly, "YYYY.MM.DD.HH[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
  28. So(indices, ShouldHaveLength, 1)
  29. So(indices[0], ShouldEqual, "2018.05.15.17-data")
  30. })
  31. indexPatternScenario(intervalDaily, "[data-]YYYY.MM.DD", tsdb.NewTimeRange(from, to), func(indices []string) {
  32. So(indices, ShouldHaveLength, 1)
  33. So(indices[0], ShouldEqual, "data-2018.05.15")
  34. })
  35. indexPatternScenario(intervalDaily, "YYYY.MM.DD[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
  36. So(indices, ShouldHaveLength, 1)
  37. So(indices[0], ShouldEqual, "2018.05.15-data")
  38. })
  39. indexPatternScenario(intervalWeekly, "[data-]GGGG.WW", tsdb.NewTimeRange(from, to), func(indices []string) {
  40. So(indices, ShouldHaveLength, 1)
  41. So(indices[0], ShouldEqual, "data-2018.20")
  42. })
  43. indexPatternScenario(intervalWeekly, "GGGG.WW[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
  44. So(indices, ShouldHaveLength, 1)
  45. So(indices[0], ShouldEqual, "2018.20-data")
  46. })
  47. indexPatternScenario(intervalMonthly, "[data-]YYYY.MM", tsdb.NewTimeRange(from, to), func(indices []string) {
  48. So(indices, ShouldHaveLength, 1)
  49. So(indices[0], ShouldEqual, "data-2018.05")
  50. })
  51. indexPatternScenario(intervalMonthly, "YYYY.MM[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
  52. So(indices, ShouldHaveLength, 1)
  53. So(indices[0], ShouldEqual, "2018.05-data")
  54. })
  55. indexPatternScenario(intervalYearly, "[data-]YYYY", tsdb.NewTimeRange(from, to), func(indices []string) {
  56. So(indices, ShouldHaveLength, 1)
  57. So(indices[0], ShouldEqual, "data-2018")
  58. })
  59. indexPatternScenario(intervalYearly, "YYYY[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
  60. So(indices, ShouldHaveLength, 1)
  61. So(indices[0], ShouldEqual, "2018-data")
  62. })
  63. Convey("Should return 01 week", func() {
  64. from = fmt.Sprintf("%d", time.Date(2018, 1, 15, 17, 50, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
  65. to = fmt.Sprintf("%d", time.Date(2018, 1, 15, 17, 55, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
  66. indexPatternScenario(intervalWeekly, "[data-]GGGG.WW", tsdb.NewTimeRange(from, to), func(indices []string) {
  67. So(indices, ShouldHaveLength, 1)
  68. So(indices[0], ShouldEqual, "data-2018.03")
  69. })
  70. })
  71. })
  72. Convey("Hourly interval", t, func() {
  73. Convey("Should return 1 interval", func() {
  74. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  75. to := time.Date(2018, 1, 1, 23, 6, 0, 0, time.UTC)
  76. intervals := (&hourlyInterval{}).Generate(from, to)
  77. So(intervals, ShouldHaveLength, 1)
  78. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 23, 0, 0, 0, time.UTC))
  79. })
  80. Convey("Should return 2 intervals", func() {
  81. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  82. to := time.Date(2018, 1, 2, 0, 6, 0, 0, time.UTC)
  83. intervals := (&hourlyInterval{}).Generate(from, to)
  84. So(intervals, ShouldHaveLength, 2)
  85. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 23, 0, 0, 0, time.UTC))
  86. So(intervals[1], ShouldEqual, time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC))
  87. })
  88. Convey("Should return 10 intervals", func() {
  89. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  90. to := time.Date(2018, 1, 2, 8, 6, 0, 0, time.UTC)
  91. intervals := (&hourlyInterval{}).Generate(from, to)
  92. So(intervals, ShouldHaveLength, 10)
  93. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 23, 0, 0, 0, time.UTC))
  94. So(intervals[4], ShouldEqual, time.Date(2018, 1, 2, 3, 0, 0, 0, time.UTC))
  95. So(intervals[9], ShouldEqual, time.Date(2018, 1, 2, 8, 0, 0, 0, time.UTC))
  96. })
  97. })
  98. Convey("Daily interval", t, func() {
  99. Convey("Should return 1 day", func() {
  100. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  101. to := time.Date(2018, 1, 1, 23, 6, 0, 0, time.UTC)
  102. intervals := (&dailyInterval{}).Generate(from, to)
  103. So(intervals, ShouldHaveLength, 1)
  104. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  105. })
  106. Convey("Should return 2 days", func() {
  107. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  108. to := time.Date(2018, 1, 2, 0, 6, 0, 0, time.UTC)
  109. intervals := (&dailyInterval{}).Generate(from, to)
  110. So(intervals, ShouldHaveLength, 2)
  111. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  112. So(intervals[1], ShouldEqual, time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC))
  113. })
  114. Convey("Should return 32 days", func() {
  115. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  116. to := time.Date(2018, 2, 1, 8, 6, 0, 0, time.UTC)
  117. intervals := (&dailyInterval{}).Generate(from, to)
  118. So(intervals, ShouldHaveLength, 32)
  119. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  120. So(intervals[30], ShouldEqual, time.Date(2018, 1, 31, 0, 0, 0, 0, time.UTC))
  121. So(intervals[31], ShouldEqual, time.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC))
  122. })
  123. })
  124. Convey("Weekly interval", t, func() {
  125. Convey("Should return 1 week (1)", func() {
  126. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  127. to := time.Date(2018, 1, 1, 23, 6, 0, 0, time.UTC)
  128. intervals := (&weeklyInterval{}).Generate(from, to)
  129. So(intervals, ShouldHaveLength, 1)
  130. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  131. })
  132. Convey("Should return 1 week (2)", func() {
  133. from := time.Date(2017, 1, 1, 23, 1, 1, 0, time.UTC)
  134. to := time.Date(2017, 1, 1, 23, 6, 0, 0, time.UTC)
  135. intervals := (&weeklyInterval{}).Generate(from, to)
  136. So(intervals, ShouldHaveLength, 1)
  137. So(intervals[0], ShouldEqual, time.Date(2016, 12, 26, 0, 0, 0, 0, time.UTC))
  138. })
  139. Convey("Should return 2 weeks (1)", func() {
  140. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  141. to := time.Date(2018, 1, 10, 23, 6, 0, 0, time.UTC)
  142. intervals := (&weeklyInterval{}).Generate(from, to)
  143. So(intervals, ShouldHaveLength, 2)
  144. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  145. So(intervals[1], ShouldEqual, time.Date(2018, 1, 8, 0, 0, 0, 0, time.UTC))
  146. })
  147. Convey("Should return 2 weeks (2)", func() {
  148. from := time.Date(2017, 1, 1, 23, 1, 1, 0, time.UTC)
  149. to := time.Date(2017, 1, 8, 23, 6, 0, 0, time.UTC)
  150. intervals := (&weeklyInterval{}).Generate(from, to)
  151. So(intervals, ShouldHaveLength, 2)
  152. So(intervals[0], ShouldEqual, time.Date(2016, 12, 26, 0, 0, 0, 0, time.UTC))
  153. So(intervals[1], ShouldEqual, time.Date(2017, 1, 2, 0, 0, 0, 0, time.UTC))
  154. })
  155. Convey("Should return 3 weeks (1)", func() {
  156. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  157. to := time.Date(2018, 1, 21, 23, 6, 0, 0, time.UTC)
  158. intervals := (&weeklyInterval{}).Generate(from, to)
  159. So(intervals, ShouldHaveLength, 3)
  160. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  161. So(intervals[1], ShouldEqual, time.Date(2018, 1, 8, 0, 0, 0, 0, time.UTC))
  162. So(intervals[2], ShouldEqual, time.Date(2018, 1, 15, 0, 0, 0, 0, time.UTC))
  163. })
  164. Convey("Should return 3 weeks (2)", func() {
  165. from := time.Date(2017, 1, 1, 23, 1, 1, 0, time.UTC)
  166. to := time.Date(2017, 1, 9, 23, 6, 0, 0, time.UTC)
  167. intervals := (&weeklyInterval{}).Generate(from, to)
  168. So(intervals, ShouldHaveLength, 3)
  169. So(intervals[0], ShouldEqual, time.Date(2016, 12, 26, 0, 0, 0, 0, time.UTC))
  170. So(intervals[1], ShouldEqual, time.Date(2017, 1, 2, 0, 0, 0, 0, time.UTC))
  171. So(intervals[2], ShouldEqual, time.Date(2017, 1, 9, 0, 0, 0, 0, time.UTC))
  172. })
  173. })
  174. Convey("Monthly interval", t, func() {
  175. Convey("Should return 1 month", func() {
  176. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  177. to := time.Date(2018, 1, 1, 23, 6, 0, 0, time.UTC)
  178. intervals := (&monthlyInterval{}).Generate(from, to)
  179. So(intervals, ShouldHaveLength, 1)
  180. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  181. })
  182. Convey("Should return 2 months", func() {
  183. from := time.Date(2018, 1, 1, 23, 1, 1, 0, time.UTC)
  184. to := time.Date(2018, 2, 2, 0, 6, 0, 0, time.UTC)
  185. intervals := (&monthlyInterval{}).Generate(from, to)
  186. So(intervals, ShouldHaveLength, 2)
  187. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  188. So(intervals[1], ShouldEqual, time.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC))
  189. })
  190. Convey("Should return 14 months", func() {
  191. from := time.Date(2017, 1, 1, 23, 1, 1, 0, time.UTC)
  192. to := time.Date(2018, 2, 1, 8, 6, 0, 0, time.UTC)
  193. intervals := (&monthlyInterval{}).Generate(from, to)
  194. So(intervals, ShouldHaveLength, 14)
  195. So(intervals[0], ShouldEqual, time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC))
  196. So(intervals[13], ShouldEqual, time.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC))
  197. })
  198. })
  199. Convey("Yearly interval", t, func() {
  200. Convey("Should return 1 year (hour diff)", func() {
  201. from := time.Date(2018, 2, 1, 23, 1, 1, 0, time.UTC)
  202. to := time.Date(2018, 2, 1, 23, 6, 0, 0, time.UTC)
  203. intervals := (&yearlyInterval{}).Generate(from, to)
  204. So(intervals, ShouldHaveLength, 1)
  205. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  206. })
  207. Convey("Should return 1 year (month diff)", func() {
  208. from := time.Date(2018, 2, 1, 23, 1, 1, 0, time.UTC)
  209. to := time.Date(2018, 12, 31, 23, 59, 59, 0, time.UTC)
  210. intervals := (&yearlyInterval{}).Generate(from, to)
  211. So(intervals, ShouldHaveLength, 1)
  212. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  213. })
  214. Convey("Should return 2 years", func() {
  215. from := time.Date(2018, 2, 1, 23, 1, 1, 0, time.UTC)
  216. to := time.Date(2019, 1, 1, 23, 59, 59, 0, time.UTC)
  217. intervals := (&yearlyInterval{}).Generate(from, to)
  218. So(intervals, ShouldHaveLength, 2)
  219. So(intervals[0], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  220. So(intervals[1], ShouldEqual, time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC))
  221. })
  222. Convey("Should return 5 years", func() {
  223. from := time.Date(2014, 1, 1, 23, 1, 1, 0, time.UTC)
  224. to := time.Date(2018, 11, 1, 23, 59, 59, 0, time.UTC)
  225. intervals := (&yearlyInterval{}).Generate(from, to)
  226. So(intervals, ShouldHaveLength, 5)
  227. So(intervals[0], ShouldEqual, time.Date(2014, 1, 1, 0, 0, 0, 0, time.UTC))
  228. So(intervals[4], ShouldEqual, time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC))
  229. })
  230. })
  231. }
  232. func indexPatternScenario(interval string, pattern string, timeRange *tsdb.TimeRange, fn func(indices []string)) {
  233. Convey(fmt.Sprintf("Index pattern (interval=%s, index=%s", interval, pattern), func() {
  234. ip, err := newIndexPattern(interval, pattern)
  235. So(err, ShouldBeNil)
  236. So(ip, ShouldNotBeNil)
  237. indices, err := ip.GetIndices(timeRange)
  238. So(err, ShouldBeNil)
  239. fn(indices)
  240. })
  241. }