window_test.go 40 KB

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