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 Conversion Metrics Properties #209

Merged
merged 9 commits into from
Nov 29, 2023
Merged
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
35 changes: 33 additions & 2 deletions dbt_semantic_interfaces/implementations/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
)
from dbt_semantic_interfaces.implementations.metadata import PydanticMetadata
from dbt_semantic_interfaces.references import MeasureReference, MetricReference
from dbt_semantic_interfaces.type_enums import MetricType, TimeGranularity
from dbt_semantic_interfaces.type_enums import (
ConversionCalculationType,
MetricType,
TimeGranularity,
)


class PydanticMetricInputMeasure(PydanticCustomInputParser, HashableBaseModel):
Expand Down Expand Up @@ -134,6 +138,26 @@ def post_aggregation_reference(self) -> MetricReference:
return MetricReference(element_name=self.alias or self.name)


class PydanticConversionTypeParams(HashableBaseModel):
"""Type params to provide context for conversion metrics properties."""

base_measure: PydanticMetricInputMeasure
conversion_measure: PydanticMetricInputMeasure
entity: str
calculation: ConversionCalculationType = ConversionCalculationType.CONVERSION_RATE
window: Optional[PydanticMetricTimeWindow]

@property
def base_measure_reference(self) -> MeasureReference:
"""Return the measure reference associated with the base measure."""
return self.base_measure.measure_reference

@property
def conversion_measure_reference(self) -> MeasureReference:
"""Return the measure reference associated with the conversion measure."""
return self.conversion_measure.measure_reference


class PydanticMetricTypeParams(HashableBaseModel):
"""Type params add additional context to certain metric types (the context depends on the metric type)."""

Expand All @@ -144,9 +168,16 @@ class PydanticMetricTypeParams(HashableBaseModel):
window: Optional[PydanticMetricTimeWindow]
grain_to_date: Optional[TimeGranularity]
metrics: Optional[List[PydanticMetricInput]]
conversion_type_params: Optional[PydanticConversionTypeParams]

input_measures: List[PydanticMetricInputMeasure] = Field(default_factory=list)

@property
def conversion_type_params_or_error(self) -> PydanticConversionTypeParams: # noqa: D
if self.conversion_type_params is None:
raise ValueError("Expected conversion_type_params to be not None.")
return self.conversion_type_params


class PydanticMetric(HashableBaseModel, ModelWithMetadataParsing):
"""Describes a metric."""
Expand All @@ -172,7 +203,7 @@ def measure_references(self) -> List[MeasureReference]:
@property
def input_metrics(self) -> Sequence[PydanticMetricInput]:
"""Return the associated input metrics for this metric."""
if self.type is MetricType.SIMPLE or self.type is MetricType.CUMULATIVE:
if self.type is MetricType.SIMPLE or self.type is MetricType.CUMULATIVE or self.type is MetricType.CONVERSION:
return ()
elif self.type is MetricType.DERIVED:
assert self.type_params.metrics is not None, f"{MetricType.DERIVED} should have type_params.metrics set"
Expand Down
11 changes: 5 additions & 6 deletions dbt_semantic_interfaces/protocols/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ def metrics(self) -> Optional[Sequence[MetricInput]]: # noqa: D
def conversion_type_params(self) -> Optional[ConversionTypeParams]: # noqa: D
pass

@property
@abstractmethod
def conversion_type_params_or_error(self) -> ConversionTypeParams: # noqa: D
WilliamDee marked this conversation as resolved.
Show resolved Hide resolved
pass


class Metric(Protocol):
"""Describes a metric."""
Expand Down Expand Up @@ -270,9 +275,3 @@ def metadata(self) -> Optional[Metadata]: # noqa: D
def label(self) -> Optional[str]:
"""Returns a string representing a human readable label for the metric."""
pass

@property
@abstractmethod
def conversion_params(self) -> ConversionTypeParams:
"""Accessor for conversion type params, enforces that it's set."""
pass