Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: sqlmesh key_metrics model #2584

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions warehouse/metrics_tools/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,17 @@ def generate_dependency_refs_for_name(self, name: str):
time_aggregation=time_aggregation,
)
)
# if there is no _source.time_aggregations or _source.rolling
# means it is a point in time metric aka key metric over the
# whole time period for the specific entity type
if not self._source.time_aggregations and not self._source.rolling:
refs.append(
PeerMetricDependencyRef(
name=name,
entity_type=entity,
time_aggregation="overall",
)
)
return refs

@property
Expand All @@ -449,8 +460,9 @@ def metric_type(self):
return "time_aggregation"
elif self._source.rolling is not None:
return "rolling"
# This _shouldn't_ happen
raise Exception("unknown metric type")
# If neither time_aggregations or rolling is set then it is a point in
# time metric
return "overall"

def generate_query_ref(
self,
Expand Down
38 changes: 38 additions & 0 deletions warehouse/metrics_tools/factory/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ def generate_model_for_rendered_query(
self.generate_time_aggregation_model_for_rendered_query(
calling_file, query_config, dependencies
)
case "overall":
self.generate_point_in_time_model_for_rendered_query(
calling_file, query_config, dependencies
)

def generate_rolling_python_model_for_rendered_query(
self,
Expand Down Expand Up @@ -546,6 +550,40 @@ def generate_time_aggregation_model_for_rendered_query(
partitioned_by=partitioned_by,
)

def generate_point_in_time_model_for_rendered_query(
self,
calling_file: str,
query_config: MetricQueryConfig,
dependencies: t.Set[str],
):
"""Generate model for point in time models"""
config = self.serializable_config(query_config)
ref = query_config["ref"]
columns = METRICS_COLUMNS_BY_ENTITY[ref["entity_type"]]

grain = [
"metric",
f"to_{ref['entity_type']}_id",
"from_artifact_id",
"event_source",
"metrics_sample_date",
]

GeneratedModel.create(
func=generated_query,
entrypoint_path=calling_file,
config=config,
name=f"{self.catalog}.{query_config['table_name']}",
kind=ModelKindName.VIEW,
Jabolol marked this conversation as resolved.
Show resolved Hide resolved
dialect="clickhouse",
columns=columns,
grain=grain,
start=self._raw_options["start"],
# since this query's over the full time range, we don't expose access to the
# macros used to get information about the specific time
additional_macros=[],
Jabolol marked this conversation as resolved.
Show resolved Hide resolved
)

def serializable_config(self, query_config: MetricQueryConfig):
# Use a simple python sql model to generate the time_aggregation model
config: t.Dict[str, t.Any] = t.cast(dict, query_config.copy())
Expand Down
Loading