window.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. package opencost
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "regexp"
  8. "strconv"
  9. "sync"
  10. "time"
  11. "github.com/opencost/opencost/core/pkg/env"
  12. "github.com/opencost/opencost/core/pkg/log"
  13. "github.com/opencost/opencost/core/pkg/util/timeutil"
  14. )
  15. const (
  16. minutesPerDay = 60 * 24
  17. minutesPerHour = 60
  18. hoursPerDay = 24
  19. )
  20. var (
  21. durationRegex = regexp.MustCompile(`^(\d+)(m|h|d|w)$`)
  22. durationOffsetRegex = regexp.MustCompile(`^(\d+)(m|h|d|w) offset (\d+)(m|h|d|w)$`)
  23. offesetRegex = regexp.MustCompile(`^(\+|-)(\d\d):(\d\d)$`)
  24. rfc3339 = `\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ`
  25. rfcRegex = regexp.MustCompile(fmt.Sprintf(`(%s),(%s)`, rfc3339, rfc3339))
  26. timestampPairRegex = regexp.MustCompile(`^(\d+)[,|-](\d+)$`)
  27. tOffsetLock sync.Mutex
  28. tOffset *time.Duration
  29. utcOffsetLock sync.Mutex
  30. utcOffsetDur *time.Duration
  31. )
  32. // get and cache the thanos offset duration.
  33. // TODO: Due to dependencies here, we have to drag a non-core config option into
  34. // TOOD: core scope. Any solution here would be a one-off until we can generalize
  35. // TODO: global configuration options.
  36. func thanosOffset() time.Duration {
  37. tOffsetLock.Lock()
  38. defer tOffsetLock.Unlock()
  39. if tOffset == nil {
  40. d, err := time.ParseDuration(env.Get("THANOS_QUERY_OFFSET", "3h"))
  41. if err != nil {
  42. d = 0
  43. }
  44. tOffset = &d
  45. }
  46. return *tOffset
  47. }
  48. // returns true if thanos is enabled
  49. // TODO: Same note as thanosOffset above - temporary work-around until more
  50. // TODO: generalized global configuration.
  51. func isThanosEnabled() bool {
  52. return env.GetBool("THANOS_ENABLED", false)
  53. }
  54. // returns the configured utc offset as a duration
  55. // TODO: Same as the above options -- we should provide a one-time initialization configuration
  56. // TODO: for these values, or deprecate their use.
  57. func utcOffset() time.Duration {
  58. utcOffsetLock.Lock()
  59. defer utcOffsetLock.Unlock()
  60. if utcOffsetDur == nil {
  61. utcOff, err := timeutil.ParseUTCOffset(env.Get("UTC_OFFSET", ""))
  62. if err != nil {
  63. utcOff = time.Duration(0)
  64. }
  65. utcOffsetDur = &utcOff
  66. }
  67. return *utcOffsetDur
  68. }
  69. // RoundBack rounds the given time back to a multiple of the given resolution
  70. // in the given time's timezone.
  71. // e.g. 2020-01-01T12:37:48-0700, 24h = 2020-01-01T00:00:00-0700
  72. func RoundBack(t time.Time, resolution time.Duration) time.Time {
  73. // if the duration is a week - roll back to the following Sunday
  74. if resolution == timeutil.Week {
  75. return timeutil.RoundToStartOfWeek(t)
  76. }
  77. _, offSec := t.Zone()
  78. return t.Add(time.Duration(offSec) * time.Second).Truncate(resolution).Add(-time.Duration(offSec) * time.Second)
  79. }
  80. // RoundForward rounds the given time forward to a multiple of the given resolution
  81. // in the given time's timezone.
  82. // e.g. 2020-01-01T12:37:48-0700, 24h = 2020-01-02T00:00:00-0700
  83. func RoundForward(t time.Time, resolution time.Duration) time.Time {
  84. back := RoundBack(t, resolution)
  85. if back.Equal(t) {
  86. // The given time is exactly a multiple of the given resolution
  87. return t
  88. }
  89. return back.Add(resolution)
  90. }
  91. // Window defines a period of time with a start and an end. If either start or
  92. // end are nil it indicates an open time period.
  93. type Window struct {
  94. start *time.Time
  95. end *time.Time
  96. }
  97. // NewWindow creates and returns a new Window instance from the given times
  98. func NewWindow(start, end *time.Time) Window {
  99. return Window{
  100. start: start,
  101. end: end,
  102. }
  103. }
  104. // NewClosedWindow creates and returns a new Window instance from the given
  105. // times, which cannot be nil, so they are value types.
  106. func NewClosedWindow(start, end time.Time) Window {
  107. return Window{
  108. start: &start,
  109. end: &end,
  110. }
  111. }
  112. // ParseWindowUTC attempts to parse the given string into a valid Window. It
  113. // accepts several formats, returning an error if the given string does not
  114. // match one of the following:
  115. // - named intervals: "today", "yesterday", "week", "month", "lastweek", "lastmonth"
  116. // - durations: "24h", "7d", etc.
  117. // - date ranges: "2020-04-01T00:00:00Z,2020-04-03T00:00:00Z", etc.
  118. // - timestamp ranges: "1586822400,1586908800", etc.
  119. func ParseWindowUTC(window string) (Window, error) {
  120. return parseWindow(window, time.Now().UTC())
  121. }
  122. // ParseWindowWithOffsetString parses the given window string within the context of
  123. // the timezone defined by the UTC offset string of format -07:00, +01:30, etc.
  124. func ParseWindowWithOffsetString(window string, offset string) (Window, error) {
  125. if offset == "UTC" || offset == "" {
  126. return ParseWindowUTC(window)
  127. }
  128. match := offesetRegex.FindStringSubmatch(offset)
  129. if match == nil {
  130. return Window{}, fmt.Errorf("illegal UTC offset: '%s'; should be of form '-07:00'", offset)
  131. }
  132. sig := 1
  133. if match[1] == "-" {
  134. sig = -1
  135. }
  136. hrs64, _ := strconv.ParseInt(match[2], 10, 64)
  137. hrs := sig * int(hrs64)
  138. mins64, _ := strconv.ParseInt(match[3], 10, 64)
  139. mins := sig * int(mins64)
  140. loc := time.FixedZone(fmt.Sprintf("UTC%s", offset), (hrs*60*60)+(mins*60))
  141. now := time.Now().In(loc)
  142. return parseWindow(window, now)
  143. }
  144. // ParseWindowWithOffset parses the given window string within the context of
  145. // the timezone defined by the UTC offset.
  146. func ParseWindowWithOffset(window string, offset time.Duration) (Window, error) {
  147. loc := time.FixedZone("", int(offset.Seconds()))
  148. now := time.Now().In(loc)
  149. return parseWindow(window, now)
  150. }
  151. // parseWindow generalizes the parsing of window strings, relative to a given
  152. // moment in time, defined as "now".
  153. func parseWindow(window string, now time.Time) (Window, error) {
  154. // compute UTC offset in terms of minutes
  155. offHr := now.UTC().Hour() - now.Hour()
  156. offMin := (now.UTC().Minute() - now.Minute()) + (offHr * 60)
  157. offset := time.Duration(offMin) * time.Minute
  158. if window == "today" {
  159. start := now
  160. start = start.Truncate(time.Hour * 24)
  161. start = start.Add(offset)
  162. end := start.Add(time.Hour * 24)
  163. return NewWindow(&start, &end), nil
  164. }
  165. if window == "yesterday" {
  166. start := now
  167. start = start.Truncate(time.Hour * 24)
  168. start = start.Add(offset)
  169. start = start.Add(time.Hour * -24)
  170. end := start.Add(time.Hour * 24)
  171. return NewWindow(&start, &end), nil
  172. }
  173. if window == "week" {
  174. // now
  175. start := now
  176. // 00:00 today, accounting for timezone offset
  177. start = start.Truncate(time.Hour * 24)
  178. start = start.Add(offset)
  179. // 00:00 Sunday of the current week
  180. start = start.Add(-24 * time.Hour * time.Duration(start.Weekday()))
  181. end := now
  182. return NewWindow(&start, &end), nil
  183. }
  184. if window == "lastweek" {
  185. // now
  186. start := now
  187. // 00:00 today, accounting for timezone offset
  188. start = start.Truncate(time.Hour * 24)
  189. start = start.Add(offset)
  190. // 00:00 Sunday of last week
  191. start = start.Add(-24 * time.Hour * time.Duration(start.Weekday()+7))
  192. end := start.Add(7 * 24 * time.Hour)
  193. return NewWindow(&start, &end), nil
  194. }
  195. if window == "month" {
  196. // now
  197. start := now
  198. // 00:00 today, accounting for timezone offset
  199. start = start.Truncate(time.Hour * 24)
  200. start = start.Add(offset)
  201. // 00:00 1st of this month
  202. start = start.Add(-24 * time.Hour * time.Duration(start.Day()-1))
  203. end := now
  204. return NewWindow(&start, &end), nil
  205. }
  206. if window == "month" {
  207. // now
  208. start := now
  209. // 00:00 today, accounting for timezone offset
  210. start = start.Truncate(time.Hour * 24)
  211. start = start.Add(offset)
  212. // 00:00 1st of this month
  213. start = start.Add(-24 * time.Hour * time.Duration(start.Day()-1))
  214. end := now
  215. return NewWindow(&start, &end), nil
  216. }
  217. if window == "lastmonth" {
  218. // now
  219. end := now
  220. // 00:00 today, accounting for timezone offset
  221. end = end.Truncate(time.Hour * 24)
  222. end = end.Add(offset)
  223. // 00:00 1st of this month
  224. end = end.Add(-24 * time.Hour * time.Duration(end.Day()-1))
  225. // 00:00 last day of last month
  226. start := end.Add(-24 * time.Hour)
  227. // 00:00 1st of last month
  228. start = start.Add(-24 * time.Hour * time.Duration(start.Day()-1))
  229. return NewWindow(&start, &end), nil
  230. }
  231. // Match duration strings; e.g. "45m", "24h", "7d"
  232. match := durationRegex.FindStringSubmatch(window)
  233. if match != nil {
  234. dur := time.Minute
  235. if match[2] == "h" {
  236. dur = time.Hour
  237. }
  238. if match[2] == "d" {
  239. dur = 24 * time.Hour
  240. }
  241. if match[2] == "w" {
  242. dur = timeutil.Week
  243. }
  244. num, _ := strconv.ParseInt(match[1], 10, 64)
  245. end := now
  246. start := end.Add(-time.Duration(num) * dur)
  247. // when using windows such as "7d", "1w", and "2h" we have to have a definition for what "the past X days/hours" means.
  248. // let "the past X days" be defined as the entirety of today plus the entirety of the past X-1 days, where
  249. // "entirety" is defined as midnight to midnight, UTC. given this definition, we round forward the calculated
  250. // start and end times to the nearest day to align with midnight boundaries
  251. // an analogous definition applies to "the past X weeks" and "the past X hours"
  252. if match[2] == "w" {
  253. // special case - with a week, we say the week ends today
  254. end = end.Truncate(timeutil.Day).Add(timeutil.Day)
  255. start = start.Truncate(timeutil.Day).Add(timeutil.Day)
  256. } else {
  257. end = now.Truncate(dur).Add(dur)
  258. start = end.Add(-time.Duration(num) * dur)
  259. }
  260. return NewWindow(&start, &end), nil
  261. }
  262. // Match duration strings with offset; e.g. "45m offset 15m", etc.
  263. match = durationOffsetRegex.FindStringSubmatch(window)
  264. if match != nil {
  265. end := now
  266. offUnit := time.Minute
  267. if match[4] == "h" {
  268. offUnit = time.Hour
  269. }
  270. if match[4] == "d" {
  271. offUnit = 24 * time.Hour
  272. }
  273. if match[4] == "w" {
  274. offUnit = 24 * timeutil.Week
  275. }
  276. offNum, _ := strconv.ParseInt(match[3], 10, 64)
  277. end = end.Add(-time.Duration(offNum) * offUnit)
  278. durUnit := time.Minute
  279. if match[2] == "h" {
  280. durUnit = time.Hour
  281. }
  282. if match[2] == "d" {
  283. durUnit = 24 * time.Hour
  284. }
  285. if match[2] == "w" {
  286. durUnit = timeutil.Week
  287. }
  288. durNum, _ := strconv.ParseInt(match[1], 10, 64)
  289. start := end.Add(-time.Duration(durNum) * durUnit)
  290. return NewWindow(&start, &end), nil
  291. }
  292. // Match timestamp pairs, e.g. "1586822400,1586908800" or "1586822400-1586908800"
  293. match = timestampPairRegex.FindStringSubmatch(window)
  294. if match != nil {
  295. s, _ := strconv.ParseInt(match[1], 10, 64)
  296. e, _ := strconv.ParseInt(match[2], 10, 64)
  297. start := time.Unix(s, 0).UTC()
  298. end := time.Unix(e, 0).UTC()
  299. return NewWindow(&start, &end), nil
  300. }
  301. // Match RFC3339 pairs, e.g. "2020-04-01T00:00:00Z,2020-04-03T00:00:00Z"
  302. match = rfcRegex.FindStringSubmatch(window)
  303. if match != nil {
  304. start, _ := time.Parse(time.RFC3339, match[1])
  305. end, _ := time.Parse(time.RFC3339, match[2])
  306. return NewWindow(&start, &end), nil
  307. }
  308. return Window{nil, nil}, fmt.Errorf("illegal window: %s", window)
  309. }
  310. // ApproximatelyEqual returns true if the start and end times of the two windows,
  311. // respectively, are within the given threshold of each other.
  312. func (w Window) ApproximatelyEqual(that Window, threshold time.Duration) bool {
  313. return approxEqual(w.start, that.start, threshold) && approxEqual(w.end, that.end, threshold)
  314. }
  315. func approxEqual(x *time.Time, y *time.Time, threshold time.Duration) bool {
  316. // both times are nil, so they are equal
  317. if x == nil && y == nil {
  318. return true
  319. }
  320. // one time is nil, but the other is not, so they are not equal
  321. if x == nil || y == nil {
  322. return false
  323. }
  324. // neither time is nil, so they are approximately close if their times are
  325. // within the given threshold
  326. delta := math.Abs((*x).Sub(*y).Seconds())
  327. return delta < threshold.Seconds()
  328. }
  329. func (w Window) Clone() Window {
  330. var start, end *time.Time
  331. var s, e time.Time
  332. if w.start != nil {
  333. s = *w.start
  334. start = &s
  335. }
  336. if w.end != nil {
  337. e = *w.end
  338. end = &e
  339. }
  340. return NewWindow(start, end)
  341. }
  342. func (w Window) Contains(t time.Time) bool {
  343. if w.start != nil && t.Before(*w.start) {
  344. return false
  345. }
  346. if w.end != nil && t.After(*w.end) {
  347. return false
  348. }
  349. return true
  350. }
  351. func (w Window) ContainsWindow(that Window) bool {
  352. // only support containing closed windows for now
  353. // could check if openness is compatible with closure
  354. if that.IsOpen() {
  355. return false
  356. }
  357. return w.Contains(*that.start) && w.Contains(*that.end)
  358. }
  359. func (w Window) Duration() time.Duration {
  360. if w.IsOpen() {
  361. // TODO test
  362. return time.Duration(math.Inf(1.0))
  363. }
  364. return w.end.Sub(*w.start)
  365. }
  366. func (w Window) End() *time.Time {
  367. return w.end
  368. }
  369. func (w Window) Equal(that Window) bool {
  370. if w.start != nil && that.start != nil && !w.start.Equal(*that.start) {
  371. // starts are not nil, but not equal
  372. return false
  373. }
  374. if w.end != nil && that.end != nil && !w.end.Equal(*that.end) {
  375. // ends are not nil, but not equal
  376. return false
  377. }
  378. if (w.start == nil && that.start != nil) || (w.start != nil && that.start == nil) {
  379. // one start is nil, the other is not
  380. return false
  381. }
  382. if (w.end == nil && that.end != nil) || (w.end != nil && that.end == nil) {
  383. // one end is nil, the other is not
  384. return false
  385. }
  386. // either both starts are nil, or they match; likewise for the ends
  387. return true
  388. }
  389. func (w Window) ExpandStart(start time.Time) Window {
  390. if w.start == nil || start.Before(*w.start) {
  391. w.start = &start
  392. }
  393. return w
  394. }
  395. func (w Window) ExpandEnd(end time.Time) Window {
  396. if w.end == nil || end.After(*w.end) {
  397. w.end = &end
  398. }
  399. return w
  400. }
  401. func (w Window) Expand(that Window) Window {
  402. if that.start == nil {
  403. w.start = nil
  404. } else {
  405. w = w.ExpandStart(*that.start)
  406. }
  407. if that.end == nil {
  408. w.end = nil
  409. } else {
  410. w = w.ExpandEnd(*that.end)
  411. }
  412. return w
  413. }
  414. func (w Window) ContractStart(start time.Time) Window {
  415. if w.start == nil || start.After(*w.start) {
  416. w.start = &start
  417. }
  418. return w
  419. }
  420. func (w Window) ContractEnd(end time.Time) Window {
  421. if w.end == nil || end.Before(*w.end) {
  422. w.end = &end
  423. }
  424. return w
  425. }
  426. func (w Window) Contract(that Window) Window {
  427. if that.start != nil {
  428. w = w.ContractStart(*that.start)
  429. }
  430. if that.end != nil {
  431. w = w.ContractEnd(*that.end)
  432. }
  433. return w
  434. }
  435. func (w Window) Hours() float64 {
  436. if w.IsOpen() {
  437. return math.Inf(1)
  438. }
  439. return w.end.Sub(*w.start).Hours()
  440. }
  441. // IsEmpty a Window is empty if it does not have a start and an end
  442. func (w Window) IsEmpty() bool {
  443. return w.start == nil && w.end == nil
  444. }
  445. // HasDuration a Window has duration if neither start and end are not nil and not equal
  446. func (w Window) HasDuration() bool {
  447. return !w.IsOpen() && !w.end.Equal(*w.Start())
  448. }
  449. // IsNegative a Window is negative if start and end are not null and end is before start
  450. func (w Window) IsNegative() bool {
  451. return !w.IsOpen() && w.end.Before(*w.Start())
  452. }
  453. // IsOpen a Window is open if it has a nil start or end
  454. func (w Window) IsOpen() bool {
  455. return w.start == nil || w.end == nil
  456. }
  457. func (w Window) MarshalJSON() ([]byte, error) {
  458. buffer := bytes.NewBufferString("{")
  459. if w.start != nil {
  460. buffer.WriteString(fmt.Sprintf("\"start\":\"%s\",", w.start.Format(time.RFC3339)))
  461. } else {
  462. buffer.WriteString(fmt.Sprintf("\"start\":\"%s\",", "null"))
  463. }
  464. if w.end != nil {
  465. buffer.WriteString(fmt.Sprintf("\"end\":\"%s\"", w.end.Format(time.RFC3339)))
  466. } else {
  467. buffer.WriteString(fmt.Sprintf("\"end\":\"%s\"", "null"))
  468. }
  469. buffer.WriteString("}")
  470. return buffer.Bytes(), nil
  471. }
  472. func (w *Window) UnmarshalJSON(bs []byte) error {
  473. // Due to the behavior of our custom MarshalJSON, we unmarshal as strings
  474. // and then manually handle the weird quoted "null" case.
  475. type PubWindow struct {
  476. Start string `json:"start"`
  477. End string `json:"end"`
  478. }
  479. var pw PubWindow
  480. err := json.Unmarshal(bs, &pw)
  481. if err != nil {
  482. return fmt.Errorf("half unmarshal: %w", err)
  483. }
  484. var start *time.Time
  485. var end *time.Time
  486. if pw.Start != "null" {
  487. t, err := time.Parse(time.RFC3339, pw.Start)
  488. if err != nil {
  489. return fmt.Errorf("parsing start as RFC3339: %w", err)
  490. }
  491. start = &t
  492. }
  493. if pw.End != "null" {
  494. t, err := time.Parse(time.RFC3339, pw.End)
  495. if err != nil {
  496. return fmt.Errorf("parsing end as RFC3339: %w", err)
  497. }
  498. end = &t
  499. }
  500. w.start = start
  501. w.end = end
  502. return nil
  503. }
  504. func (w Window) Minutes() float64 {
  505. if w.IsOpen() {
  506. return math.Inf(1)
  507. }
  508. return w.end.Sub(*w.start).Minutes()
  509. }
  510. // Overlaps returns true iff the two given Windows share an amount of temporal
  511. // coverage.
  512. // TODO complete (with unit tests!) and then implement in AllocationSet.accumulate
  513. // TODO:CLEANUP
  514. // func (w Window) Overlaps(x Window) bool {
  515. // if (w.start == nil && w.end == nil) || (x.start == nil && x.end == nil) {
  516. // // one window is completely open, so overlap is guaranteed
  517. // // <---------->
  518. // // ?------?
  519. // return true
  520. // }
  521. // // Neither window is completely open (nil, nil), but one or the other might
  522. // // still be future- or past-open.
  523. // if w.start == nil {
  524. // // w is past-open, future-closed
  525. // // <------]
  526. // if x.start != nil && !x.start.Before(*w.end) {
  527. // // x starts after w ends (or eq)
  528. // // <------]
  529. // // [------?
  530. // return false
  531. // }
  532. // // <-----]
  533. // // ?-----?
  534. // return true
  535. // }
  536. // if w.end == nil {
  537. // // w is future-open, past-closed
  538. // // [------>
  539. // if x.end != nil && !x.end.After(*w.end) {
  540. // // x ends before w begins (or eq)
  541. // // [------>
  542. // // ?------]
  543. // return false
  544. // }
  545. // // [------>
  546. // // ?------?
  547. // return true
  548. // }
  549. // // Now we know w is closed, but we don't know about x
  550. // // [------]
  551. // // ?------?
  552. // if x.start == nil {
  553. // // TODO
  554. // }
  555. // if x.end == nil {
  556. // // TODO
  557. // }
  558. // // Both are closed.
  559. // if !x.start.Before(*w.end) && !x.end.Before(*w.end) {
  560. // // x starts and ends after w ends
  561. // // [------]
  562. // // [------]
  563. // return false
  564. // }
  565. // if !x.start.After(*w.start) && !x.end.After(*w.start) {
  566. // // x starts and ends before w starts
  567. // // [------]
  568. // // [------]
  569. // return false
  570. // }
  571. // // w and x must overlap
  572. // // [------]
  573. // // [------]
  574. // return true
  575. // }
  576. func (w *Window) Set(start, end *time.Time) {
  577. w.start = start
  578. w.end = end
  579. }
  580. // Shift adds the given duration to both the start and end times of the window
  581. func (w Window) Shift(dur time.Duration) Window {
  582. if w.start != nil {
  583. s := w.start.Add(dur)
  584. w.start = &s
  585. }
  586. if w.end != nil {
  587. e := w.end.Add(dur)
  588. w.end = &e
  589. }
  590. return w
  591. }
  592. func (w Window) Start() *time.Time {
  593. return w.start
  594. }
  595. func (w Window) String() string {
  596. if w.start == nil && w.end == nil {
  597. return "[nil, nil)"
  598. }
  599. if w.start == nil {
  600. return fmt.Sprintf("[nil, %s)", w.end.Format("2006-01-02T15:04:05-0700"))
  601. }
  602. if w.end == nil {
  603. return fmt.Sprintf("[%s, nil)", w.start.Format("2006-01-02T15:04:05-0700"))
  604. }
  605. return fmt.Sprintf("[%s, %s)", w.start.Format("2006-01-02T15:04:05-0700"), w.end.Format("2006-01-02T15:04:05-0700"))
  606. }
  607. // DurationOffset returns durations representing the duration and offset of the
  608. // given window
  609. func (w Window) DurationOffset() (time.Duration, time.Duration, error) {
  610. if w.IsOpen() || w.IsNegative() {
  611. return 0, 0, fmt.Errorf("illegal window: %s", w)
  612. }
  613. duration := w.Duration()
  614. offset := time.Since(*w.End())
  615. return duration, offset, nil
  616. }
  617. // DurationOffsetStrings returns formatted, Prometheus-compatible strings representing
  618. // the duration and offset of the window in terms of days, hours, minutes, or seconds;
  619. // e.g. ("7d", "1441m", "30m", "1s", "")
  620. func (w Window) DurationOffsetStrings() (string, string) {
  621. dur, off, err := w.DurationOffset()
  622. if err != nil {
  623. return "", ""
  624. }
  625. return timeutil.DurationOffsetStrings(dur, off)
  626. }
  627. // GetPercentInWindow Determine pct of item time contained the window.
  628. // determined by the overlap of the start/end with the given
  629. // window, which will be negative if there is no overlap. If
  630. // there is positive overlap, compare it with the total mins.
  631. //
  632. // e.g. here are the two possible scenarios as simplidied
  633. // 10m windows with dashes representing item's time running:
  634. //
  635. // 1. item falls entirely within one CloudCostSet window
  636. // | ---- | | |
  637. // totalMins = 4.0
  638. // pct := 4.0 / 4.0 = 1.0 for window 1
  639. // pct := 0.0 / 4.0 = 0.0 for window 2
  640. // pct := 0.0 / 4.0 = 0.0 for window 3
  641. //
  642. // 2. item overlaps multiple CloudCostSet windows
  643. // | ----|----------|-- |
  644. // totalMins = 16.0
  645. // pct := 4.0 / 16.0 = 0.250 for window 1
  646. // pct := 10.0 / 16.0 = 0.625 for window 2
  647. // pct := 2.0 / 16.0 = 0.125 for window 3
  648. func (w Window) GetPercentInWindow(that Window) float64 {
  649. if that.IsOpen() {
  650. log.Errorf("Window: GetPercentInWindow: invalid window %s", that.String())
  651. return 0
  652. }
  653. s := *that.Start()
  654. if s.Before(*w.Start()) {
  655. s = *w.Start()
  656. }
  657. e := *that.End()
  658. if e.After(*w.End()) {
  659. e = *w.End()
  660. }
  661. mins := e.Sub(s).Minutes()
  662. if mins <= 0.0 {
  663. return 0.0
  664. }
  665. totalMins := that.Duration().Minutes()
  666. pct := mins / totalMins
  667. return pct
  668. }
  669. // GetAccumulateWindow rounds the start and end of the window to the given accumulation option
  670. func (w Window) GetAccumulateWindow(accumOpt AccumulateOption) (Window, error) {
  671. if w.IsOpen() {
  672. return w, fmt.Errorf("could not get accumlate window for open window")
  673. }
  674. switch accumOpt {
  675. case AccumulateOptionAll:
  676. // just return the entire window
  677. return w.Clone(), nil
  678. case AccumulateOptionHour:
  679. return w.getHourlyWindow(), nil
  680. case AccumulateOptionDay:
  681. return w.getDailyWindow(), nil
  682. case AccumulateOptionWeek:
  683. return w.getWeeklyWindow(), nil
  684. case AccumulateOptionMonth:
  685. return w.getMonthlyWindow(), nil
  686. case AccumulateOptionQuarter:
  687. return w.getQuarterlyWindow(), nil
  688. case AccumulateOptionNone:
  689. // the default behavior of the app currently is to return the highest resolution steps
  690. // possible
  691. fallthrough
  692. default:
  693. // if we are here, it means someone wants a window older than what we can query for
  694. return w, fmt.Errorf("cannot round window to given accumulation option %s", string(accumOpt))
  695. }
  696. }
  697. // GetAccumulateWindows breaks provided window into a []Window with each window having the resolution of the provided AccumulateOption
  698. func (w Window) GetAccumulateWindows(accumOpt AccumulateOption) ([]Window, error) {
  699. if w.IsOpen() {
  700. return nil, fmt.Errorf("could not get accumlate window for open window")
  701. }
  702. switch accumOpt {
  703. case AccumulateOptionAll:
  704. // just return the entire window
  705. return []Window{w.Clone()}, nil
  706. case AccumulateOptionDay:
  707. wins := w.getDailyWindows()
  708. return wins, nil
  709. case AccumulateOptionWeek:
  710. wins := w.getWeeklyWindows()
  711. return wins, nil
  712. case AccumulateOptionMonth:
  713. wins := w.getMonthlyWindows()
  714. return wins, nil
  715. case AccumulateOptionHour:
  716. // our maximum resolution is hourly
  717. wins := w.getHourlyWindows()
  718. return wins, nil
  719. case AccumulateOptionQuarter:
  720. wins := w.getQuarterlyWindows()
  721. return wins, nil
  722. case AccumulateOptionNone:
  723. // the default behavior of the app currently is to return the highest resolution steps
  724. // possible
  725. fallthrough
  726. default:
  727. // if we are here, it means someone wants a window older than what we can query for
  728. return nil, fmt.Errorf("store does not have coverage window starting at %v", w.Start())
  729. }
  730. }
  731. func (w Window) getHourlyWindow() Window {
  732. origStart := w.Start()
  733. origEnd := w.End()
  734. // round the start and end windows to the calendar hour start and ends, respectively
  735. roundedStart := time.Date(origStart.Year(), origStart.Month(), origStart.Day(), origStart.Hour(), 0, 0, 0, origStart.Location())
  736. roundedEnd := time.Date(origEnd.Year(), origEnd.Month(), origEnd.Day(), origEnd.Hour()+1, 0, 0, 0, origEnd.Location())
  737. // edge case - if user has exactly specified first instant of new hour, does not need rounding
  738. if origEnd.Minute() == 0 && origEnd.Second() == 0 {
  739. roundedEnd = *origEnd
  740. }
  741. return NewClosedWindow(roundedStart, roundedEnd)
  742. }
  743. // getHourlyWindows breaks up a window into hours
  744. func (w Window) getHourlyWindows() []Window {
  745. wins := []Window{}
  746. roundedWindow := w.getHourlyWindow()
  747. roundedStart := *roundedWindow.Start()
  748. roundedEnd := *roundedWindow.End()
  749. currStart := roundedStart
  750. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day(), currStart.Hour()+1, 0, 0, 0, currStart.Location())
  751. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  752. wins = append(wins, NewClosedWindow(currStart, currEnd))
  753. currStart = currEnd
  754. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day(), currEnd.Hour()+1, 0, 0, 0, currStart.Location())
  755. }
  756. return wins
  757. }
  758. func (w Window) getDailyWindow() Window {
  759. origStart := w.Start()
  760. origEnd := w.End()
  761. // round the start and end windows to the calendar day start and ends, respectively
  762. roundedStart := time.Date(origStart.Year(), origStart.Month(), origStart.Day(), 0, 0, 0, 0, origStart.Location())
  763. roundedEnd := time.Date(origEnd.Year(), origEnd.Month(), origEnd.Day()+1, 0, 0, 0, 0, origEnd.Location())
  764. // edge case - if user has exactly specified first instant of new day, does not need rounding
  765. if origEnd.Minute() == 0 && origEnd.Second() == 0 && origEnd.Hour() == 0 {
  766. roundedEnd = *origEnd
  767. }
  768. return NewClosedWindow(roundedStart, roundedEnd)
  769. }
  770. // getDailyWindows breaks up a window into days
  771. func (w Window) getDailyWindows() []Window {
  772. wins := []Window{}
  773. roundedWindow := w.getDailyWindow()
  774. roundedStart := *roundedWindow.Start()
  775. roundedEnd := *roundedWindow.End()
  776. currStart := roundedStart
  777. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day()+1, 0, 0, 0, 0, currStart.Location())
  778. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  779. wins = append(wins, NewClosedWindow(currStart, currEnd))
  780. currStart = currEnd
  781. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day()+1, 0, 0, 0, 0, currStart.Location())
  782. }
  783. return wins
  784. }
  785. func (w Window) getWeeklyWindow() Window {
  786. origStart := w.Start()
  787. origEnd := w.End()
  788. // round the start and end windows to the calendar month start and ends, respectively
  789. roundedStart := origStart.Add(-1 * time.Duration(origStart.Weekday()) * time.Hour * 24)
  790. roundedStart = time.Date(roundedStart.Year(), roundedStart.Month(), roundedStart.Day(), 0, 0, 0, 0, origEnd.Location())
  791. roundedEnd := origEnd.Add(time.Duration(6-origEnd.Weekday()) * time.Hour * 24)
  792. roundedEnd = time.Date(roundedEnd.Year(), roundedEnd.Month(), roundedEnd.Day()+1, 0, 0, 0, 0, origEnd.Location())
  793. // edge case - if user has exactly specified first instant of new day, does not need rounding
  794. if origEnd.Weekday() == 0 && origEnd.Second() == 0 && origEnd.Hour() == 0 {
  795. roundedEnd = *origEnd
  796. }
  797. return NewClosedWindow(roundedStart, roundedEnd)
  798. }
  799. // getWeeklyWindows breaks up a window into weeks, with weeks starting on Sunday
  800. func (w Window) getWeeklyWindows() []Window {
  801. wins := []Window{}
  802. roundedWindow := w.getDailyWindow()
  803. roundedStart := *roundedWindow.Start()
  804. roundedEnd := *roundedWindow.End()
  805. currStart := roundedStart
  806. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day()+7, 0, 0, 0, 0, currStart.Location())
  807. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  808. wins = append(wins, NewClosedWindow(currStart, currEnd))
  809. currStart = currEnd
  810. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day()+7, 0, 0, 0, 0, currStart.Location())
  811. }
  812. return wins
  813. }
  814. func (w Window) getMonthlyWindow() Window {
  815. origStart := w.Start()
  816. origEnd := w.End()
  817. // round the start and end windows to the calendar month start and ends, respectively
  818. roundedStart := time.Date(origStart.Year(), origStart.Month(), 1, 0, 0, 0, 0, origStart.Location())
  819. roundedEnd := time.Date(origEnd.Year(), origEnd.Month()+1, 1, 0, 0, 0, 0, origEnd.Location())
  820. // edge case - if user has exactly specified first instant of new month, does not need rounding
  821. if origEnd.Day() == 1 && origEnd.Hour() == 0 && origEnd.Minute() == 0 && origEnd.Second() == 0 {
  822. roundedEnd = *origEnd
  823. }
  824. return NewClosedWindow(roundedStart, roundedEnd)
  825. }
  826. // getMonthlyWindows breaks up a window into calendar months
  827. func (w Window) getMonthlyWindows() []Window {
  828. wins := []Window{}
  829. roundedWindow := w.getMonthlyWindow()
  830. roundedStart := *roundedWindow.Start()
  831. roundedEnd := *roundedWindow.End()
  832. currStart := roundedStart
  833. currEnd := time.Date(currStart.Year(), currStart.Month()+1, 1, 0, 0, 0, 0, currStart.Location())
  834. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  835. wins = append(wins, NewClosedWindow(currStart, currEnd))
  836. currStart = currEnd
  837. currEnd = time.Date(currEnd.Year(), currEnd.Month()+1, 1, 0, 0, 0, 0, currStart.Location())
  838. }
  839. return wins
  840. }
  841. func (w Window) getQuarterlyWindow() Window {
  842. origStart := w.Start()
  843. origEnd := w.End()
  844. // round the start and end windows to the calendar quarter start and ends, respectively
  845. // get quarter fraction from month
  846. startQuarterNum := int(math.Ceil(float64(origStart.Month()) / 3.0))
  847. endQuarterNum := int(math.Ceil(float64(origEnd.Month()) / 3.0))
  848. roundedStart := time.Date(origStart.Year(), time.Month((startQuarterNum*3)-2), 1, 0, 0, 0, 0, origStart.Location())
  849. roundedEnd := time.Date(origEnd.Year(), time.Month(((endQuarterNum+1)*3)-2), 1, 0, 0, 0, 0, origEnd.Location())
  850. // edge case - if user has exactly specified first instant of new quarter, does not need rounding
  851. if origEnd.Month() == time.Month(((endQuarterNum)*3)-2) && origEnd.Day() == 1 && origEnd.Hour() == 0 && origEnd.Minute() == 0 && origEnd.Second() == 0 {
  852. roundedEnd = *origEnd
  853. }
  854. return NewClosedWindow(roundedStart, roundedEnd)
  855. }
  856. // getQuarterlyWindows breaks up a window into calendar months
  857. func (w Window) getQuarterlyWindows() []Window {
  858. wins := []Window{}
  859. roundedWindow := w.getQuarterlyWindow()
  860. roundedStart := *roundedWindow.Start()
  861. roundedEnd := *roundedWindow.End()
  862. currStart := roundedStart
  863. currEnd := time.Date(currStart.Year(), currStart.Month()+3, 1, 0, 0, 0, 0, currStart.Location())
  864. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  865. wins = append(wins, NewClosedWindow(currStart, currEnd))
  866. currStart = currEnd
  867. currEnd = time.Date(currEnd.Year(), currEnd.Month()+3, 1, 0, 0, 0, 0, currStart.Location())
  868. }
  869. return wins
  870. }
  871. // GetWindows returns a slice of Window with equal size between the given start and end. If windowSize does not evenly
  872. // divide the period between start and end, the last window is not added
  873. // Deprecated: in v1.107 use Window.GetWindows() instead
  874. func GetWindows(start time.Time, end time.Time, windowSize time.Duration) ([]Window, error) {
  875. // Ensure the range is evenly divisible into windows of the given duration
  876. dur := end.Sub(start)
  877. if int(dur.Minutes())%int(windowSize.Minutes()) != 0 {
  878. return nil, fmt.Errorf("range not divisible by window: [%s, %s] by %s", start, end, windowSize)
  879. }
  880. // Ensure that provided times are multiples of the provided windowSize (e.g. midnight for daily windows, on the hour for hourly windows)
  881. if start != RoundBack(start, windowSize) {
  882. return nil, fmt.Errorf("provided times are not divisible by provided window: [%s, %s] by %s", start, end, windowSize)
  883. }
  884. // Ensure timezones match
  885. _, sz := start.Zone()
  886. _, ez := end.Zone()
  887. if sz != ez {
  888. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  889. }
  890. if sz != int(utcOffset().Seconds()) {
  891. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", utcOffset(), sz)
  892. }
  893. // Build array of windows to cover the CloudCostSetRange
  894. windows := []Window{}
  895. s, e := start, start.Add(windowSize)
  896. for !e.After(end) {
  897. ws := s
  898. we := e
  899. windows = append(windows, NewWindow(&ws, &we))
  900. s = s.Add(windowSize)
  901. e = e.Add(windowSize)
  902. }
  903. return windows, nil
  904. }
  905. // GetWindowsForQueryWindow breaks up a window into an array of windows with a max size of queryWindow
  906. func GetWindowsForQueryWindow(start time.Time, end time.Time, queryWindow time.Duration) ([]Window, error) {
  907. // Ensure timezones match
  908. _, sz := start.Zone()
  909. _, ez := end.Zone()
  910. if sz != ez {
  911. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  912. }
  913. if sz != int(utcOffset().Seconds()) {
  914. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", utcOffset(), sz)
  915. }
  916. // Build array of windows to cover the CloudCostSetRange
  917. windows := []Window{}
  918. s, e := start, start.Add(queryWindow)
  919. for s.Before(end) {
  920. ws := s
  921. we := e
  922. windows = append(windows, NewWindow(&ws, &we))
  923. s = s.Add(queryWindow)
  924. e = e.Add(queryWindow)
  925. if e.After(end) {
  926. e = end
  927. }
  928. }
  929. return windows, nil
  930. }
  931. type BoundaryError struct {
  932. Requested Window
  933. Supported Window
  934. Message string
  935. }
  936. func NewBoundaryError(req, sup Window, msg string) *BoundaryError {
  937. return &BoundaryError{
  938. Requested: req,
  939. Supported: sup,
  940. Message: msg,
  941. }
  942. }
  943. func (be *BoundaryError) Error() string {
  944. if be == nil {
  945. return "<nil>"
  946. }
  947. return fmt.Sprintf("boundary error: requested %s; supported %s: %s", be.Requested, be.Supported, be.Message)
  948. }
  949. func (be *BoundaryError) Is(target error) bool {
  950. if _, ok := target.(*BoundaryError); ok {
  951. return true
  952. }
  953. return false
  954. }