Repository navigation
Switch sleep dashboard reads to ClickHouse #1151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1a9174e
Switch sleep dashboard reads to ClickHouse
Asherlc 930e14d
Merge remote-tracking branch 'origin/main' into Asherlc/dashboard-sle…
Asherlc e091b8b
Remove unused sleep SQL fragments
Asherlc 38d91f9
Fix sleep ClickHouse CI tests
Asherlc d2101e9
Filter v_sleep proxy view from secondary migration replay
Asherlc 2445ac4
Avoid alias shadowing in ClickHouse v_sleep window query
Asherlc 9c2a0d4
Fix v_sleep query alias shadowing in outer SELECT
Asherlc c204846
Merge remote-tracking branch 'origin/main' into Asherlc/dashboard-sle…
Asherlc 03cc283
Address PR review comments
Asherlc 7449152
Constrain fetchLatestSleepNight to analysis endDate
Asherlc 29a096e
Fix sleep dashboard regressions from ClickHouse migration
Asherlc 6cab36a
Fix ClickHouse sleep CI and review comments
Asherlc f5d0407
Address CodeRabbit test fixture comments
Asherlc 91ed8bd
Improve life events mutation coverage
Asherlc 46b0758
Improve healthspan mutation coverage
Asherlc e241de9
Stabilize Apple Health sleep fixture
Asherlc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
docs/superpowers/plans/2026-05-20-clickhouse-sleep-dashboard.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # ClickHouse Sleep Dashboard Implementation Plan | ||
|
|
||
| > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. | ||
|
|
||
| **Goal:** Remove dashboard/runtime dependence on stale `fitness.v_sleep` and read deduped sleep data from ClickHouse `analytics.v_sleep`. | ||
|
|
||
| **Architecture:** Keep raw sleep writes in Postgres. Treat ClickHouse `analytics.v_sleep` as the runtime deduped sleep read model and fail loudly when a route needs it without `ctx.sensorStore`. Keep Postgres only for raw `sleep_stage` rows where stage intervals are not mirrored yet. | ||
|
|
||
| **Tech Stack:** TypeScript, tRPC, Zod, Drizzle SQL for Postgres, `ActivitySensorStore.query()` for ClickHouse, Vitest. | ||
|
|
||
| --- | ||
|
|
||
| ### Task 1: Add Red Tests For ClickHouse Sleep Reads | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| **Files:** | ||
| - Modify: `packages/server/src/repositories/sleep-repository.test.ts` | ||
| - Modify: `packages/server/src/routers/sleep-need.test.ts` | ||
| - Modify: `packages/server/src/routers/mobile-dashboard.test.ts` | ||
|
|
||
| - [ ] **Step 1: Add repository tests proving list/latest sleep data comes from the sensor store** | ||
|
|
||
| Add tests that construct `SleepRepository` with `makeMockSensorStore()` and assert `analytics.v_sleep` appears in the ClickHouse query while `db.execute` is not called for `list()` and `getLatest()`. | ||
|
|
||
| - [ ] **Step 2: Add router tests proving sleep need and mobile dashboard use ClickHouse sleep rows** | ||
|
|
||
| Add tests that return sleep rows from `sensorStore.query()` and assert generated query strings include `analytics.v_sleep` and do not include `fitness.v_sleep`. | ||
|
|
||
| - [ ] **Step 3: Run red tests** | ||
|
|
||
| Run: | ||
|
|
||
| ```bash | ||
| pnpm vitest packages/server/src/repositories/sleep-repository.test.ts packages/server/src/routers/sleep-need.test.ts packages/server/src/routers/mobile-dashboard.test.ts | ||
| ``` | ||
|
|
||
| Expected: FAIL because production code still queries `fitness.v_sleep`. | ||
|
|
||
| ### Task 2: Add Shared ClickHouse Sleep Helpers | ||
|
|
||
| **Files:** | ||
| - Create: `packages/server/src/repositories/clickhouse-sleep-repository.ts` | ||
| - Modify: `packages/server/src/routers/test-helpers.ts` | ||
|
|
||
| - [ ] **Step 1: Implement helper functions** | ||
|
|
||
| Create helpers for one-row-per-night sleep queries over `analytics.v_sleep`, including `fetchSleepNights()`, `fetchLatestSleepNight()`, and `fetchSleepEfficiencyByDate()`. | ||
|
|
||
| - [ ] **Step 2: Parse query results with Zod** | ||
|
|
||
| Use schemas with nullable numeric fields and string dates/timestamps, matching the existing route output contracts. | ||
|
|
||
| ### Task 3: Switch Dashboard Sleep Routes | ||
|
|
||
| **Files:** | ||
| - Modify: `packages/server/src/repositories/sleep-repository.ts` | ||
| - Modify: `packages/server/src/routers/sleep.ts` | ||
| - Modify: `packages/server/src/routers/sleep-need.ts` | ||
| - Modify: `packages/server/src/routers/mobile-dashboard.ts` | ||
| - Modify: `packages/server/src/routers/recovery.ts` | ||
| - Modify: `packages/server/src/routers/stress.ts` | ||
|
|
||
| - [ ] **Step 1: Pass `ctx.sensorStore` into `SleepRepository`** | ||
|
|
||
| Routes that expose sleep list/latest data should require ClickHouse for deduped sleep data. | ||
|
|
||
| - [ ] **Step 2: Replace `fitness.v_sleep` dashboard joins with ClickHouse helper output** | ||
|
|
||
| Keep daily metrics in Postgres where they already live, but fetch sleep rows from ClickHouse and combine in TypeScript or via small values CTEs. | ||
|
|
||
| - [ ] **Step 3: Keep stage interval reads on raw Postgres tables** | ||
|
|
||
| `getStages()` and `getLatestStages()` can still read `fitness.sleep_stage`, but the selected/latest sleep session should come from ClickHouse. | ||
|
|
||
| ### Task 4: Remove Postgres Materialized View Ownership | ||
|
|
||
| **Files:** | ||
| - Add: `drizzle/0025_drop_v_sleep.sql` | ||
| - Delete: `drizzle/_views/02_v_sleep.sql` | ||
| - Modify docs mentioning the old Postgres materialized view. | ||
|
|
||
| - [ ] **Step 1: Add a forward migration that drops `fitness.v_sleep`** | ||
|
|
||
| Use `DROP MATERIALIZED VIEW IF EXISTS fitness.v_sleep;` with no replacement compatibility view. | ||
|
|
||
| - [ ] **Step 2: Remove the canonical Postgres view file** | ||
|
|
||
| Delete `drizzle/_views/02_v_sleep.sql` so future deploy tooling cannot recreate the materialized view. | ||
|
|
||
| ### Task 5: Verify | ||
|
|
||
| **Files:** | ||
| - Modify: tests touched by the route/repository changes. | ||
|
|
||
| - [ ] **Step 1: Run targeted tests** | ||
|
|
||
| Run: | ||
|
|
||
| ```bash | ||
| pnpm vitest packages/server/src/repositories/sleep-repository.test.ts packages/server/src/routers/sleep-need.test.ts packages/server/src/routers/mobile-dashboard.test.ts packages/server/src/routers/recovery.test.ts packages/server/src/routers/stress.test.ts | ||
| ``` | ||
|
|
||
| - [ ] **Step 2: Search for remaining runtime references** | ||
|
|
||
| Run: | ||
|
|
||
| ```bash | ||
| rg -n "fitness\\.v_sleep|REFRESH MATERIALIZED VIEW.*v_sleep" packages/server/src src drizzle docs | ||
| ``` | ||
|
|
||
| Expected: no production runtime references to `fitness.v_sleep`; historical docs/tests may mention it only when describing removal. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| DROP VIEW IF EXISTS clickhouse.v_sleep; | ||
| DROP MATERIALIZED VIEW IF EXISTS fitness.v_sleep; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.