Skip to content

fix: auto-fetch all SleepHQ history in 30-day batches with 90s delay#98

Draft
camden-bock wants to merge 3 commits into
joshuamyers-dev:mainfrom
camden-bock:bugfix/sleephq-scope
Draft

fix: auto-fetch all SleepHQ history in 30-day batches with 90s delay#98
camden-bock wants to merge 3 commits into
joshuamyers-dev:mainfrom
camden-bock:bugfix/sleephq-scope

Conversation

@camden-bock
Copy link
Copy Markdown
Contributor

@camden-bock camden-bock commented May 27, 2026

Problem

  1. The SleepHQ import only fetches 30 days of data — the frontend had no lookback_days control and the DB defaulted to 30.
  2. The sleephq-client library defaults to requesting read write delete OAuth scopes, but SleepHQ OAuth apps may only authorize read access.

Changes

  1. importer/sleephq_import.py — Added fetch_all_machine_dates() that automatically discovers the full extent of SleepHQ history by walking backwards in 30-day windows with 90 seconds between batches. run_sleephq_import uses this when no explicit date range is given (the server/trigger path). The existing CLI --days/--from/--to path is unchanged.

  2. importer/sleephq_import.py — Pass scope="read" to create_client() instead of relying on the library default.

  3. api/routers/import_settings.py — Removed lookback_days from the trigger path since it no longer applies.

  4. frontend/src/pages/Settings.tsx — Removed the Lookback days input (added in a prior commit) and added a sentence clarifying that only read access is used.

Testing

  • ruff check — clean
  • pytest — 30 passed, 63 skipped (DB tests require Postgres)
  • tsc --noEmit — no errors
  • vitest run — 1 passed

The sleephq-client library defaults to scope='read write delete',
but SleepHQ OAuth apps may only authorize read access. Explicitly
request scope='read' to match the importer's read-only data flow.

Also document this in the UI settings description so users know
no data is written or deleted on SleepHQ.
The frontend had no UI control for lookback_days, so the DB default
of 30 was always used. Add a number input in the SleepHQ settings
card and include it in the save payload so users can configure how
many days of history to fetch on import.
@camden-bock camden-bock changed the title fix: request read-only scope from SleepHQ OAuth fix: request read-only scope and add lookback_days control to SleepHQ import May 27, 2026
Replaces the configurable lookback_days with an automatic full-history
walk that discovers the extent of SleepHQ data by fetching successive
30-day windows with a 90-second pause between batches.

- Add fetch_all_machine_dates() that walks backwards from today in
  30-day windows with _BATCH_DELAY=90s between each batch
- run_sleephq_import auto-selects full walk when no explicit date
  range is given (CLI path unchanged)
- Remove lookback_days from the server trigger path and frontend
  Settings UI since it no longer applies
@camden-bock camden-bock changed the title fix: request read-only scope and add lookback_days control to SleepHQ import fix: auto-fetch all SleepHQ history in 30-day batches with 90s delay May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant