: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(signals): sort the scout findings summary without a task run join (#120867)
posthog[bot]b36188859f5d
diff --git a/products/signals/backend/scout_harness/tools/runs.py b/products/signals/backend/scout_harness/tools/runs.pyindex 4b93a083cf98..528ab9f54571 100644--- a/products/signals/backend/scout_harness/tools/runs.py+++ b/products/signals/backend/scout_harness/tools/runs.py@@ -26,7 +26,6 @@ from uuid import UUIDfrom django.db import connectionfrom django.db.models import Q-from django.db.models.functions import Coalescefrom django.utils import timezoneimport structlog@@ -536,26 +535,39 @@ def fleet_findings_summary(*, team_id: int, window_hours: int = DEFAULT_FINDINGStouched_a_report = (~Q(emitted_report_ids=[]) & ~Q(emitted_report_ids__isnull=True)) | (~Q(edited_report_ids=[]) & ~Q(edited_report_ids__isnull=True))- # Load the ids first. As an `id__in` subquery, the planner can start from `posthog_task_run`,- # which holds every product's task runs, instead of from these few scout runs.- candidate_ids = list(+ # No index can serve a sort on `Coalesce(task_run.completed_at, created_at)`, so read the+ # candidates without a task run join and sort them below on completion times read by primary key.+ candidates = list(SignalScoutRun.objects.filter(team_id=team_id, created_at__gte=window_start).exclude(metadata__has_key="scout_trial").filter(Q(emitted_count__gt=0) | touched_a_report).order_by("-created_at", "-id")- .values_list("id", flat=True)[:FLEET_FINDINGS_SUMMARY_CANDIDATE_RUNS]+ .values_list(+ "id",+ "task_run_id",+ "created_at",+ "emitted_count",+ "skill_name",+ "emitted_report_ids",+ "edited_report_ids",+ )[:FLEET_FINDINGS_SUMMARY_CANDIDATE_RUNS])+ completed_at_by_task_run = tasks_facade.get_task_run_completed_at(+ [candidate[1] for candidate in candidates], team_id=team_id+ )++ def run_emitted_at(task_run_id: UUID, created_at: datetime) -> datetime:+ return completed_at_by_task_run.get(task_run_id) or created_at+# Order by completion (fall back to creation) so the cap keeps the *most recently emitted* runs,# matching the frontend's `completed_at ?? created_at` sort; `-id` tie-breaks on the time-ordered PK.- rows = (- SignalScoutRun.objects.filter(team_id=team_id, id__in=candidate_ids)- .annotate(_emitted_at=Coalesce("task_run__completed_at", "created_at"))- .order_by("-_emitted_at", "-id")- .values_list("emitted_count", "skill_name", "_emitted_at", "emitted_report_ids", "edited_report_ids")[+ ranked = sorted(candidates, key=lambda c: (run_emitted_at(c[1], c[2]), c[0]), reverse=True)+ materialized = [+ (emitted_count, skill_name, run_emitted_at(task_run_id, created_at), emitted_report_ids, edited_report_ids)+ for _, task_run_id, created_at, emitted_count, skill_name, emitted_report_ids, edited_report_ids in ranked[:FLEET_FINDINGS_SUMMARY_RUN_CAP]- )- materialized = list(rows)+ ]count = 0scouts: set[str] = set()latest_at: datetime | None = Nonediff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 8e16fbd9b38c..2d4c078c41cd 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -303,6 +303,7 @@ __all__ = ["task_access_scope","normalize_verified_pull_request_urls_batch","get_task_id_for_run",+ "get_task_run_completed_at","get_task_run","get_task_run_session","sync_task_run_session",@@ -1198,6 +1199,17 @@ def get_task_id_for_run(run_id: str | UUID, team_id: int) -> UUID | None:return TaskRun.objects.filter(id=run_id, team_id=team_id).values_list("task_id", flat=True).first()+def get_task_run_completed_at(run_ids: Iterable[str | UUID], team_id: int) -> dict[UUID, datetime | None]:+ """Each run's ``completed_at`` by run id, team-scoped. Runs not found for the team are left out.++ A primary-key read for callers that only need the completion time, so they skip a join to the+ task run table.+ """+ return dict(+ TaskRun.objects.filter(id__in=list(run_ids), team_id=team_id).order_by().values_list("id", "completed_at")+ )++def task_access_scope(*, team_id: int, user: User) -> contracts.TaskAccessScope:team = Team.objects.get(id=team_id)access = UserAccessControl(user=user, team=team)