window.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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" and "1w", we have to have a definition for what "the past X days" 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. if match[2] == "d" || match[2] == "w" {
  252. end = end.Truncate(timeutil.Day).Add(timeutil.Day)
  253. start = start.Truncate(timeutil.Day).Add(timeutil.Day)
  254. }
  255. return NewWindow(&start, &end), nil
  256. }
  257. // Match duration strings with offset; e.g. "45m offset 15m", etc.
  258. match = durationOffsetRegex.FindStringSubmatch(window)
  259. if match != nil {
  260. end := now
  261. offUnit := time.Minute
  262. if match[4] == "h" {
  263. offUnit = time.Hour
  264. }
  265. if match[4] == "d" {
  266. offUnit = 24 * time.Hour
  267. }
  268. if match[4] == "w" {
  269. offUnit = 24 * timeutil.Week
  270. }
  271. offNum, _ := strconv.ParseInt(match[3], 10, 64)
  272. end = end.Add(-time.Duration(offNum) * offUnit)
  273. durUnit := time.Minute
  274. if match[2] == "h" {
  275. durUnit = time.Hour
  276. }
  277. if match[2] == "d" {
  278. durUnit = 24 * time.Hour
  279. }
  280. if match[2] == "w" {
  281. durUnit = timeutil.Week
  282. }
  283. durNum, _ := strconv.ParseInt(match[1], 10, 64)
  284. start := end.Add(-time.Duration(durNum) * durUnit)
  285. return NewWindow(&start, &end), nil
  286. }
  287. // Match timestamp pairs, e.g. "1586822400,1586908800" or "1586822400-1586908800"
  288. match = timestampPairRegex.FindStringSubmatch(window)
  289. if match != nil {
  290. s, _ := strconv.ParseInt(match[1], 10, 64)
  291. e, _ := strconv.ParseInt(match[2], 10, 64)
  292. start := time.Unix(s, 0).UTC()
  293. end := time.Unix(e, 0).UTC()
  294. return NewWindow(&start, &end), nil
  295. }
  296. // Match RFC3339 pairs, e.g. "2020-04-01T00:00:00Z,2020-04-03T00:00:00Z"
  297. match = rfcRegex.FindStringSubmatch(window)
  298. if match != nil {
  299. start, _ := time.Parse(time.RFC3339, match[1])
  300. end, _ := time.Parse(time.RFC3339, match[2])
  301. return NewWindow(&start, &end), nil
  302. }
  303. return Window{nil, nil}, fmt.Errorf("illegal window: %s", window)
  304. }
  305. // ApproximatelyEqual returns true if the start and end times of the two windows,
  306. // respectively, are within the given threshold of each other.
  307. func (w Window) ApproximatelyEqual(that Window, threshold time.Duration) bool {
  308. return approxEqual(w.start, that.start, threshold) && approxEqual(w.end, that.end, threshold)
  309. }
  310. func approxEqual(x *time.Time, y *time.Time, threshold time.Duration) bool {
  311. // both times are nil, so they are equal
  312. if x == nil && y == nil {
  313. return true
  314. }
  315. // one time is nil, but the other is not, so they are not equal
  316. if x == nil || y == nil {
  317. return false
  318. }
  319. // neither time is nil, so they are approximately close if their times are
  320. // within the given threshold
  321. delta := math.Abs((*x).Sub(*y).Seconds())
  322. return delta < threshold.Seconds()
  323. }
  324. func (w Window) Clone() Window {
  325. var start, end *time.Time
  326. var s, e time.Time
  327. if w.start != nil {
  328. s = *w.start
  329. start = &s
  330. }
  331. if w.end != nil {
  332. e = *w.end
  333. end = &e
  334. }
  335. return NewWindow(start, end)
  336. }
  337. func (w Window) Contains(t time.Time) bool {
  338. if w.start != nil && t.Before(*w.start) {
  339. return false
  340. }
  341. if w.end != nil && t.After(*w.end) {
  342. return false
  343. }
  344. return true
  345. }
  346. func (w Window) ContainsWindow(that Window) bool {
  347. // only support containing closed windows for now
  348. // could check if openness is compatible with closure
  349. if that.IsOpen() {
  350. return false
  351. }
  352. return w.Contains(*that.start) && w.Contains(*that.end)
  353. }
  354. func (w Window) Duration() time.Duration {
  355. if w.IsOpen() {
  356. // TODO test
  357. return time.Duration(math.Inf(1.0))
  358. }
  359. return w.end.Sub(*w.start)
  360. }
  361. func (w Window) End() *time.Time {
  362. return w.end
  363. }
  364. func (w Window) Equal(that Window) bool {
  365. if w.start != nil && that.start != nil && !w.start.Equal(*that.start) {
  366. // starts are not nil, but not equal
  367. return false
  368. }
  369. if w.end != nil && that.end != nil && !w.end.Equal(*that.end) {
  370. // ends are not nil, but not equal
  371. return false
  372. }
  373. if (w.start == nil && that.start != nil) || (w.start != nil && that.start == nil) {
  374. // one start is nil, the other is not
  375. return false
  376. }
  377. if (w.end == nil && that.end != nil) || (w.end != nil && that.end == nil) {
  378. // one end is nil, the other is not
  379. return false
  380. }
  381. // either both starts are nil, or they match; likewise for the ends
  382. return true
  383. }
  384. func (w Window) ExpandStart(start time.Time) Window {
  385. if w.start == nil || start.Before(*w.start) {
  386. w.start = &start
  387. }
  388. return w
  389. }
  390. func (w Window) ExpandEnd(end time.Time) Window {
  391. if w.end == nil || end.After(*w.end) {
  392. w.end = &end
  393. }
  394. return w
  395. }
  396. func (w Window) Expand(that Window) Window {
  397. if that.start == nil {
  398. w.start = nil
  399. } else {
  400. w = w.ExpandStart(*that.start)
  401. }
  402. if that.end == nil {
  403. w.end = nil
  404. } else {
  405. w = w.ExpandEnd(*that.end)
  406. }
  407. return w
  408. }
  409. func (w Window) ContractStart(start time.Time) Window {
  410. if w.start == nil || start.After(*w.start) {
  411. w.start = &start
  412. }
  413. return w
  414. }
  415. func (w Window) ContractEnd(end time.Time) Window {
  416. if w.end == nil || end.Before(*w.end) {
  417. w.end = &end
  418. }
  419. return w
  420. }
  421. func (w Window) Contract(that Window) Window {
  422. if that.start != nil {
  423. w = w.ContractStart(*that.start)
  424. }
  425. if that.end != nil {
  426. w = w.ContractEnd(*that.end)
  427. }
  428. return w
  429. }
  430. func (w Window) Hours() float64 {
  431. if w.IsOpen() {
  432. return math.Inf(1)
  433. }
  434. return w.end.Sub(*w.start).Hours()
  435. }
  436. // IsEmpty a Window is empty if it does not have a start and an end
  437. func (w Window) IsEmpty() bool {
  438. return w.start == nil && w.end == nil
  439. }
  440. // HasDuration a Window has duration if neither start and end are not nil and not equal
  441. func (w Window) HasDuration() bool {
  442. return !w.IsOpen() && !w.end.Equal(*w.Start())
  443. }
  444. // IsNegative a Window is negative if start and end are not null and end is before start
  445. func (w Window) IsNegative() bool {
  446. return !w.IsOpen() && w.end.Before(*w.Start())
  447. }
  448. // IsOpen a Window is open if it has a nil start or end
  449. func (w Window) IsOpen() bool {
  450. return w.start == nil || w.end == nil
  451. }
  452. func (w Window) MarshalJSON() ([]byte, error) {
  453. buffer := bytes.NewBufferString("{")
  454. if w.start != nil {
  455. buffer.WriteString(fmt.Sprintf("\"start\":\"%s\",", w.start.Format(time.RFC3339)))
  456. } else {
  457. buffer.WriteString(fmt.Sprintf("\"start\":\"%s\",", "null"))
  458. }
  459. if w.end != nil {
  460. buffer.WriteString(fmt.Sprintf("\"end\":\"%s\"", w.end.Format(time.RFC3339)))
  461. } else {
  462. buffer.WriteString(fmt.Sprintf("\"end\":\"%s\"", "null"))
  463. }
  464. buffer.WriteString("}")
  465. return buffer.Bytes(), nil
  466. }
  467. func (w *Window) UnmarshalJSON(bs []byte) error {
  468. // Due to the behavior of our custom MarshalJSON, we unmarshal as strings
  469. // and then manually handle the weird quoted "null" case.
  470. type PubWindow struct {
  471. Start string `json:"start"`
  472. End string `json:"end"`
  473. }
  474. var pw PubWindow
  475. err := json.Unmarshal(bs, &pw)
  476. if err != nil {
  477. return fmt.Errorf("half unmarshal: %w", err)
  478. }
  479. var start *time.Time
  480. var end *time.Time
  481. if pw.Start != "null" {
  482. t, err := time.Parse(time.RFC3339, pw.Start)
  483. if err != nil {
  484. return fmt.Errorf("parsing start as RFC3339: %w", err)
  485. }
  486. start = &t
  487. }
  488. if pw.End != "null" {
  489. t, err := time.Parse(time.RFC3339, pw.End)
  490. if err != nil {
  491. return fmt.Errorf("parsing end as RFC3339: %w", err)
  492. }
  493. end = &t
  494. }
  495. w.start = start
  496. w.end = end
  497. return nil
  498. }
  499. func (w Window) Minutes() float64 {
  500. if w.IsOpen() {
  501. return math.Inf(1)
  502. }
  503. return w.end.Sub(*w.start).Minutes()
  504. }
  505. // Overlaps returns true iff the two given Windows share an amount of temporal
  506. // coverage.
  507. // TODO complete (with unit tests!) and then implement in AllocationSet.accumulate
  508. // TODO:CLEANUP
  509. // func (w Window) Overlaps(x Window) bool {
  510. // if (w.start == nil && w.end == nil) || (x.start == nil && x.end == nil) {
  511. // // one window is completely open, so overlap is guaranteed
  512. // // <---------->
  513. // // ?------?
  514. // return true
  515. // }
  516. // // Neither window is completely open (nil, nil), but one or the other might
  517. // // still be future- or past-open.
  518. // if w.start == nil {
  519. // // w is past-open, future-closed
  520. // // <------]
  521. // if x.start != nil && !x.start.Before(*w.end) {
  522. // // x starts after w ends (or eq)
  523. // // <------]
  524. // // [------?
  525. // return false
  526. // }
  527. // // <-----]
  528. // // ?-----?
  529. // return true
  530. // }
  531. // if w.end == nil {
  532. // // w is future-open, past-closed
  533. // // [------>
  534. // if x.end != nil && !x.end.After(*w.end) {
  535. // // x ends before w begins (or eq)
  536. // // [------>
  537. // // ?------]
  538. // return false
  539. // }
  540. // // [------>
  541. // // ?------?
  542. // return true
  543. // }
  544. // // Now we know w is closed, but we don't know about x
  545. // // [------]
  546. // // ?------?
  547. // if x.start == nil {
  548. // // TODO
  549. // }
  550. // if x.end == nil {
  551. // // TODO
  552. // }
  553. // // Both are closed.
  554. // if !x.start.Before(*w.end) && !x.end.Before(*w.end) {
  555. // // x starts and ends after w ends
  556. // // [------]
  557. // // [------]
  558. // return false
  559. // }
  560. // if !x.start.After(*w.start) && !x.end.After(*w.start) {
  561. // // x starts and ends before w starts
  562. // // [------]
  563. // // [------]
  564. // return false
  565. // }
  566. // // w and x must overlap
  567. // // [------]
  568. // // [------]
  569. // return true
  570. // }
  571. func (w *Window) Set(start, end *time.Time) {
  572. w.start = start
  573. w.end = end
  574. }
  575. // Shift adds the given duration to both the start and end times of the window
  576. func (w Window) Shift(dur time.Duration) Window {
  577. if w.start != nil {
  578. s := w.start.Add(dur)
  579. w.start = &s
  580. }
  581. if w.end != nil {
  582. e := w.end.Add(dur)
  583. w.end = &e
  584. }
  585. return w
  586. }
  587. func (w Window) Start() *time.Time {
  588. return w.start
  589. }
  590. func (w Window) String() string {
  591. if w.start == nil && w.end == nil {
  592. return "[nil, nil)"
  593. }
  594. if w.start == nil {
  595. return fmt.Sprintf("[nil, %s)", w.end.Format("2006-01-02T15:04:05-0700"))
  596. }
  597. if w.end == nil {
  598. return fmt.Sprintf("[%s, nil)", w.start.Format("2006-01-02T15:04:05-0700"))
  599. }
  600. return fmt.Sprintf("[%s, %s)", w.start.Format("2006-01-02T15:04:05-0700"), w.end.Format("2006-01-02T15:04:05-0700"))
  601. }
  602. // DurationOffset returns durations representing the duration and offset of the
  603. // given window
  604. func (w Window) DurationOffset() (time.Duration, time.Duration, error) {
  605. if w.IsOpen() || w.IsNegative() {
  606. return 0, 0, fmt.Errorf("illegal window: %s", w)
  607. }
  608. duration := w.Duration()
  609. offset := time.Since(*w.End())
  610. return duration, offset, nil
  611. }
  612. // DurationOffsetForPrometheus returns strings representing durations for the
  613. // duration and offset of the given window, factoring in the Thanos offset if
  614. // necessary. Whereas duration is a simple duration string (e.g. "1d"), the
  615. // offset includes the word "offset" (e.g. " offset 2d") so that the values
  616. // returned can be used directly in the formatting string "some_metric[%s]%s"
  617. // to generate the query "some_metric[1d] offset 2d".
  618. func (w Window) DurationOffsetForPrometheus() (string, string, error) {
  619. duration, offset, err := w.DurationOffset()
  620. if err != nil {
  621. return "", "", err
  622. }
  623. // If using Thanos, increase offset to 3 hours, reducing the duration by
  624. // equal measure to maintain the same starting point.
  625. // TODO: This logic should technically be decoupled from this type, but
  626. // TODO: current use cases are unclear. To ensure we do not break existing
  627. // TODO: (or legacy) use-cases, temporarily support this one-off logic.
  628. thanosDur := thanosOffset()
  629. if offset < thanosDur && isThanosEnabled() {
  630. diff := thanosDur - offset
  631. offset += diff
  632. duration -= diff
  633. }
  634. // If duration < 0, return an error
  635. if duration < 0 {
  636. return "", "", fmt.Errorf("negative duration: %s", duration)
  637. }
  638. // Negative offset means that the end time is in the future. Prometheus
  639. // fails for non-positive offset values, so shrink the duration and
  640. // remove the offset altogether.
  641. if offset < 0 {
  642. duration = duration + offset
  643. offset = 0
  644. }
  645. durStr, offStr := timeutil.DurationOffsetStrings(duration, offset)
  646. if offset < time.Minute {
  647. offStr = ""
  648. } else {
  649. offStr = " offset " + offStr
  650. }
  651. return durStr, offStr, nil
  652. }
  653. // DurationOffsetStrings returns formatted, Prometheus-compatible strings representing
  654. // the duration and offset of the window in terms of days, hours, minutes, or seconds;
  655. // e.g. ("7d", "1441m", "30m", "1s", "")
  656. func (w Window) DurationOffsetStrings() (string, string) {
  657. dur, off, err := w.DurationOffset()
  658. if err != nil {
  659. return "", ""
  660. }
  661. return timeutil.DurationOffsetStrings(dur, off)
  662. }
  663. // GetPercentInWindow Determine pct of item time contained the window.
  664. // determined by the overlap of the start/end with the given
  665. // window, which will be negative if there is no overlap. If
  666. // there is positive overlap, compare it with the total mins.
  667. //
  668. // e.g. here are the two possible scenarios as simplidied
  669. // 10m windows with dashes representing item's time running:
  670. //
  671. // 1. item falls entirely within one CloudCostSet window
  672. // | ---- | | |
  673. // totalMins = 4.0
  674. // pct := 4.0 / 4.0 = 1.0 for window 1
  675. // pct := 0.0 / 4.0 = 0.0 for window 2
  676. // pct := 0.0 / 4.0 = 0.0 for window 3
  677. //
  678. // 2. item overlaps multiple CloudCostSet windows
  679. // | ----|----------|-- |
  680. // totalMins = 16.0
  681. // pct := 4.0 / 16.0 = 0.250 for window 1
  682. // pct := 10.0 / 16.0 = 0.625 for window 2
  683. // pct := 2.0 / 16.0 = 0.125 for window 3
  684. func (w Window) GetPercentInWindow(that Window) float64 {
  685. if that.IsOpen() {
  686. log.Errorf("Window: GetPercentInWindow: invalid window %s", that.String())
  687. return 0
  688. }
  689. s := *that.Start()
  690. if s.Before(*w.Start()) {
  691. s = *w.Start()
  692. }
  693. e := *that.End()
  694. if e.After(*w.End()) {
  695. e = *w.End()
  696. }
  697. mins := e.Sub(s).Minutes()
  698. if mins <= 0.0 {
  699. return 0.0
  700. }
  701. totalMins := that.Duration().Minutes()
  702. pct := mins / totalMins
  703. return pct
  704. }
  705. // GetAccumulateWindow rounds the start and end of the window to the given accumulation option
  706. func (w Window) GetAccumulateWindow(accumOpt AccumulateOption) (Window, error) {
  707. if w.IsOpen() {
  708. return w, fmt.Errorf("could not get accumlate window for open window")
  709. }
  710. switch accumOpt {
  711. case AccumulateOptionAll:
  712. // just return the entire window
  713. return w.Clone(), nil
  714. case AccumulateOptionHour:
  715. return w.getHourlyWindow(), nil
  716. case AccumulateOptionDay:
  717. return w.getDailyWindow(), nil
  718. case AccumulateOptionWeek:
  719. return w.getWeeklyWindow(), nil
  720. case AccumulateOptionMonth:
  721. return w.getMonthlyWindow(), nil
  722. case AccumulateOptionQuarter:
  723. return w.getQuarterlyWindow(), nil
  724. case AccumulateOptionNone:
  725. // the default behavior of the app currently is to return the highest resolution steps
  726. // possible
  727. fallthrough
  728. default:
  729. // if we are here, it means someone wants a window older than what we can query for
  730. return w, fmt.Errorf("cannot round window to given accumulation option %s", string(accumOpt))
  731. }
  732. }
  733. // GetAccumulateWindows breaks provided window into a []Window with each window having the resolution of the provided AccumulateOption
  734. func (w Window) GetAccumulateWindows(accumOpt AccumulateOption) ([]Window, error) {
  735. if w.IsOpen() {
  736. return nil, fmt.Errorf("could not get accumlate window for open window")
  737. }
  738. switch accumOpt {
  739. case AccumulateOptionAll:
  740. // just return the entire window
  741. return []Window{w.Clone()}, nil
  742. case AccumulateOptionDay:
  743. wins := w.getDailyWindows()
  744. return wins, nil
  745. case AccumulateOptionWeek:
  746. wins := w.getWeeklyWindows()
  747. return wins, nil
  748. case AccumulateOptionMonth:
  749. wins := w.getMonthlyWindows()
  750. return wins, nil
  751. case AccumulateOptionHour:
  752. // our maximum resolution is hourly
  753. wins := w.getHourlyWindows()
  754. return wins, nil
  755. case AccumulateOptionQuarter:
  756. wins := w.getQuarterlyWindows()
  757. return wins, nil
  758. case AccumulateOptionNone:
  759. // the default behavior of the app currently is to return the highest resolution steps
  760. // possible
  761. fallthrough
  762. default:
  763. // if we are here, it means someone wants a window older than what we can query for
  764. return nil, fmt.Errorf("store does not have coverage window starting at %v", w.Start())
  765. }
  766. }
  767. func (w Window) getHourlyWindow() Window {
  768. origStart := w.Start()
  769. origEnd := w.End()
  770. // round the start and end windows to the calendar hour start and ends, respectively
  771. roundedStart := time.Date(origStart.Year(), origStart.Month(), origStart.Day(), origStart.Hour(), 0, 0, 0, origStart.Location())
  772. roundedEnd := time.Date(origEnd.Year(), origEnd.Month(), origEnd.Day(), origEnd.Hour()+1, 0, 0, 0, origEnd.Location())
  773. // edge case - if user has exactly specified first instant of new hour, does not need rounding
  774. if origEnd.Minute() == 0 && origEnd.Second() == 0 {
  775. roundedEnd = *origEnd
  776. }
  777. return NewClosedWindow(roundedStart, roundedEnd)
  778. }
  779. // getHourlyWindows breaks up a window into hours
  780. func (w Window) getHourlyWindows() []Window {
  781. wins := []Window{}
  782. roundedWindow := w.getHourlyWindow()
  783. roundedStart := *roundedWindow.Start()
  784. roundedEnd := *roundedWindow.End()
  785. currStart := roundedStart
  786. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day(), currStart.Hour()+1, 0, 0, 0, currStart.Location())
  787. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  788. wins = append(wins, NewClosedWindow(currStart, currEnd))
  789. currStart = currEnd
  790. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day(), currEnd.Hour()+1, 0, 0, 0, currStart.Location())
  791. }
  792. return wins
  793. }
  794. func (w Window) getDailyWindow() Window {
  795. origStart := w.Start()
  796. origEnd := w.End()
  797. // round the start and end windows to the calendar day start and ends, respectively
  798. roundedStart := time.Date(origStart.Year(), origStart.Month(), origStart.Day(), 0, 0, 0, 0, origStart.Location())
  799. roundedEnd := time.Date(origEnd.Year(), origEnd.Month(), origEnd.Day()+1, 0, 0, 0, 0, origEnd.Location())
  800. // edge case - if user has exactly specified first instant of new day, does not need rounding
  801. if origEnd.Minute() == 0 && origEnd.Second() == 0 && origEnd.Hour() == 0 {
  802. roundedEnd = *origEnd
  803. }
  804. return NewClosedWindow(roundedStart, roundedEnd)
  805. }
  806. // getDailyWindows breaks up a window into days
  807. func (w Window) getDailyWindows() []Window {
  808. wins := []Window{}
  809. roundedWindow := w.getDailyWindow()
  810. roundedStart := *roundedWindow.Start()
  811. roundedEnd := *roundedWindow.End()
  812. currStart := roundedStart
  813. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day()+1, 0, 0, 0, 0, currStart.Location())
  814. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  815. wins = append(wins, NewClosedWindow(currStart, currEnd))
  816. currStart = currEnd
  817. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day()+1, 0, 0, 0, 0, currStart.Location())
  818. }
  819. return wins
  820. }
  821. func (w Window) getWeeklyWindow() Window {
  822. origStart := w.Start()
  823. origEnd := w.End()
  824. // round the start and end windows to the calendar month start and ends, respectively
  825. roundedStart := origStart.Add(-1 * time.Duration(origStart.Weekday()) * time.Hour * 24)
  826. roundedStart = time.Date(roundedStart.Year(), roundedStart.Month(), roundedStart.Day(), 0, 0, 0, 0, origEnd.Location())
  827. roundedEnd := origEnd.Add(time.Duration(6-origEnd.Weekday()) * time.Hour * 24)
  828. roundedEnd = time.Date(roundedEnd.Year(), roundedEnd.Month(), roundedEnd.Day()+1, 0, 0, 0, 0, origEnd.Location())
  829. // edge case - if user has exactly specified first instant of new day, does not need rounding
  830. if origEnd.Weekday() == 0 && origEnd.Second() == 0 && origEnd.Hour() == 0 {
  831. roundedEnd = *origEnd
  832. }
  833. return NewClosedWindow(roundedStart, roundedEnd)
  834. }
  835. // getWeeklyWindows breaks up a window into weeks, with weeks starting on Sunday
  836. func (w Window) getWeeklyWindows() []Window {
  837. wins := []Window{}
  838. roundedWindow := w.getDailyWindow()
  839. roundedStart := *roundedWindow.Start()
  840. roundedEnd := *roundedWindow.End()
  841. currStart := roundedStart
  842. currEnd := time.Date(currStart.Year(), currStart.Month(), currStart.Day()+7, 0, 0, 0, 0, currStart.Location())
  843. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  844. wins = append(wins, NewClosedWindow(currStart, currEnd))
  845. currStart = currEnd
  846. currEnd = time.Date(currEnd.Year(), currEnd.Month(), currEnd.Day()+7, 0, 0, 0, 0, currStart.Location())
  847. }
  848. return wins
  849. }
  850. func (w Window) getMonthlyWindow() Window {
  851. origStart := w.Start()
  852. origEnd := w.End()
  853. // round the start and end windows to the calendar month start and ends, respectively
  854. roundedStart := time.Date(origStart.Year(), origStart.Month(), 1, 0, 0, 0, 0, origStart.Location())
  855. roundedEnd := time.Date(origEnd.Year(), origEnd.Month()+1, 1, 0, 0, 0, 0, origEnd.Location())
  856. // edge case - if user has exactly specified first instant of new month, does not need rounding
  857. if origEnd.Day() == 1 && origEnd.Hour() == 0 && origEnd.Minute() == 0 && origEnd.Second() == 0 {
  858. roundedEnd = *origEnd
  859. }
  860. return NewClosedWindow(roundedStart, roundedEnd)
  861. }
  862. // getMonthlyWindows breaks up a window into calendar months
  863. func (w Window) getMonthlyWindows() []Window {
  864. wins := []Window{}
  865. roundedWindow := w.getMonthlyWindow()
  866. roundedStart := *roundedWindow.Start()
  867. roundedEnd := *roundedWindow.End()
  868. currStart := roundedStart
  869. currEnd := time.Date(currStart.Year(), currStart.Month()+1, 1, 0, 0, 0, 0, currStart.Location())
  870. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  871. wins = append(wins, NewClosedWindow(currStart, currEnd))
  872. currStart = currEnd
  873. currEnd = time.Date(currEnd.Year(), currEnd.Month()+1, 1, 0, 0, 0, 0, currStart.Location())
  874. }
  875. return wins
  876. }
  877. func (w Window) getQuarterlyWindow() Window {
  878. origStart := w.Start()
  879. origEnd := w.End()
  880. // round the start and end windows to the calendar quarter start and ends, respectively
  881. // get quarter fraction from month
  882. startQuarterNum := int(math.Ceil(float64(origStart.Month()) / 3.0))
  883. endQuarterNum := int(math.Ceil(float64(origEnd.Month()) / 3.0))
  884. roundedStart := time.Date(origStart.Year(), time.Month((startQuarterNum*3)-2), 1, 0, 0, 0, 0, origStart.Location())
  885. roundedEnd := time.Date(origEnd.Year(), time.Month(((endQuarterNum+1)*3)-2), 1, 0, 0, 0, 0, origEnd.Location())
  886. // edge case - if user has exactly specified first instant of new quarter, does not need rounding
  887. if origEnd.Month() == time.Month(((endQuarterNum)*3)-2) && origEnd.Day() == 1 && origEnd.Hour() == 0 && origEnd.Minute() == 0 && origEnd.Second() == 0 {
  888. roundedEnd = *origEnd
  889. }
  890. return NewClosedWindow(roundedStart, roundedEnd)
  891. }
  892. // getQuarterlyWindows breaks up a window into calendar months
  893. func (w Window) getQuarterlyWindows() []Window {
  894. wins := []Window{}
  895. roundedWindow := w.getQuarterlyWindow()
  896. roundedStart := *roundedWindow.Start()
  897. roundedEnd := *roundedWindow.End()
  898. currStart := roundedStart
  899. currEnd := time.Date(currStart.Year(), currStart.Month()+3, 1, 0, 0, 0, 0, currStart.Location())
  900. for currEnd.Before(roundedEnd) || currEnd.Equal(roundedEnd) {
  901. wins = append(wins, NewClosedWindow(currStart, currEnd))
  902. currStart = currEnd
  903. currEnd = time.Date(currEnd.Year(), currEnd.Month()+3, 1, 0, 0, 0, 0, currStart.Location())
  904. }
  905. return wins
  906. }
  907. // GetWindows returns a slice of Window with equal size between the given start and end. If windowSize does not evenly
  908. // divide the period between start and end, the last window is not added
  909. // Deprecated: in v1.107 use Window.GetWindows() instead
  910. func GetWindows(start time.Time, end time.Time, windowSize time.Duration) ([]Window, error) {
  911. // Ensure the range is evenly divisible into windows of the given duration
  912. dur := end.Sub(start)
  913. if int(dur.Minutes())%int(windowSize.Minutes()) != 0 {
  914. return nil, fmt.Errorf("range not divisible by window: [%s, %s] by %s", start, end, windowSize)
  915. }
  916. // Ensure that provided times are multiples of the provided windowSize (e.g. midnight for daily windows, on the hour for hourly windows)
  917. if start != RoundBack(start, windowSize) {
  918. return nil, fmt.Errorf("provided times are not divisible by provided window: [%s, %s] by %s", start, end, windowSize)
  919. }
  920. // Ensure timezones match
  921. _, sz := start.Zone()
  922. _, ez := end.Zone()
  923. if sz != ez {
  924. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  925. }
  926. if sz != int(utcOffset().Seconds()) {
  927. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", utcOffset(), sz)
  928. }
  929. // Build array of windows to cover the CloudCostSetRange
  930. windows := []Window{}
  931. s, e := start, start.Add(windowSize)
  932. for !e.After(end) {
  933. ws := s
  934. we := e
  935. windows = append(windows, NewWindow(&ws, &we))
  936. s = s.Add(windowSize)
  937. e = e.Add(windowSize)
  938. }
  939. return windows, nil
  940. }
  941. // GetWindowsForQueryWindow breaks up a window into an array of windows with a max size of queryWindow
  942. func GetWindowsForQueryWindow(start time.Time, end time.Time, queryWindow time.Duration) ([]Window, error) {
  943. // Ensure timezones match
  944. _, sz := start.Zone()
  945. _, ez := end.Zone()
  946. if sz != ez {
  947. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  948. }
  949. if sz != int(utcOffset().Seconds()) {
  950. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", utcOffset(), sz)
  951. }
  952. // Build array of windows to cover the CloudCostSetRange
  953. windows := []Window{}
  954. s, e := start, start.Add(queryWindow)
  955. for s.Before(end) {
  956. ws := s
  957. we := e
  958. windows = append(windows, NewWindow(&ws, &we))
  959. s = s.Add(queryWindow)
  960. e = e.Add(queryWindow)
  961. if e.After(end) {
  962. e = end
  963. }
  964. }
  965. return windows, nil
  966. }
  967. type BoundaryError struct {
  968. Requested Window
  969. Supported Window
  970. Message string
  971. }
  972. func NewBoundaryError(req, sup Window, msg string) *BoundaryError {
  973. return &BoundaryError{
  974. Requested: req,
  975. Supported: sup,
  976. Message: msg,
  977. }
  978. }
  979. func (be *BoundaryError) Error() string {
  980. if be == nil {
  981. return "<nil>"
  982. }
  983. return fmt.Sprintf("boundary error: requested %s; supported %s: %s", be.Requested, be.Supported, be.Message)
  984. }
  985. func (be *BoundaryError) Is(target error) bool {
  986. if _, ok := target.(*BoundaryError); ok {
  987. return true
  988. }
  989. return false
  990. }