Skip to content

Commit

Permalink
Remove metrics that must be queried with metric_time from linkable me…
Browse files Browse the repository at this point in the history
…tric options
  • Loading branch information
courtneyholcomb committed May 3, 2024
1 parent 737e1cc commit 4a824cb
Show file tree
Hide file tree
Showing 6 changed files with 712 additions and 1,015 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from dbt_semantic_interfaces.enum_extension import assert_values_exhausted
from dbt_semantic_interfaces.protocols.dimension import Dimension, DimensionType
from dbt_semantic_interfaces.protocols.metric import Metric
from dbt_semantic_interfaces.protocols.semantic_manifest import SemanticManifest
from dbt_semantic_interfaces.protocols.semantic_model import SemanticModel
from dbt_semantic_interfaces.references import (
Expand Down Expand Up @@ -137,12 +138,14 @@ def __init__(
self._entity_to_semantic_model[entity.reference.element_name].append(semantic_model)

self._metric_to_linkable_element_sets: Dict[str, List[LinkableElementSet]] = {}
self._metric_references_to_metrics: Dict[MetricReference, Metric] = {}
self._joinable_metrics_for_entities: Dict[EntityReference, Set[MetricSubqueryJoinPathElement]] = defaultdict(
set
)

start_time = time.time()
for metric in self._semantic_manifest.metrics:
self._metric_references_to_metrics[MetricReference(metric.name)] = metric
linkable_sets_for_measure = []
for measure in metric.measure_references:
# Cumulative metrics currently can't be queried by other time granularities.
Expand Down Expand Up @@ -180,8 +183,13 @@ def __init__(

self._metric_to_linkable_element_sets[metric.name] = linkable_sets_for_measure

# This loop must happen after the one above so that _metric_to_linkable_element_sets is populated.
# Populate storage dicts with linkable metrics. This loop must happen after the one above so that
# _metric_to_linkable_element_sets is populated with entities and dimensions.
for metric in self._semantic_manifest.metrics:
# Cumulative metrics and time offset metrics require grouping by metric_time, which is not yet available for
# linkable metrics. So skip those.
if self._metric_requires_metric_time(metric):
continue
metric_reference = MetricReference(metric.name)
linkable_element_set_for_metric = self.get_linkable_elements_for_metrics([metric_reference])
for linkable_entities in linkable_element_set_for_metric.path_key_to_linkable_entities.values():
Expand All @@ -197,6 +205,7 @@ def __init__(
self._joinable_metrics_for_entities[linkable_entity.reference].add(
metric_subquery_join_path_element
)
# TODO: update _metric_to_linkable_element_sets to have linkable metrics

# If no metrics are specified, the query interface supports querying distinct values for dimensions, entities,
# and group by metrics.
Expand All @@ -214,6 +223,27 @@ def __init__(

logger.info(f"Building valid group-by-item indexes took: {time.time() - start_time:.2f}s")

def _metric_requires_metric_time(self, metric: Metric) -> bool:
"""Checks if the metric can only be queried with metric_time. Also checks input metrics.
True if the metric uses cumulative time component or a time offset.
"""
metrics_to_check = [metric]
while metrics_to_check:
metric_to_check = metrics_to_check.pop()
if metric_to_check.type_params.window is not None or metric_to_check.type_params.grain_to_date is not None:
return True
for input_metric in metric_to_check.input_metrics:
if input_metric.offset_window is not None or input_metric.offset_to_grain is not None:
return True
metric_for_input_metric = self._metric_references_to_metrics.get(MetricReference(input_metric.name))
assert (
metric_for_input_metric
), f"Did not find input metric {input_metric.name} in registered metrics. This indicates internal misconfiguration."
metrics_to_check.append(metric_for_input_metric)

return False

def _get_semantic_model_for_measure(self, measure_reference: MeasureReference) -> SemanticModel:
semantic_models_where_measure_was_found = []
for semantic_model in self._semantic_models:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
'listing__bookers',
'listing__booking_fees',
'listing__booking_fees',
'listing__booking_fees_last_week_per_booker_this_week',
'listing__booking_fees_last_week_per_booker_this_week',
'listing__booking_fees_per_booker',
'listing__booking_fees_per_booker',
'listing__booking_fees_since_start_of_month',
'listing__booking_fees_since_start_of_month',
'listing__booking_payments',
'listing__booking_payments',
'listing__booking_value',
Expand All @@ -34,30 +30,12 @@
'listing__booking_value_sub_instant_add_10',
'listing__bookings',
'listing__bookings',
'listing__bookings_5_day_lag',
'listing__bookings_5_day_lag',
'listing__bookings_at_start_of_month',
'listing__bookings_at_start_of_month',
'listing__bookings_fill_nulls_with_0',
'listing__bookings_fill_nulls_with_0',
'listing__bookings_fill_nulls_with_0_without_time_spine',
'listing__bookings_fill_nulls_with_0_without_time_spine',
'listing__bookings_growth_2_weeks',
'listing__bookings_growth_2_weeks',
'listing__bookings_growth_2_weeks_fill_nulls_with_0',
'listing__bookings_growth_2_weeks_fill_nulls_with_0',
'listing__bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset',
'listing__bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset',
'listing__bookings_growth_since_start_of_month',
'listing__bookings_growth_since_start_of_month',
'listing__bookings_join_to_time_spine',
'listing__bookings_join_to_time_spine',
'listing__bookings_month_start_compared_to_1_month_prior',
'listing__bookings_month_start_compared_to_1_month_prior',
'listing__bookings_offset_once',
'listing__bookings_offset_once',
'listing__bookings_offset_twice',
'listing__bookings_offset_twice',
'listing__bookings_per_booker',
'listing__bookings_per_booker',
'listing__bookings_per_dollar',
Expand Down Expand Up @@ -115,12 +93,6 @@
'listing__ds__quarter',
'listing__ds__week',
'listing__ds__year',
'listing__every_2_days_bookers_2_days_ago',
'listing__every_2_days_bookers_2_days_ago',
'listing__every_two_days_bookers',
'listing__every_two_days_bookers',
'listing__every_two_days_bookers_fill_nulls_with_0',
'listing__every_two_days_bookers_fill_nulls_with_0',
'listing__instant_booking_fraction_of_max_value',
'listing__instant_booking_fraction_of_max_value',
'listing__instant_booking_value',
Expand Down Expand Up @@ -148,9 +120,7 @@
'listing__lux_listing__average_instant_booking_value',
'listing__lux_listing__bookers',
'listing__lux_listing__booking_fees',
'listing__lux_listing__booking_fees_last_week_per_booker_this_week',
'listing__lux_listing__booking_fees_per_booker',
'listing__lux_listing__booking_fees_since_start_of_month',
'listing__lux_listing__booking_payments',
'listing__lux_listing__booking_value',
'listing__lux_listing__booking_value_for_non_null_listing_id',
Expand All @@ -159,18 +129,9 @@
'listing__lux_listing__booking_value_sub_instant',
'listing__lux_listing__booking_value_sub_instant_add_10',
'listing__lux_listing__bookings',
'listing__lux_listing__bookings_5_day_lag',
'listing__lux_listing__bookings_at_start_of_month',
'listing__lux_listing__bookings_fill_nulls_with_0',
'listing__lux_listing__bookings_fill_nulls_with_0_without_time_spine',
'listing__lux_listing__bookings_growth_2_weeks',
'listing__lux_listing__bookings_growth_2_weeks_fill_nulls_with_0',
'listing__lux_listing__bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset',
'listing__lux_listing__bookings_growth_since_start_of_month',
'listing__lux_listing__bookings_join_to_time_spine',
'listing__lux_listing__bookings_month_start_compared_to_1_month_prior',
'listing__lux_listing__bookings_offset_once',
'listing__lux_listing__bookings_offset_twice',
'listing__lux_listing__bookings_per_booker',
'listing__lux_listing__bookings_per_dollar',
'listing__lux_listing__bookings_per_listing',
Expand All @@ -180,9 +141,6 @@
'listing__lux_listing__derived_bookings_1',
'listing__lux_listing__discrete_booking_value_p99',
'listing__lux_listing__double_counted_delayed_bookings',
'listing__lux_listing__every_2_days_bookers_2_days_ago',
'listing__lux_listing__every_two_days_bookers',
'listing__lux_listing__every_two_days_bookers_fill_nulls_with_0',
'listing__lux_listing__instant_booking_fraction_of_max_value',
'listing__lux_listing__instant_booking_value',
'listing__lux_listing__instant_booking_value_ratio',
Expand Down Expand Up @@ -253,9 +211,7 @@
'user__average_instant_booking_value',
'user__bookers',
'user__booking_fees',
'user__booking_fees_last_week_per_booker_this_week',
'user__booking_fees_per_booker',
'user__booking_fees_since_start_of_month',
'user__booking_payments',
'user__booking_value',
'user__booking_value_for_non_null_listing_id',
Expand All @@ -264,18 +220,9 @@
'user__booking_value_sub_instant',
'user__booking_value_sub_instant_add_10',
'user__bookings',
'user__bookings_5_day_lag',
'user__bookings_at_start_of_month',
'user__bookings_fill_nulls_with_0',
'user__bookings_fill_nulls_with_0_without_time_spine',
'user__bookings_growth_2_weeks',
'user__bookings_growth_2_weeks_fill_nulls_with_0',
'user__bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset',
'user__bookings_growth_since_start_of_month',
'user__bookings_join_to_time_spine',
'user__bookings_month_start_compared_to_1_month_prior',
'user__bookings_offset_once',
'user__bookings_offset_twice',
'user__bookings_per_booker',
'user__bookings_per_dollar',
'user__bookings_per_listing',
Expand All @@ -289,9 +236,7 @@
'user__company__average_instant_booking_value',
'user__company__bookers',
'user__company__booking_fees',
'user__company__booking_fees_last_week_per_booker_this_week',
'user__company__booking_fees_per_booker',
'user__company__booking_fees_since_start_of_month',
'user__company__booking_payments',
'user__company__booking_value',
'user__company__booking_value_for_non_null_listing_id',
Expand All @@ -300,18 +245,9 @@
'user__company__booking_value_sub_instant',
'user__company__booking_value_sub_instant_add_10',
'user__company__bookings',
'user__company__bookings_5_day_lag',
'user__company__bookings_at_start_of_month',
'user__company__bookings_fill_nulls_with_0',
'user__company__bookings_fill_nulls_with_0_without_time_spine',
'user__company__bookings_growth_2_weeks',
'user__company__bookings_growth_2_weeks_fill_nulls_with_0',
'user__company__bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset',
'user__company__bookings_growth_since_start_of_month',
'user__company__bookings_join_to_time_spine',
'user__company__bookings_month_start_compared_to_1_month_prior',
'user__company__bookings_offset_once',
'user__company__bookings_offset_twice',
'user__company__bookings_per_booker',
'user__company__bookings_per_dollar',
'user__company__bookings_per_view',
Expand All @@ -320,9 +256,6 @@
'user__company__derived_bookings_1',
'user__company__discrete_booking_value_p99',
'user__company__double_counted_delayed_bookings',
'user__company__every_2_days_bookers_2_days_ago',
'user__company__every_two_days_bookers',
'user__company__every_two_days_bookers_fill_nulls_with_0',
'user__company__identity_verifications',
'user__company__instant_booking_fraction_of_max_value',
'user__company__instant_booking_value',
Expand All @@ -344,11 +277,8 @@
'user__company__regional_starting_balance_ratios',
'user__company__revenue',
'user__company__revenue_all_time',
'user__company__revenue_mtd',
'user__company__smallest_listing',
'user__company__total_account_balance_first_day',
'user__company__trailing_2_months_revenue',
'user__company__trailing_2_months_revenue_sub_10',
'user__company__twice_bookings_fill_nulls_with_0_without_time_spine',
'user__company__views',
'user__company__views',
Expand Down Expand Up @@ -445,9 +375,6 @@
'user__ds_partitioned__quarter',
'user__ds_partitioned__week',
'user__ds_partitioned__year',
'user__every_2_days_bookers_2_days_ago',
'user__every_two_days_bookers',
'user__every_two_days_bookers_fill_nulls_with_0',
'user__home_state',
'user__home_state_latest',
'user__identity_verifications',
Expand Down Expand Up @@ -478,16 +405,10 @@
'user__revenue',
'user__revenue_all_time',
'user__revenue_all_time',
'user__revenue_mtd',
'user__revenue_mtd',
'user__smallest_listing',
'user__smallest_listing',
'user__total_account_balance_first_day',
'user__total_account_balance_first_day',
'user__trailing_2_months_revenue',
'user__trailing_2_months_revenue',
'user__trailing_2_months_revenue_sub_10',
'user__trailing_2_months_revenue_sub_10',
'user__twice_bookings_fill_nulls_with_0_without_time_spine',
'user__views',
'user__views',
Expand Down
Loading

0 comments on commit 4a824cb

Please sign in to comment.