Skip to content

Implement analytics metrics retention policy and purge cron #497

@temma02

Description

@temma02

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

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave program

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions