: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(customer-analytics): start cold metric queries together (#124447)
Shy Alter089f27e6b383
diff --git a/.github/new-events-schema-targets.txt b/.github/new-events-schema-targets.txtindex 68d14640dbac..039dc5118c29 100644--- a/.github/new-events-schema-targets.txt+++ b/.github/new-events-schema-targets.txt@@ -29,6 +29,7 @@ products/ai_observability/backend/tests/test_facade_api.pyproducts/ai_observability/backend/tests/test_trace_views.pyproducts/cohortsproducts/customer_analytics/backend/hogql_queries+products/customer_analytics/backend/test/test_key_metrics.pyproducts/customer_analytics/backend/test/test_membership_backfill.pyproducts/error_tracking/backend/hogql_queriesproducts/error_tracking/backend/tests/test_issue_summaries.pydiff --git a/products/customer_analytics/backend/logic/key_metrics.py b/products/customer_analytics/backend/logic/key_metrics.pyindex f15558ca3fbd..8325098d37a0 100644--- a/products/customer_analytics/backend/logic/key_metrics.py+++ b/products/customer_analytics/backend/logic/key_metrics.py@@ -144,26 +144,31 @@ def key_metrics(team: Team, user: User) -> list[KeyMetric] | None:for key, condition in conditions.items())rows = _run(daily, team, user)- if rows is None:+ weekly = (+ _run(+ _select(+ WEEKLY_PEOPLE,+ start=_midnight(team, today - timedelta(days=14)),+ week=_midnight(team, today - timedelta(days=7)),+ end=_midnight(team, today),+ match=conditions[PEOPLE_METRIC],+ test_accounts=clone_expr(test_accounts),+ ),+ team,+ user,+ )+ if PEOPLE_METRIC in conditions+ else []+ )+ if rows is None or weekly is None:return Noneby_day = {_day(row[0]): row[1:] for row in rows}days = [today - timedelta(days=offset) for offset in range(DAYS, 0, -1)]metrics = []- for index, (key, condition) in enumerate(conditions.items()):+ for index, key in enumerate(conditions):points = [(day, float(by_day[day][index]) if day in by_day else 0.0) for day in days]if key == PEOPLE_METRIC:- weekly_query = _select(- WEEKLY_PEOPLE,- start=_midnight(team, today - timedelta(days=14)),- week=_midnight(team, today - timedelta(days=7)),- end=_midnight(team, today),- match=condition,- test_accounts=clone_expr(test_accounts),- )- weekly = _run(weekly_query, team, user)- if weekly is None:- return Nonethis_week, last_week = (float(value) for value in weekly[0])else:this_week, last_week = sum(v for _, v in points[-7:]), sum(v for _, v in points[-14:-7])diff --git a/products/customer_analytics/backend/test/test_key_metrics.py b/products/customer_analytics/backend/test/test_key_metrics.pyindex 63ec7b6f61ec..2e7511b55364 100644--- a/products/customer_analytics/backend/test/test_key_metrics.py+++ b/products/customer_analytics/backend/test/test_key_metrics.py@@ -127,9 +127,14 @@ class TestKeyMetrics(ClickhouseTestMixin, BaseTest):activity = metrics[0]self.assertEqual((activity.this_week, activity.daily[-1]), (1.0, (date(2026, 3, 1), 1.0)))- def test_an_uncached_query_returns_nothing_and_calculates_in_the_background(self) -> None:+ @parameterized.expand([("activity_event", "opened_file", 2), ("signup_event", "signed_up", 1)])+ def test_uncached_queries_start_together_without_waiting_for_another_load(+ self, metric: str, event: str, queries: int+ ) -> None:config = self.team.customer_analytics_config- config.activity_event = {"kind": "EventsNode", "event": "opened_file"}+ for key in key_metrics_logic.KEY_METRICS:+ setattr(config, key, {})+ setattr(config, metric, {"kind": "EventsNode", "event": event})config.save()with (@@ -138,7 +143,7 @@ class TestKeyMetrics(ClickhouseTestMixin, BaseTest):):metrics = get_key_metrics(team=self.team, user=self.user)- self.assertEqual((metrics, enqueue.call_count), (None, 1))+ self.assertEqual((metrics, enqueue.call_count), (None, queries))@parameterized.expand([