Add dashboard ClickHouse read models

Merged
AS
6mo ago
+1432-789

Summary

  • Add dbt-owned ClickHouse read models for healthspan, recovery, and strain so dashboard APIs read compact serving tables instead of recomputing dashboard metrics per request.
  • Switch recovery, stress, sleep need, mobile dashboard, and healthspan query paths to the new read models, using FINAL where freshness depends on ReplacingMergeTree.
  • Add ClickHouse table creation, dbt safe-model ordering, test helper coverage, and docs for the dashboard serving layer.

Verification

  • set -a; . ./.env.local; set +a; pnpm lint
  • pnpm tsc --noEmit
  • cd packages/server && pnpm tsc --noEmit
  • cd packages/web && pnpm tsc --noEmit
  • set -a; . ./.env.local; set +a; pnpm test --run

Summary by cubic

Adds ClickHouse dashboard read models for recovery, strain, and healthspan, and switches related routes to read compact tables to cut latency and database load. Incremental builds are now micro-batched and bounded per user/day/week to avoid backfills; healthspan uses VO2 started date and strain date filters are timezone-safe.

  • New Features

    • Added read models: analytics.recovery_read_model, analytics.strain_read_model, analytics.healthspan_read_model using ReplacingMergeTree(refresh_version) with bounded incremental materialization; routes use FINAL where needed.
    • Switched recovery, stress (incl. strength-stress), sleep-need, mobile dashboard, and healthspan APIs to these models; strain queries pass the user timezone; simplified the healthspan query path.
  • Migration

    • Run ClickHouse migrations, including 0026_create_named_dashboard_read_model_tables.ts, then rebuild dbt models with the updated safe model list.
    • No API surface changes or client updates required.

Summary by CodeRabbit

  • New Features
    • Added dedicated precomputed dashboard models serving Recovery, Strain, and Healthspan metrics for route-facing queries.
  • Performance Improvements
    • Dashboard metrics for recovery, strain, and healthspan now load from compact read models, reducing request-time aggregation and latency.
  • Documentation
    • Updated analytics and deploy docs and added a production incident entry describing the change and mitigation.
  • Tests & Maintenance
    • Added migration and test coverage for the new read models and updated integration/unit tests.