Skip to content

Commit

Permalink
Separate the metric name and metric id
Browse files Browse the repository at this point in the history
  • Loading branch information
dyang415 committed Sep 15, 2023
1 parent 6065c2a commit 633c0c9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/app/insight/datasource/bqMetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def build_metrics(self,
df: pd.DataFrame,
value_by_date_df: pd.DataFrame) -> MetricInsight:
insight = MetricInsight()
insight.name = metric.get_id()
insight.name = metric.get_display_name()
insight.keyDimensions = self.find_key_dimensions(df)
insight.dimensions = self._get_dimensions(df)
insight.totalSegments = calculate_total_segments(insight.dimensions)
Expand Down
3 changes: 2 additions & 1 deletion backend/app/insight/services/insight_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def gen_value_by_date(self, df: polars.DataFrame, metric: Metric):

def build_metric_insight(self, metric: Metric, parent_metric: Optional[Metric] = None) -> MetricInsight:
insight = MetricInsight()
insight.name = metric.get_id()
insight.id = metric.get_id()
insight.name = metric.get_display_name()
insight.filters = metric.filters if isinstance(
metric, SingleColumnMetric) else None
insight.baselineNumRows = self.overall_aggregated_df['count_baseline'].sum()
Expand Down
19 changes: 16 additions & 3 deletions backend/app/insight/services/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def get_aggregation_exprs(self, agg_override: Optional[AggregateMethod] = None)
def get_id(self):
pass

@abstractmethod
def get_display_name(self):
pass

@abstractmethod
def get_metric_type(self):
pass
Expand All @@ -106,15 +110,21 @@ def get_id(self):
if self.name is not None:
return self.name

filters_hash = ""
filters_hash_suffix = ""
if len(self.filters) > 0:
filters_in_json = orjson.dumps(
self.filters, option=orjson.OPT_SORT_KEYS)
hasher = hashlib.sha1()
hasher.update(filters_in_json)
filters_hash = f"_{hasher.hexdigest()[:6]}"
filters_hash_suffix = f"_{hasher.hexdigest()[:6]}"

return f"{self.column}_{self.aggregate_method.name}{filters_hash}"
return f"{self.column}_{self.aggregate_method.name}{filters_hash_suffix}"

def get_display_name(self):
if self.name is not None:
return self.name

return f"{self.aggregate_method.name} {self.column}"

def get_aggregation_exprs(self, agg_override: Optional[AggregateMethod] = None) -> Iterable[Expr]:
col = pl.col(self.column)
Expand Down Expand Up @@ -147,6 +157,9 @@ def __post_init__(self):
self.numerator_metric.name = f"{self.name} numerator"
self.denominator_metric.name = f"{self.name} denominator"

def get_display_name(self):
return self.name

def get_id(self):
return self.name

Expand Down
1 change: 1 addition & 0 deletions frontend/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface InsightMetric {
id: string;
name: string;
keyDimensions: string[];
totalSegments: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function DimensionSliceDetailModalMetricCard({
return null;
}

const relatedSegments = relatedSegmentsByMetricName[metric.name];
const relatedSegments = relatedSegmentsByMetricName[metric.id];

const maxImpact = Math.max(
...relatedSegments.map((info) => Math.abs(info.impact))
Expand Down

0 comments on commit 633c0c9

Please sign in to comment.