Skip to content

Commit

Permalink
WIP - enable entity object syntax in metric filters
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 5, 2024
1 parent 3b1eeec commit e61836c
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Sequence
from typing import Sequence, Union

from dbt_semantic_interfaces.call_parameter_sets import (
MetricCallParameterSet,
Expand All @@ -19,6 +19,8 @@
from metricflow_semantics.specs.column_assoc import ColumnAssociationResolver
from metricflow_semantics.specs.rendered_spec_tracker import RenderedSpecTracker

from metricflow_semantics.specs.where_filter_entity import WhereFilterEntity


class WhereFilterMetric(ProtocolHint[QueryInterfaceMetric]):
"""A metric that is passed in through the where filter parameter."""
Expand Down Expand Up @@ -94,13 +96,21 @@ def __init__( # noqa
self._where_filter_location = where_filter_location
self._rendered_spec_tracker = rendered_spec_tracker

def create(self, metric_name: str, group_by: Sequence[str] = ()) -> WhereFilterMetric:
def create(self, metric_name: str, group_by: Sequence[Union[str, WhereFilterEntity]] = ()) -> WhereFilterMetric:
"""Create a WhereFilterMetric."""
return WhereFilterMetric(
column_association_resolver=self._column_association_resolver,
resolved_spec_lookup=self._resolved_spec_lookup,
where_filter_location=self._where_filter_location,
rendered_spec_tracker=self._rendered_spec_tracker,
element_name=metric_name,
group_by=tuple(LinkableElementReference(group_by_name.lower()) for group_by_name in group_by),
group_by=tuple(
LinkableElementReference(
# TODO: add entity links
group_by_item.lower()
if isinstance(group_by_item, str)
else group_by_item._element_name
)
for group_by_item in group_by
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,11 @@ metric:
denominator:
name: listings
filter: "{{ Metric('views', ['listing']) }} > 10"
---
metric:
name: really_active_listings
description: Listings with at least 5 bookings
type: simple
type_params:
measure: listings
filter: "{{ Metric('bookings', [Entity('listing')]) }} > 5"
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
'listing__nested_fill_nulls_without_time_spine',
'listing__non_referred_bookings_pct',
'listing__popular_listing_bookings_per_booker',
'listing__really_active_listings',
'listing__referred_bookings',
'listing__smallest_listing',
'listing__twice_bookings_fill_nulls_with_0_without_time_spine',
Expand Down Expand Up @@ -291,6 +292,7 @@
'user__listing__user__nested_fill_nulls_without_time_spine',
'user__listing__user__non_referred_bookings_pct',
'user__listing__user__popular_listing_bookings_per_booker',
'user__listing__user__really_active_listings',
'user__listing__user__referred_bookings',
'user__listing__user__smallest_listing',
'user__listing__user__twice_bookings_fill_nulls_with_0_without_time_spine',
Expand All @@ -299,6 +301,7 @@
'user__listings',
'user__lux_listings',
'user__popular_listing_bookings_per_booker',
'user__really_active_listings',
'user__regional_starting_balance_ratios',
'user__revenue',
'user__revenue_all_time',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('listing',)", "('listing',)") nested_fill_nulls_without_time_spine ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") non_referred_bookings_pct ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") popular_listing_bookings_per_booker ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") really_active_listings ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") referred_bookings ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") smallest_listing ['JOINED', 'METRIC']
('listings_latest',) ("('listing',)", "('listing',)") twice_bookings_fill_nulls_with_0_without_time_spine ['JOINED', 'METRIC']
Expand Down Expand Up @@ -168,6 +169,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('user',)", "('listing', 'user')") nested_fill_nulls_without_time_spine ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") non_referred_bookings_pct ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") popular_listing_bookings_per_booker ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") really_active_listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") referred_bookings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") smallest_listing ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('listing', 'user')") twice_bookings_fill_nulls_with_0_without_time_spine ['JOINED', 'METRIC']
Expand All @@ -183,6 +185,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('user',)", "('user',)") listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") lux_listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") popular_listing_bookings_per_booker ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") really_active_listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") regional_starting_balance_ratios ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") revenue ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") revenue_all_time ['JOINED', 'METRIC']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
'company__user__company__listings',
'company__user__company__lux_listings',
'company__user__company__popular_listing_bookings_per_booker',
'company__user__company__really_active_listings',
'company__user__company__regional_starting_balance_ratios',
'company__user__company__revenue',
'company__user__company__revenue_all_time',
Expand Down Expand Up @@ -361,6 +362,7 @@
'listing__nested_fill_nulls_without_time_spine',
'listing__non_referred_bookings_pct',
'listing__popular_listing_bookings_per_booker',
'listing__really_active_listings',
'listing__referred_bookings',
'listing__smallest_listing',
'listing__twice_bookings_fill_nulls_with_0_without_time_spine',
Expand Down Expand Up @@ -414,6 +416,7 @@
'lux_listing__listing__lux_listing__nested_fill_nulls_without_time_spine',
'lux_listing__listing__lux_listing__non_referred_bookings_pct',
'lux_listing__listing__lux_listing__popular_listing_bookings_per_booker',
'lux_listing__listing__lux_listing__really_active_listings',
'lux_listing__listing__lux_listing__referred_bookings',
'lux_listing__listing__lux_listing__smallest_listing',
'lux_listing__listing__lux_listing__twice_bookings_fill_nulls_with_0_without_time_spine',
Expand Down Expand Up @@ -525,6 +528,7 @@
'user__listing__user__nested_fill_nulls_without_time_spine',
'user__listing__user__non_referred_bookings_pct',
'user__listing__user__popular_listing_bookings_per_booker',
'user__listing__user__really_active_listings',
'user__listing__user__referred_bookings',
'user__listing__user__smallest_listing',
'user__listing__user__twice_bookings_fill_nulls_with_0_without_time_spine',
Expand All @@ -533,6 +537,7 @@
'user__listings',
'user__lux_listings',
'user__popular_listing_bookings_per_booker',
'user__really_active_listings',
'user__regional_starting_balance_ratios',
'user__revenue',
'user__revenue_all_time',
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ classifiers = [
dependencies = [
"Jinja2>=3.1.3",
"PyYAML>=6.0, <7.0.0",
"dbt-semantic-interfaces>=0.5.1, <0.6.0",
"graphviz>=0.18.2, <0.21",
"more-itertools>=8.10.0, <10.2.0",
"pydantic>=1.10.0, <1.11.0",
Expand Down Expand Up @@ -114,6 +113,7 @@ description = "Environment for development. Includes a DuckDB-backed client."
pre-install-commands = [
"pip install -e ./metricflow-semantics",
"pip install -e ./dbt-metricflow[duckdb]",
"pip install -e ../dbt-semantic-interfaces",
]

features = [
Expand Down
Loading

0 comments on commit e61836c

Please sign in to comment.