Skip to content

Commit

Permalink
Improve easy of specifying window on metrics
Browse files Browse the repository at this point in the history
We want end users to be able to specify metric windows like
```
window: 14 days
```
instead of having to do
```
window:
  count: 14
  granularity: day
```
  • Loading branch information
QMalcolm committed Jun 8, 2023
1 parent 3c111f9 commit 4add088
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
10 changes: 2 additions & 8 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,18 +599,12 @@ class UnparsedMetricInputMeasure(dbtClassMixin):
alias: Optional[str] = None


@dataclass
class UnparsedMetricTimeWindow(dbtClassMixin):
count: int
granularity: str # an TimeGranularity Enum


@dataclass
class UnparsedMetricInput(dbtClassMixin):
name: str
filter: Optional[str] = None
alias: Optional[str] = None
offset_window: Optional[UnparsedMetricTimeWindow] = None
offset_window: Optional[str] = None
offset_to_grain: Optional[str] = None # str is really a TimeGranularity Enum


Expand All @@ -621,7 +615,7 @@ class UnparsedMetricTypeParams(dbtClassMixin):
numerator: Optional[Union[UnparsedMetricInputMeasure, str]] = None
denominator: Optional[Union[UnparsedMetricInputMeasure, str]] = None
expr: Optional[str] = None
window: Optional[UnparsedMetricTimeWindow] = None
window: Optional[str] = None
grain_to_date: Optional[str] = None # str is really a TimeGranularity Enum
metrics: Optional[List[Union[UnparsedMetricInput, str]]] = None

Expand Down
39 changes: 35 additions & 4 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
UnparsedMetric,
UnparsedMetricInput,
UnparsedMetricInputMeasure,
UnparsedMetricTimeWindow,
UnparsedMetricTypeParams,
)
from dbt.contracts.graph.nodes import (
Expand Down Expand Up @@ -183,12 +182,44 @@ def _get_input_measures(

def _get_time_window(
self,
unparsed_window: Optional[UnparsedMetricTimeWindow],
unparsed_window: Optional[str],
) -> Optional[MetricTimeWindow]:
if unparsed_window is not None:
parts = unparsed_window.split(" ")
if len(parts) != 2:
raise YamlParseDictError(
self.yaml.path,
"window",
{"window": unparsed_window},
f"Invalid window ({unparsed_window}) in cumulative metric. Should be of the form `<count> <granularity>`, "
"e.g., `28 days`",
)

granularity = parts[1]
# once we drop python 3.8 this could just be `granularity = parts[0].removesuffix('s')
if granularity.endswith("s"):
# months -> month
granularity = granularity[:-1]
if granularity not in [item.value for item in TimeGranularity]:
raise YamlParseDictError(
self.yaml.path,
"window",
{"window": unparsed_window},
f"Invalid time granularity {granularity} in cumulative metric window string: ({unparsed_window})",
)

count = parts[0]
if not count.isdigit():
raise YamlParseDictError(
self.yaml.path,
"window",
{"window": unparsed_window},
f"Invalid count ({count}) in cumulative metric window string: ({unparsed_window})",
)

return MetricTimeWindow(
count=unparsed_window.count,
granularity=TimeGranularity(unparsed_window.granularity),
count=int(count),
granularity=TimeGranularity(granularity),
)
else:
return None
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/metrics/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@
measure:
name: years_tenure
filter: "loves_dbt is true"
window:
count: 14
granularity: day
window: 14 days
"""

Expand Down

0 comments on commit 4add088

Please sign in to comment.