Skip to content

[#503] perf: compute admin stats with database aggregates instead of loading all rows - #531

Open
Myart352 wants to merge 9 commits into
JSE-ORG:devfrom
Myart352:fix/503-admin-stats-aggregates
Open

[#503] perf: compute admin stats with database aggregates instead of loading all rows#531
Myart352 wants to merge 9 commits into
JSE-ORG:devfrom
Myart352:fix/503-admin-stats-aggregates

Conversation

@Myart352

@Myart352 Myart352 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Closes #503

Problem

AdminStatsService.getStats ran escrow.findMany({}) and dispute.findMany({}) with no filter and no projection, then computed every figure in JavaScript. All eight numbers it returns are aggregates the database can compute. Memory use and latency were linear in total table size.

Solution

Replaced findAll + in-memory reduce/set computations with Prisma count, aggregate, groupBy and select-based distinct queries.

Changes Made

  • src/prisma/prisma.service.ts: Added aggregate() and groupBy() to escrow object, added dispute.count()
  • src/admin/stats/admin-stats.service.ts: Rewrote getStats() to use database-level aggregations

Test Results

All 8 existing tests pass with identical expected values.

@Myart352
Myart352 requested a review from Omoboi-dev as a code owner July 29, 2026 01:40
@drips-wave

drips-wave Bot commented Jul 29, 2026

Copy link
Copy Markdown

@Myart352 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Myart352 Myart352 changed the title perf(admin): compute stats with database aggregates [#503] perf: compute admin stats with database aggregates instead of loading all rows Jul 29, 2026
@Myart352
Myart352 force-pushed the fix/503-admin-stats-aggregates branch from 66e5645 to 4cb0a91 Compare July 29, 2026 09:44
@Myart352
Myart352 force-pushed the fix/503-admin-stats-aggregates branch from 4cb0a91 to ecbbdea Compare July 29, 2026 09:54
Myart352 added 3 commits July 29, 2026 10:06
- api-keys.controller.spec.ts: mock guards with overrideProvider to fix
  401 vs 403 mismatch; use Bearer prefix to distinguish admin/non-admin
- prisma.service.spec.ts: wrap sync-throwing create in async function
  so .rejects matcher works instead of timing out
- tracking-poll.worker.intervals.spec.ts: add claimDelivery and
  clearDeliveryClaim to escrowRepository mock
@Myart352
Myart352 force-pushed the fix/503-admin-stats-aggregates branch from 285f841 to e4620a1 Compare July 29, 2026 10:31
Use jest.requireActual instead of dynamic import for
credential-encryption.util to avoid ESM module errors.
@Myart352
Myart352 force-pushed the fix/503-admin-stats-aggregates branch from e4620a1 to f69ab29 Compare July 29, 2026 10:42
@Myart352
Myart352 force-pushed the fix/503-admin-stats-aggregates branch from 10ba682 to dde1fbf Compare July 29, 2026 11:10
Myart352 added 2 commits July 29, 2026 11:14
…irectly

NestJS @UseGuards resolves classes directly regardless of mock
providers, so the module-based approach could never work.
Test the controller business logic directly instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET /admin/stats loads every escrow and every dispute into memory

1 participant