Skip to content

Analytics endpoint loads the entire approved-proof table into memory to sum rewards #61

Description

@cybermax4200

Why this matters now: Platform analytics is an admin endpoint that will OOM / latency-spike as proof volume grows into the millions.

Problem / What: getPlatformAnalytics (analyticsController.ts:11-15) calls prisma.proof.findMany({ where: { status: 'APPROVED' } }) with no limit and sums rewardAmountMicros in JS. This fetches every approved proof row (including the joined task) on each call.

Key Challenges: Rewriting as a SQL aggregation (SUM(reward_amount_micros) with a WHERE status = 'APPROVED') via Prisma aggregate/groupBy or $queryRaw; keeping the same response shape.

Acceptance Criteria:

  • The endpoint issues a single aggregate query and its memory footprint is independent of proof count.
  • Response values match the previous implementation on a seeded dataset (test).

Relevant files/functions: src/controllers/analyticsController.ts:4-34, prisma/schema.prisma (Proof/Task).

Out of scope: Caching layer (can be a 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