Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Add Window Attribute for Metrics (dbt-labs#5722)
Browse files Browse the repository at this point in the history
* file changes

* changing to window

* adding test

* adding changie for feature

* fixing commits

* fixing tests

* adding timestamp

* fixing graph unparsed

* changing default value
  • Loading branch information
callum-mcdata authored and josephberni committed Sep 16, 2022
1 parent 6c394be commit 43d0da6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220831-121319.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Adding the `window` parameter to the metric spec.
time: 2022-08-31T12:13:19.48356-05:00
custom:
Author: callum-mcdata
Issue: "5721"
PR: "5722"
5 changes: 5 additions & 0 deletions core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ class ParsedMetric(UnparsedBaseNode, HasUniqueID, HasFqn):
filters: List[MetricFilter]
time_grains: List[str]
dimensions: List[str]
window: Optional[str]
model: Optional[str] = None
model_unique_id: Optional[str] = None
resource_type: NodeType = NodeType.Metric
Expand All @@ -834,6 +835,9 @@ def search_name(self):
def same_model(self, old: "ParsedMetric") -> bool:
return self.model == old.model

def same_window(self, old: "ParsedMetric") -> bool:
return self.window == old.window

def same_dimensions(self, old: "ParsedMetric") -> bool:
return self.dimensions == old.dimensions

Expand Down Expand Up @@ -866,6 +870,7 @@ def same_contents(self, old: Optional["ParsedMetric"]) -> bool:

return (
self.same_model(old)
and self.same_window(old)
and self.same_dimensions(old)
and self.same_filters(old)
and self.same_description(old)
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,13 @@ class UnparsedMetric(dbtClassMixin, Replaceable):
name: str
label: str
type: str
timestamp: str
model: Optional[str] = None
description: str = ""
sql: Union[str, int] = ""
timestamp: Optional[str] = None
time_grains: List[str] = field(default_factory=list)
dimensions: List[str] = field(default_factory=list)
window: Optional[str] = None
filters: List[MetricFilter] = field(default_factory=list)
meta: Dict[str, Any] = field(default_factory=dict)
tags: List[str] = field(default_factory=list)
Expand Down
1 change: 1 addition & 0 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ def parse_metric(self, unparsed: UnparsedMetric) -> ParsedMetric:
sql=str(unparsed.sql),
timestamp=unparsed.timestamp,
dimensions=unparsed.dimensions,
window=unparsed.window,
time_grains=unparsed.time_grains,
filters=unparsed.filters,
meta=unparsed.meta,
Expand Down
7 changes: 6 additions & 1 deletion test/unit/test_contracts_graph_unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def get_ok_dict(self):
"operator": "=",
}
],
'window': '14 days',
'tags': [],
'meta': {
'is_okr': True
Expand All @@ -710,9 +711,11 @@ def get_ok_expression_dict(self):
'description': '',
'type': 'expression',
'sql': "{{ metric('revenue') }} / {{ metric('customers') }}",
'timestamp': "signup_date",
'time_grains': ['day', 'week', 'month'],
'dimensions': [],
'filters': [],
'window': '',
'tags': [],
'meta': {
'is_okr': True
Expand All @@ -735,6 +738,7 @@ def test_ok(self):
value='True',
operator="=",
)],
window="14 days",
meta={'is_okr': True},
)
dct = self.get_ok_dict()
Expand All @@ -750,9 +754,10 @@ def test_ok_metric_no_model(self):
description="",
type='expression',
sql="{{ metric('revenue') }} / {{ metric('customers') }}",
timestamp=None,
timestamp="signup_date",
time_grains=['day', 'week', 'month'],
dimensions=[],
window='',
meta={'is_okr': True},
)
dct = self.get_ok_expression_dict()
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_graph_selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ def make_metric(pkg, name, path=None):
value=True,
operator="=",
)],
window='',
meta={'is_okr': True},
tags=['okrs'],
)
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def setUp(self):
)],
meta={'is_okr': True},
tags=['okrs'],
window='',
resource_type=NodeType.Metric,
depends_on=DependsOn(nodes=['model.root.multi']),
refs=[['multi']],
Expand Down
22 changes: 21 additions & 1 deletion tests/functional/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@
operator: 'is'
value: 'true'
- name: collective_window
label: "Collective window"
description: Testing window
model: "ref('people')"
type: sum
sql: tenure
timestamp: created_at
time_grains: [day]
window: 14 days
filters:
- field: loves_dbt
operator: 'is'
value: 'true'
"""

models__people_sql = """
Expand Down Expand Up @@ -66,7 +80,11 @@ def test_simple_metric(
assert len(results) == 1
manifest = get_manifest(project.project_root)
metric_ids = list(manifest.metrics.keys())
expected_metric_ids = ["metric.test.number_of_people", "metric.test.collective_tenure"]
expected_metric_ids = [
"metric.test.number_of_people",
"metric.test.collective_tenure",
"metric.test.collective_window",
]
assert metric_ids == expected_metric_ids


Expand Down Expand Up @@ -260,6 +278,7 @@ def test_names_with_spaces(self, project):
time_grains: {{ m.time_grains }}
dimensions: {{ m.dimensions }}
filters: {{ m.filters }}
window: {{ m.window }}
{% endfor %}
{% endif %}
Expand Down Expand Up @@ -430,6 +449,7 @@ def test_expression_metric(
"time_grains",
"dimensions",
"filters",
"window",
]:
expected_value = getattr(parsed_metric_node, property)
assert f"{property}: {expected_value}" in compiled_code

0 comments on commit 43d0da6

Please sign in to comment.