window_test.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. package kubecost
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/google/go-cmp/cmp"
  9. "github.com/opencost/opencost/pkg/util/timeutil"
  10. "github.com/opencost/opencost/pkg/env"
  11. )
  12. func TestRoundBack(t *testing.T) {
  13. boulder := time.FixedZone("Boulder", -7*60*60)
  14. beijing := time.FixedZone("Beijing", 8*60*60)
  15. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  16. tb := RoundBack(to, 24*time.Hour)
  17. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  18. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  19. }
  20. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  21. tb = RoundBack(to, 24*time.Hour)
  22. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  23. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  24. }
  25. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  26. tb = RoundBack(to, 24*time.Hour)
  27. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  28. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  29. }
  30. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  31. tb = RoundBack(to, 24*time.Hour)
  32. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  33. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  34. }
  35. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  36. tb = RoundBack(to, 24*time.Hour)
  37. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  38. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  39. }
  40. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  41. tb = RoundBack(to, 24*time.Hour)
  42. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  43. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  44. }
  45. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  46. tb = RoundBack(to, 24*time.Hour)
  47. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  48. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  49. }
  50. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  51. tb = RoundBack(to, 24*time.Hour)
  52. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  53. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  54. }
  55. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  56. tb = RoundBack(to, 24*time.Hour)
  57. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  58. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  59. }
  60. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  61. tb = RoundBack(to, 24*time.Hour)
  62. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  63. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  64. }
  65. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  66. tb = RoundBack(to, 24*time.Hour)
  67. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  68. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  69. }
  70. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  71. tb = RoundBack(to, 24*time.Hour)
  72. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  73. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  74. }
  75. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  76. tb = RoundBack(to, timeutil.Week)
  77. if !tb.Equal(time.Date(2019, time.December, 29, 0, 0, 0, 0, time.UTC)) {
  78. t.Fatalf("RoundForward: expected 2019-12-29T00:00:00Z; actual %s", tb)
  79. }
  80. }
  81. func TestRoundForward(t *testing.T) {
  82. boulder := time.FixedZone("Boulder", -7*60*60)
  83. beijing := time.FixedZone("Beijing", 8*60*60)
  84. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  85. tb := RoundForward(to, 24*time.Hour)
  86. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  87. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  88. }
  89. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  90. tb = RoundForward(to, 24*time.Hour)
  91. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  92. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  93. }
  94. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  95. tb = RoundForward(to, 24*time.Hour)
  96. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  97. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  98. }
  99. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  100. tb = RoundForward(to, 24*time.Hour)
  101. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  102. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  103. }
  104. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  105. tb = RoundForward(to, 24*time.Hour)
  106. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  107. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  108. }
  109. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  110. tb = RoundForward(to, 24*time.Hour)
  111. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  112. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  113. }
  114. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  115. tb = RoundForward(to, 24*time.Hour)
  116. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  117. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  118. }
  119. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  120. tb = RoundForward(to, 24*time.Hour)
  121. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  122. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  123. }
  124. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  125. tb = RoundForward(to, 24*time.Hour)
  126. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  127. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00Z; actual %s", tb)
  128. }
  129. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  130. tb = RoundForward(to, 24*time.Hour)
  131. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  132. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  133. }
  134. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  135. tb = RoundForward(to, 24*time.Hour)
  136. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  137. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  138. }
  139. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  140. tb = RoundForward(to, 24*time.Hour)
  141. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  142. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  143. }
  144. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  145. tb = RoundForward(to, timeutil.Week)
  146. if !tb.Equal(time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)) {
  147. t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
  148. }
  149. to = time.Date(2020, time.January, 5, 23, 59, 0, 0, time.UTC)
  150. tb = RoundForward(to, timeutil.Week)
  151. if !tb.Equal(time.Date(2020, time.January, 12, 0, 0, 0, 0, time.UTC)) {
  152. t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
  153. }
  154. to = time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)
  155. tb = RoundForward(to, timeutil.Week)
  156. if !tb.Equal(time.Date(2020, time.January, 5, 0, 0, 0, 0, time.UTC)) {
  157. t.Fatalf("RoundForward: expected 2020-01-05T00:00:00Z; actual %s", tb)
  158. }
  159. }
  160. func TestParseWindowUTC(t *testing.T) {
  161. now := time.Now().UTC()
  162. // "today" should span Now() and not produce an error
  163. today, err := ParseWindowUTC("today")
  164. if err != nil {
  165. t.Fatalf(`unexpected error parsing "today": %s`, err)
  166. }
  167. if today.Duration().Hours() != 24 {
  168. t.Fatalf(`expect: window "today" to have duration 24 hour; actual: %f hours`, today.Duration().Hours())
  169. }
  170. if !today.Contains(time.Now().UTC()) {
  171. t.Fatalf(`expect: window "today" to contain now; actual: %s`, today)
  172. }
  173. // "yesterday" should span Now() and not produce an error
  174. yesterday, err := ParseWindowUTC("yesterday")
  175. if err != nil {
  176. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  177. }
  178. if yesterday.Duration().Hours() != 24 {
  179. t.Fatalf(`expect: window "yesterday" to have duration 24 hour; actual: %f hours`, yesterday.Duration().Hours())
  180. }
  181. if !yesterday.End().Before(time.Now().UTC()) {
  182. t.Fatalf(`expect: window "yesterday" to end before now; actual: %s ends after %s`, yesterday, time.Now().UTC())
  183. }
  184. week, err := ParseWindowUTC("week")
  185. hoursThisWeek := float64(time.Now().UTC().Weekday()) * 24.0
  186. if err != nil {
  187. t.Fatalf(`unexpected error parsing "week": %s`, err)
  188. }
  189. if week.Duration().Hours() < hoursThisWeek {
  190. t.Fatalf(`expect: window "week" to have at least %f hours; actual: %f hours`, hoursThisWeek, week.Duration().Hours())
  191. }
  192. if week.End().After(time.Now().UTC()) {
  193. t.Fatalf(`expect: window "week" to end before now; actual: %s ends after %s`, week, time.Now().UTC())
  194. }
  195. month, err := ParseWindowUTC("month")
  196. hoursThisMonth := float64(time.Now().UTC().Day()) * 24.0
  197. if err != nil {
  198. t.Fatalf(`unexpected error parsing "month": %s`, err)
  199. }
  200. if month.Duration().Hours() > hoursThisMonth || month.Duration().Hours() < (hoursThisMonth-24.0) {
  201. t.Fatalf(`expect: window "month" to have approximately %f hours; actual: %f hours`, hoursThisMonth, month.Duration().Hours())
  202. }
  203. // this test fails periodically if execution is so fast that time.Now() during the condition
  204. // check is the same as the end of the current month time computed by ParseWindowUTC
  205. // so we add one nanosecond to sure time.Now() is later than when invoked earlier
  206. if !month.End().Before(time.Now().UTC().Add(time.Nanosecond)) {
  207. t.Fatalf(`expect: window "month" to end before now; actual: %s ends after %s`, month, time.Now().UTC())
  208. }
  209. // TODO lastweek
  210. lastmonth, err := ParseWindowUTC("lastmonth")
  211. monthMinHours := float64(24 * 28)
  212. monthMaxHours := float64(24 * 31)
  213. firstOfMonth := now.Truncate(time.Hour * 24).Add(-24 * time.Hour * time.Duration(now.Day()-1))
  214. if err != nil {
  215. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  216. }
  217. if lastmonth.Duration().Hours() > monthMaxHours || lastmonth.Duration().Hours() < monthMinHours {
  218. t.Fatalf(`expect: window "lastmonth" to have approximately %f hours; actual: %f hours`, hoursThisMonth, lastmonth.Duration().Hours())
  219. }
  220. if !lastmonth.End().Equal(firstOfMonth) {
  221. t.Fatalf(`expect: window "lastmonth" to end on the first of the current month; actual: %s doesn't end on %s`, lastmonth, firstOfMonth)
  222. }
  223. ago12h := time.Now().UTC().Add(-12 * time.Hour)
  224. ago36h := time.Now().UTC().Add(-36 * time.Hour)
  225. ago60h := time.Now().UTC().Add(-60 * time.Hour)
  226. // "24h" should have 24 hour duration and not produce an error
  227. dur24h, err := ParseWindowUTC("24h")
  228. if err != nil {
  229. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  230. }
  231. if dur24h.Duration().Hours() != 24 {
  232. t.Fatalf(`expect: window "24h" to have duration 24 hour; actual: %f hours`, dur24h.Duration().Hours())
  233. }
  234. if !dur24h.Contains(ago12h) {
  235. t.Fatalf(`expect: window "24h" to contain 12 hours ago; actual: %s doesn't contain %s`, dur24h, ago12h)
  236. }
  237. if dur24h.Contains(ago36h) {
  238. t.Fatalf(`expect: window "24h" to not contain 36 hours ago; actual: %s contains %s`, dur24h, ago36h)
  239. }
  240. // "2d" should have 2 day duration and not produce an error
  241. dur2d, err := ParseWindowUTC("2d")
  242. if err != nil {
  243. t.Fatalf(`unexpected error parsing "2d": %s`, err)
  244. }
  245. if dur2d.Duration().Hours() != 48 {
  246. t.Fatalf(`expect: window "2d" to have duration 48 hour; actual: %f hours`, dur2d.Duration().Hours())
  247. }
  248. if !dur2d.Contains(ago36h) {
  249. t.Fatalf(`expect: window "2d" to contain 36 hours ago; actual: %s doesn't contain %s`, dur2d, ago36h)
  250. }
  251. if dur2d.Contains(ago60h) {
  252. t.Fatalf(`expect: window "2d" to not contain 60 hours ago; actual: %s contains %s`, dur2d, ago60h)
  253. }
  254. // "24h offset 14h" should have 24 hour duration and not produce an error
  255. dur24hOff14h, err := ParseWindowUTC("24h offset 14h")
  256. if err != nil {
  257. t.Fatalf(`unexpected error parsing "24h offset 14h": %s`, err)
  258. }
  259. if dur24hOff14h.Duration().Hours() != 24 {
  260. t.Fatalf(`expect: window "24h offset 14h" to have duration 24 hour; actual: %f hours`, dur24hOff14h.Duration().Hours())
  261. }
  262. if dur24hOff14h.Contains(ago12h) {
  263. t.Fatalf(`expect: window "24h offset 14h" not to contain 12 hours ago; actual: %s contains %s`, dur24hOff14h, ago12h)
  264. }
  265. if !dur24hOff14h.Contains(ago36h) {
  266. t.Fatalf(`expect: window "24h offset 14h" to contain 36 hours ago; actual: %s does not contain %s`, dur24hOff14h, ago36h)
  267. }
  268. april152020, _ := time.Parse(time.RFC3339, "2020-04-15T00:00:00Z")
  269. april102020, _ := time.Parse(time.RFC3339, "2020-04-10T00:00:00Z")
  270. april052020, _ := time.Parse(time.RFC3339, "2020-04-05T00:00:00Z")
  271. // "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z" should have 96 hour duration and not produce an error
  272. april8to12, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  273. if err != nil {
  274. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  275. }
  276. if april8to12.Duration().Hours() != 96 {
  277. t.Fatalf(`expect: window %s to have duration 96 hour; actual: %f hours`, april8to12, april8to12.Duration().Hours())
  278. }
  279. if !april8to12.Contains(april102020) {
  280. t.Fatalf(`expect: window April 8-12 to contain April 10; actual: %s doesn't contain %s`, april8to12, april102020)
  281. }
  282. if april8to12.Contains(april052020) {
  283. t.Fatalf(`expect: window April 8-12 to not contain April 5; actual: %s contains %s`, april8to12, april052020)
  284. }
  285. if april8to12.Contains(april152020) {
  286. t.Fatalf(`expect: window April 8-12 to not contain April 15; actual: %s contains %s`, april8to12, april152020)
  287. }
  288. march152020, _ := time.Parse(time.RFC3339, "2020-03-15T00:00:00Z")
  289. march102020, _ := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
  290. march052020, _ := time.Parse(time.RFC3339, "2020-03-05T00:00:00Z")
  291. // "1583712000,1583884800" should have 48 hour duration and not produce an error
  292. march9to11, err := ParseWindowUTC("1583712000,1583884800")
  293. if err != nil {
  294. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  295. }
  296. if march9to11.Duration().Hours() != 48 {
  297. t.Fatalf(`expect: window %s to have duration 48 hour; actual: %f hours`, march9to11, march9to11.Duration().Hours())
  298. }
  299. if !march9to11.Contains(march102020) {
  300. t.Fatalf(`expect: window March 9-11 to contain March 10; actual: %s doesn't contain %s`, march9to11, march102020)
  301. }
  302. if march9to11.Contains(march052020) {
  303. t.Fatalf(`expect: window March 9-11 to not contain March 5; actual: %s contains %s`, march9to11, march052020)
  304. }
  305. if march9to11.Contains(march152020) {
  306. t.Fatalf(`expect: window March 9-11 to not contain March 15; actual: %s contains %s`, march9to11, march152020)
  307. }
  308. }
  309. func BenchmarkParseWindowUTC(b *testing.B) {
  310. for n := 0; n < b.N; n++ {
  311. _, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  312. if err != nil {
  313. b.Fatalf("error running benchmark: %s", err.Error())
  314. }
  315. }
  316. }
  317. func TestParseWindowWithOffsetString(t *testing.T) {
  318. // ParseWindowWithOffsetString should equal ParseWindowUTC when location == "UTC"
  319. // for all window string formats
  320. todayUTC, err := ParseWindowUTC("today")
  321. if err != nil {
  322. t.Fatalf(`unexpected error parsing "today": %s`, err)
  323. }
  324. todayTZ, err := ParseWindowWithOffsetString("today", "UTC")
  325. if err != nil {
  326. t.Fatalf(`unexpected error parsing "today": %s`, err)
  327. }
  328. if !todayUTC.ApproximatelyEqual(todayTZ, time.Millisecond) {
  329. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "UTC"; actual: %s not equal %s`, todayUTC, todayTZ)
  330. }
  331. yesterdayUTC, err := ParseWindowUTC("yesterday")
  332. if err != nil {
  333. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  334. }
  335. yesterdayTZ, err := ParseWindowWithOffsetString("yesterday", "UTC")
  336. if err != nil {
  337. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  338. }
  339. if !yesterdayUTC.ApproximatelyEqual(yesterdayTZ, time.Millisecond) {
  340. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "UTC"; actual: %s not equal %s`, yesterdayUTC, yesterdayTZ)
  341. }
  342. weekUTC, err := ParseWindowUTC("week")
  343. if err != nil {
  344. t.Fatalf(`unexpected error parsing "week": %s`, err)
  345. }
  346. weekTZ, err := ParseWindowWithOffsetString("week", "UTC")
  347. if err != nil {
  348. t.Fatalf(`unexpected error parsing "week": %s`, err)
  349. }
  350. if !weekUTC.ApproximatelyEqual(weekTZ, time.Millisecond) {
  351. t.Fatalf(`expect: window "week" UTC to equal "week" with timezone "UTC"; actual: %s not equal %s`, weekUTC, weekTZ)
  352. }
  353. monthUTC, err := ParseWindowUTC("month")
  354. if err != nil {
  355. t.Fatalf(`unexpected error parsing "month": %s`, err)
  356. }
  357. monthTZ, err := ParseWindowWithOffsetString("month", "UTC")
  358. if err != nil {
  359. t.Fatalf(`unexpected error parsing "month": %s`, err)
  360. }
  361. if !monthUTC.ApproximatelyEqual(monthTZ, time.Millisecond) {
  362. t.Fatalf(`expect: window "month" UTC to equal "month" with timezone "UTC"; actual: %s not equal %s`, monthUTC, monthTZ)
  363. }
  364. lastweekUTC, err := ParseWindowUTC("lastweek")
  365. if err != nil {
  366. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  367. }
  368. lastweekTZ, err := ParseWindowWithOffsetString("lastweek", "UTC")
  369. if err != nil {
  370. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  371. }
  372. if !lastweekUTC.ApproximatelyEqual(lastweekTZ, time.Millisecond) {
  373. t.Fatalf(`expect: window "lastweek" UTC to equal "lastweek" with timezone "UTC"; actual: %s not equal %s`, lastweekUTC, lastweekTZ)
  374. }
  375. lastmonthUTC, err := ParseWindowUTC("lastmonth")
  376. if err != nil {
  377. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  378. }
  379. lastmonthTZ, err := ParseWindowWithOffsetString("lastmonth", "UTC")
  380. if err != nil {
  381. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  382. }
  383. if !lastmonthUTC.ApproximatelyEqual(lastmonthTZ, time.Millisecond) {
  384. t.Fatalf(`expect: window "lastmonth" UTC to equal "lastmonth" with timezone "UTC"; actual: %s not equal %s`, lastmonthUTC, lastmonthTZ)
  385. }
  386. dur10mUTC, err := ParseWindowUTC("10m")
  387. if err != nil {
  388. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  389. }
  390. dur10mTZ, err := ParseWindowWithOffsetString("10m", "UTC")
  391. if err != nil {
  392. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  393. }
  394. if !dur10mUTC.ApproximatelyEqual(dur10mTZ, time.Millisecond) {
  395. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "UTC"; actual: %s not equal %s`, dur10mUTC, dur10mTZ)
  396. }
  397. dur24hUTC, err := ParseWindowUTC("24h")
  398. if err != nil {
  399. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  400. }
  401. dur24hTZ, err := ParseWindowWithOffsetString("24h", "UTC")
  402. if err != nil {
  403. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  404. }
  405. if !dur24hUTC.ApproximatelyEqual(dur24hTZ, time.Millisecond) {
  406. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "UTC"; actual: %s not equal %s`, dur24hUTC, dur24hTZ)
  407. }
  408. dur37dUTC, err := ParseWindowUTC("37d")
  409. if err != nil {
  410. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  411. }
  412. dur37dTZ, err := ParseWindowWithOffsetString("37d", "UTC")
  413. if err != nil {
  414. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  415. }
  416. if !dur37dUTC.ApproximatelyEqual(dur37dTZ, time.Millisecond) {
  417. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "UTC"; actual: %s not equal %s`, dur37dUTC, dur37dTZ)
  418. }
  419. // ParseWindowWithOffsetString should be the correct relative to ParseWindowUTC; i.e.
  420. // - for durations, the times should match, but the representations should differ
  421. // by the number of hours offset
  422. // - for words like "today" and "yesterday", the times may not match, in which
  423. // case, for example, "today" UTC-08:00 might equal "yesterday" UTC
  424. // fmtWindow only compares date and time to the minute, not second or
  425. // timezone. Helper for comparing timezone shifted windows.
  426. fmtWindow := func(w Window) string {
  427. s := "nil"
  428. if w.start != nil {
  429. s = w.start.Format("2006-01-02T15:04")
  430. }
  431. e := "nil"
  432. if w.end != nil {
  433. e = w.end.Format("2006-01-02T15:04")
  434. }
  435. return fmt.Sprintf("[%s, %s]", s, e)
  436. }
  437. // Test UTC-08:00 (California), UTC+03:00 (Moscow), UTC+12:00 (New Zealand), and UTC itself
  438. for _, offsetHrs := range []int{-8, 3, 12, 0} {
  439. offStr := fmt.Sprintf("+%02d:00", offsetHrs)
  440. if offsetHrs < 0 {
  441. offStr = fmt.Sprintf("-%02d:00", -offsetHrs)
  442. }
  443. off := time.Duration(offsetHrs) * time.Hour
  444. dur10mTZ, err = ParseWindowWithOffsetString("10m", offStr)
  445. if err != nil {
  446. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  447. }
  448. if !dur10mTZ.ApproximatelyEqual(dur10mUTC, time.Second) {
  449. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "%s"; actual: %s not equal %s`, offStr, dur10mUTC, dur10mTZ)
  450. }
  451. if fmtWindow(dur10mTZ.Shift(-off)) != fmtWindow(dur10mUTC) {
  452. t.Fatalf(`expect: date, hour, and minute of window "10m" UTC to equal that of "10m" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur10mUTC), fmtWindow(dur10mTZ.Shift(-off)))
  453. }
  454. dur24hTZ, err = ParseWindowWithOffsetString("24h", offStr)
  455. if err != nil {
  456. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  457. }
  458. if !dur24hTZ.ApproximatelyEqual(dur24hUTC, time.Second) {
  459. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "%s"; actual: %s not equal %s`, offStr, dur24hUTC, dur24hTZ)
  460. }
  461. if fmtWindow(dur24hTZ.Shift(-off)) != fmtWindow(dur24hUTC) {
  462. t.Fatalf(`expect: date, hour, and minute of window "24h" UTC to equal that of "24h" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur24hUTC), fmtWindow(dur24hTZ.Shift(-off)))
  463. }
  464. dur37dTZ, err = ParseWindowWithOffsetString("37d", offStr)
  465. if err != nil {
  466. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  467. }
  468. if !dur37dTZ.ApproximatelyEqual(dur37dUTC, time.Second) {
  469. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "%s"; actual: %s not equal %s`, offStr, dur37dUTC, dur37dTZ)
  470. }
  471. if fmtWindow(dur37dTZ.Shift(-off)) != fmtWindow(dur37dUTC) {
  472. t.Fatalf(`expect: date, hour, and minute of window "37d" UTC to equal that of "37d" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur37dUTC), fmtWindow(dur37dTZ.Shift(-off)))
  473. }
  474. // "today" and "yesterday" should comply with the current day in each
  475. // respective timezone, depending on if it is ahead of, equal to, or
  476. // behind UTC at the given moment.
  477. todayTZ, err = ParseWindowWithOffsetString("today", offStr)
  478. if err != nil {
  479. t.Fatalf(`unexpected error parsing "today": %s`, err)
  480. }
  481. yesterdayTZ, err = ParseWindowWithOffsetString("yesterday", offStr)
  482. if err != nil {
  483. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  484. }
  485. hoursSinceYesterdayUTC := time.Now().UTC().Sub(time.Now().UTC().Truncate(24.0 * time.Hour)).Hours()
  486. hoursUntilTomorrowUTC := 24.0 - hoursSinceYesterdayUTC
  487. aheadOfUTC := float64(offsetHrs)-hoursUntilTomorrowUTC > 0
  488. behindUTC := float64(offsetHrs)+hoursSinceYesterdayUTC < 0
  489. // yesterday in this timezone should equal today UTC
  490. if aheadOfUTC {
  491. if fmtWindow(yesterdayTZ) != fmtWindow(todayUTC) {
  492. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, todayUTC)
  493. }
  494. }
  495. // today in this timezone should equal yesterday UTC
  496. if behindUTC {
  497. if fmtWindow(todayTZ) != fmtWindow(yesterdayUTC) {
  498. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, yesterdayUTC)
  499. }
  500. }
  501. // today in this timezone should equal today UTC, likewise for yesterday
  502. if !aheadOfUTC && !behindUTC {
  503. if fmtWindow(todayTZ) != fmtWindow(todayUTC) {
  504. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, todayUTC)
  505. }
  506. // yesterday in this timezone should equal yesterday UTC
  507. if fmtWindow(yesterdayTZ) != fmtWindow(yesterdayUTC) {
  508. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, yesterdayUTC)
  509. }
  510. }
  511. }
  512. }
  513. func TestWindow_DurationOffsetStrings(t *testing.T) {
  514. w, err := ParseWindowUTC("1d")
  515. if err != nil {
  516. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  517. }
  518. dur, off := w.DurationOffsetStrings()
  519. if dur != "1d" {
  520. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  521. }
  522. if off != "" {
  523. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  524. }
  525. w, err = ParseWindowUTC("3h")
  526. if err != nil {
  527. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  528. }
  529. dur, off = w.DurationOffsetStrings()
  530. if dur != "3h" {
  531. t.Fatalf(`expect: window to be "3h"; actual: "%s"`, dur)
  532. }
  533. if off != "" {
  534. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  535. }
  536. w, err = ParseWindowUTC("10m")
  537. if err != nil {
  538. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  539. }
  540. dur, off = w.DurationOffsetStrings()
  541. if dur != "10m" {
  542. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  543. }
  544. if off != "" {
  545. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  546. }
  547. w, err = ParseWindowUTC("1589448338,1589534798")
  548. if err != nil {
  549. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  550. }
  551. dur, off = w.DurationOffsetStrings()
  552. if dur != "1441m" {
  553. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  554. }
  555. if off == "" {
  556. t.Fatalf(`expect: offset to not be ""; actual: "%s"`, off)
  557. }
  558. w, err = ParseWindowUTC("yesterday")
  559. if err != nil {
  560. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  561. }
  562. dur, _ = w.DurationOffsetStrings()
  563. if dur != "1d" {
  564. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  565. }
  566. }
  567. func TestWindow_DurationOffsetForPrometheus(t *testing.T) {
  568. // Set-up and tear-down
  569. thanosEnabled := env.GetBool(env.ThanosEnabledEnvVar, false)
  570. defer env.SetBool(env.ThanosEnabledEnvVar, thanosEnabled)
  571. // Test for Prometheus (env.IsThanosEnabled() == false)
  572. env.SetBool(env.ThanosEnabledEnvVar, false)
  573. if env.IsThanosEnabled() {
  574. t.Fatalf("expected env.IsThanosEnabled() == false")
  575. }
  576. w, err := ParseWindowUTC("1d")
  577. if err != nil {
  578. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  579. }
  580. dur, off, err := w.DurationOffsetForPrometheus()
  581. if err != nil {
  582. t.Fatalf("unexpected error: %s", err)
  583. }
  584. if dur != "1d" {
  585. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  586. }
  587. if off != "" {
  588. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  589. }
  590. w, err = ParseWindowUTC("2h")
  591. if err != nil {
  592. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  593. }
  594. dur, off, err = w.DurationOffsetForPrometheus()
  595. if err != nil {
  596. t.Fatalf("unexpected error: %s", err)
  597. }
  598. if dur != "2h" {
  599. t.Fatalf(`expect: window to be "2h"; actual: "%s"`, dur)
  600. }
  601. if off != "" {
  602. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  603. }
  604. w, err = ParseWindowUTC("10m")
  605. if err != nil {
  606. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  607. }
  608. dur, off, err = w.DurationOffsetForPrometheus()
  609. if err != nil {
  610. t.Fatalf("unexpected error: %s", err)
  611. }
  612. if dur != "10m" {
  613. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  614. }
  615. if off != "" {
  616. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  617. }
  618. w, err = ParseWindowUTC("1589448338,1589534798")
  619. if err != nil {
  620. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  621. }
  622. dur, off, err = w.DurationOffsetForPrometheus()
  623. if err != nil {
  624. t.Fatalf("unexpected error: %s", err)
  625. }
  626. if dur != "1441m" {
  627. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  628. }
  629. if !strings.HasPrefix(off, " offset ") {
  630. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  631. }
  632. w, err = ParseWindowUTC("yesterday")
  633. if err != nil {
  634. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  635. }
  636. dur, off, err = w.DurationOffsetForPrometheus()
  637. if err != nil {
  638. t.Fatalf("unexpected error: %s", err)
  639. }
  640. if dur != "1d" {
  641. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  642. }
  643. if !strings.HasPrefix(off, " offset ") {
  644. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  645. }
  646. // Test for Thanos (env.IsThanosEnabled() == true)
  647. env.SetBool(env.ThanosEnabledEnvVar, true)
  648. if !env.IsThanosEnabled() {
  649. t.Fatalf("expected env.IsThanosEnabled() == true")
  650. }
  651. w, err = ParseWindowUTC("1d")
  652. if err != nil {
  653. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  654. }
  655. dur, off, err = w.DurationOffsetForPrometheus()
  656. if err != nil {
  657. t.Fatalf("unexpected error: %s", err)
  658. }
  659. if dur != "21h" {
  660. t.Fatalf(`expect: window to be "21d"; actual: "%s"`, dur)
  661. }
  662. if off != " offset 3h" {
  663. t.Fatalf(`expect: offset to be " offset 3h"; actual: "%s"`, off)
  664. }
  665. w, err = ParseWindowUTC("2h")
  666. if err != nil {
  667. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  668. }
  669. dur, off, err = w.DurationOffsetForPrometheus()
  670. if err == nil {
  671. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  672. }
  673. w, err = ParseWindowUTC("10m")
  674. if err != nil {
  675. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  676. }
  677. dur, off, err = w.DurationOffsetForPrometheus()
  678. if err == nil {
  679. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  680. }
  681. w, err = ParseWindowUTC("1589448338,1589534798")
  682. if err != nil {
  683. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  684. }
  685. dur, off, err = w.DurationOffsetForPrometheus()
  686. if err != nil {
  687. t.Fatalf("unexpected error: %s", err)
  688. }
  689. if dur != "1441m" {
  690. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  691. }
  692. if !strings.HasPrefix(off, " offset ") {
  693. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  694. }
  695. }
  696. // TODO
  697. // func TestWindow_Overlaps(t *testing.T) {}
  698. // TODO
  699. // func TestWindow_Contains(t *testing.T) {}
  700. // TODO
  701. // func TestWindow_Duration(t *testing.T) {}
  702. // TODO
  703. // func TestWindow_End(t *testing.T) {}
  704. // TODO
  705. // func TestWindow_Equal(t *testing.T) {}
  706. // TODO
  707. // func TestWindow_ExpandStart(t *testing.T) {}
  708. // TODO
  709. // func TestWindow_ExpandEnd(t *testing.T) {}
  710. func TestWindow_Expand(t *testing.T) {
  711. t1 := time.Now().Round(time.Hour)
  712. t2 := t1.Add(34 * time.Minute)
  713. t3 := t1.Add(50 * time.Minute)
  714. t4 := t1.Add(84 * time.Minute)
  715. cases := []struct {
  716. windowToExpand Window
  717. windowArgument Window
  718. expected Window
  719. }{
  720. {
  721. windowToExpand: NewClosedWindow(t1, t2),
  722. windowArgument: NewClosedWindow(t3, t4),
  723. expected: NewClosedWindow(t1, t4),
  724. },
  725. {
  726. windowToExpand: NewClosedWindow(t3, t4),
  727. windowArgument: NewClosedWindow(t1, t2),
  728. expected: NewClosedWindow(t1, t4),
  729. },
  730. {
  731. windowToExpand: NewClosedWindow(t1, t3),
  732. windowArgument: NewClosedWindow(t2, t4),
  733. expected: NewClosedWindow(t1, t4),
  734. },
  735. {
  736. windowToExpand: NewClosedWindow(t2, t4),
  737. windowArgument: NewClosedWindow(t1, t3),
  738. expected: NewClosedWindow(t1, t4),
  739. },
  740. {
  741. windowToExpand: Window{},
  742. windowArgument: NewClosedWindow(t1, t2),
  743. expected: NewClosedWindow(t1, t2),
  744. },
  745. {
  746. windowToExpand: NewWindow(nil, &t2),
  747. windowArgument: NewWindow(nil, &t3),
  748. expected: NewWindow(nil, &t3),
  749. },
  750. {
  751. windowToExpand: NewWindow(&t2, nil),
  752. windowArgument: NewWindow(&t1, nil),
  753. expected: NewWindow(&t1, nil),
  754. },
  755. }
  756. for _, c := range cases {
  757. result := c.windowToExpand.Expand(c.windowArgument)
  758. if !result.Equal(c.expected) {
  759. t.Errorf("Expand %s with %s, expected %s but got %s", c.windowToExpand, c.windowArgument, c.expected, result)
  760. }
  761. }
  762. }
  763. // TODO
  764. // func TestWindow_Start(t *testing.T) {}
  765. // TODO
  766. // func TestWindow_String(t *testing.T) {}
  767. func TestWindow_GetPercentInWindow(t *testing.T) {
  768. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  769. dayEnd := dayStart.Add(timeutil.Day)
  770. window := NewClosedWindow(dayStart, dayEnd)
  771. testcases := map[string]struct {
  772. window Window
  773. itemStart time.Time
  774. itemEnd time.Time
  775. expected float64
  776. }{
  777. "matching start/matching end": {
  778. window: window,
  779. itemStart: dayStart,
  780. itemEnd: dayEnd,
  781. expected: 1.0,
  782. },
  783. "matching start/contained end": {
  784. window: window,
  785. itemStart: dayStart,
  786. itemEnd: dayEnd.Add(-time.Hour * 6),
  787. expected: 1.0,
  788. },
  789. "contained start/matching end": {
  790. window: window,
  791. itemStart: dayStart.Add(time.Hour * 6),
  792. itemEnd: dayEnd,
  793. expected: 1.0,
  794. },
  795. "contained start/contained end": {
  796. window: window,
  797. itemStart: dayStart.Add(time.Hour * 6),
  798. itemEnd: dayEnd.Add(-time.Hour * 6),
  799. expected: 1.0,
  800. },
  801. "before start/contained end": {
  802. window: window,
  803. itemStart: dayStart.Add(-time.Hour * 12),
  804. itemEnd: dayEnd.Add(-time.Hour * 12),
  805. expected: 0.5,
  806. },
  807. "before start/before end": {
  808. window: window,
  809. itemStart: dayStart.Add(-time.Hour * 24),
  810. itemEnd: dayEnd.Add(-time.Hour * 24),
  811. expected: 0.0,
  812. },
  813. "contained start/after end": {
  814. window: window,
  815. itemStart: dayStart.Add(time.Hour * 12),
  816. itemEnd: dayEnd.Add(time.Hour * 12),
  817. expected: 0.5,
  818. },
  819. "after start/after end": {
  820. window: window,
  821. itemStart: dayStart.Add(time.Hour * 24),
  822. itemEnd: dayEnd.Add(time.Hour * 24),
  823. expected: 0.0,
  824. },
  825. "before start/after end": {
  826. window: window,
  827. itemStart: dayStart.Add(-time.Hour * 12),
  828. itemEnd: dayEnd.Add(time.Hour * 12),
  829. expected: 0.5,
  830. },
  831. }
  832. for name, tc := range testcases {
  833. t.Run(name, func(t *testing.T) {
  834. thatWindow := NewWindow(&tc.itemStart, &tc.itemEnd)
  835. if actual := tc.window.GetPercentInWindow(thatWindow); actual != tc.expected {
  836. t.Errorf("GetPercentInWindow() = %v, want %v", actual, tc.expected)
  837. }
  838. })
  839. }
  840. }
  841. func TestWindow_GetWindows(t *testing.T) {
  842. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  843. dayEnd := dayStart.Add(timeutil.Day)
  844. loc, _ := time.LoadLocation("America/Vancouver")
  845. testCases := map[string]struct {
  846. start time.Time
  847. end time.Time
  848. windowSize time.Duration
  849. expected []Window
  850. expectedErr bool
  851. }{
  852. "mismatching tz": {
  853. start: dayStart,
  854. end: dayEnd.In(loc),
  855. windowSize: time.Hour,
  856. expected: nil,
  857. expectedErr: true,
  858. },
  859. "hour windows over 1 hours": {
  860. start: dayStart,
  861. end: dayStart.Add(time.Hour),
  862. windowSize: time.Hour,
  863. expected: []Window{
  864. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  865. },
  866. expectedErr: false,
  867. },
  868. "hour windows over 3 hours": {
  869. start: dayStart,
  870. end: dayStart.Add(time.Hour * 3),
  871. windowSize: time.Hour,
  872. expected: []Window{
  873. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  874. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  875. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  876. },
  877. expectedErr: false,
  878. },
  879. "hour windows off hour grid": {
  880. start: dayStart.Add(time.Minute),
  881. end: dayEnd.Add(time.Minute),
  882. windowSize: time.Hour,
  883. expected: nil,
  884. expectedErr: true,
  885. },
  886. "hour windows range not divisible by hour": {
  887. start: dayStart,
  888. end: dayStart.Add(time.Minute * 90),
  889. windowSize: time.Hour,
  890. expected: nil,
  891. expectedErr: true,
  892. },
  893. "day windows over 1 day": {
  894. start: dayStart,
  895. end: dayEnd,
  896. windowSize: timeutil.Day,
  897. expected: []Window{
  898. NewClosedWindow(dayStart, dayEnd),
  899. },
  900. expectedErr: false,
  901. },
  902. "day windows over 3 days": {
  903. start: dayStart,
  904. end: dayStart.Add(timeutil.Day * 3),
  905. windowSize: timeutil.Day,
  906. expected: []Window{
  907. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  908. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  909. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  910. },
  911. expectedErr: false,
  912. },
  913. "day windows off day grid": {
  914. start: dayStart.Add(time.Hour),
  915. end: dayEnd.Add(time.Hour),
  916. windowSize: timeutil.Day,
  917. expected: nil,
  918. expectedErr: true,
  919. },
  920. "day windows range not divisible by day": {
  921. start: dayStart,
  922. end: dayEnd.Add(time.Hour),
  923. windowSize: timeutil.Day,
  924. expected: nil,
  925. expectedErr: true,
  926. },
  927. }
  928. for name, tc := range testCases {
  929. t.Run(name, func(t *testing.T) {
  930. actual, err := GetWindows(tc.start, tc.end, tc.windowSize)
  931. if (err != nil) != tc.expectedErr {
  932. t.Errorf("GetWindows() error = %v, expectedErr %v", err, tc.expectedErr)
  933. return
  934. }
  935. if len(tc.expected) != len(actual) {
  936. t.Errorf("GetWindows() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  937. }
  938. for i, actualWindow := range actual {
  939. expectedWindow := tc.expected[i]
  940. if !actualWindow.Equal(expectedWindow) {
  941. t.Errorf("GetWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  942. }
  943. }
  944. })
  945. }
  946. }
  947. func TestWindow_GetWindowsForQueryWindow(t *testing.T) {
  948. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  949. dayEnd := dayStart.Add(timeutil.Day)
  950. loc, _ := time.LoadLocation("America/Vancouver")
  951. testCases := map[string]struct {
  952. start time.Time
  953. end time.Time
  954. windowSize time.Duration
  955. expected []Window
  956. expectedErr bool
  957. }{
  958. "mismatching tz": {
  959. start: dayStart,
  960. end: dayEnd.In(loc),
  961. windowSize: time.Hour,
  962. expected: nil,
  963. expectedErr: true,
  964. },
  965. "hour windows over 1 hours": {
  966. start: dayStart,
  967. end: dayStart.Add(time.Hour),
  968. windowSize: time.Hour,
  969. expected: []Window{
  970. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  971. },
  972. expectedErr: false,
  973. },
  974. "hour windows over 3 hours": {
  975. start: dayStart,
  976. end: dayStart.Add(time.Hour * 3),
  977. windowSize: time.Hour,
  978. expected: []Window{
  979. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  980. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  981. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  982. },
  983. expectedErr: false,
  984. },
  985. "hour windows off hour grid": {
  986. start: dayStart.Add(time.Minute),
  987. end: dayStart.Add(time.Minute * 61),
  988. windowSize: time.Hour,
  989. expected: []Window{
  990. NewClosedWindow(dayStart.Add(time.Minute), dayStart.Add(time.Minute*61)),
  991. },
  992. expectedErr: false,
  993. },
  994. "hour windows range not divisible by hour": {
  995. start: dayStart,
  996. end: dayStart.Add(time.Minute * 90),
  997. windowSize: time.Hour,
  998. expected: []Window{
  999. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  1000. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Minute*90)),
  1001. },
  1002. expectedErr: false,
  1003. },
  1004. "day windows over 1 day": {
  1005. start: dayStart,
  1006. end: dayEnd,
  1007. windowSize: timeutil.Day,
  1008. expected: []Window{
  1009. NewClosedWindow(dayStart, dayEnd),
  1010. },
  1011. expectedErr: false,
  1012. },
  1013. "day windows over 3 days": {
  1014. start: dayStart,
  1015. end: dayStart.Add(timeutil.Day * 3),
  1016. windowSize: timeutil.Day,
  1017. expected: []Window{
  1018. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  1019. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  1020. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  1021. },
  1022. expectedErr: false,
  1023. },
  1024. "day windows off day grid": {
  1025. start: dayStart.Add(time.Hour),
  1026. end: dayEnd.Add(time.Hour),
  1027. windowSize: timeutil.Day,
  1028. expected: []Window{
  1029. NewClosedWindow(dayStart.Add(time.Hour), dayEnd.Add(time.Hour)),
  1030. },
  1031. expectedErr: false,
  1032. },
  1033. "day windows range not divisible by day": {
  1034. start: dayStart,
  1035. end: dayEnd.Add(time.Hour),
  1036. windowSize: timeutil.Day,
  1037. expected: []Window{
  1038. NewClosedWindow(dayStart, dayEnd),
  1039. NewClosedWindow(dayEnd, dayEnd.Add(time.Hour)),
  1040. },
  1041. expectedErr: false,
  1042. },
  1043. }
  1044. for name, tc := range testCases {
  1045. t.Run(name, func(t *testing.T) {
  1046. actual, err := GetWindowsForQueryWindow(tc.start, tc.end, tc.windowSize)
  1047. if (err != nil) != tc.expectedErr {
  1048. t.Errorf("GetWindowsForQueryWindow() error = %v, expectedErr %v", err, tc.expectedErr)
  1049. return
  1050. }
  1051. if len(tc.expected) != len(actual) {
  1052. t.Errorf("GetWindowsForQueryWindow() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  1053. }
  1054. for i, actualWindow := range actual {
  1055. expectedWindow := tc.expected[i]
  1056. if !actualWindow.Equal(expectedWindow) {
  1057. t.Errorf("GetWindowsForQueryWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  1058. }
  1059. }
  1060. })
  1061. }
  1062. }
  1063. func TestMarshalUnmarshal(t *testing.T) {
  1064. t1 := time.Date(2023, 03, 11, 01, 29, 15, 0, time.UTC)
  1065. t2 := t1.Add(8 * time.Minute)
  1066. cases := []struct {
  1067. w Window
  1068. }{
  1069. {
  1070. w: NewClosedWindow(t1, t2),
  1071. },
  1072. {
  1073. w: NewWindow(&t1, nil),
  1074. },
  1075. {
  1076. w: NewWindow(nil, &t2),
  1077. },
  1078. {
  1079. w: NewWindow(nil, nil),
  1080. },
  1081. }
  1082. for _, c := range cases {
  1083. name := c.w.String()
  1084. t.Run(name, func(t *testing.T) {
  1085. marshaled, err := json.Marshal(c.w)
  1086. if err != nil {
  1087. t.Fatalf("marshaling: %s", err)
  1088. }
  1089. var unmarshaledW Window
  1090. err = json.Unmarshal(marshaled, &unmarshaledW)
  1091. if err != nil {
  1092. t.Fatalf("unmarshaling: %s", err)
  1093. }
  1094. if diff := cmp.Diff(c.w, unmarshaledW); len(diff) > 0 {
  1095. t.Errorf(diff)
  1096. }
  1097. })
  1098. }
  1099. }