window_test.go 40 KB

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