Skip to content

Commit

Permalink
Begin creating metrics from measures with create_metric = True
Browse files Browse the repository at this point in the history
This likely won't be the final implementation, but a stepping stone.
The reason for that is: THIS DOESN'T ACCOUNT FOR PARTIAL PARSING. Not
accounting for partial parsing is problematic. There are 3 specific
cases that we need to keep in mind / figure out:

1. What happens when a measure maintains `create_metric = True` and
partial parsing happens? With this current workflow, a parsing error
is raised because the measure tries to create the metric again as it
has no way to know a metric has already been created

2. What happens when a measure changes from `create_metric = True` to
`create_metric = False` and partial parsing happens?? With this current
workflow, the metric will continue to exist because it doesn't know
to check if a metric needs to be removed, or even how to check for
that case.

3. What happens when a measure changes from `create_metric = False` to
`create_metric = True` and partial parsing? With the current workflow,
the metric gets created, which is what we want.
  • Loading branch information
QMalcolm committed Aug 3, 2023
1 parent eecc254 commit 233cb44
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,19 @@ def _get_measures(self, unparsed_measures: List[UnparsedMeasure]) -> List[Measur
)
return measures

def _create_metric(self, measure: UnparsedMeasure, enabled: bool) -> None:
unparsed_metric = UnparsedMetric(
name=measure.name,
label=measure.name,
type="simple",
type_params=UnparsedMetricTypeParams(measure=measure.name, expr=measure.name),
description=measure.description or f"Metric created from measure {measure.name}",
config={"enabled": enabled},
)

parser = MetricParser(self.schema_parser, yaml=self.yaml)
parser.parse_metric(unparsed=unparsed_metric)

def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
package_name = self.project.project_name
unique_id = f"{NodeType.SemanticModel}.{package_name}.{unparsed.name}"
Expand Down Expand Up @@ -550,6 +563,11 @@ def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
# No ability to disable a semantic model at this time
self.manifest.add_semantic_model(self.yaml.file, parsed)

# Create a metric for each measure with `create_metric = True`
for measure in unparsed.measures:
if measure.create_metric is True:
self._create_metric(measure=measure, enabled=parsed.config.enabled)

def parse(self):
for data in self.get_key_dicts():
try:
Expand Down

0 comments on commit 233cb44

Please sign in to comment.