Skip to content

Commit

Permalink
Add Metric.time_granularity to metric spec (#10378)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb authored Jul 16, 2024
1 parent b032915 commit 4c7d922
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240627-162953.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add time_granularity to metric spec.
time: 2024-06-27T16:29:53.500917-07:00
custom:
Author: courtneyholcomb
Issue: "10376"
1 change: 1 addition & 0 deletions core/dbt/artifacts/resources/v1/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Metric(GraphResource):
type_params: MetricTypeParams
filter: Optional[WhereFilterIntersection] = None
metadata: Optional[SourceFileMetadata] = None
time_granularity: Optional[TimeGranularity] = None
resource_type: Literal[NodeType.Metric]
meta: Dict[str, Any] = field(default_factory=dict, metadata=MergeBehavior.Update.meta())
tags: List[str] = field(default_factory=list)
Expand Down
1 change: 1 addition & 0 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ class UnparsedMetric(dbtClassMixin):
description: str = ""
# Note: `Union` must be the outermost part of the type annotation for serialization to work properly.
filter: Union[str, List[str], None] = None
time_granularity: Optional[str] = None
# metadata: Optional[Unparsedetadata] = None # TODO
meta: Dict[str, Any] = field(default_factory=dict)
tags: List[str] = field(default_factory=list)
Expand Down
3 changes: 3 additions & 0 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ def parse_metric(self, unparsed: UnparsedMetric, generated: bool = False) -> Non
label=unparsed.label,
type=MetricType(unparsed.type),
type_params=self._get_metric_type_params(unparsed),
time_granularity=(
TimeGranularity(unparsed.time_granularity) if unparsed.time_granularity else None
),
filter=parse_where_filter(unparsed.filter),
meta=unparsed.meta,
tags=unparsed.tags,
Expand Down
2 changes: 1 addition & 1 deletion core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
# Accept patches but avoid automatically updating past a set minor version range.
"dbt-extractor>=0.5.0,<=0.6",
"minimal-snowplow-tracker>=0.0.2,<0.1",
"dbt-semantic-interfaces>=0.6.1,<0.7",
"dbt-semantic-interfaces>=0.6.8,<0.7",
# Minor versions for these are expected to be backwards-compatible
"dbt-common>=1.3.0,<2.0",
"dbt-adapters>=1.1.1,<2.0",
Expand Down
46 changes: 46 additions & 0 deletions schemas/dbt/manifest/v12.json
Original file line number Diff line number Diff line change
Expand Up @@ -9044,6 +9044,29 @@
],
"default": null
},
"time_granularity": {
"anyOf": [
{
"enum": [
"nanosecond",
"microsecond",
"millisecond",
"second",
"minute",
"hour",
"day",
"week",
"month",
"quarter",
"year"
]
},
{
"type": "null"
}
],
"default": null
},
"meta": {
"type": "object",
"propertyNames": {
Expand Down Expand Up @@ -18020,6 +18043,29 @@
],
"default": null
},
"time_granularity": {
"anyOf": [
{
"enum": [
"nanosecond",
"microsecond",
"millisecond",
"second",
"minute",
"hour",
"day",
"week",
"month",
"quarter",
"year"
]
},
{
"type": "null"
}
],
"default": null
},
"meta": {
"type": "object",
"propertyNames": {
Expand Down
1 change: 1 addition & 0 deletions tests/functional/metrics/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
type: simple
type_params:
measure: people
time_granularity: month
config:
meta:
my_meta_config: 'config'
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def test_simple_metric(
)
== 2
)
assert (
manifest.metrics["metric.test.number_of_people"].time_granularity
== TimeGranularity.MONTH
)
assert manifest.metrics["metric.test.collective_tenure"].time_granularity is None


class TestInvalidRefMetrics:
Expand Down

0 comments on commit 4c7d922

Please sign in to comment.