window.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. // GetWindows returns a slice of Window with equal size between the given start and end. If windowSize does not evenly
  662. // divide the period between start and end, the last window is not added
  663. func GetWindows(start time.Time, end time.Time, windowSize time.Duration) ([]Window, error) {
  664. // Ensure the range is evenly divisible into windows of the given duration
  665. dur := end.Sub(start)
  666. if int(dur.Minutes())%int(windowSize.Minutes()) != 0 {
  667. return nil, fmt.Errorf("range not divisible by window: [%s, %s] by %s", start, end, windowSize)
  668. }
  669. // Ensure that provided times are multiples of the provided windowSize (e.g. midnight for daily windows, on the hour for hourly windows)
  670. if start != RoundBack(start, windowSize) {
  671. return nil, fmt.Errorf("provided times are not divisible by provided window: [%s, %s] by %s", start, end, windowSize)
  672. }
  673. // Ensure timezones match
  674. _, sz := start.Zone()
  675. _, ez := end.Zone()
  676. if sz != ez {
  677. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  678. }
  679. if sz != int(env.GetParsedUTCOffset().Seconds()) {
  680. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", env.GetParsedUTCOffset(), sz)
  681. }
  682. // Build array of windows to cover the CloudCostSetRange
  683. windows := []Window{}
  684. s, e := start, start.Add(windowSize)
  685. for !e.After(end) {
  686. ws := s
  687. we := e
  688. windows = append(windows, NewWindow(&ws, &we))
  689. s = s.Add(windowSize)
  690. e = e.Add(windowSize)
  691. }
  692. return windows, nil
  693. }
  694. // GetWindowsForQueryWindow breaks up a window into an array of windows with a max size of queryWindow
  695. func GetWindowsForQueryWindow(start time.Time, end time.Time, queryWindow time.Duration) ([]Window, error) {
  696. // Ensure timezones match
  697. _, sz := start.Zone()
  698. _, ez := end.Zone()
  699. if sz != ez {
  700. return nil, fmt.Errorf("range has mismatched timezones: %s, %s", start, end)
  701. }
  702. if sz != int(env.GetParsedUTCOffset().Seconds()) {
  703. return nil, fmt.Errorf("range timezone doesn't match configured timezone: expected %s; found %ds", env.GetParsedUTCOffset(), sz)
  704. }
  705. // Build array of windows to cover the CloudCostSetRange
  706. windows := []Window{}
  707. s, e := start, start.Add(queryWindow)
  708. for s.Before(end) {
  709. ws := s
  710. we := e
  711. windows = append(windows, NewWindow(&ws, &we))
  712. s = s.Add(queryWindow)
  713. e = e.Add(queryWindow)
  714. if e.After(end) {
  715. e = end
  716. }
  717. }
  718. return windows, nil
  719. }
  720. type BoundaryError struct {
  721. Requested Window
  722. Supported Window
  723. Message string
  724. }
  725. func NewBoundaryError(req, sup Window, msg string) *BoundaryError {
  726. return &BoundaryError{
  727. Requested: req,
  728. Supported: sup,
  729. Message: msg,
  730. }
  731. }
  732. func (be *BoundaryError) Error() string {
  733. if be == nil {
  734. return "<nil>"
  735. }
  736. return fmt.Sprintf("boundary error: requested %s; supported %s: %s", be.Requested, be.Supported, be.Message)
  737. }
  738. func (be *BoundaryError) Is(target error) bool {
  739. if _, ok := target.(*BoundaryError); ok {
  740. return true
  741. }
  742. return false
  743. }