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.

feat(web-analytics): warm lazy precompute jobs for enrolled teams via cache warming (#68670)

Lucas Ricoyec74771003d5
diff --git a/products/web_analytics/dags/cache_warming.py b/products/web_analytics/dags/cache_warming.py
index 83a175ccd95a..22bc7f724b83 100644
--- a/products/web_analytics/dags/cache_warming.py
+++ b/products/web_analytics/dags/cache_warming.py
@@ -19,6 +19,7 @@ from posthog.hogql_queries.query_runner import get_query_runner
from posthog.models import Team
from posthog.models.instance_setting import get_instance_setting
+from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import is_precompute_enabled_for_team
from products.web_analytics.dags.web_preaggregated_utils import check_for_concurrent_runs
STALE_WEB_QUERIES_GAUGE = Gauge(
@@ -53,6 +54,38 @@ def get_teams_enabled_for_web_analytics_cache_warming() -> list[int]:
return value if isinstance(value, list) else []
+# Query kinds that carry the `useWebAnalyticsPrecompute` per-query opt-in.
+LAZY_PRECOMPUTE_QUERY_KINDS = frozenset(
+ {"WebStatsTableQuery", "WebOverviewQuery", "WebGoalsQuery", "WebVitalsPathBreakdownQuery"}
+)
+
+
+def maybe_opt_into_lazy_precompute(team: Team, query_json: dict) -> dict:
+ """Opt an enrolled team's replayed query into the lazy precompute path.
+
+ Replayed queries are the team's real production shapes, which for a
+ restricted-enrolled team carry no per-query opt-in (users only send one via
+ the UI toggle). Without this, warming such a team never computes its lazy
+ jobs — so enrolling a team on `WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS` plus
+ this warming list is what populates its precompute from real query shapes
+ while its user-facing reads stay on the raw path (their queries still have
+ no opt-in). That combination is how a team is evaluated before its reads
+ are switched over.
+
+ The opt-in changes the runner's cache key, so the warmed Django-cache entry
+ differs from the one raw user reads hit — acceptable: the durable output of
+ warming an enrolled team is the precompute jobs (shared by query shape),
+ not the result-cache row.
+ """
+ if query_json.get("kind") not in LAZY_PRECOMPUTE_QUERY_KINDS:
+ return query_json
+ if query_json.get("useWebAnalyticsPrecompute") is not None:
+ return query_json
+ if not is_precompute_enabled_for_team(team):
+ return query_json
+ return {**query_json, "useWebAnalyticsPrecompute": True}
+
+
def queries_to_keep_fresh(
context: dagster.OpExecutionContext, team_id: int, days: int = 7, minimum_query_count: int = 10
) -> list[dict]:
@@ -167,6 +200,8 @@ def warm_queries_op(context: dagster.OpExecutionContext, queries: dict) -> None:
query_json = query_info["query_json"]
normalized_query_hash = query_info["normalized_query_hash"]
+ query_json = maybe_opt_into_lazy_precompute(team, query_json)
+
runner = get_query_runner(
query=query_json,
team=team,
diff --git a/products/web_analytics/dags/tests/test_cache_warming.py b/products/web_analytics/dags/tests/test_cache_warming.py
new file mode 100644
index 000000000000..6708746a4c78
--- /dev/null
+++ b/products/web_analytics/dags/tests/test_cache_warming.py
@@ -0,0 +1,34 @@
+from posthog.test.base import BaseTest
+
+from parameterized import parameterized
+
+from products.web_analytics.dags.cache_warming import maybe_opt_into_lazy_precompute
+
+
+class TestMaybeOptIntoLazyPrecompute(BaseTest):
+ def test_enrolled_team_web_query_gets_opt_in(self) -> None:
+ # If this breaks, warming an enrolled-but-restricted team silently stops
+ # computing its lazy jobs — the pre-enrollment evaluation pipeline reads empty.
+ query = {"kind": "WebStatsTableQuery", "properties": []}
+ with self.settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=[self.team.id]):
+ result = maybe_opt_into_lazy_precompute(self.team, query)
+
+ self.assertIs(result["useWebAnalyticsPrecompute"], True)
+ self.assertNotIn("useWebAnalyticsPrecompute", query) # input not mutated
+
+ @parameterized.expand(
+ [
+ # Non-web kinds don't carry the field; injecting it would break validation.
+ ("non_web_kind", {"kind": "TrendsQuery"}, True),
+ # Teams outside the enrollment lists must warm exactly what users run.
+ ("not_enrolled", {"kind": "WebStatsTableQuery"}, False),
+ # An explicit user opt-out in the replayed shape is preserved.
+ ("explicit_opt_out", {"kind": "WebStatsTableQuery", "useWebAnalyticsPrecompute": False}, True),
+ ]
+ )
+ def test_leaves_query_untouched(self, _name: str, query: dict, enrolled: bool) -> None:
+ team_ids = [self.team.id] if enrolled else []
+ with self.settings(WEB_ANALYTICS_LAZY_PRECOMPUTE_TEAM_IDS=team_ids):
+ result = maybe_opt_into_lazy_precompute(self.team, query)
+
+ self.assertEqual(result, query)