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.

perf(signals): count inbox search matches in the page scan (#125978)

Dylan Martin9158ccfac3a6
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index 79cdc7ad3f90..9a1310ac884d 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -2592,12 +2592,12 @@ class TestSignalReportListAPI(APIBaseTest):
[
("short_first_page", 10, 0, 3, False, False),
("short_last_page", 2, 2, 1, False, False),
- ("exact_full_page", 3, 0, 3, True, False),
- ("full_page_with_more", 2, 0, 2, True, True),
+ ("exact_full_page", 3, 0, 3, False, False),
+ ("full_page_with_more", 2, 0, 2, False, True),
("offset_past_the_end", 2, 5, 0, True, False),
]
)
- def test_search_page_counts_without_a_second_scan_when_the_page_is_short(
+ def test_search_page_counts_without_a_second_scan(
self, _name: str, limit: int, offset: int, rows: int, runs_count_query: bool, has_next: bool
) -> None:
for i in range(3):
@@ -2605,12 +2605,15 @@ class TestSignalReportListAPI(APIBaseTest):
self._create_report(title="Unrelated")
with CaptureQueriesContext(connection) as ctx:
- response = self.client.get(self._list_url(search="checkout", limit=limit, offset=offset))
+ response = self.client.get(
+ self._list_url(search="checkout", ordering="-created_at", limit=limit, offset=offset)
+ )
assert response.status_code == status.HTTP_200_OK
body = response.json()
assert body["count"] == 3
- assert len(body["results"]) == rows
+ newest_first = ["Checkout failure 2", "Checkout failure 1", "Checkout failure 0"]
+ assert [r["title"] for r in body["results"]] == newest_first[offset : offset + rows]
assert (body["next"] is not None) == has_next
count_queries = [
q for q in ctx.captured_queries if 'COUNT(*) AS "__count" FROM "signals_signalreport"' in q["sql"]
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 47586fa8ab81..73f988be4893 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -30,6 +30,7 @@ from django.db.models import (
Sum,
Value,
When,
+ Window,
)
from django.db.models.expressions import OrderBy
from django.db.models.functions import Cast, Coalesce
@@ -1303,15 +1304,32 @@ class SignalReportListPagination(LimitOffsetPagination):
return None
self.offset = self.get_offset(request)
self.request = request
- page = list(queryset[self.offset : self.offset + self.limit])
+ if request.query_params.get("search"):
+ page, window_count = self._search_page(queryset, self.offset, self.limit)
+ else:
+ page, window_count = list(queryset[self.offset : self.offset + self.limit]), None
if len(page) < self.limit and (page or self.offset == 0):
self.count = self.offset + len(page)
+ elif window_count is not None:
+ self.count = window_count
else:
self.count = self.get_count(queryset)
if self.count > self.limit and self.template is not None:
self.display_page_controls = True
return page
+ def _search_page(self, queryset, offset: int, limit: int) -> tuple[list[SignalReport], int | None]:
+ # Ids only, because COUNT(*) OVER () buffers every match and full report rows spill to disk.
+ rows = list(
+ queryset.annotate(search_total=Window(Count("*"))).values_list("pk", "search_total")[
+ offset : offset + limit
+ ]
+ )
+ if not rows:
+ return [], None
+ reports = {report.pk: report for report in queryset.filter(pk__in=[pk for pk, _ in rows]).order_by()}
+ return [reports[pk] for pk, _ in rows if pk in reports], rows[0][1]
+
def get_paginated_response_schema(self, schema):
paginated = super().get_paginated_response_schema(schema)
paginated["properties"]["ranker"] = {
diff --git a/products/signals/frontend/inbox/logics/inboxFiltersLogic.test.ts b/products/signals/frontend/inbox/logics/inboxFiltersLogic.test.ts
index 55b485d50dd8..177f48f4d934 100644
--- a/products/signals/frontend/inbox/logics/inboxFiltersLogic.test.ts
+++ b/products/signals/frontend/inbox/logics/inboxFiltersLogic.test.ts
@@ -15,6 +15,7 @@ import { initKeaTests } from '~/test/init'
import { INBOX_EVENTS } from '../inboxAnalytics'
import {
+ appliedInboxSearch,
buildSignalReportListOrdering,
buildSignalReportListSortParams,
filterSearchParams,
@@ -70,6 +71,17 @@ describe('inboxFiltersLogic', () => {
})
})
+ describe('appliedInboxSearch', () => {
+ it.each([
+ ['', undefined],
+ [' ai ', undefined],
+ [' api ', 'api'],
+ ['checkout crash', 'checkout crash'],
+ ])('applies %p as %p', (input, expected) => {
+ expect(appliedInboxSearch(input)).toBe(expected)
+ })
+ })
+
describe('filter URL params', () => {
// Keeps shared links clean: a default view must not carry any filter params.
it('omits all default filters from the URL', () => {
diff --git a/products/signals/frontend/inbox/logics/inboxFiltersLogic.ts b/products/signals/frontend/inbox/logics/inboxFiltersLogic.ts
index 02724fb8493c..b559df9e2d73 100644
--- a/products/signals/frontend/inbox/logics/inboxFiltersLogic.ts
+++ b/products/signals/frontend/inbox/logics/inboxFiltersLogic.ts
@@ -298,6 +298,13 @@ export function buildSignalReportListOrdering(field: InboxSortField, direction:
return field === 'updated_at' ? `${fieldKey},status` : `${fieldKey},status,-updated_at`
}
+const MIN_TRIGRAM_SEARCH_LENGTH = 3
+
+export function appliedInboxSearch(searchQuery: string): string | undefined {
+ const search = searchQuery.trim()
+ return search.length >= MIN_TRIGRAM_SEARCH_LENGTH ? search : undefined
+}
+
/**
* The sort params for the list request. Recommended is a server preset that a user flag resolves
* to a ranker, so it goes out as `sort` with no `ordering`, because `ordering` overrides the preset.
@@ -789,7 +796,7 @@ export const inboxFiltersLogic = kea<inboxFiltersLogicType>([
priorityFilter: SignalReportPriority[],
activeCreatedWindow: InboxCreatedWindow | null
): boolean =>
- searchQuery.trim().length > 0 ||
+ appliedInboxSearch(searchQuery) !== undefined ||
sourceProductFilter.length > 0 ||
scoutFilter.length > 0 ||
priorityFilter.length > 0 ||
diff --git a/products/signals/frontend/inbox/logics/reportListLogic.ts b/products/signals/frontend/inbox/logics/reportListLogic.ts
index 0926c2007f35..b065f58d01a9 100644
--- a/products/signals/frontend/inbox/logics/reportListLogic.ts
+++ b/products/signals/frontend/inbox/logics/reportListLogic.ts
@@ -44,7 +44,7 @@ import { isReportMetricsEnabled, mergeReportMetricSnapshots, reportNeedsMetricRe
import { reportPullRequests, primaryReportPullRequest } from '../utils/reportPullRequests'
import { setReportState } from '../utils/writeTimeout'
import { inboxBulkActionsLogic } from './inboxBulkActionsLogic'
-import { buildSignalReportListSortParams, inboxFiltersLogic } from './inboxFiltersLogic'
+import { appliedInboxSearch, buildSignalReportListSortParams, inboxFiltersLogic } from './inboxFiltersLogic'
import type { InboxCreatedWindow, InboxFilterState, InboxSortDirection, InboxSortField } from './inboxFiltersLogic'
import { prCiStatusLogic } from './prCiStatusLogic'
@@ -130,7 +130,7 @@ function requestContextFromValues(values: {
return {
scope: values.scope,
hasActiveFilters:
- values.searchQuery.trim().length > 0 ||
+ appliedInboxSearch(values.searchQuery) !== undefined ||
values.activeSourceProductFilter.length > 0 ||
values.activeScoutFilter.length > 0 ||
values.priorityFilter.length > 0 ||
@@ -748,7 +748,7 @@ export const reportListLogic = kea<reportListLogicType>([
) => {
return {
...listParams,
- search: searchQuery.trim() || undefined,
+ search: appliedInboxSearch(searchQuery),
...buildSignalReportListSortParams(sortField, sortDirection),
source_product: sourceProductFilter.length > 0 ? sourceProductFilter.join(',') : undefined,
scout: scoutFilter.length > 0 ? scoutFilter.join(',') : undefined,