Skip to content

fix: bound loans getStats query with SQL aggregation #66

Description

@EmeditWeb

Problem

In loans.service.ts (lines 376-378), getStats() does select * from loans with no LIMIT clause. On a production database with millions of loans, this query will crash with OOM or timeout.

User impact: At scale, the protocol stats endpoint will crash the database.

Root Cause

No pagination was added to the stats query during development.

What To Build

  1. Add a LIMIT clause to the stats query (e.g., LIMIT 10000 for recent loans)
  2. Aggregate stats using SQL aggregation (COUNT, SUM, AVG) instead of loading all rows
  3. Cache the result with a 5-minute TTL

Files To Touch

  • src/modules/loans/loans.service.ts — lines 376-378 add limit and aggregation

Acceptance Criteria

  • Stats query uses SQL aggregation
  • No unbounded SELECT * queries
  • Result cached for 5 minutes
  • Build passes

Mandatory Checks

  • context/ files read
  • npm run build passes
  • PR references this issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions