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): serve frustration metrics from lazy precompute (#59981)

Lucas Ricoy723e051d31d2
diff --git a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
index 50337639141c..48f99493ca14 100644
--- a/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
+++ b/frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
@@ -2293,6 +2293,10 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
limit: 10,
doPathCleaning: isPathCleaningEnabled,
tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
+ // The backend frustration lazy precompute gate decides whether
+ // this query is actually served from the precomputed table; we
+ // just pass the per-team opt-in through so it's eligible.
+ useWebAnalyticsPrecompute,
},
embedded: true,
showActions: true,
diff --git a/products/web_analytics/backend/hogql_queries/stats_table.py b/products/web_analytics/backend/hogql_queries/stats_table.py
index 97aefc5b90a5..9f271ee264d2 100644
--- a/products/web_analytics/backend/hogql_queries/stats_table.py
+++ b/products/web_analytics/backend/hogql_queries/stats_table.py
@@ -40,6 +40,10 @@ from products.web_analytics.backend.hogql_queries.stats_table_strategies import
StatsTableQueryStrategy,
)
from products.web_analytics.backend.hogql_queries.web_analytics_query_runner import WebAnalyticsQueryRunner, map_columns
+from products.web_analytics.backend.hogql_queries.web_stats_frustration_lazy_precompute import (
+ can_use_lazy_precompute as can_use_frustration_lazy_precompute,
+ execute_lazy_precomputed_read as execute_frustration_lazy_precomputed_read,
+)
from products.web_analytics.backend.hogql_queries.web_stats_lazy_precompute import (
LazyStatsResult,
can_use_lazy_precompute,
@@ -433,6 +437,72 @@ class WebStatsTableQueryRunner(WebAnalyticsQueryRunner[WebStatsTableQueryRespons
self.used_preaggregated_tables = True
return self._build_response_from_lazy_rows(rows, limit=limit, offset=offset)
+ def _maybe_calculate_via_frustration_lazy_precompute(self) -> Optional[WebStatsTableQueryResponse]:
+ """Short-circuit through the FRUSTRATION lazy precompute table when eligible.
+
+ Returns None when ineligible or on any failure, in which case the caller
+ falls through to the live HogQL path.
+ """
+ if not can_use_frustration_lazy_precompute(self):
+ return None
+ limit = self.paginator.limit
+ offset = self.paginator.offset or 0
+ rows = execute_frustration_lazy_precomputed_read(self, limit=limit + 1, offset=offset)
+ if rows is None:
+ return None
+ self.used_preaggregated_tables = True
+ return self._build_frustration_response_from_lazy_rows(rows, limit=limit, offset=offset)
+
+ def _build_frustration_response_from_lazy_rows(
+ self, rows: list[tuple], *, limit: int, offset: int
+ ) -> WebStatsTableQueryResponse:
+ """Materialise the SQL-paginated frustration rows into the wire shape.
+ Pivots the flat columns (breakdown, rage_c, rage_p, dead_c, dead_p,
+ errors_c, errors_p) back into the runner's tuple-of-(current, previous)
+ shape, matching the live `FrustrationMetricsStrategy.build_query()`
+ response."""
+ include_previous = bool(self.query_compare_to_date_range)
+ has_more = len(rows) > limit
+ page = rows[:limit]
+
+ results = []
+ for row in page:
+ (
+ breakdown_value,
+ rage_clicks,
+ prev_rage_clicks,
+ dead_clicks,
+ prev_dead_clicks,
+ errors,
+ prev_errors,
+ ) = row
+ results.append(
+ [
+ breakdown_value,
+ (rage_clicks, prev_rage_clicks if include_previous else None),
+ (dead_clicks, prev_dead_clicks if include_previous else None),
+ (errors, prev_errors if include_previous else None),
+ "", # cross_sell placeholder
+ ]
+ )
+
+ return WebStatsTableQueryResponse(
+ columns=[
+ "context.columns.breakdown_value",
+ "context.columns.rage_clicks",
+ "context.columns.dead_clicks",
+ "context.columns.errors",
+ "context.columns.cross_sell",
+ ],
+ results=results,
+ modifiers=self.modifiers,
+ usedPreAggregatedTables=True,
+ usedLazyPrecompute=True,
+ hasMore=has_more,
+ limit=limit,
+ offset=offset,
+ )
+
def _build_response_from_lazy_rows(
self, rows: list[tuple], *, limit: int, offset: int
) -> WebStatsTableQueryResponse:
@@ -513,12 +583,17 @@ class WebStatsTableQueryRunner(WebAnalyticsQueryRunner[WebStatsTableQueryRespons
return field, direction
def _calculate(self):
- # Try the PATHS lazy precompute first when the query targets the paths tile,
- # otherwise fall back to the simple-breakdowns lazy path.
+ # Try each lazy precompute path in turn. Each `can_use_*` check short-
+ # circuits on the wrong `breakdownBy`, so the order only matters for
+ # rejection reason attribution in logs/metrics.
lazy_response = self._maybe_calculate_via_lazy_precompute()
if lazy_response is not None:
return lazy_response
+ lazy_response = self._maybe_calculate_via_frustration_lazy_precompute()
+ if lazy_response is not None:
+ return lazy_response
+
lazy_result = self.get_lazy_precomputed_result()
if lazy_result is not None:
return self._build_response_from_lazy(lazy_result)
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_stats_frustration_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_stats_frustration_lazy_precompute.py
new file mode 100644
index 000000000000..ca0cc4134a57
--- /dev/null
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_stats_frustration_lazy_precompute.py
@@ -0,0 +1,290 @@
+import unittest
+from freezegun import freeze_time
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person
+from unittest.mock import patch
+
+from django.test import override_settings
+
+from parameterized import parameterized
+
+from posthog.schema import (
+ CompareFilter,
+ DateRange,
+ EventPropertyFilter,
+ HogQLQueryModifiers,
+ PropertyOperator,
+ SessionPropertyFilter,
+ SessionsV2JoinMode,
+ WebAnalyticsOrderByDirection,
+ WebAnalyticsOrderByFields,
+ WebAnalyticsSampling,
+ WebStatsBreakdown,
+ WebStatsTableQuery,
+)
+
+from posthog.models.utils import uuid7
+
+from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob
+from products.web_analytics.backend.hogql_queries.stats_table import WebStatsTableQueryRunner
+from products.web_analytics.backend.hogql_queries.web_stats_frustration_lazy_precompute import can_use_lazy_precompute
+
+
+@override_settings(IN_UNIT_TESTING=True)
+class TestWebStatsFrustrationLazyPrecompute(ClickhouseTestMixin, APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ PreaggregationJob.objects.filter(team_id=self.team.pk).delete()
+
+ def _enable_lazy(self):
+ # Same flag the paths/overview tests patch — the gate evaluates the
+ # `web-analytics-precompute-toggle` org flag via posthoganalytics.
+ return patch(
+ "products.web_analytics.backend.hogql_queries.web_lazy_precompute_common.posthoganalytics.feature_enabled",
+ return_value=True,
+ )
+
+ def _seed_frustration_events(self) -> None:
+ """One session on /buggy with rage clicks + an exception, one on /ok
+ with a single pageview (so it's filtered out by the HAVING's
+ any-metric-non-zero clause)."""
+ s1 = str(uuid7("2024-01-02"))
+ s2 = str(uuid7("2024-01-03"))
+ _create_person(team_id=self.team.pk, distinct_ids=["p1"], properties={"name": "p1"})
+ _create_person(team_id=self.team.pk, distinct_ids=["p2"], properties={"name": "p2"})
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="p1",
+ timestamp="2024-01-02T10:00:00Z",
+ properties={
+ "$session_id": s1,
+ "$host": "example.com",
+ "$pathname": "/buggy",
+ "$current_url": "https://example.com/buggy",
+ },
+ )
+ _create_event(
+ team=self.team,
+ event="$rageclick",
+ distinct_id="p1",
+ timestamp="2024-01-02T10:00:05Z",
+ properties={
+ "$session_id": s1,
+ "$host": "example.com",
+ "$pathname": "/buggy",
+ },
+ )
+ _create_event(
+ team=self.team,
+ event="$exception",
+ distinct_id="p1",
+ timestamp="2024-01-02T10:00:08Z",
+ properties={
+ "$session_id": s1,
+ "$host": "example.com",
+ "$pathname": "/buggy",
+ },
+ )
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="p2",
+ timestamp="2024-01-03T11:00:00Z",
+ properties={
+ "$session_id": s2,
+ "$host": "example.com",
+ "$pathname": "/ok",
+ "$current_url": "https://example.com/ok",
+ },
+ )
+
+ def _build_query(
+ self,
+ *,
+ date_from: str = "2024-01-01",
+ date_to: str = "2024-01-07",
+ properties: list | None = None,
+ compare: bool = False,
+ opt_in_precompute: bool = True,
+ breakdown_by: WebStatsBreakdown = WebStatsBreakdown.FRUSTRATION_METRICS,
+ sampling: WebAnalyticsSampling | None = None,
+ conversion_goal=None,
+ order_by: list | None = None,
+ modifiers: HogQLQueryModifiers | None = None,
+ ) -> WebStatsTableQuery:
+ return WebStatsTableQuery(
+ dateRange=DateRange(date_from=date_from, date_to=date_to),
+ properties=properties or [],
+ compareFilter=CompareFilter(compare=compare) if compare else None,
+ breakdownBy=breakdown_by,
+ useWebAnalyticsPrecompute=opt_in_precompute,
+ sampling=sampling,
+ conversionGoal=conversion_goal,
+ orderBy=order_by,
+ modifiers=modifiers,
+ )
+
+ def _runner(self, query: WebStatsTableQuery) -> WebStatsTableQueryRunner:
+ return WebStatsTableQueryRunner(team=self.team, query=query)
+
+ # ----------------------------------------------------------------------
+ # Eligibility — these do not need ClickHouse, they exercise the gate only.
+ # ----------------------------------------------------------------------
+
+ def test_eligible_when_flag_on_and_opt_in_set(self):
+ with self._enable_lazy():
+ assert can_use_lazy_precompute(self._runner(self._build_query())) is True
+
+ def test_rejected_when_org_flag_off(self):
+ # Default mocked-off feature flag: gate refuses.
+ with patch(
+ "products.web_analytics.backend.hogql_queries.web_lazy_precompute_common.posthoganalytics.feature_enabled",
+ return_value=False,
+ ):
+ assert can_use_lazy_precompute(self._runner(self._build_query())) is False
+
+ def test_rejected_when_per_query_opt_in_missing(self):
+ with self._enable_lazy():
+ assert can_use_lazy_precompute(self._runner(self._build_query(opt_in_precompute=False))) is False
+
+ @parameterized.expand(
+ [
+ (WebStatsBreakdown.PAGE,),
+ (WebStatsBreakdown.INITIAL_PAGE,),
+ (WebStatsBreakdown.BROWSER,),
+ (WebStatsBreakdown.COUNTRY,),
+ ]
+ )
+ def test_rejected_for_non_frustration_breakdowns(self, breakdown: WebStatsBreakdown):
+ with self._enable_lazy():
+ assert can_use_lazy_precompute(self._runner(self._build_query(breakdown_by=breakdown))) is False
+
+ def test_rejected_when_conversion_goal_set(self):
+ from posthog.schema import ActionConversionGoal
+
+ with self._enable_lazy():
+ assert (
+ can_use_lazy_precompute(
+ self._runner(self._build_query(conversion_goal=ActionConversionGoal(actionId=1)))
+ )
+ is False
+ )
+
+ def test_rejected_when_sampling_enabled(self):
+ with self._enable_lazy():
+ assert (
+ can_use_lazy_precompute(self._runner(self._build_query(sampling=WebAnalyticsSampling(enabled=True))))
+ is False
+ )
+
+ def test_rejected_for_non_event_property_filter(self):
+ # SessionPropertyFilter is not in the gate's allowlist (only EventPropertyFilter on $host).
+ with self._enable_lazy():
+ assert (
+ can_use_lazy_precompute(
+ self._runner(
+ self._build_query(
+ properties=[
+ SessionPropertyFilter(
+ key="$entry_pathname", operator=PropertyOperator.EXACT, value="/x"
+ )
+ ]
+ )
+ )
+ )
+ is False
+ )
+
+ def test_rejected_for_unsupported_filter_key(self):
+ with self._enable_lazy():
+ assert (
+ can_use_lazy_precompute(
+ self._runner(
+ self._build_query(
+ properties=[
+ EventPropertyFilter(key="$browser", operator=PropertyOperator.EXACT, value="Chrome")
+ ]
+ )
+ )
+ )
+ is False
+ )
+
+ def test_rejected_for_sessions_v2_uuid_mode(self):
+ with self._enable_lazy():
+ modifiers = HogQLQueryModifiers(sessionsV2JoinMode=SessionsV2JoinMode.UUID)
+ assert can_use_lazy_precompute(self._runner(self._build_query(modifiers=modifiers))) is False
+
+ def test_rejected_for_unsupported_order_by_field(self):
+ with self._enable_lazy():
+ order = [WebAnalyticsOrderByFields.VISITORS, WebAnalyticsOrderByDirection.DESC]
+ assert can_use_lazy_precompute(self._runner(self._build_query(order_by=order))) is False
+
+ @parameterized.expand(
+ [
+ (WebAnalyticsOrderByFields.ERRORS,),
+ (WebAnalyticsOrderByFields.RAGE_CLICKS,),
+ (WebAnalyticsOrderByFields.DEAD_CLICKS,),
+ ]
+ )
+ def test_accepted_for_supported_order_by_field(self, field: WebAnalyticsOrderByFields):
+ with self._enable_lazy():
+ order = [field, WebAnalyticsOrderByDirection.DESC]
+ assert can_use_lazy_precompute(self._runner(self._build_query(order_by=order))) is True
+
+ # ----------------------------------------------------------------------
+ # Round-trip — these exercise the real INSERT + read path. They mirror the
+ # paths-tile tests and inherit the same CI-flake skip while the underlying
+ # read-after-write visibility issue is investigated.
+ # ----------------------------------------------------------------------
+
+ @freeze_time("2024-01-15T12:00:00Z")
+ def test_round_trip_creates_precompute_job(self):
+ self._seed_frustration_events()
+ with self._enable_lazy():
+ self._runner(self._build_query()).calculate()
+
+ jobs = list(PreaggregationJob.objects.filter(team_id=self.team.pk))
+ assert len(jobs) > 0, "expected at least one precompute job to be created"
+
+ @unittest.skip(
+ "Mirrors the CI-only flake in test_web_stats_paths_lazy_precompute.py — "
+ "lazy path returns empty rows despite READY job. Re-enable once the "
+ "read-after-write visibility issue tracked there is resolved."
+ )
+ @freeze_time("2024-01-15T12:00:00Z")
+ def test_lazy_response_matches_live(self):
+ """Compare rage / dead / errors per path between the live and lazy paths."""
+ self._seed_frustration_events()
+
+ live_response = self._runner(self._build_query()).calculate()
+ live_by_path = self._collect_metrics(live_response.results)
+
+ with self._enable_lazy():
+ lazy_response = self._runner(self._build_query()).calculate()
+
+ ready_jobs = PreaggregationJob.objects.filter(
+ team_id=self.team.pk, status=PreaggregationJob.Status.READY
+ ).count()
+ assert ready_jobs > 0, "expected at least one READY precompute job"
+ assert lazy_response.usedLazyPrecompute is True
+ assert lazy_response.usedPreAggregatedTables is True
+
+ lazy_by_path = self._collect_metrics(lazy_response.results)
+ assert lazy_by_path == live_by_path, f"lazy/live mismatch: lazy={lazy_by_path}, live={live_by_path}"
+
+ @staticmethod
+ def _collect_metrics(results) -> dict[str, dict]:
+ """Build a `{path: {rage, dead, errors}}` dict from a table response.
+
+ Each row is `[breakdown_value, (rage, prev), (dead, prev), (errors, prev), cross_sell]`.
+ """
+ out: dict[str, dict] = {}
+ for row in results:
+ breakdown = row[0]
+ out[breakdown] = {
+ "rage_clicks": row[1][0],
+ "dead_clicks": row[2][0],
+ "errors": row[3][0],
+ }
+ return out
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
index ec3e7a0d6735..0344b926f2bb 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_stats_lazy_precompute.py
@@ -315,14 +315,6 @@ class TestWebStatsLazyPrecompute(ClickhouseTestMixin, APIBaseTest):
assert self._job_count() == 0
- @freeze_time("2024-01-15T12:00:00Z")
- def test_frustration_metrics_breakdown_falls_through(self):
- self._seed()
- with self._enable_lazy():
- self._run(self._build_query(breakdown_by=WebStatsBreakdown.FRUSTRATION_METRICS))
-
- assert self._job_count() == 0
-
@freeze_time("2024-01-15T12:00:00Z")
def test_language_breakdown_falls_through(self):
# LANGUAGE needs an extra topK aggregation column that can't be rebuilt
diff --git a/products/web_analytics/backend/hogql_queries/web_stats_frustration_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/web_stats_frustration_lazy_precompute.py
new file mode 100644
index 000000000000..70c518d6fdaa
--- /dev/null
+++ b/products/web_analytics/backend/hogql_queries/web_stats_frustration_lazy_precompute.py
@@ -0,0 +1,536 @@
+"""Lazy precompute path for the Web Analytics FRUSTRATION metrics tile.
+
+Mirrors `web_stats_paths_lazy_precompute.py` and shares its eligibility gate
+via `web_lazy_precompute_common`. The precomputed table stores one row per
+(team, job, UTC hour, breakdown_value) where `breakdown_value` is whatever
+the strategy's `_counts_breakdown_value()` emits (typically the URL path
+for the only `breakdownBy` the frustration tile ships today). For each
+session we sum the per-session rage / dead / exception counts; rows under
+the same `(hour, breakdown_value)` are summed via `sumMerge` at read time.
+"""
+
+import time
+from datetime import UTC, datetime
+from typing import TYPE_CHECKING, Optional
+
+import structlog
+from prometheus_client import Counter, Histogram
+
+from posthog.schema import HogQLQueryModifiers, WebAnalyticsOrderByFields, WebStatsBreakdown
+
+from posthog.hogql import ast
+from posthog.hogql.parser import parse_select
+from posthog.hogql.query import execute_hogql_query
+
+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
+
+from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (
+ LazyComputationResult,
+ LazyComputationTable,
+ ensure_precomputed,
+)
+from products.web_analytics.backend.hogql_queries.web_lazy_precompute_common import (
+ LAZY_TTL_SECONDS,
+ SESSION_FORWARD_PAD_MINUTES,
+ LazyPrecomputeIneligible,
+ ceil_utc_day,
+ check_common_eligibility,
+ floor_utc_day,
+ host_filter_expr,
+ log_eligibility_outcome,
+ test_account_filter_expr,
+)
+
+if TYPE_CHECKING:
+ from products.web_analytics.backend.hogql_queries.stats_table import WebStatsTableQueryRunner
+
+logger = structlog.get_logger(__name__)
+
+
+# Allowlist of exception class names we expect on the lazy path. Anything
+# outside this set is collapsed to "other" so dependency-leaking dynamic
+# exception names can't blow up Prometheus label cardinality.
+_KNOWN_FAILED_ERROR_TYPES: set[str] = {
+ "ServerException",
+ "NetworkError",
+ "OperationalError",
+ "IntegrityError",
+ "AssertionError",
+ "AttributeError",
+ "KeyError",
+ "ValueError",
+ "TypeError",
+ "TimeoutError",
+}
+
+
+def _bucket_error_label(exc: BaseException) -> str:
+ name = type(exc).__name__
+ return name if name in _KNOWN_FAILED_ERROR_TYPES else "other"
+
+
+WEB_STATS_FRUSTRATION_LAZY_FAILED = Counter(
+ "web_stats_frustration_lazy_precompute_failed_total",
+ "Lazy precompute path (frustration tile) failures, by error class",
+ ["error_type"],
+)
+
+WEB_STATS_FRUSTRATION_LAZY_EMPTY = Counter(
+ "web_stats_frustration_lazy_precompute_empty_total",
+ "Lazy precompute reads that returned zero rows.",
+)
+
+WEB_STATS_FRUSTRATION_LAZY_ROWS = Histogram(
+ "web_stats_frustration_lazy_precompute_rows",
+ "Distinct `breakdown_value` rows returned by the lazy precompute read (post-LIMIT cap).",
+ buckets=(1, 10, 100, 500, 1000, 2500, 5000, 7500, 10000, float("inf")),
+)
+
+
+class WrongBreakdown(LazyPrecomputeIneligible):
+ pass
+
+
+class UnsupportedOrderBy(LazyPrecomputeIneligible):
+ def __init__(self, field: object):
+ self.field = field
+ super().__init__(f"field={field!r}")
+
+
+# Order-by fields the lazy read can produce. The live strategy hard-codes
+# (errors DESC, rage_clicks DESC, dead_clicks DESC); we mirror that as the
+# default and accept the same three as user-driven overrides.
+SUPPORTED_ORDER_BY_FIELDS: set = {
+ WebAnalyticsOrderByFields.ERRORS,
+ WebAnalyticsOrderByFields.RAGE_CLICKS,
+ WebAnalyticsOrderByFields.DEAD_CLICKS,
+}
+
+
+def can_use_lazy_precompute(runner: "WebStatsTableQueryRunner") -> bool:
+ """Return True iff the FRUSTRATION tile can be served from precompute."""
+ try:
+ _check_eligible(runner)
+ except LazyPrecomputeIneligible as exc:
+ log_eligibility_outcome(log_prefix="web_stats_frustration_lazy_precompute", team_id=runner.team.pk, error=exc)
+ return False
+ log_eligibility_outcome(log_prefix="web_stats_frustration_lazy_precompute", team_id=runner.team.pk, error=None)
+ return True
+
+
+def _check_eligible(runner: "WebStatsTableQueryRunner") -> None:
+ query = runner.query
+ if query.breakdownBy != WebStatsBreakdown.FRUSTRATION_METRICS:
+ raise WrongBreakdown(f"breakdownBy={query.breakdownBy!r}")
+ if query.orderBy:
+ order_field = query.orderBy[0]
+ if order_field not in SUPPORTED_ORDER_BY_FIELDS:
+ raise UnsupportedOrderBy(order_field)
+
+ check_common_eligibility(
+ team=runner.team,
+ use_web_analytics_precompute=query.useWebAnalyticsPrecompute,
+ conversion_goal=query.conversionGoal,
+ sampling=query.sampling,
+ modifiers=query.modifiers,
+ properties=query.properties or [],
+ resolve_date_range=lambda: (runner.query_date_range.date_from(), runner.query_date_range.date_to()),
+ )
+
+
+def _events_session_id_expr(runner: "WebStatsTableQueryRunner") -> ast.Expr:
+ return runner.events_session_property
+
+
+def _breakdown_value_expr(runner: "WebStatsTableQueryRunner") -> ast.Expr:
+ """Raw pathname for the FRUSTRATION_METRICS breakdown — path cleaning is
+ applied at READ time (see `_READ_SQL_TEMPLATE`), not here. Storing raw
+ paths keeps the precompute rule-independent: a team can edit cleaning
+ rules and the existing precomputed rows remain valid — the next read
+ just groups them by the new cleaned values. Mirrors the same pattern in
+ `web_stats_paths_lazy_precompute.py`."""
+ return ast.Field(chain=["events", "properties", "$pathname"])
+
+
+# HogQL template for the precompute INSERT — a state-converted version of
+# the live `FRUSTRATION_METRICS_INNER_QUERY` + `FrustrationMetricsStrategy`
+# outer aggregation. The lazy_computation framework substitutes the listed
+# placeholders (including `time_window_min` / `time_window_max`), parses the
+# result, and INSERTs into `web_stats_frustration_preaggregated`. The
+# framework automatically prepends `team_id`, `job_id` and appends
+# `expires_at` to the SELECT.
+#
+# The inner subquery mirrors the live `FRUSTRATION_METRICS_INNER_QUERY`
+# verbatim (per-session counts of rage / dead / exception events), and the
+# outer aggregation mirrors the live `FrustrationMetricsStrategy.build_query`
+# OUTER (sums collapsed by breakdown) — but bucketed hourly so reads can
+# answer arbitrary date ranges via `sumMergeIf`. `sumState(...)` and `sum(...)`
+# both aggregate over the same grouped rows, so the HAVING can filter on the
+# regular `sum(...)` value while emitting the `sumState(...)` column for
+# storage. The outer HAVING is the state-equivalent of the live
+# `FrustrationMetricsStrategy._having()` collapse — drop hour-breakdown
+# tuples where every metric is zero, which matches what the live outer
+# `HAVING or(metric > 0, ...)` drops. Saves storing the all-zero rows that
+# the read query would only re-filter via its own `HAVING or(rage > 0, ...)`.
+#
+# The `event IN (...)` filter restricts the scan to events that can possibly
+# contribute to any of the three metrics, plus `$pageview` / `$screen` so the
+# session-start anchoring is correct. The forward pad lets sessions that span
+# a UTC-day boundary aggregate cleanly — same reasoning as overview/paths.
+INSERT_QUERY_TEMPLATE = """
+SELECT
+ toStartOfHour(start_timestamp) AS time_window_start,
+ breakdown_value AS breakdown_value,
+ sumState(assumeNotNull(toInt(rage_clicks_count))) AS sum_rage_clicks_state,
+ sumState(assumeNotNull(toInt(dead_clicks_count))) AS sum_dead_clicks_state,
+ sumState(assumeNotNull(toInt(errors_count))) AS sum_errors_state
+FROM (
+ SELECT
+ {events_session_id} AS session_id,
+ {breakdown_value_expr} AS breakdown_value,
+ countIf(events.event = '$rageclick') AS rage_clicks_count,
+ countIf(events.event = '$dead_click') AS dead_clicks_count,
+ countIf(events.event = '$exception') AS errors_count,
+ min(session.$start_timestamp) AS start_timestamp
+ FROM events
+ WHERE and(
+ {events_session_id} IS NOT NULL,
+ events.event IN ('$pageview', '$screen', '$rageclick', '$dead_click', '$exception'),
+ timestamp >= {time_window_min},
+ timestamp < ({time_window_max} + toIntervalMinute({pad_minutes})),
+ {user_filter},
+ {test_account_filter}
+ )
+ GROUP BY session_id, breakdown_value
+ HAVING and(
+ breakdown_value IS NOT NULL,
+ toStartOfHour(min(session.$start_timestamp)) >= {time_window_min},
+ toStartOfHour(min(session.$start_timestamp)) < {time_window_max}
+ )
+)
+GROUP BY time_window_start, breakdown_value
+HAVING or(
+ sum(rage_clicks_count) > 0,
+ sum(dead_clicks_count) > 0,
+ sum(errors_count) > 0
+)
+"""
+
+
+def ensure_web_stats_frustration_precomputed(
+ runner: "WebStatsTableQueryRunner",
+ time_range_start: datetime,
+ time_range_end: datetime,
+) -> LazyComputationResult:
+ placeholders: dict[str, ast.Expr] = {
+ "events_session_id": _events_session_id_expr(runner),
+ "breakdown_value_expr": _breakdown_value_expr(runner),
+ "user_filter": host_filter_expr(runner.query.properties or []),
+ "test_account_filter": test_account_filter_expr(
+ test_account_filters=runner._test_account_filters, team=runner.team
+ ),
+ "pad_minutes": ast.Constant(value=SESSION_FORWARD_PAD_MINUTES),
+ }
+
+ return ensure_precomputed(
+ team=runner.team,
+ insert_query=INSERT_QUERY_TEMPLATE,
+ time_range_start=time_range_start,
+ time_range_end=time_range_end,
+ ttl_seconds=LAZY_TTL_SECONDS,
+ table=LazyComputationTable.WEB_STATS_FRUSTRATION_PREAGGREGATED,
+ placeholders=placeholders,
+ query_type="web_stats_frustration_lazy_insert",
+ )
+
+
+# Soft budget for the cumulative `ensure_precomputed` time inside a single
+# request. The framework's default `wait_timeout_seconds` is 180 s per call; a
+# compare-period request makes two back-to-back calls. If the first burns most
+# of that budget we skip the second and fall through to the live path to keep
+# the overall HTTP request from sitting on a worker past that.
+ENSURE_BUDGET_MS = 120 * 1000
+
+
+# Map runner orderBy → SELECT column name. The strategy's hard-coded default
+# is (errors DESC, rage_clicks DESC, dead_clicks DESC) — when the runner has
+# no explicit orderBy we sort by errors DESC to match.
+_ORDER_BY_TO_COLUMN: dict = {
+ WebAnalyticsOrderByFields.ERRORS: "errors",
+ WebAnalyticsOrderByFields.RAGE_CLICKS: "rage_clicks",
+ WebAnalyticsOrderByFields.DEAD_CLICKS: "dead_clicks",
+}
+
+
+# Read template. The live strategy's `_having()` keeps rows where any of the
+# three metrics is non-zero across both periods (matching how the tuple
+# comparison `(cur, prev) > (0, 0)` collapses); we mirror that here so the
+# pages of zeroes that would otherwise dominate are filtered server-side.
+#
+# `breakdown_expr` is the (raw or cleaned) breakdown column. Path cleaning is
+# applied here in the read rather than baked into the precompute, so rule
+# edits don't invalidate stored rows. Cleaning is a chain of nested
+# `replaceRegexpAll` calls — see `apply_path_cleaning` — and ClickHouse
+# `sumMerge*` is associative across the GROUP BY change, so different cleaned
+# values that collapse onto the same key sum correctly.
+_READ_SQL_TEMPLATE = """
+SELECT
+ {breakdown_expr} AS breakdown,
+ sumMergeIf(sum_rage_clicks_state, and(time_window_start >= {cur_start}, time_window_start < {cur_end})) AS rage_clicks,
+ sumMergeIf(sum_rage_clicks_state, and(time_window_start >= {prev_start}, time_window_start < {prev_end})) AS previous_rage_clicks,
+ sumMergeIf(sum_dead_clicks_state, and(time_window_start >= {cur_start}, time_window_start < {cur_end})) AS dead_clicks,
+ sumMergeIf(sum_dead_clicks_state, and(time_window_start >= {prev_start}, time_window_start < {prev_end})) AS previous_dead_clicks,
+ sumMergeIf(sum_errors_state, and(time_window_start >= {cur_start}, time_window_start < {cur_end})) AS errors,
+ sumMergeIf(sum_errors_state, and(time_window_start >= {prev_start}, time_window_start < {prev_end})) AS previous_errors
+FROM posthog.web_stats_frustration_preaggregated
+WHERE and(team_id = {team_id}, job_id IN {job_ids})
+GROUP BY {breakdown_expr}
+HAVING or(
+ rage_clicks > 0, previous_rage_clicks > 0,
+ dead_clicks > 0, previous_dead_clicks > 0,
+ errors > 0, previous_errors > 0
+)
+"""
+
+
+def _resolve_sort(runner: "WebStatsTableQueryRunner") -> tuple[str, str]:
+ """Return `(column, direction)` for the lazy read's ORDER BY.
+
+ Defaults to (errors, DESC) to match the live strategy's primary sort.
+ If `runner.query.orderBy` carries a field this module doesn't know how to
+ serve, raise rather than silently falling back — the eligibility gate is
+ the single source of truth for supported sort fields and the two should
+ not drift.
+ """
+ if runner.query.orderBy:
+ field, direction = runner.query.orderBy
+ if field not in _ORDER_BY_TO_COLUMN:
+ # Defensive: `_check_eligible` already rejects unsupported fields
+ # via `SUPPORTED_ORDER_BY_FIELDS`, so reaching here means the gate
+ # and `_ORDER_BY_TO_COLUMN` have drifted.
+ raise AssertionError(f"unsupported lazy frustration orderBy field={field!r}")
+ return _ORDER_BY_TO_COLUMN[field], direction.value if hasattr(direction, "value") else str(direction)
+ return "errors", "DESC"
+
+
+# The live `FrustrationMetricsStrategy._order_by()` is hard-coded to
+# `(errors DESC, rage_clicks DESC, dead_clicks DESC)`. We mirror those as
+# tiebreakers after the user's primary sort so paginated results are stable
+# across the lazy and live paths: rows tied on the primary metric resolve to
+# the same order on both engines, which means page N has the same rows
+# regardless of which path served it.
+_TIEBREAKER_CHAIN = ["errors", "rage_clicks", "dead_clicks"]
+
+
+def _build_order_by(sort_column: str, sort_direction: str) -> list[ast.OrderExpr]:
+ """Sort by the user's pick, then by the live strategy's tiebreaker chain
+ (errors DESC, rage_clicks DESC, dead_clicks DESC), then by breakdown for
+ a final deterministic tiebreaker."""
+ order: list[ast.OrderExpr] = [
+ ast.OrderExpr(expr=ast.Field(chain=[sort_column]), order=sort_direction), # type: ignore[arg-type]
+ ]
+ for column in _TIEBREAKER_CHAIN:
+ if column != sort_column:
+ order.append(ast.OrderExpr(expr=ast.Field(chain=[column]), order="DESC"))
+ order.append(ast.OrderExpr(expr=ast.Field(chain=["breakdown"]), order="ASC"))
+ return order
+
+
+def execute_read_query(
+ *,
+ runner: "WebStatsTableQueryRunner",
+ job_ids: list[str],
+ current_start_utc: datetime,
+ current_end_utc: datetime,
+ previous_start_utc: Optional[datetime],
+ previous_end_utc: Optional[datetime],
+ sort_column: str,
+ sort_direction: str,
+ limit: int,
+ offset: int,
+) -> list:
+ """Read the precomputed FRUSTRATION rows via HogQL.
+
+ Returns the raw `response.results` (list of tuples) so the caller can
+ materialize without depending on HogQL's response type. Sort and
+ pagination are computed in SQL — the caller materialises the page
+ directly without any in-Python re-sort.
+ """
+ # Sentinel for the no-compare case: an unsatisfiable window so the
+ # `sumMergeIf` aggregates return 0 for the "previous" columns without
+ # changing the column shape.
+ prev_start = previous_start_utc if previous_start_utc is not None else datetime(1970, 1, 1, tzinfo=UTC)
+ prev_end = previous_end_utc if previous_end_utc is not None else datetime(1970, 1, 1, tzinfo=UTC)
+
+ placeholders: dict[str, ast.Expr] = {
+ "team_id": ast.Constant(value=runner.team.pk),
+ "job_ids": ast.Constant(value=[str(jid) for jid in job_ids]),
+ "cur_start": ast.Constant(value=current_start_utc),
+ "cur_end": ast.Constant(value=current_end_utc),
+ "prev_start": ast.Constant(value=prev_start),
+ "prev_end": ast.Constant(value=prev_end),
+ "breakdown_expr": runner._apply_path_cleaning(ast.Field(chain=["breakdown_value"])),
+ }
+
+ parsed = parse_select(_READ_SQL_TEMPLATE, placeholders=placeholders)
+ assert isinstance(parsed, ast.SelectQuery), "lazy frustration read template must parse to a SelectQuery"
+ parsed.order_by = _build_order_by(sort_column, sort_direction)
+ parsed.limit = ast.Constant(value=limit)
+ parsed.offset = ast.Constant(value=offset)
+
+ # The precomputed `time_window_start` is UTC; `convertToProjectTimezone`
+ # would wrap it in `toTimeZone(..., team_tz)` and break the direct
+ # comparison against our UTC bounds.
+ modifiers = runner.modifiers.model_copy() if runner.modifiers else HogQLQueryModifiers()
+ modifiers.convertToProjectTimezone = False
+
+ tag_queries(product=Product.WEB_ANALYTICS, feature=Feature.QUERY, query_type="web_stats_frustration_lazy_query")
+ response = execute_hogql_query(
+ query_type="web_stats_frustration_lazy_query",
+ query=parsed,
+ team=runner.team,
+ timings=runner.timings,
+ modifiers=modifiers,
+ limit_context=runner.limit_context,
+ )
+ return list(response.results or [])
+
+
+def execute_lazy_precomputed_read(
+ runner: "WebStatsTableQueryRunner",
+ *,
+ limit: int,
+ offset: int,
+) -> Optional[list[tuple]]:
+ """Orchestrate the lazy precompute + read. Returns the list of result rows,
+ or None on any failure (caller falls through to the live path).
+
+ Each row is `(breakdown, rage_clicks, previous_rage_clicks, dead_clicks,
+ previous_dead_clicks, errors, previous_errors)`. The caller fetches
+ `limit + 1` to detect `hasMore` and pivots the flat columns back into
+ the runner's tuple-of-(current, previous) response shape.
+ """
+ tag_queries(product=Product.WEB_ANALYTICS, feature=Feature.QUERY)
+ team_id = runner.team.pk
+ overall_started = time.perf_counter()
+ try:
+ date_from = runner.query_date_range.date_from()
+ date_to = runner.query_date_range.date_to()
+ assert date_from is not None and date_to is not None
+
+ current_start_utc = date_from.astimezone(UTC)
+ current_end_utc = date_to.astimezone(UTC)
+ time_range_start = floor_utc_day(current_start_utc)
+ time_range_end = ceil_utc_day(current_end_utc)
+
+ if time_range_start >= time_range_end:
+ logger.info(
+ "web_stats_frustration_lazy_precompute_empty_range",
+ team_id=team_id,
+ time_range_start=time_range_start.isoformat(),
+ time_range_end=time_range_end.isoformat(),
+ )
+ return None
+
+ logger.info(
+ "web_stats_frustration_lazy_precompute_started",
+ team_id=team_id,
+ time_range_start=time_range_start.isoformat(),
+ time_range_end=time_range_end.isoformat(),
+ time_range_days=(time_range_end - time_range_start).days,
+ )
+
+ ensure_started = time.perf_counter()
+ result = ensure_web_stats_frustration_precomputed(
+ runner=runner,
+ time_range_start=time_range_start,
+ time_range_end=time_range_end,
+ )
+ ensure_duration_ms = int((time.perf_counter() - ensure_started) * 1000)
+ logger.info(
+ "web_stats_frustration_lazy_precompute_ensure_done",
+ team_id=team_id,
+ job_count=len(result.job_ids),
+ ensure_duration_ms=ensure_duration_ms,
+ )
+
+ if not result.job_ids or not result.ready:
+ return None
+
+ job_ids: list[str] = [str(jid) for jid in result.job_ids]
+
+ previous_start_utc: Optional[datetime] = None
+ previous_end_utc: Optional[datetime] = None
+ if runner.query_compare_to_date_range is not None:
+ prev_from = runner.query_compare_to_date_range.date_from()
+ prev_to = runner.query_compare_to_date_range.date_to()
+ if prev_from is not None and prev_to is not None:
+ previous_start_utc = prev_from.astimezone(UTC)
+ previous_end_utc = prev_to.astimezone(UTC)
+ prev_range_start = floor_utc_day(previous_start_utc)
+ prev_range_end = ceil_utc_day(previous_end_utc)
+ if prev_range_start < prev_range_end:
+ if ensure_duration_ms >= ENSURE_BUDGET_MS:
+ logger.info(
+ "web_stats_frustration_lazy_precompute_compare_budget_exceeded",
+ team_id=team_id,
+ elapsed_ms=ensure_duration_ms,
+ budget_ms=ENSURE_BUDGET_MS,
+ )
+ return None
+ prev_ensure_started = time.perf_counter()
+ prev_result = ensure_web_stats_frustration_precomputed(
+ runner=runner,
+ time_range_start=prev_range_start,
+ time_range_end=prev_range_end,
+ )
+ ensure_duration_ms += int((time.perf_counter() - prev_ensure_started) * 1000)
+ if not prev_result.ready:
+ logger.info(
+ "web_stats_frustration_lazy_precompute_previous_not_ready",
+ team_id=team_id,
+ prev_job_count=len(prev_result.job_ids),
+ )
+ return None
+ job_ids.extend(str(jid) for jid in prev_result.job_ids)
+
+ sort_column, sort_direction = _resolve_sort(runner)
+ read_started = time.perf_counter()
+ rows = execute_read_query(
+ runner=runner,
+ job_ids=job_ids,
+ current_start_utc=current_start_utc,
+ current_end_utc=current_end_utc,
+ previous_start_utc=previous_start_utc,
+ previous_end_utc=previous_end_utc,
+ sort_column=sort_column,
+ sort_direction=sort_direction,
+ limit=limit,
+ offset=offset,
+ )
+ read_duration_ms = int((time.perf_counter() - read_started) * 1000)
+ total_duration_ms = int((time.perf_counter() - overall_started) * 1000)
+
+ rows_returned = len(rows) if rows else 0
+ WEB_STATS_FRUSTRATION_LAZY_ROWS.observe(rows_returned)
+ if rows_returned == 0:
+ WEB_STATS_FRUSTRATION_LAZY_EMPTY.inc()
+ logger.info(
+ "web_stats_frustration_lazy_precompute_completed",
+ team_id=team_id,
+ job_count=len(result.job_ids),
+ rows_returned=rows_returned,
+ ensure_duration_ms=ensure_duration_ms,
+ read_duration_ms=read_duration_ms,
+ total_duration_ms=total_duration_ms,
+ )
+ return list(rows) if rows else []
+ except Exception as exc:
+ WEB_STATS_FRUSTRATION_LAZY_FAILED.labels(error_type=_bucket_error_label(exc)).inc()
+ logger.exception(
+ "web_stats_frustration_lazy_precompute_failed",
+ team_id=team_id,
+ error_type=type(exc).__name__,
+ total_duration_ms=int((time.perf_counter() - overall_started) * 1000),
+ )
+ return None