window.go 32 KB

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