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.

fix(signals): cut per-row artefact subqueries from the inbox list (#94877)

posthog[bot]2c61f1b87751
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.py
index 5e5df4560151..f30380b44ce9 100644
--- a/products/signals/backend/models.py
+++ b/products/signals/backend/models.py
@@ -1,3 +1,4 @@
+import uuid
import logging
from collections import defaultdict
from datetime import datetime, timedelta
@@ -1181,6 +1182,39 @@ class SignalReportArtefact(UUIDModel):
models.Index(fields=["claim"], name="signals_artefact_claim_idx"),
]
+ @classmethod
+ def counts_by_report(cls, report_ids: list[str]) -> dict[str, int]:
+ """How many artefacts each report has, in one grouped query over the page.
+
+ The inbox list renders this count for every row it returns. A correlated subquery makes
+ Postgres count a report's artefacts before the page limit applies, so the whole team's
+ reports get counted to render 25. Reports with no artefacts are omitted.
+ """
+ if not report_ids:
+ return {}
+ rows = (
+ cls.objects.filter(report_id__in=report_ids).values("report_id").annotate(artefact_count=models.Count("*"))
+ )
+ return {str(row["report_id"]): row["artefact_count"] for row in rows}
+
+ @classmethod
+ def live_channel_ids_by_report(cls, report_ids: list[str]) -> dict[str, uuid.UUID]:
+ """The space each report is assigned to, in one query over the page.
+
+ The assignment is the newest `channel_assignment` artefact. A report whose newest
+ assignment points at a deleted space counts as unassigned, and is omitted like a report
+ that was never assigned.
+ """
+ if not report_ids:
+ return {}
+ rows = (
+ cls.objects.filter(report_id__in=report_ids, type=cls.ArtefactType.CHANNEL_ASSIGNMENT)
+ .order_by("report_id", "-created_at")
+ .distinct("report_id")
+ .values_list("report_id", "channel_id", "channel__deleted")
+ )
+ return {str(report_id): channel_id for report_id, channel_id, deleted in rows if deleted is False}
+
@classmethod
def _create(
cls,
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index d0bb7a5ccce7..b2c87ab09b04 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -676,16 +676,36 @@ class TestSignalReportListAPI(APIBaseTest):
assert response.json()["priority"] == "P0"
@parameterized.expand([("unassigned", False), ("assigned", True)])
- def test_retrieve_includes_channel_id(self, _name, assign):
+ def test_channel_id_is_the_same_in_the_list_and_the_detail(self, _name, assign):
channel = Channel.objects.create(team=self.team, name="Reports") if assign else None
report = self._create_report()
if channel:
self._assign_channel(report, channel)
+ expected = str(channel.id) if channel else None
url = f"/api/projects/{self.team.id}/signals/reports/{report.id}/"
response = self.client.get(url)
assert response.status_code == status.HTTP_200_OK
- assert response.json()["channel_id"] == (str(channel.id) if channel else None)
+ assert response.json()["channel_id"] == expected
+
+ list_response = self.client.get(self._list_url())
+ assert list_response.status_code == status.HTTP_200_OK
+ row = next(r for r in list_response.json()["results"] if r["id"] == str(report.id))
+ assert row["channel_id"] == expected
+
+ def test_artefact_count_is_the_same_in_the_list_and_the_detail(self):
+ report = self._create_report()
+ self._priority_artefact(report, priority="P1")
+ self._actionability_artefact(report, actionability="immediately_actionable")
+
+ list_response = self.client.get(self._list_url())
+ assert list_response.status_code == status.HTTP_200_OK
+ row = next(r for r in list_response.json()["results"] if r["id"] == str(report.id))
+ assert row["artefact_count"] == 2
+
+ response = self.client.get(f"/api/projects/{self.team.id}/signals/reports/{report.id}/")
+ assert response.status_code == status.HTTP_200_OK
+ assert response.json()["artefact_count"] == 2
def test_filter_by_channel_id_narrows_to_that_space(self):
channel = Channel.objects.create(team=self.team, name="Reports")
@@ -724,6 +744,10 @@ class TestSignalReportListAPI(APIBaseTest):
assert response.status_code == status.HTTP_200_OK
assert response.json()["channel_id"] is None
+ list_response = self.client.get(self._list_url())
+ row = next(r for r in list_response.json()["results"] if r["id"] == str(report.id))
+ assert row["channel_id"] is None
+
def test_filter_by_channel_id_rejects_non_uuid(self):
response = self.client.get(self._list_url(channel_id="not-a-uuid"))
assert response.status_code == status.HTTP_400_BAD_REQUEST
@@ -1051,6 +1075,38 @@ class TestSignalReportListAPI(APIBaseTest):
row = next(r for r in response.json()["results"] if r["id"] == str(report.id))
assert row["is_suggested_reviewer"] is False
+ def test_is_suggested_reviewer_is_the_same_in_the_list_and_the_detail(self):
+ # The list resolves the reviewer set once for the team; the detail resolves it for the one
+ # report it renders. A report must not read as "needs your review" in only one of them.
+ UserSocialAuth.objects.create(
+ user=self.user,
+ provider="github",
+ uid="github-test-suggested-list-detail",
+ extra_data={"login": "suggestedgh"},
+ )
+ mine = self._create_report()
+ self._actionability_artefact(mine, actionability="immediately_actionable")
+ someone_elses = self._create_report()
+ self._actionability_artefact(someone_elses, actionability="immediately_actionable")
+ for report, login in ((mine, "suggestedgh"), (someone_elses, "someoneelse")):
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.SUGGESTED_REVIEWERS,
+ content=json.dumps([{"github_login": login}]),
+ )
+
+ list_response = self.client.get(self._list_url(status="ready"))
+ assert list_response.status_code == status.HTTP_200_OK
+ rows = {r["id"]: r["is_suggested_reviewer"] for r in list_response.json()["results"]}
+ assert rows[str(mine.id)] is True
+ assert rows[str(someone_elses.id)] is False
+
+ for report, expected in ((mine, True), (someone_elses, False)):
+ detail = self.client.get(f"/api/projects/{self.team.id}/signals/reports/{report.id}/")
+ assert detail.status_code == status.HTTP_200_OK
+ assert detail.json()["is_suggested_reviewer"] is expected
+
# --- implementation_pr_url ---
def _create_implementation_task_with_run(
@@ -1393,7 +1449,7 @@ class TestSignalReportListAPI(APIBaseTest):
self._create_implementation_task_with_run(report_with_pr, pr_url="https://github.com/org/repo/pull/42")
with CaptureQueriesContext(connection) as ctx:
- response = self.client.get(self._list_url(has_implementation_pr="true"))
+ response = self.client.get(self._list_url(has_implementation_pr="true", count_only="true"))
assert response.status_code == status.HTTP_200_OK
# All association subqueries must stay team-scoped. Without the scope the planner can
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 09626c039eb3..e6645b6935f6 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -949,6 +949,8 @@ class SignalReportViewSet(
return self._apply_signal_report_status_filter(qs)
if self.action in {"retrieve", "signals"}:
qs = self._scope_signal_report_queryset(queryset)
+ qs = self._annotate_artefact_count(qs)
+ qs = self._annotate_channel_id(qs)
qs = self._apply_signal_report_status_filter(qs)
qs = self._annotate_latest_actionability(qs)
qs = self._prefetch_signal_report_priority_artefacts(qs)
@@ -956,6 +958,14 @@ class SignalReportViewSet(
return annotate_first_billable_pr_run_at(qs)
qs = queryset
qs = self._scope_signal_report_queryset(qs)
+ renders_reports = self.action not in self._MULTI_REPORT_ACTIONS
+ if renders_reports:
+ # Both annotations are correlated subqueries, and the ordering keeps the page limit
+ # above them, so they cost one artefact walk per report in the team. `list` reads them
+ # from batched per-page lookups instead, and `bulk_state` renders no report.
+ qs = self._annotate_artefact_count(qs)
+ if renders_reports or self._channel_id_filter_value() is not None:
+ qs = self._annotate_channel_id(qs)
qs = self._apply_signal_report_status_filter(qs)
qs = self._apply_signal_report_search_filter(qs)
qs = self._apply_signal_report_source_product_filter(qs)
@@ -970,14 +980,20 @@ class SignalReportViewSet(
qs = self._apply_signal_report_inbox_scope_filter(qs)
qs = self._apply_signal_report_task_filter(qs)
qs = self._annotate_latest_actionability(qs)
+ if self._needs_already_addressed_annotation():
+ qs = self._annotate_latest_already_addressed(qs)
qs = self._apply_signal_report_actionability_filter(qs)
qs = self._apply_signal_report_already_addressed_filter(qs)
qs = self._apply_signal_report_inbox_view_filter(qs)
qs = self._annotate_signal_report_status_rank(qs)
- qs = self._annotate_signal_report_priority(qs)
- qs = self._apply_signal_report_priority_filter(qs)
+ if self._needs_priority_annotation():
+ qs = self._annotate_signal_report_priority(qs)
+ qs = self._apply_signal_report_priority_filter(qs)
qs = self._prefetch_signal_report_priority_artefacts(qs)
- qs = self._annotate_is_suggested_reviewer(qs)
+ if self.action != "bulk_state":
+ # `bulk_state` answers with one outcome per id, never a serialized report, and the list
+ # ordering that reads this value does not apply to it either.
+ qs = self._annotate_is_suggested_reviewer(qs)
if self.action not in self._MULTI_REPORT_ACTIONS:
# This correlated subquery costs one walk per matching row. Multi-row actions do
# without it: `list` serves the value from a batched page lookup, and `bulk_state`
@@ -986,6 +1002,10 @@ class SignalReportViewSet(
return qs
def _scope_signal_report_queryset(self, queryset):
+ # The serializer renders the reverse OneToOne rows inline.
+ return queryset.filter(team=self.team).select_related("refund", "tracker_issue")
+
+ def _annotate_artefact_count(self, queryset):
# Count via a correlated subquery instead of `Count("artefacts")`,
# so the main query doesn't LEFT JOIN + GROUP BY the full artefact table
artefact_count_subquery = Subquery(
@@ -995,6 +1015,11 @@ class SignalReportViewSet(
.values("count"),
output_field=IntegerField(),
)
+ return queryset.annotate(
+ artefact_count=Coalesce(artefact_count_subquery, Value(0), output_field=IntegerField())
+ )
+
+ def _annotate_channel_id(self, queryset):
channel_id_subquery = Subquery(
SignalReportArtefact.objects.filter(
report_id=OuterRef("id"),
@@ -1011,15 +1036,7 @@ class SignalReportViewSet(
.values("live_channel_id")[:1],
output_field=models.UUIDField(),
)
- # The serializer renders the reverse OneToOne rows inline.
- return (
- queryset.filter(team=self.team)
- .select_related("refund", "tracker_issue")
- .annotate(
- artefact_count=Coalesce(artefact_count_subquery, Value(0), output_field=IntegerField()),
- channel_id=channel_id_subquery,
- )
- )
+ return queryset.annotate(channel_id=channel_id_subquery)
# Deleted reports are terminal, so `deleted` never reaches any endpoint (detail, list,
# actions) and is never a valid filter target either.
@@ -1200,9 +1217,8 @@ class SignalReportViewSet(
report_ids_with_prefix = fetch_report_ids_for_scout_prefix(self.team, scout_prefix)
return queryset.filter(id__in=report_ids_with_prefix)
- def _latest_suggested_reviewers_qs(self):
- """`suggested_reviewers` rows that are the *current* (latest) version for the correlated
- outer report (`OuterRef("id")`).
+ def _current_suggested_reviewer_artefacts(self, scope: Q, where: str, params: list[str]):
+ """Current reviewer artefacts in `scope` that match the parameterized predicate.
suggested_reviewers is append-only, so only the newest row is the live reviewer set —
older versions remain as history and must not match. A row is current iff no newer row of
@@ -1215,10 +1231,19 @@ class SignalReportViewSet(
created_at__gt=OuterRef("created_at"),
)
)
- return SignalReportArtefact.objects.filter(
- report_id=OuterRef("id"),
+ reviewer_artefacts = SignalReportArtefact.objects.filter(
+ scope,
type=SignalReportArtefact.ArtefactType.SUGGESTED_REVIEWERS,
- ).filter(~has_newer)
+ )
+ # nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)
+ reviewer_artefacts = reviewer_artefacts.extra(where=[where], params=params)
+ return reviewer_artefacts.filter(~has_newer)
+
+ def _reports_with_suggested_reviewers(self, where: str, params: list[str]):
+ return self._current_suggested_reviewer_artefacts(Q(team=self.team), where, params).values("report_id")
+
+ def _report_has_suggested_reviewer(self, where: str, params: list[str]):
+ return Exists(self._current_suggested_reviewer_artefacts(Q(report_id=OuterRef("id")), where, params))
def _implementation_pr_report_filter(self):
return implementation_pr_report_filter(team_id=self.team.id)
@@ -1270,16 +1295,21 @@ class SignalReportViewSet(
actor = self._request_attribution()
return queryset.filter(reports_with_active_claim(team_id=self.team_id, actor=actor))
- def _apply_signal_report_channel_filter(self, queryset):
+ def _channel_id_filter_value(self) -> uuid.UUID | None:
# `channel_id=<uuid>` narrows to reports assigned to one space. Absent or empty
# leaves the list unchanged (the general view lists every report); a non-UUID is a 400.
raw = self.request.query_params.get("channel_id")
if raw is None or not raw.strip():
- return queryset
+ return None
try:
- channel_id = uuid.UUID(raw.strip())
+ return uuid.UUID(raw.strip())
except (ValueError, AttributeError):
raise serializers.ValidationError({"channel_id": f"Invalid value: {raw!r}. Expected a UUID."})
+
+ def _apply_signal_report_channel_filter(self, queryset):
+ channel_id = self._channel_id_filter_value()
+ if channel_id is None:
+ return queryset
return queryset.filter(channel_id=channel_id)
def _apply_signal_report_suggested_reviewer_filter(self, queryset):
@@ -1315,15 +1345,7 @@ class SignalReportViewSet(
)
reviewer_params.append(github_login)
reviewer_where = " OR ".join(reviewer_clauses)
- return queryset.filter(
- Exists(
- # nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)
- self._latest_suggested_reviewers_qs().extra(
- where=[reviewer_where],
- params=reviewer_params,
- )
- )
- )
+ return queryset.filter(id__in=self._reports_with_suggested_reviewers(reviewer_where, reviewer_params))
def _apply_signal_report_inbox_scope_filter(self, queryset):
scope = self.request.query_params.get("scope")
@@ -1532,10 +1554,28 @@ class SignalReportViewSet(
)
def _annotate_latest_actionability(self, queryset):
- return queryset.annotate(
- latest_actionability_value=self._latest_actionability_field("actionability"),
- latest_already_addressed_value=self._latest_actionability_field("already_addressed"),
- )
+ return queryset.annotate(latest_actionability_value=self._latest_actionability_field("actionability"))
+
+ def _annotate_latest_already_addressed(self, queryset):
+ return queryset.annotate(latest_already_addressed_value=self._latest_actionability_field("already_addressed"))
+
+ def _needs_already_addressed_annotation(self) -> bool:
+ # A second correlated subquery over the same artefact row, so it is worth carrying only for
+ # the two request shapes that read it: the `already_addressed` filter, and the Actionable
+ # view, which hides reports whose issue is already being handled.
+ raw = self.request.query_params.get("already_addressed")
+ if raw is not None and raw.strip():
+ return True
+ return self.request.query_params.get("view") == "actionable"
+
+ def _needs_priority_annotation(self) -> bool:
+ # The priority value is a correlated subquery too, and the serializer renders priority from
+ # the prefetched artefacts instead, so only a priority filter or a priority sort needs it.
+ params = self.request.query_params
+ if params.get("priority") or params.get("use_priority_preference", "false").lower() == "true":
+ return True
+ priority_clause = self._SIGNAL_REPORT_ORDERING_FIELDS["priority"]
+ return any(clause.lstrip("-") == priority_clause for clause in self._parse_signal_report_ordering())
def _prefetch_signal_report_priority_artefacts(self, queryset):
return queryset.prefetch_related(
@@ -1585,18 +1625,17 @@ class SignalReportViewSet(
identity_filters.append(json.dumps([{"github_login": github_login}]))
identity_where = " OR ".join(["content::jsonb @> %s::jsonb"] * len(identity_filters))
- suggested_exists = Exists(
- # nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)
- self._latest_suggested_reviewers_qs().extra(
- where=[identity_where],
- params=identity_filters,
- )
+ names_the_user = (
+ Q(id__in=self._reports_with_suggested_reviewers(identity_where, identity_filters))
+ if self.action == "list"
+ else Q(self._report_has_suggested_reviewer(identity_where, identity_filters))
)
return queryset.annotate(
is_suggested_reviewer=Case(
When(self._Q_READY_NOT_ACTIONABLE, then=Value(False)),
When(status=SignalReport.Status.FAILED, then=Value(False)),
- default=suggested_exists,
+ When(names_the_user, then=Value(True)),
+ default=Value(False),
output_field=BooleanField(),
),
)
@@ -2099,6 +2138,16 @@ class SignalReportViewSet(
# actions carry, for the serializer's refund_ineligibility_reason field.
with tracer.start_as_current_span("signals.reports.list.fetch_billable_pr_runs"):
first_billable_pr_run_at_map = first_billable_pr_run_at_by_report(report_ids)
+
+ # Same trade for the two artefact-derived fields the row renders: one query each over the
+ # page, rather than a correlated subquery the sort makes Postgres run for every report in
+ # the team. The serializer reads both off the instance, as it does for the other actions.
+ with tracer.start_as_current_span("signals.reports.list.fetch_artefact_fields"):
+ artefact_counts = SignalReportArtefact.counts_by_report(report_ids)
+ live_channel_ids = SignalReportArtefact.live_channel_ids_by_report(report_ids)
+ for report in reports:
+ report.artefact_count = artefact_counts.get(str(report.id), 0)
+ report.channel_id = live_channel_ids.get(str(report.id))
context = {
**self.get_serializer_context(),
"source_products_map": {rid: meta.source_products for rid, meta in signal_meta_map.items()},