Description
MetricsCollectionService.applyRetentionPolicy is tested in apps/backend/tests/metrics/collection.test.ts but no cron job calls it. The deployment_analytics table will grow unbounded in production, degrading query performance over time.
Requirements and context
- Must be secure, tested, and documented where applicable
- Should stay reviewable and fit the current monorepo structure
- Relevant files:
apps/backend/tests/metrics/collection.test.ts, apps/backend/src/services/analytics.service.ts, apps/backend/src/app/api/cron/
Suggested execution
- Create branch:
issue-023-analytics-retention-policy-cron
- Keep changes scoped to the issue and reference the task IDs in the PR
Implement changes
- Add
applyRetentionPolicy(retentionDays) to AnalyticsService that deletes records older than the threshold
- Create
apps/backend/src/app/api/cron/purge-analytics/route.ts calling the service
- Default retention: 90 days; configurable via
ANALYTICS_RETENTION_DAYS env var
- Register in
vercel.json crons (daily schedule)
Test and commit
- Add cron route tests: records within retention kept, records outside deleted, env var override respected
- Verify the cron returns
{ deleted: N } in the response body
- Edge case: retention days set to 0 — treat as disabled, delete nothing
Example commit message
feat(cron): add daily analytics retention purge job
Guidelines
- Prefer small, reviewable PRs
- Keep naming and data contracts consistent with the spec docs
Description
MetricsCollectionService.applyRetentionPolicyis tested inapps/backend/tests/metrics/collection.test.tsbut no cron job calls it. Thedeployment_analyticstable will grow unbounded in production, degrading query performance over time.Requirements and context
apps/backend/tests/metrics/collection.test.ts,apps/backend/src/services/analytics.service.ts,apps/backend/src/app/api/cron/Suggested execution
issue-023-analytics-retention-policy-cronImplement changes
applyRetentionPolicy(retentionDays)toAnalyticsServicethat deletes records older than the thresholdapps/backend/src/app/api/cron/purge-analytics/route.tscalling the serviceANALYTICS_RETENTION_DAYSenv varvercel.jsoncrons (daily schedule)Test and commit
{ deleted: N }in the response bodyExample commit message
Guidelines