Skip to content

perf(system): collapse dashboard user metrics into one query #790

Closed

Description

@dr-hoseyn

Problem

GET /api/system/users builds dashboard user metrics with two sequential database statements: one grouped status count and one recent-online count. The dashboard polls this endpoint every five seconds and the Statistics page every two seconds, so every active dashboard continuously multiplies database round trips.

The current queries return correct data and use admin-scoped indexes where available, but the two scans/round trips are avoidable because all metrics come from the same filtered users relation.

Proposed solution

  • Compute total, per-status, and recent-online counts with one conditional-aggregation statement.
  • Preserve global and admin-scoped behavior, the two-minute online window, and the existing API response.
  • Keep the result fully fresh; do not introduce a cache or stale response window.
  • Add regression coverage for metric values, admin isolation, empty datasets, and a one-SELECT query budget.
  • Validate generated SQL on supported dialects and benchmark the consolidated query against the current two-query path.

Alternatives considered

  • Frontend polling changes: already covered by perf(frontend): integrate page visibility API for smart polling #604 and do not remove backend duplication for visible dashboards.
  • Short-lived caching: reduces load but introduces multi-worker invalidation/freshness concerns and is not required to remove the duplicate round trip.
  • New indexes: should only be added if production-representative EXPLAIN output shows that the consolidated query needs them.

Area

API / Database

Impact

Important. This is a continuously polled dashboard endpoint, so small per-request savings compound across active administrators and workers.

Acceptance criteria

  • One SQL SELECT returns all user count metrics for the endpoint.
  • Global and admin-scoped metrics match current behavior.
  • Empty status groups return zero.
  • Online users use the existing two-minute threshold.
  • No API schema, cache, migration, or frontend change.
  • Focused tests and a reproducible benchmark are included in the PR evidence.

Version

Current dev at 3331421e.

Activity

  1. dr-hoseyn commented on Aug 14, 2026

    @dr-hoseyn
    ContributorAuthor

    Implementation note: a 500k-row synthetic benchmark showed that one conditional table scan improved the global path (~47%) but regressed the admin-scoped path because it stopped using the existing (admin_id, status) and (admin_id, online_at) indexes. The implementation will therefore remain one SQL statement while using conditional aggregation globally and index-friendly scalar aggregates for scoped requests. This preserves the one-round-trip acceptance criterion without trading away scoped query performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions