step_align.go 955 B

1234567891011121314151617181920212223242526
  1. package costmodel
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/util/timeutil"
  5. )
  6. // alignStepStart aligns the first step boundary of a stepped query so that
  7. // step-sized buckets correspond to natural calendar intervals rather than
  8. // arbitrary offsets from the request's start time.
  9. //
  10. // This is currently used only for the weekly step, which is rolled back to
  11. // 00:00 UTC on the preceding Sunday via timeutil.RoundToStartOfWeek. Other
  12. // step durations are returned unchanged.
  13. //
  14. // For example, a request with a window starting mid-week and step=1w would,
  15. // without alignment, produce buckets that do not correspond to a calendar
  16. // week. With alignment, the first bucket begins at the preceding Sunday
  17. // 00:00 UTC, so subsequent bucket boundaries fall on Sunday 00:00 UTC.
  18. func alignStepStart(start time.Time, step time.Duration) time.Time {
  19. if step == timeutil.Week {
  20. return timeutil.RoundToStartOfWeek(start)
  21. }
  22. return start
  23. }