Skip to content

Back monthly analytics aggregates with a materialized view instead of repeated full-table scans in src/routes/analytics.ts #660

Description

@Jagadeeshftw

Description

src/routes/analytics.ts computes aggregation rollups on demand against payments, escrowEvents, and agreementEvents, fronted only by the short-lived in-process AnalyticsCache (src/utils/analytics-cache.ts, TTL from ANALYTICS_CACHE_TTL_MS). Every cache miss still re-runs the full aggregation query. Add a Postgres materialized view precomputing the monthly rollups so a cache miss becomes a cheap indexed read instead of a full scan, refreshed on a schedule.

Requirements and context

  • Add a materialized view definition and refresh migration under src/db/migrations/ covering the same aggregation shape src/routes/analytics.ts currently computes in application code
  • Add a REFRESH MATERIALIZED VIEW CONCURRENTLY job (script or scheduled task) and document its cadence relative to ANALYTICS_CACHE_TTL_MS
  • Update src/routes/analytics.ts to read from the view first, falling back to the live aggregation query if the view is stale beyond a configurable bound
  • Must be secure, tested, and documented
  • Should be efficient and easy to review

Suggested execution

Fork the repo and create a branch

git checkout -b feature/analytics-materialized-view

Implement changes

  • Update/Write: src/db/schema.ts
  • Update/Write: src/db/migrations/ (new migration)
  • Update/Write: src/routes/analytics.ts
  • Add/Update tests: src/routes/analytics.aggregation.test.ts
  • Add documentation: docs/routes/analytics.md
  • Include clear code comments and TS types
  • Validate security assumptions

Test and commit

  • Run tests: pnpm test
  • Cover edge cases
  • Include test output and security notes

Example commit message

feat: back analytics aggregates with a materialized view

Guidelines

  • Minimum 95 percent test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial Campaign | FWC26GrantFox official campaign issuebackendBackend / API workdatabaseDatabase / schema / indexing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions