From 79093d1f04da3e3fc57d0100bd00fe7bca99479b Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Thu, 7 Dec 2023 15:49:00 -0800 Subject: [PATCH] Expose saved queries in MFEngine --- metricflow/engine/metricflow_engine.py | 9 ++++++++- metricflow/engine/models.py | 28 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/metricflow/engine/metricflow_engine.py b/metricflow/engine/metricflow_engine.py index 76bd8d9261..6cd6d5a5fa 100644 --- a/metricflow/engine/metricflow_engine.py +++ b/metricflow/engine/metricflow_engine.py @@ -27,7 +27,7 @@ from metricflow.dataset.convert_semantic_model import SemanticModelToDataSetConverter from metricflow.dataset.dataset import DataSet from metricflow.dataset.semantic_model_adapter import SemanticModelDataSet -from metricflow.engine.models import Dimension, Entity, Measure, Metric +from metricflow.engine.models import Dimension, Entity, Measure, Metric, SavedQuery from metricflow.engine.time_source import ServerTimeSource from metricflow.errors.errors import ExecutionException from metricflow.execution.execution_plan import ExecutionPlan, SqlQuery @@ -679,6 +679,13 @@ def list_metrics(self) -> List[Metric]: # noqa: D for metric in metrics ] + @log_call(module_name=__name__, telemetry_reporter=_telemetry_reporter) + def list_saved_queries(self) -> List[SavedQuery]: # noqa: D + return [ + SavedQuery.from_pydantic(saved_query) + for saved_query in self._semantic_manifest_lookup.semantic_manifest.saved_queries + ] + @log_call(module_name=__name__, telemetry_reporter=_telemetry_reporter) def get_dimension_values( # noqa: D self, diff --git a/metricflow/engine/models.py b/metricflow/engine/models.py index 979493978c..b984e597e7 100644 --- a/metricflow/engine/models.py +++ b/metricflow/engine/models.py @@ -16,6 +16,12 @@ from dbt_semantic_interfaces.protocols.metadata import Metadata from dbt_semantic_interfaces.protocols.metric import Metric as SemanticManifestMetric from dbt_semantic_interfaces.protocols.metric import MetricInputMeasure, MetricType, MetricTypeParams +from dbt_semantic_interfaces.protocols.saved_query import ( + SavedQuery as SemanticManifestSavedQuery, +) +from dbt_semantic_interfaces.protocols.saved_query import ( + SavedQueryQueryParams, +) from dbt_semantic_interfaces.protocols.where_filter import WhereFilterIntersection from dbt_semantic_interfaces.transformations.add_input_metric_measures import AddInputMetricMeasuresRule from dbt_semantic_interfaces.type_enums.aggregation_type import AggregationType @@ -144,3 +150,25 @@ class Measure: description: Optional[str] = None expr: Optional[str] = None agg_params: Optional[MeasureAggregationParameters] = None + + +@dataclass(frozen=True) +class SavedQuery: + """Dataclass representation of a SavedQuery.""" + + name: str + description: Optional[str] + label: Optional[str] + query_params: SavedQueryQueryParams + metadata: Optional[Metadata] + + @classmethod + def from_pydantic(cls, pydantic_saved_query: SemanticManifestSavedQuery) -> SavedQuery: + """Build from pydantic SavedQuery object.""" + return cls( + name=pydantic_saved_query.name, + description=pydantic_saved_query.description, + label=pydantic_saved_query.label, + query_params=pydantic_saved_query.query_params, + metadata=pydantic_saved_query.metadata, + )