perf(server): use monthly serving models

Merged
AS
4mo ago
+253-41

Summary

  • remove recursive activity, sleep, metrics, and resting-heart-rate views from the monthly report request path
  • read the compact activity, daily sleep, and daily recovery serving models instead
  • add a real-ClickHouse regression that succeeds with the recursive views absent

Production evidence

  • old exact query: 120.012s timeout, 3,777,652 rows / 995.16 MiB read, 1.04 GiB peak
  • compact-model query: 0.101s on the same production data

Validation

  • pnpm test:integration -- packages/server/src/repositories/monthly-report-repository.integration.test.ts
  • pnpm exec vitest run packages/server/src/repositories/monthly-report-repository.test.ts
  • targeted Biome check
  • pnpm typecheck
  • pnpm --dir packages/server typecheck

Fixes DOFEK-SERVER-5C.


Summary by cubic

Switch the monthly report to ClickHouse serving models to eliminate timeouts and cut query time from ~120s to ~0.101s. Fixes DOFEK-SERVER-5C with no changes to the response shape.

  • Bug Fixes

    • Read activity from analytics.activity_summary, sleep from analytics.daily_sleep FINAL, and recovery from analytics.daily_recovery FINAL; removed recursive views and the resting‑HR CTE.
    • Added a real ClickHouse integration test that seeds only serving models, drops recursive views, and validates a 12‑month report and serving lifecycle; added a unit test to verify user and month parameter bindings.
    • Recorded exact ClickHouse query‑log evidence and runbook classification in the production incident baseline.
  • Refactors

    • Removed the repository/router timezone param; queries rely on UTC and serving‑model dates. Deleted the related unit test.

Summary by CodeRabbit

  • Bug Fixes

    • Improved monthly report performance to prevent ClickHouse timeouts.
    • Monthly reports now use optimized analytics data sources while preserving existing calculations and response formats.
    • Applied timezone-aware date handling and excluded deleted sleep and recovery records.
  • Tests

    • Added integration coverage validating monthly report values, averages, and historical results against ClickHouse data.
  • Documentation

    • Documented the production incident, mitigation, validation results, and remaining follow-up steps.