Skip to content

Leaderboard loads all approved proofs into memory and aggregates in JS #62

Description

@cybermax4200

Why this matters now: Same unbounded-memory class as #10 but on a hot, publicly-hit endpoint; will degrade under real traffic.

Problem / What: getLeaderboard (leaderboardController.ts:20-31) does prisma.proof.findMany({ where: { status: 'APPROVED', createdAt... } }) with no limit, builds a Map in JS, then does a second findMany for users. At scale this is both a memory and a latency problem, and the in-JS sort is O(n log n) on a full table scan.

Key Challenges: Pushing aggregation into SQL (window functions / GROUP BY userId with SUM and COUNT, plus a time-window filter) and paginating the top-N without materializing all rows.

Acceptance Criteria:

  • Leaderboard computation is a bounded SQL query; memory is independent of total proof count.
  • Results (including period filter) match the current implementation on a seeded dataset.

Relevant files/functions: src/controllers/leaderboardController.ts:4-59, prisma/schema.prisma.

Out of scope: Leaderboard caching/denormalization (follow-up).

Labels: advanced, performance

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions