Skip to content

Add date-range variants of get_max_metrics/get_rhr_day/get_hrv_data/get_sleep_data - #402

Open
mannmann2 wants to merge 2 commits into
cyberjunky:masterfrom
mannmann2:feat/wellness-daily-ranges
Open

Add date-range variants of get_max_metrics/get_rhr_day/get_hrv_data/get_sleep_data#402
mannmann2 wants to merge 2 commits into
cyberjunky:masterfrom
mannmann2:feat/wellness-daily-ranges

Conversation

@mannmann2

@mannmann2 mannmann2 commented Jul 28, 2026

Copy link
Copy Markdown

Summary

Several single-day methods call Garmin endpoints that natively support a date range, but hardcode fromDate = untilDate = cdate, forcing callers who want history into N single-day requests:

  • get_max_metrics(cdate).../metrics-service/metrics/maxmet/daily/{cdate}/{cdate}
  • get_rhr_day(cdate).../userstats-service/wellness/daily/{display_name} with fromDate=untilDate=cdate
  • get_hrv_data(cdate).../hrv-service/hrv/{cdate} (range form lives at .../hrv-service/hrv/daily/{start}/{end})
  • get_sleep_data(cdate) → single-night detail only; no daily-summary-range equivalent exists

This PR adds range-capable siblings rather than changing existing signatures, so nothing breaks:

  • get_max_metrics_range(start, end)
  • get_hrv_data_range(start, end)
  • get_rhr_daily(start, end) — same wellness-stats endpoint as get_rhr_day, filtered down to the WELLNESS_RESTING_HEART_RATE series
  • get_sleep_daily(start, end) — daily sleep summaries over a range, chunked to the sleep-stats endpoint's 28-day cap (same pattern already used by get_daily_steps for its own 28-day limit)
  • get_calories_daily(start, end) — new method (no existing calories wrapper), reuses the same wellness-stats endpoint as get_rhr_daily with metricId=[22, 23] for active/BMR calories

I verified the underlying endpoint paths/params against a live Garmin account before writing this (not just guessed from adjacent code), then re-confirmed each one against this repo's own already-merged patterns (get_daily_steps's chunking, get_lactate_threshold's range-vs-latest split) to keep the style consistent.

Test plan

  • pytest tests -m "not integration" — 201 passed (was 192, +9 new)
  • ruff check / ruff format --check clean on touched files
  • New unit tests cover: URL/param construction for each new method, RHR/calories metricsMap filtering (including null-value rows dropped), sleep single-chunk dedup+sort, sleep multi-chunk splitting across the 28-day boundary, and date-order/format validation

Summary by CodeRabbit

  • New Features
    • Added date-range queries for maximum metrics and HRV data.
    • Added daily range retrieval for sleep, resting heart rate, and calories.
    • Calories are now returned with per-date active, resting, and total values.
  • Bug Fixes
    • Improved handling of malformed or inverted date ranges by validating start/end inputs.
    • Resting heart rate results now omit entries with missing metric values.
    • Sleep data is deduplicated, sorted by date, and split into API-friendly range chunks.
  • Tests
    • Expanded unit test coverage for range methods, merging behavior, filtering, chunking, and validation.

get_max_metrics, get_rhr_day, get_hrv_data, and get_sleep_data all hit
Garmin endpoints that natively support a date range, but the wrappers
hardcode start == end == cdate. Add range-capable siblings that reuse
the same URLs/params:

- get_max_metrics_range(start, end)
- get_hrv_data_range(start, end)
- get_rhr_daily(start, end) — filters the wellness-stats metricsMap
  response down to WELLNESS_RESTING_HEART_RATE
- get_sleep_daily(start, end) — chunks to the sleep-stats endpoint's
  28-day cap, following the same pattern as get_daily_steps
- get_calories_daily(start, end) — new method, reuses the same
  wellness-stats endpoint as get_rhr_daily with metricId 22/23
  (active/BMR calories), which wasn't wrapped at all before

Each avoids N+1 single-day calls when a caller wants history rather
than a snapshot. Unit tests added for URL/param construction and
sleep's chunking behavior.
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5def5e82-9e44-4285-bb30-b2030b95ee8f

📥 Commits

Reviewing files that changed from the base of the PR and between 86036c8 and 1f5fa51.

📒 Files selected for processing (2)
  • garminconnect/__init__.py
  • tests/test_garmin_unit.py

Walkthrough

Added five Garmin date-range methods for max metrics, sleep, resting heart rate, calories, and HRV. The implementation validates dates, queries range endpoints, normalizes responses, and adds unit tests for URL construction, filtering, aggregation, sorting, and chunking.

Changes

Garmin wellness date-range APIs

Layer / File(s) Summary
Date-range endpoint methods
garminconnect/__init__.py, tests/test_garmin_unit.py
Adds shared date validation plus max-metrics and HRV range methods with distinct start/end URL parameters and validation tests.
Chunked daily sleep retrieval
garminconnect/__init__.py, tests/test_garmin_unit.py
Fetches sleep data in ranges of up to 28 days, de-duplicates by calendarDate, sorts results, and validates range ordering.
Resting heart rate and calorie aggregation
garminconnect/__init__.py, tests/test_garmin_unit.py
Extracts non-null resting heart rate values and combines active and BMR calories into daily totals, including missing-value handling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: cyberjunky, dragilla, tamcore

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change by adding date-range variants of the Garmin data methods.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mannmann2
mannmann2 marked this pull request as ready for review July 28, 2026 17:23

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@garminconnect/__init__.py`:
- Around line 1404-1409: Wrap the public-method docstrings to follow PEP 8
line-length conventions. Update get_max_metrics_range and the RHR, calorie, and
HRV methods in garminconnect/__init__.py at lines 1404-1409, 1830-1836,
1857-1863, and 1910-1915 respectively; preserve their wording and behavior while
splitting overly long lines appropriately.
- Around line 1410-1412: Reject inverted date ranges consistently by adding and
reusing a shared ordered-range validator after the existing date normalization
in the max-metrics, RHR, calorie, and HRV methods; ensure validation occurs
before URL construction or API requests. In tests/test_garmin_unit.py lines
351-445, add parameterized reversed-range coverage for every variant and assert
that no API request is made.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: df42a380-fd69-419a-a4e4-f32f0b7b4e68

📥 Commits

Reviewing files that changed from the base of the PR and between e4e9748 and 86036c8.

📒 Files selected for processing (2)
  • garminconnect/__init__.py
  • tests/test_garmin_unit.py

Comment thread garminconnect/__init__.py
Comment thread garminconnect/__init__.py Outdated
CodeRabbit caught that get_max_metrics_range, get_rhr_daily,
get_calories_daily, and get_hrv_data_range validated date format but not
ordering, silently sending a reversed fromDate/untilDate to Garmin instead
of rejecting start > end (get_sleep_daily already had this check).

Add a shared _validate_date_range() helper and use it in all five methods.
Also wraps docstrings to stay under the line-length limit, and adds a
parameterized regression test asserting each method rejects an inverted
range without making an API call.
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