-
Notifications
You must be signed in to change notification settings - Fork 1
Use timestamp windows for activity power curve #1569
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba6385a
fix: use timestamp windows for activity power curve
Asherlc 488a87d
fix: compute power curve avg power from raw sample windows
Asherlc a1f8cdd
fix: extend varying-power test window to span 5 seconds
Asherlc 87893c5
fix: time-weight avg_power using segment_seconds
Asherlc 2e5e228
fix(power-curve): add window_sample.row_number to GROUP BY — prevents…
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
202 changes: 202 additions & 0 deletions
202
packages/server/src/routers/activity-power-curve-read-model.integration.test.ts
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,202 @@ | ||
| import { randomUUID } from "node:crypto"; | ||
| import { sql } from "drizzle-orm"; | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
| import { z } from "zod"; | ||
| import { readModelSql } from "../../../../analytics/models/read_models/read-model-sql-test-helpers.ts"; | ||
| import { setupTestDatabase, type TestContext } from "../../../../src/db/test-helpers.ts"; | ||
| import type { ActivitySensorStore } from "../repositories/activity-repository.ts"; | ||
| import { | ||
| type ClickHouseMetricStreamSeedRow, | ||
| createClickHouseTestActivitySensorStore, | ||
| seedClickHouseMetricStreamRows, | ||
| syncClickHouseTestActivitySensorStore, | ||
| } from "./clickhouse-integration-test-helpers.ts"; | ||
|
|
||
| const testUserId = "00000000-0000-0000-0000-000000000001"; | ||
| const regularActivityStartedAt = "2026-07-01T12:00:00.000Z"; | ||
| const gappedActivityStartedAt = "2026-07-01T13:00:00.000Z"; | ||
| const varyingPowerStartedAt = "2026-07-01T14:00:00.000Z"; | ||
| const readModelRowSchema = z.object({ | ||
| activity_id: z.string(), | ||
| duration_seconds: z.coerce.number(), | ||
| best_power: z.coerce.number().nullable(), | ||
| is_deleted: z.coerce.number(), | ||
| }); | ||
|
|
||
| function renderNonIncrementalActivityPowerCurveSql(): string { | ||
| return readModelSql("activity_power_curve.sql") | ||
| .replace(/^\{\{ config\([\s\S]*?\n\) \}\}\s*/, "") | ||
| .replace(/\{\{\s*ref\('activity_summary_rows'\)\s*\}\}/g, "analytics.activity_summary") | ||
| .replace(/\{\{\s*ref\('([^']+)'\)\s*\}\}/g, "analytics.$1") | ||
| .replace(/FROM analytics\.activity_summary FINAL/g, "FROM analytics.activity_summary") | ||
| .replace( | ||
| /\n {4}WHERE is_deleted = 0\n {8}AND ended_at IS NOT NULL/, | ||
| "\n WHERE ended_at IS NOT NULL", | ||
| ) | ||
| .replace(/\{%\s*if is_incremental\(\)\s*%\}[\s\S]*?\{%\s*endif\s*%\}/g, ""); | ||
| } | ||
|
|
||
| function powerSampleRows( | ||
| activityId: string, | ||
| startedAt: string, | ||
| samples: readonly { offsetSeconds: number; power: number }[], | ||
| ): ClickHouseMetricStreamSeedRow[] { | ||
| const startedAtMs = Date.parse(startedAt); | ||
|
|
||
| return samples.map((sample) => ({ | ||
| activityId, | ||
| userId: testUserId, | ||
| recordedAt: new Date(startedAtMs + sample.offsetSeconds * 1000).toISOString(), | ||
| providerId: "test_provider", | ||
| sourceType: "api", | ||
| channel: "power", | ||
| scalar: sample.power, | ||
| })); | ||
| } | ||
|
|
||
| async function insertActivity( | ||
| testContext: TestContext, | ||
| activityId: string, | ||
| name: string, | ||
| startedAt: string, | ||
| endedAt: string, | ||
| ): Promise<void> { | ||
| await testContext.db.execute(sql` | ||
| INSERT INTO fitness.activity ( | ||
| id, provider_id, user_id, external_id, activity_type, started_at, ended_at, name | ||
| ) VALUES ( | ||
| ${activityId}, 'test_provider', ${testUserId}, ${`${name}-${activityId}`}, 'cycling', | ||
| ${startedAt}, ${endedAt}, ${name} | ||
| ) | ||
| `); | ||
| } | ||
|
|
||
| describe("activity_power_curve read model", () => { | ||
| let testContext: TestContext; | ||
| let sensorStore: ActivitySensorStore; | ||
|
|
||
| beforeAll(async () => { | ||
| testContext = await setupTestDatabase(); | ||
| await testContext.db.execute(sql` | ||
| INSERT INTO fitness.provider (id, name, user_id) | ||
| VALUES ('test_provider', 'Test Provider', ${testUserId}) | ||
| ON CONFLICT DO NOTHING | ||
| `); | ||
| sensorStore = await createClickHouseTestActivitySensorStore(testContext); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await testContext?.cleanup(); | ||
| }); | ||
|
|
||
| it("uses elapsed timestamp duration instead of sample count for power windows", async () => { | ||
| const regularActivityId = randomUUID(); | ||
| const gappedActivityId = randomUUID(); | ||
| const renderedSql = renderNonIncrementalActivityPowerCurveSql(); | ||
|
|
||
| await insertActivity( | ||
| testContext, | ||
| regularActivityId, | ||
| "regular-power", | ||
| regularActivityStartedAt, | ||
| "2026-07-01T12:00:30.000Z", | ||
| ); | ||
| await insertActivity( | ||
| testContext, | ||
| gappedActivityId, | ||
| "gapped-power", | ||
| gappedActivityStartedAt, | ||
| "2026-07-01T13:00:30.000Z", | ||
| ); | ||
| await syncClickHouseTestActivitySensorStore(testContext); | ||
| await seedClickHouseMetricStreamRows(testContext, [ | ||
| ...powerSampleRows(regularActivityId, regularActivityStartedAt, [ | ||
| { offsetSeconds: 0, power: 200 }, | ||
| { offsetSeconds: 1, power: 200 }, | ||
| { offsetSeconds: 2, power: 200 }, | ||
| { offsetSeconds: 3, power: 200 }, | ||
| { offsetSeconds: 4, power: 200 }, | ||
| { offsetSeconds: 5, power: 200 }, | ||
| ]), | ||
| ...powerSampleRows(gappedActivityId, gappedActivityStartedAt, [ | ||
| { offsetSeconds: 0, power: 100 }, | ||
| { offsetSeconds: 1, power: 100 }, | ||
| { offsetSeconds: 2, power: 100 }, | ||
| { offsetSeconds: 20, power: 500 }, | ||
| { offsetSeconds: 21, power: 500 }, | ||
| { offsetSeconds: 22, power: 500 }, | ||
| ]), | ||
| ]); | ||
|
|
||
| const rows = await sensorStore.query( | ||
| readModelRowSchema, | ||
| ` | ||
| SELECT | ||
| toString(activity_id) AS activity_id, | ||
| duration_seconds, | ||
| best_power, | ||
| is_deleted | ||
| FROM (${renderedSql}) AS power_curve | ||
| WHERE duration_seconds = 5 | ||
| ORDER BY activity_id | ||
| `, | ||
| ); | ||
|
|
||
| expect(rows).toEqual([ | ||
| { | ||
| activity_id: regularActivityId, | ||
| best_power: 200, | ||
| duration_seconds: 5, | ||
| is_deleted: 0, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("computes average power correctly for varying-power windows", async () => { | ||
| const varyingActivityId = randomUUID(); | ||
| const renderedSql = renderNonIncrementalActivityPowerCurveSql(); | ||
|
|
||
| await insertActivity( | ||
| testContext, | ||
| varyingActivityId, | ||
| "varying-power", | ||
| varyingPowerStartedAt, | ||
| "2026-07-01T14:00:06.000Z", | ||
| ); | ||
| await syncClickHouseTestActivitySensorStore(testContext); | ||
| await seedClickHouseMetricStreamRows(testContext, [ | ||
| ...powerSampleRows(varyingActivityId, varyingPowerStartedAt, [ | ||
| { offsetSeconds: 0, power: 100 }, | ||
| { offsetSeconds: 1, power: 200 }, | ||
| { offsetSeconds: 2, power: 300 }, | ||
| { offsetSeconds: 3, power: 400 }, | ||
| { offsetSeconds: 4, power: 500 }, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| { offsetSeconds: 5, power: 300 }, | ||
| ]), | ||
| ]); | ||
|
|
||
| const rows = await sensorStore.query( | ||
| readModelRowSchema, | ||
| ` | ||
| SELECT | ||
| toString(activity_id) AS activity_id, | ||
| duration_seconds, | ||
| best_power, | ||
| is_deleted | ||
| FROM (${renderedSql}) AS power_curve | ||
| WHERE activity_id = '${varyingActivityId}' | ||
| AND duration_seconds = 5 | ||
| ORDER BY activity_id | ||
| `, | ||
| ); | ||
|
|
||
| expect(rows).toEqual([ | ||
| { | ||
| activity_id: varyingActivityId, | ||
| best_power: 300, | ||
| duration_seconds: 5, | ||
| is_deleted: 0, | ||
| }, | ||
| ]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
|
Asherlc marked this conversation as resolved.
|
||
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.