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(visual-review): split the baselines overview into named units (#101075)

posthog[bot]96e0cb75f0aa
diff --git a/products/visual_review/backend/logic/baseline_overview.py b/products/visual_review/backend/logic/baseline_overview.py
index 851c4e36b9b4..49ed77389bbb 100644
--- a/products/visual_review/backend/logic/baseline_overview.py
+++ b/products/visual_review/backend/logic/baseline_overview.py
@@ -3,10 +3,10 @@
from __future__ import annotations
from collections import Counter
-from datetime import datetime
+from datetime import datetime, timedelta
from uuid import UUID
-from django.db.models import Avg, Count, Q
+from django.db.models import Avg, Count, Q, QuerySet
from django.utils import timezone
from posthog.dataclasses import frozen
@@ -16,6 +16,9 @@ from ..models import QuarantinedIdentifier, Run, RunSnapshot, ToleratedHash
from . import run_queries, toleration
from .run_queries import SnapshotKey
+# A baseline counts as frequently tolerated at this many intentional tolerations in the window.
+_FREQUENT_TOLERATE_MIN = 3
+
def get_baselines_overview(repo_id: UUID) -> _BaselineOverviewRaw:
"""Universe of identifiers with a current baseline, plus aggregates.
@@ -35,52 +38,17 @@ def get_baselines_overview(repo_id: UUID) -> _BaselineOverviewRaw:
- 2 queries for the recent-drift average (resolve last-N runs, aggregate)
- 3 cheap aggregate queries for totals
"""
- from datetime import timedelta
-
from ..facade.contracts import BASELINE_DRIFT_RECENT_RUN_COUNT, BASELINE_OVERVIEW_MAX_ENTRIES, VARIANT_PILEUP_MIN
now = timezone.now()
- # 1. Find the latest *completed* run on the default branch per (repo, branch, run_type).
+ # Find the latest *completed* run on the default branch per (repo, branch, run_type).
universe_runs = run_queries.latest_default_branch_runs(repo_id)
universe_run_ids = [r.id for r in universe_runs]
if not universe_run_ids:
- return _BaselineOverviewRaw(
- entries=[],
- tolerate_30d_by_id={},
- tolerate_90d_by_id={},
- active_variants_by_key={},
- active_quarantines_by_key={},
- change_count_by_key={},
- recent_drift_by_key={},
- totals_all=0,
- totals_recent=0,
- totals_frequent=0,
- totals_quarantined=0,
- totals_variant_pileups=0,
- by_run_type={},
- truncated=False,
- generated_at=now,
- )
+ return _empty_overview(now)
- # 2. Pull the universe rows. select_related the chain we need for thumbnails.
- universe_qs = (
- RunSnapshot.objects.filter(run_id__in=universe_run_ids)
- .select_related("run", "current_artifact__thumbnail")
- .only(
- "identifier",
- "metadata",
- "run__id",
- "run__run_type",
- "run__completed_at",
- "run__created_at",
- "current_artifact__width",
- "current_artifact__height",
- "current_artifact__thumbnail__content_hash",
- )
- # Stable ordering so truncation is deterministic; newest baselines first.
- .order_by("-run__completed_at", "identifier")
- )
+ universe_qs = _universe_queryset(universe_run_ids)
total_universe = universe_qs.count()
truncated = total_universe > BASELINE_OVERVIEW_MAX_ENTRIES
universe = list(universe_qs[:BASELINE_OVERVIEW_MAX_ENTRIES]) if truncated else list(universe_qs)
@@ -89,242 +57,292 @@ def get_baselines_overview(repo_id: UUID) -> _BaselineOverviewRaw:
# otherwise truncation makes them undercount in misleading ways (a 6000-id
# repo would show 0 frequently-tolerated if all of them sat past the slice).
universe_identifiers = list({s.identifier for s in universe})
- if truncated:
- full_universe_identifiers = list(universe_qs.values_list("identifier", flat=True).distinct())
- else:
- full_universe_identifiers = universe_identifiers
-
- # 3a. Tolerate counts in 30d / 90d windows. Single grouped query each.
- tolerate_30d_by_id: dict[str, int] = {}
- tolerate_90d_by_id: dict[str, int] = {}
- if universe_identifiers:
- tol_30d_cutoff = now - timedelta(days=30)
- tol_90d_cutoff = now - timedelta(days=90)
- for identifier, count in (
- ToleratedHash.objects.filter(
- repo_id=repo_id,
- identifier__in=universe_identifiers,
- reason__in=INTENTIONAL_TOLERATE_REASONS,
- created_at__gte=tol_30d_cutoff,
- )
- .values_list("identifier")
- .annotate(c=Count("id"))
- .values_list("identifier", "c")
- ):
- tolerate_30d_by_id[identifier] = count
- for identifier, count in (
- ToleratedHash.objects.filter(
- repo_id=repo_id,
- identifier__in=universe_identifiers,
- reason__in=INTENTIONAL_TOLERATE_REASONS,
- created_at__gte=tol_90d_cutoff,
- )
- .values_list("identifier")
- .annotate(c=Count("id"))
- .values_list("identifier", "c")
- ):
- tolerate_90d_by_id[identifier] = count
+ # `.order_by()` clears the ordering before DISTINCT. Django appends an ORDER BY column to
+ # the select list of a DISTINCT query, so the `-run__completed_at` ordering above would join
+ # the DISTINCT key and yield one row per (identifier, run) rather than one per identifier.
+ # The list feeds `identifier__in` filters, so that pushes the parameter count toward the
+ # Postgres bind-parameter limit and adds a join and a sort that the caller has no use for.
+ full_universe_identifiers = (
+ list(universe_qs.order_by().values_list("identifier", flat=True).distinct())
+ if truncated
+ else universe_identifiers
+ )
- # 3a-bis. Accepted variants still standing against each baseline's current hash. Scoped to the
+ # Accepted variants still standing against each baseline's current hash. Scoped to the
# whole universe rather than the truncated slice, because the totals below read it too.
active_variants_by_key = toleration.count_active_variants_against_current_baseline(
repo_id, now=now, newest_run_by_type=run_queries.newest_run_by_run_type(universe_runs)
)
+ active_quarantines_by_key = _active_quarantines_by_key(repo_id, universe_identifiers, now=now)
- # 3b. Active quarantines for this repo, scoped to the universe identifiers
- # AND the run_types they live on (quarantine is per (repo, run_type, id)).
- # We hydrate the full row (not just identity) so the overview can render
- # reason / expiry / who / source-run inline without a per-card fetch.
- # `select_related("source_run")` is a single JOIN, capped by
- # `BASELINE_OVERVIEW_MAX_ENTRIES`. `Run.metadata` (JSONField) and
- # `Run.error_message` (TextField) can be large and aren't needed by the
- # summary — defer them to keep the response light.
- active_quarantines_by_key: dict[SnapshotKey, QuarantinedIdentifier] = {}
- if universe_identifiers:
- for q in (
- QuarantinedIdentifier.objects.filter(
- repo_id=repo_id,
- identifier__in=universe_identifiers,
- )
- .filter(Q(expires_at__isnull=True) | Q(expires_at__gt=now))
- .select_related("source_run")
- .defer("source_run__metadata", "source_run__error_message")
- .order_by("-created_at")
- ):
- key = SnapshotKey(run_type=q.run_type, identifier=q.identifier)
- # Multiple active rows for the same key shouldn't happen — create
- # auto-supersedes prior — but if it does, keep the latest (sorted
- # above) and ignore the rest.
- if key not in active_quarantines_by_key:
- active_quarantines_by_key[key] = q
-
- # 3c. Per-baseline stability signals: a lifetime baseline-flip count and a
- # smoothed recent-drift average. Replaces a daily-bucket sparkline that
- # had to scan millions of RunSnapshot rows on every request (~7s, OOMed on
- # the web pod for repos with thousands of identifiers — see git history).
- # Both queries here are scoped tightly enough to finish in tens of ms:
- #
- # change_count_by_key: COUNT(*) WHERE result IN ('changed','removed')
- # across all completed master/main runs ever. Real baseline flips on
- # master leave a CHANGED/REMOVED row in the run that introduced them
- # (subsequent runs see UNCHANGED against the new YAML baseline), so
- # this count IS the number of times the YAML moved. "Ever" reaches
- # back as far as retention keeps default-branch runs, which is
- # `retention.DEFAULT_BRANCH_RUN_RETENTION_DAYS`. Postgres uses
- # the `snapshot_run_result` index on (run_id, result) to bitmap-scan
- # straight to the rare event rows (~1k of millions). No window
- # function, no per-row LAG comparison.
- #
- # recent_drift_by_key: AVG(diff_percentage) over the last 10 master/
- # main completed runs per (run_type). Bounded by run count, not by
- # time window — caps the scanned set regardless of CI cadence. We
- # resolve the run IDs first (sub-ms) and aggregate via PK-indexed
- # run_id__in, otherwise the planner inlines a CTE that produces a
- # ROW_NUMBER plan over the full RunSnapshot table.
- change_count_by_key: dict[SnapshotKey, int] = {}
- recent_drift_by_key: dict[SnapshotKey, float] = {}
+ # Per-baseline stability signals: a lifetime baseline-flip count and a smoothed
+ # recent-drift average. Both cover the whole repo history rather than a set of
+ # identifiers, so an empty universe is the only thing that can skip them. They
+ # replace a daily-bucket sparkline that had to scan millions of RunSnapshot rows
+ # on every request (~7s, OOMed on the web pod for repos with thousands of
+ # identifiers — see git history). Both are scoped tightly enough to finish in
+ # tens of ms.
if universe_identifiers:
- for identifier, run_type, c in (
- RunSnapshot.objects.filter(
- run__repo_id=repo_id,
- run__branch__in=run_queries._DEFAULT_BRANCHES,
- run__status=RunStatus.COMPLETED,
- result__in=(SnapshotResult.CHANGED, SnapshotResult.REMOVED),
- )
- .values("identifier", "run__run_type")
- .annotate(c=Count("id"))
- .values_list("identifier", "run__run_type", "c")
- ):
- change_count_by_key[SnapshotKey(run_type=run_type, identifier=identifier)] = c
-
- # Top-N per run_type via window function. There's no pure-ORM
- # equivalent: Postgres doesn't allow filtering on a window result,
- # and a per-run_type loop balloons to thousands of queries on repos
- # where each Storybook story registers as its own run_type
- # (benchmarked: 4ms raw vs 5.6s loop on a 30k-run repo with 2154
- # run_types). The query is parameterized — every dynamic value
- # passes through %s binding, no string concatenation, table name
- # comes from the model. nosemgrep is required because the rule
- # blanket-flags any .raw() use.
- recent_run_sql = f"""
- SELECT id, run_type FROM (
- SELECT id, run_type,
- ROW_NUMBER() OVER (PARTITION BY run_type ORDER BY created_at DESC) AS rn
- FROM {Run._meta.db_table}
- WHERE repo_id = %s AND branch = ANY(%s) AND status = %s
- ) ranked WHERE rn <= %s
- """ # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql
- recent_run_ids = list(
- Run.objects.raw( # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql
- recent_run_sql,
- [
- str(repo_id),
- list(run_queries._DEFAULT_BRANCHES),
- RunStatus.COMPLETED,
- BASELINE_DRIFT_RECENT_RUN_COUNT,
- ],
- )
- )
- if recent_run_ids:
- for identifier, run_type, drift_avg in (
- RunSnapshot.objects.filter(run_id__in=[r.id for r in recent_run_ids])
- .values("identifier", "run__run_type")
- .annotate(drift_avg=Avg("diff_percentage", filter=Q(diff_percentage__gt=0)))
- .values_list("identifier", "run__run_type", "drift_avg")
- ):
- if drift_avg is not None:
- recent_drift_by_key[SnapshotKey(run_type=run_type, identifier=identifier)] = drift_avg
-
- # 4. Totals computed across the *full* universe (not the truncated slice)
- # so the stat row stays correct when the entries are clipped.
- if truncated:
- # Re-issue a small COUNT-only query for accurate totals across the
- # universe; we already have the truncated list in memory.
- totals_all = total_universe
+ change_count_by_key = _baseline_change_counts_by_key(repo_id)
+ recent_drift_by_key = _recent_drift_by_key(repo_id, recent_run_count=BASELINE_DRIFT_RECENT_RUN_COUNT)
else:
- totals_all = len(universe)
-
- # Recently / frequently tolerated — counts of distinct identifiers with
- # ≥1 (or ≥3) intentional tolerations in the rolling window. Scope across
- # the *full* universe so the stat row stays correct under truncation, and
- # match the per-entry counts above by excluding AUTO_THRESHOLD.
- recent_cutoff = now - timedelta(days=30)
- frequent_cutoff = now - timedelta(days=90)
- recent_ids: set[str] = set()
- frequent_ids: set[str] = set()
- if full_universe_identifiers:
- recent_ids = set(
- ToleratedHash.objects.filter(
- repo_id=repo_id,
- identifier__in=full_universe_identifiers,
- reason__in=INTENTIONAL_TOLERATE_REASONS,
- created_at__gte=recent_cutoff,
- )
- .values_list("identifier", flat=True)
- .distinct()
- )
- frequent_grouped = (
- ToleratedHash.objects.filter(
- repo_id=repo_id,
- identifier__in=full_universe_identifiers,
- reason__in=INTENTIONAL_TOLERATE_REASONS,
- created_at__gte=frequent_cutoff,
- )
- .values("identifier")
- .annotate(c=Count("id"))
- .filter(c__gte=3)
- .values_list("identifier", flat=True)
- )
- frequent_ids = set(frequent_grouped)
+ change_count_by_key = {}
+ recent_drift_by_key = {}
# `active_quarantines_by_key` was built from the truncated set above (per-entry
# attached). Re-query for the totals so they cover the full universe.
if truncated and full_universe_identifiers:
- quarantined_id_count = (
- QuarantinedIdentifier.objects.filter(
- repo_id=repo_id,
- identifier__in=full_universe_identifiers,
- )
- .filter(Q(expires_at__isnull=True) | Q(expires_at__gt=now))
- .values("identifier")
- .distinct()
- .count()
- )
+ quarantined_id_count = _quarantined_identifier_count(repo_id, full_universe_identifiers, now=now)
else:
quarantined_id_count = len({key.identifier for key in active_quarantines_by_key})
- # by_run_type counts every entry in the universe. Aggregate query under
- # truncation so it doesn't undercount; in-memory Counter when not truncated
- # (we already paid for the row hydration).
- if truncated:
- by_run_type = dict(
- universe_qs.values_list("run__run_type")
- .order_by()
- .annotate(c=Count("id"))
- .values_list("run__run_type", "c")
- )
- else:
- by_run_type = dict(Counter(s.run.run_type for s in universe))
-
return _BaselineOverviewRaw(
entries=universe,
- tolerate_30d_by_id=tolerate_30d_by_id,
- tolerate_90d_by_id=tolerate_90d_by_id,
+ tolerate_30d_by_id=_tolerate_counts_by_identifier(
+ repo_id, universe_identifiers, since=now - timedelta(days=30)
+ ),
+ tolerate_90d_by_id=_tolerate_counts_by_identifier(
+ repo_id, universe_identifiers, since=now - timedelta(days=90)
+ ),
active_variants_by_key=active_variants_by_key,
active_quarantines_by_key=active_quarantines_by_key,
change_count_by_key=change_count_by_key,
recent_drift_by_key=recent_drift_by_key,
- totals_all=totals_all,
- totals_recent=len(recent_ids),
- totals_frequent=len(frequent_ids),
+ totals_all=total_universe,
+ totals_recent=_recently_tolerated_count(repo_id, full_universe_identifiers, since=now - timedelta(days=30)),
+ totals_frequent=_frequently_tolerated_count(repo_id, full_universe_identifiers, since=now - timedelta(days=90)),
totals_quarantined=quarantined_id_count,
totals_variant_pileups=sum(1 for count in active_variants_by_key.values() if count >= VARIANT_PILEUP_MIN),
- by_run_type=by_run_type,
+ by_run_type=_counts_by_run_type(universe_qs, universe, truncated=truncated),
truncated=truncated,
generated_at=now,
)
+def _empty_overview(now: datetime) -> _BaselineOverviewRaw:
+ return _BaselineOverviewRaw(
+ entries=[],
+ tolerate_30d_by_id={},
+ tolerate_90d_by_id={},
+ active_variants_by_key={},
+ active_quarantines_by_key={},
+ change_count_by_key={},
+ recent_drift_by_key={},
+ totals_all=0,
+ totals_recent=0,
+ totals_frequent=0,
+ totals_quarantined=0,
+ totals_variant_pileups=0,
+ by_run_type={},
+ truncated=False,
+ generated_at=now,
+ )
+
+
+def _universe_queryset(universe_run_ids: list[UUID]) -> QuerySet[RunSnapshot]:
+ """The universe rows. select_related the chain we need for thumbnails."""
+ return (
+ RunSnapshot.objects.filter(run_id__in=universe_run_ids)
+ .select_related("run", "current_artifact__thumbnail")
+ .only(
+ "identifier",
+ "metadata",
+ "run__id",
+ "run__run_type",
+ "run__completed_at",
+ "run__created_at",
+ "current_artifact__width",
+ "current_artifact__height",
+ "current_artifact__thumbnail__content_hash",
+ )
+ # Stable ordering so truncation is deterministic; newest baselines first.
+ .order_by("-run__completed_at", "identifier")
+ )
+
+
+def _intentional_tolerations(repo_id: UUID, identifiers: list[str], since: datetime) -> QuerySet[ToleratedHash]:
+ """Tolerations a person chose, in a rolling window. Excludes AUTO_THRESHOLD."""
+ return ToleratedHash.objects.filter(
+ repo_id=repo_id,
+ identifier__in=identifiers,
+ reason__in=INTENTIONAL_TOLERATE_REASONS,
+ created_at__gte=since,
+ )
+
+
+def _tolerate_counts_by_identifier(repo_id: UUID, identifiers: list[str], since: datetime) -> dict[str, int]:
+ """Per-identifier tolerate count in the window. Single grouped query."""
+ if not identifiers:
+ return {}
+ return dict(
+ _intentional_tolerations(repo_id, identifiers, since)
+ .values_list("identifier")
+ .annotate(c=Count("id"))
+ .values_list("identifier", "c")
+ )
+
+
+def _recently_tolerated_count(repo_id: UUID, identifiers: list[str], since: datetime) -> int:
+ """Distinct identifiers with at least one intentional toleration in the window."""
+ if not identifiers:
+ return 0
+ return _intentional_tolerations(repo_id, identifiers, since).values("identifier").distinct().count()
+
+
+def _frequently_tolerated_count(repo_id: UUID, identifiers: list[str], since: datetime) -> int:
+ """Distinct identifiers tolerated repeatedly in the window."""
+ if not identifiers:
+ return 0
+ return (
+ _intentional_tolerations(repo_id, identifiers, since)
+ .values("identifier")
+ .annotate(c=Count("id"))
+ .filter(c__gte=_FREQUENT_TOLERATE_MIN)
+ .count()
+ )
+
+
+def _active_quarantines_qs(repo_id: UUID, identifiers: list[str], now: datetime) -> QuerySet[QuarantinedIdentifier]:
+ return QuarantinedIdentifier.objects.filter(
+ repo_id=repo_id,
+ identifier__in=identifiers,
+ ).filter(Q(expires_at__isnull=True) | Q(expires_at__gt=now))
+
+
+def _active_quarantines_by_key(
+ repo_id: UUID, identifiers: list[str], now: datetime
+) -> dict[SnapshotKey, QuarantinedIdentifier]:
+ """Active quarantines, scoped to the universe identifiers.
+
+ Quarantine is per `(repo, run_type, identifier)`. We hydrate the full row (not
+ just identity) so the overview can render reason / expiry / who / source-run
+ inline without a per-card fetch. `select_related("source_run")` is a single
+ JOIN, capped by `BASELINE_OVERVIEW_MAX_ENTRIES`. `Run.metadata` (JSONField)
+ and `Run.error_message` (TextField) can be large and aren't needed by the
+ summary — defer them to keep the response light.
+ """
+ if not identifiers:
+ return {}
+ by_key: dict[SnapshotKey, QuarantinedIdentifier] = {}
+ for q in (
+ _active_quarantines_qs(repo_id, identifiers, now)
+ .select_related("source_run")
+ .defer("source_run__metadata", "source_run__error_message")
+ .order_by("-created_at")
+ ):
+ key = SnapshotKey(run_type=q.run_type, identifier=q.identifier)
+ # Multiple active rows for the same key shouldn't happen — create
+ # auto-supersedes prior — but if it does, keep the latest (sorted
+ # above) and ignore the rest.
+ if key not in by_key:
+ by_key[key] = q
+ return by_key
+
+
+def _quarantined_identifier_count(repo_id: UUID, identifiers: list[str], now: datetime) -> int:
+ return _active_quarantines_qs(repo_id, identifiers, now).values("identifier").distinct().count()
+
+
+def _baseline_change_counts_by_key(repo_id: UUID) -> dict[SnapshotKey, int]:
+ """Lifetime baseline-flip count per baseline.
+
+ COUNT(*) WHERE result IN ('changed','removed') across all completed
+ master/main runs ever. Real baseline flips on master leave a CHANGED/REMOVED
+ row in the run that introduced them (subsequent runs see UNCHANGED against
+ the new YAML baseline), so this count IS the number of times the YAML moved.
+ "Ever" reaches back as far as retention keeps default-branch runs, which is
+ `retention.DEFAULT_BRANCH_RUN_RETENTION_DAYS`. Postgres uses the
+ `snapshot_run_result` index on (run_id, result) to bitmap-scan straight to
+ the rare event rows (~1k of millions). No window function, no per-row LAG
+ comparison.
+ """
+ return {
+ SnapshotKey(run_type=run_type, identifier=identifier): c
+ for identifier, run_type, c in RunSnapshot.objects.filter(
+ run__repo_id=repo_id,
+ run__branch__in=run_queries._DEFAULT_BRANCHES,
+ run__status=RunStatus.COMPLETED,
+ result__in=(SnapshotResult.CHANGED, SnapshotResult.REMOVED),
+ )
+ .values("identifier", "run__run_type")
+ .annotate(c=Count("id"))
+ .values_list("identifier", "run__run_type", "c")
+ }
+
+
+def _recent_run_ids_by_run_type(repo_id: UUID, recent_run_count: int) -> list[UUID]:
+ """The last N completed default-branch runs per run type.
+
+ Top-N per run_type via window function. There's no pure-ORM equivalent:
+ Postgres doesn't allow filtering on a window result, and a per-run_type loop
+ balloons to thousands of queries on repos where each Storybook story
+ registers as its own run_type (benchmarked: 4ms raw vs 5.6s loop on a 30k-run
+ repo with 2154 run_types). The query is parameterized — every dynamic value
+ passes through %s binding, no string concatenation, table name comes from the
+ model. nosemgrep is required because the rule blanket-flags any .raw() use.
+ """
+ recent_run_sql = f"""
+ SELECT id, run_type FROM (
+ SELECT id, run_type,
+ ROW_NUMBER() OVER (PARTITION BY run_type ORDER BY created_at DESC) AS rn
+ FROM {Run._meta.db_table}
+ WHERE repo_id = %s AND branch = ANY(%s) AND status = %s
+ ) ranked WHERE rn <= %s
+ """ # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql
+ return [
+ r.id
+ for r in Run.objects.raw( # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql
+ recent_run_sql,
+ [
+ str(repo_id),
+ list(run_queries._DEFAULT_BRANCHES),
+ RunStatus.COMPLETED,
+ recent_run_count,
+ ],
+ )
+ ]
+
+
+def _recent_drift_by_key(repo_id: UUID, recent_run_count: int) -> dict[SnapshotKey, float]:
+ """Smoothed recent-drift average per baseline.
+
+ AVG(diff_percentage) over the last N master/main completed runs per
+ `run_type`. Bounded by run count, not by time window — caps the scanned set
+ regardless of CI cadence. We resolve the run IDs first (sub-ms) and aggregate
+ via PK-indexed run_id__in, otherwise the planner inlines a CTE that produces
+ a ROW_NUMBER plan over the full RunSnapshot table.
+ """
+ recent_run_ids = _recent_run_ids_by_run_type(repo_id, recent_run_count)
+ if not recent_run_ids:
+ return {}
+ return {
+ SnapshotKey(run_type=run_type, identifier=identifier): drift_avg
+ for identifier, run_type, drift_avg in RunSnapshot.objects.filter(run_id__in=recent_run_ids)
+ .values("identifier", "run__run_type")
+ .annotate(drift_avg=Avg("diff_percentage", filter=Q(diff_percentage__gt=0)))
+ .values_list("identifier", "run__run_type", "drift_avg")
+ if drift_avg is not None
+ }
+
+
+def _counts_by_run_type(
+ universe_qs: QuerySet[RunSnapshot], entries: list[RunSnapshot], *, truncated: bool
+) -> dict[str, int]:
+ """Every entry in the universe, per run type.
+
+ Aggregate query under truncation so it doesn't undercount; in-memory Counter
+ when not truncated (we already paid for the row hydration).
+ """
+ if truncated:
+ return dict(
+ universe_qs.values_list("run__run_type")
+ .order_by()
+ .annotate(c=Count("id"))
+ .values_list("run__run_type", "c")
+ )
+ return dict(Counter(s.run.run_type for s in entries))
+
+
@frozen
class _BaselineOverviewRaw:
"""Internal raw shape — the facade layer reshapes this into the public DTOs.
diff --git a/products/visual_review/backend/tests/test_baselines.py b/products/visual_review/backend/tests/test_baselines.py
index f21f816a1ca0..340223164372 100644
--- a/products/visual_review/backend/tests/test_baselines.py
+++ b/products/visual_review/backend/tests/test_baselines.py
@@ -8,6 +8,7 @@ Coverage:
- Sparkline buckets (clean / tolerated / changed / quarantined).
- Totals computed across the universe (not the truncated slice).
- Browser metadata flows through for Playwright runs.
+- The query count stays flat as the universe grows.
"""
from datetime import timedelta
@@ -16,8 +17,13 @@ from uuid import uuid4
from posthog.test.base import APIBaseTest
from unittest.mock import patch
+from django.db import connections
+from django.test.utils import CaptureQueriesContext
from django.utils import timezone
+from parameterized import parameterized
+
+from products.visual_review.backend.db import WRITER_DB
from products.visual_review.backend.facade import api as vr_api
from products.visual_review.backend.facade.contracts import BASELINE_OVERVIEW_MAX_ENTRIES
from products.visual_review.backend.facade.enums import RunStatus, RunType, SnapshotResult, ToleratedReason
@@ -476,6 +482,51 @@ class TestBaselinesOverview(VisualReviewTeamScopedTestMixin, APIBaseTest):
# all_snapshots total reflects the full universe even when truncated
assert result.totals.all_snapshots == 7
+ @parameterized.expand([("full_universe", None), ("truncated_universe", 5)])
+ def test_query_count_does_not_grow_with_the_universe(self, _name: str, cap: int | None):
+ # The per-request query budget that `baseline_overview` documents has to
+ # hold as the universe grows. A field the facade reads outside
+ # `_universe_queryset`'s `only(...)`, a dropped `select_related`, or a
+ # lookup inside the facade's per-entry loop reloads once per entry, up to
+ # the cap, and still returns every value the tests above assert.
+ def seed(count: int, run_type: str, offset_hours: int) -> None:
+ run = _mk_run(self.repo, run_type=run_type, completed_offset=timedelta(hours=offset_hours))
+ for i in range(count):
+ identifier = f"id-{i:03d}"
+ artifact = _mk_artifact(self.repo, f"c-{run_type}-{i}", with_thumbnail=f"t-{run_type}-{i}")
+ _mk_snapshot(run, identifier=identifier, artifact=artifact, baseline_hash="b")
+ # Quarantine every entry so the `source_run` and `created_by`
+ # preloads the summary depends on are on the measured path.
+ QuarantinedIdentifier.objects.create(
+ repo=self.repo,
+ team_id=self.team.id,
+ identifier=identifier,
+ run_type=run_type,
+ reason="flaky",
+ created_by_id=self.user.id,
+ source_run=run,
+ )
+
+ def measure() -> tuple[int, int]:
+ with CaptureQueriesContext(connections[WRITER_DB]) as captured:
+ result = vr_api.get_baselines_overview(self.repo.id)
+ return len(captured.captured_queries), len(result.entries)
+
+ with patch(
+ "products.visual_review.backend.facade.contracts.BASELINE_OVERVIEW_MAX_ENTRIES",
+ BASELINE_OVERVIEW_MAX_ENTRIES if cap is None else cap,
+ ):
+ # Both measurements sit on the same side of the cap, so the fixed
+ # cost of the truncated path does not read as growth.
+ seed(8, RunType.STORYBOOK, offset_hours=1)
+ small_queries, small_entries = measure()
+
+ seed(30, RunType.PLAYWRIGHT, offset_hours=2)
+ large_queries, large_entries = measure()
+
+ assert small_entries and large_entries
+ assert large_queries <= small_queries
+
def test_endpoint_returns_serialized_overview(self):
run = _mk_run(self.repo)
artifact = _mk_artifact(self.repo, content_hash="h1", with_thumbnail="t1")