Skip to content

Commit

Permalink
Add ratio type information to metric structure
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Sep 5, 2023
1 parent 48ed186 commit bc1b6c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions piperider_cli/dbtutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,9 @@ def _create_metric(name, filter=None, alias=None, derived_metric=None):
time_grains = derived_time_grains

m = Metric(metric.get('name'),
calculation_method='derived',
expression=f"{numerator.get('name')} / {denominator.get('name')}",
calculation_method='ratio',
numerator=numerator.get('name'),
denominator=denominator.get('name'),
time_grains=time_grains,
label=metric.get('label'), description=metric.get('description'), ref_metrics=ref_metrics,
ref_id=metric.get('unique_id'))
Expand Down
11 changes: 9 additions & 2 deletions piperider_cli/metrics_engine/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def __init__(
calculation_method=None,
time_grains=None,
expression: str = None,
numerator: str = None,
denominator: str = None,
label=None,
description=None,
ref_metrics=None,
Expand All @@ -63,6 +65,8 @@ def __init__(
self.calculation_method = calculation_method
self.time_grains = time_grains
self.expression = expression
self.numerator = numerator
self.denominator = denominator
self.label = label
self.description = description
self.ref_metrics: List[Metric] = ref_metrics or []
Expand Down Expand Up @@ -111,7 +115,7 @@ def _compose_query_name(grain: str, dimensions: List[str], label=False) -> str:
def _get_query_stmt(self, metric: Metric, grain: str, dimension: List[str], date_spine_model: CTE):
metric_column_name = metric.name

if metric.calculation_method == 'derived':
if metric.calculation_method == 'derived' or metric.calculation_method == 'ratio':

Check warning on line 118 in piperider_cli/metrics_engine/metrics.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/metrics_engine/metrics.py#L118

Added line #L118 was not covered by tests
selectable = None

# Join all parent metrics
Expand All @@ -123,8 +127,11 @@ def _get_query_stmt(self, metric: Metric, grain: str, dimension: List[str], date
else:
selectable = join(selectable, cte, selectable.c.d == cte.c.d)

# a / b / c -> a / nullif(b, 0) / nullif(c, 0)
expression = metric.expression
if metric.calculation_method == 'ratio':
expression = f"{metric.numerator}/{metric.denominator}"

Check warning on line 132 in piperider_cli/metrics_engine/metrics.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/metrics_engine/metrics.py#L131-L132

Added lines #L131 - L132 were not covered by tests

# a / b / c -> a / nullif(b, 0) / nullif(c, 0)
if '/' in expression:
expression_list = expression.split('/')
dividend = expression_list[0]
Expand Down

0 comments on commit bc1b6c3

Please sign in to comment.