fix(metrics): speed up top workflows by runs count query

Merged
MI
1d ago
+233-51

Summary

The SDLC Insights "top workflows" metric (OrgMetricsService/TopWorkflowsByRunsCount) had two inefficiencies, visible in production traces for a large organization:

  • The aggregate query reached workflow_runs through the organizations and workflows tables, so it could not use the (organization_id, created_at) index. Its latency varied widely between calls (6ms to 300ms in sampled traces).
  • After aggregating, it loaded every workflow with runs in the time window one by one, plus its project (about 2 queries per workflow, ~160 sequential queries in the sampled traces), and only then kept the top N (at most 20).

This change:

  • Aggregates directly on workflow_runs.organization_id so the existing index drives the scan.
  • Selects the top N workflows before loading them, and loads them with their projects in a single query.
  • No longer counts runs of soft-deleted workflows, which previously could appear in the top list.
  • Breaks ties by workflow ID so the order is deterministic.

Refs PFM-8118. This reduces the latency and variance of one of the SDLC Insights requests. It does not on its own confirm the root cause of the intermittent code 13 errors reported in the ticket.

This change was produced with the assistance of Claude Code.