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
Description
src/routes/analytics.tscomputes aggregation rollups on demand againstpayments,escrowEvents, andagreementEvents, fronted only by the short-lived in-processAnalyticsCache(src/utils/analytics-cache.ts, TTL fromANALYTICS_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
src/db/migrations/covering the same aggregation shapesrc/routes/analytics.tscurrently computes in application codeREFRESH MATERIALIZED VIEW CONCURRENTLYjob (script or scheduled task) and document its cadence relative toANALYTICS_CACHE_TTL_MSsrc/routes/analytics.tsto read from the view first, falling back to the live aggregation query if the view is stale beyond a configurable boundSuggested execution
Fork the repo and create a branch
Implement changes
src/db/schema.tssrc/db/migrations/(new migration)src/routes/analytics.tssrc/routes/analytics.aggregation.test.tsdocs/routes/analytics.mdTest and commit
pnpm testExample commit message
Guidelines