Skip to content

Fix sleep data parsing to match actual API response#11

Open
talison wants to merge 1 commit intosteipete:mainfrom
talison:fix-sleep-data-parsing
Open

Fix sleep data parsing to match actual API response#11
talison wants to merge 1 commit intosteipete:mainfrom
talison:fix-sleep-data-parsing

Conversation

@talison
Copy link

@talison talison commented Jan 31, 2026

Summary

This PR addresses two issues with the sleep commands:

  1. Range command flag parsing (sleep_range.go): The --from and --to flags were being read via viper.GetString() which wasn't receiving the flag values properly. Changed to read directly from cobra's cmd.Flags().GetString().

  2. SleepDay struct field mapping (eightsleep.go): The struct fields didn't match the actual API response structure. The API returns:

    • sleepDuration (not sleepDurationSeconds)
    • deepDuration, remDuration, lightDuration for sleep stages
    • sleepStart, sleepEnd timestamps
    • Nested sleepQualityScore with hrv.current, heartRate.current, etc.

Changes

Updated the struct and commands to properly parse and display:

  • Sleep duration in hours (more readable than seconds)
  • Deep/REM/Light sleep durations
  • Sleep start/end times
  • RHR (resting heart rate)
  • HRV
  • Respiratory rate

Before/After

Before:

date        score  duration  tnt  resp_rate  heart_rate  hrv_score
2026-01-30  94     0         9    0          0           0

After:

date        score  duration_hrs  deep_hrs  rem_hrs  light_hrs  rhr  hrv   resp_rate  tnt
2026-01-30  94     7.1           1.5       1.9      3.5        57   54.9  15.3       9

Test plan

  • Tested eightctl sleep day against live API
  • Tested eightctl sleep range --from X --to Y against live API
  • Verified JSON output format

🤖 Generated with Claude Code

This PR addresses two issues with the sleep commands:

1. **Range command flag parsing** (`sleep_range.go`): The `--from` and `--to`
   flags were being read via `viper.GetString()` which wasn't receiving the
   flag values properly. Changed to read directly from cobra's
   `cmd.Flags().GetString()`.

2. **SleepDay struct field mapping** (`eightsleep.go`): The struct fields
   didn't match the actual API response structure. The API returns:
   - `sleepDuration` (not `sleepDurationSeconds`)
   - `deepDuration`, `remDuration`, `lightDuration` for sleep stages
   - `sleepStart`, `sleepEnd` timestamps
   - Nested `sleepQualityScore` with `hrv.current`, `heartRate.current`, etc.

Updated the struct and commands to properly parse and display:
- Sleep duration in hours
- Deep/REM/Light sleep durations
- Sleep start/end times
- RHR (resting heart rate)
- HRV
- Respiratory rate

Tested against live Eight Sleep API.
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

Comments