Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(experiments): remove unused get_metric and count saved metrics in run telemetry (#109314)

Anders88516c9351c6
diff --git a/posthog/temporal/experiments/utils.py b/posthog/temporal/experiments/utils.py
index 48701a55c757..74a8d5fe5470 100644
--- a/posthog/temporal/experiments/utils.py
+++ b/posthog/temporal/experiments/utils.py
@@ -1,12 +1,9 @@
from datetime import datetime
-from typing import Union
from django.db.models import Q
import structlog
-from posthog.schema import ExperimentFunnelMetric, ExperimentMeanMetric, ExperimentRatioMetric
-
from posthog.cdp.internal_events import InternalEventEvent, produce_internal_event
from products.experiments.backend.facade.timeseries import resolve_saved_metric_definition
@@ -40,18 +37,6 @@ def recalculation_hour_filter(hour: int) -> Q:
return match
-def get_metric(metric_data: dict) -> Union[ExperimentMeanMetric, ExperimentFunnelMetric, ExperimentRatioMetric]:
- metric_type = metric_data.get("metric_type")
- if metric_type == "mean":
- return ExperimentMeanMetric(**metric_data)
- elif metric_type == "funnel":
- return ExperimentFunnelMetric(**metric_data)
- elif metric_type == "ratio":
- return ExperimentRatioMetric(**metric_data)
- else:
- raise ValueError(f"Unknown metric type: {metric_type}")
-
-
def _get_significant_variant_keys(result_dict: dict) -> set[str]:
variant_results = result_dict.get("variant_results") or []
return {v["key"] for v in variant_results if v.get("significant")}
diff --git a/products/experiments/backend/temporal/recalculation_logic.py b/products/experiments/backend/temporal/recalculation_logic.py
index 755a5216e9cf..9fd45210a4c1 100644
--- a/products/experiments/backend/temporal/recalculation_logic.py
+++ b/products/experiments/backend/temporal/recalculation_logic.py
@@ -348,8 +348,11 @@ def _capture_results_refresh_completed(update: RecalculationProgressUpdate) -> N
experiment_duration_hours = (
round((timezone.now() - experiment.start_date).total_seconds() / 3600) if experiment.start_date else None
)
- primary_metrics_count = len(experiment.metrics or [])
- secondary_metrics_count = len(experiment.metrics_secondary or [])
+ # Count from the discovery list that also sets total_metrics, so saved metrics count and legacy
+ # metrics that the run never calculates do not.
+ roles = [metric.metric_type for metric in discover_experiment_metrics(experiment)]
+ primary_metrics_count = roles.count("primary")
+ secondary_metrics_count = roles.count("secondary")
# Global client, like most Temporal workflows: the worker is long-lived, so its background flush
# runs fine, and a scoped client's synchronous shutdown stalls the activity (~2x flush_interval).
posthoganalytics.capture(
diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.py
index 0642db09c0ce..ffcf9dd9ae9a 100644
--- a/products/experiments/backend/temporal/test_recalculation_activities.py
+++ b/products/experiments/backend/temporal/test_recalculation_activities.py
@@ -1646,8 +1646,11 @@ class TestRecalculationAnalytics(BaseTest):
assert props["trigger"] == "manual"
def test_results_refresh_completed_event_on_finish(self):
- exp = self._experiment(flag_key="an-finish", metrics=[_mean_metric("m1")])
- recalc = self._recalc(exp, metric_uuids=["m1"])
+ exp = self._experiment(flag_key="an-finish", metrics=[_mean_metric("m1")], secondary=[_mean_metric("s1")])
+ for uuid, role in [("sp1", "primary"), ("ss1", "secondary")]:
+ saved = ExperimentSavedMetric.objects.create(team=self.team, name=f"saved-{uuid}", query=_mean_metric(uuid))
+ ExperimentToSavedMetric.objects.create(experiment=exp, saved_metric=saved, metadata={"type": role})
+ recalc = self._recalc(exp, metric_uuids=["m1", "s1", "sp1", "ss1"])
with _record_captures() as captured:
_update(
@@ -1670,6 +1673,8 @@ class TestRecalculationAnalytics(BaseTest):
assert props["status"] == "completed"
assert props["succeeded_metrics"] == 3
assert props["failed_metrics"] == 1
+ assert props["primary_metrics_count"] == 2
+ assert props["secondary_metrics_count"] == 2
assert props["execution_mode"] == "recalculation"
assert "total_duration_ms" in props