Skip to content

Commit

Permalink
Upgrade DSI + handle new granularities
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 7, 2024
1 parent b30776a commit be60e23
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1,111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ class DateutilTimePeriodAdjuster(TimePeriodAdjuster):

def _relative_delta_for_window(self, time_granularity: TimeGranularity, count: int) -> relativedelta:
"""Relative-delta to cover time windows specified at different grains."""
if time_granularity is TimeGranularity.DAY:
if time_granularity is TimeGranularity.NANOSECOND:
# TODO: figure out a workaround when enabling time constraints
raise ValueError("`relativedelta` does not support nanoseconds.")
elif time_granularity is TimeGranularity.MICROSECOND:
return relativedelta(microseconds=count)
elif time_granularity is TimeGranularity.MILLISECOND:
return relativedelta(microseconds=count * 1000)
elif time_granularity is TimeGranularity.SECOND:
return relativedelta(seconds=count)
elif time_granularity is TimeGranularity.MINUTE:
return relativedelta(minutes=count)
elif time_granularity is TimeGranularity.HOUR:
return relativedelta(hours=count)
elif time_granularity is TimeGranularity.DAY:
return relativedelta(days=count)
elif time_granularity is TimeGranularity.WEEK:
return relativedelta(weeks=count)
Expand Down Expand Up @@ -53,8 +66,18 @@ def expand_time_constraint_to_fill_granularity(
def adjust_to_start_of_period(
self, time_granularity: TimeGranularity, date_to_adjust: datetime.datetime
) -> datetime.datetime:
if time_granularity is TimeGranularity.DAY:
# TODO: update these options once time constraints support a full timestamp
if (
time_granularity is TimeGranularity.NANOSECOND
or time_granularity is TimeGranularity.MICROSECOND
or time_granularity is TimeGranularity.MILLISECOND
or time_granularity is TimeGranularity.SECOND
or time_granularity is TimeGranularity.MINUTE
or time_granularity is TimeGranularity.HOUR
or time_granularity is TimeGranularity.DAY
):
return date_to_adjust

elif time_granularity is TimeGranularity.WEEK:
return date_to_adjust + relativedelta(weekday=dateutil.relativedelta.MO(-1))
elif time_granularity is TimeGranularity.MONTH:
Expand All @@ -77,8 +100,18 @@ def adjust_to_start_of_period(
def adjust_to_end_of_period(
self, time_granularity: TimeGranularity, date_to_adjust: datetime.datetime
) -> datetime.datetime:
if time_granularity is TimeGranularity.DAY:
# TODO: update these options once time constraints support a full timestamp
if (
time_granularity is TimeGranularity.NANOSECOND
or time_granularity is TimeGranularity.MICROSECOND
or time_granularity is TimeGranularity.MILLISECOND
or time_granularity is TimeGranularity.SECOND
or time_granularity is TimeGranularity.MINUTE
or time_granularity is TimeGranularity.HOUR
or time_granularity is TimeGranularity.DAY
):
return date_to_adjust

elif time_granularity is TimeGranularity.WEEK:
return date_to_adjust + relativedelta(weekday=dateutil.relativedelta.SU(1))
elif time_granularity is TimeGranularity.MONTH:
Expand Down
10 changes: 0 additions & 10 deletions metricflow-semantics/metricflow_semantics/time/time_constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
from __future__ import annotations

from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity

# Python formatting string to use for converting datetime to ISO8601
ISO8601_PYTHON_FORMAT = "%Y-%m-%d"
ISO8601_PYTHON_TS_FORMAT = "%Y-%m-%d %H:%M:%S"

SUPPORTED_GRANULARITIES = [
TimeGranularity.DAY,
TimeGranularity.WEEK,
TimeGranularity.MONTH,
TimeGranularity.QUARTER,
TimeGranularity.YEAR,
]
Loading

0 comments on commit be60e23

Please sign in to comment.