Skip to content

Add TDD calculation for Loop users and Basal Variability chart#640

Open
MtlPhil wants to merge 1 commit intoloopandlearn:devfrom
achkars-org:feat/stats-basal-TDD
Open

Add TDD calculation for Loop users and Basal Variability chart#640
MtlPhil wants to merge 1 commit intoloopandlearn:devfrom
achkars-org:feat/stats-basal-TDD

Conversation

@MtlPhil
Copy link
Copy Markdown
Contributor

@MtlPhil MtlPhil commented May 6, 2026

Feat: TDD calculation for Loop and Basal Variability chart

Summary

Two additions to the Statistics view for Loop users, plus a set of correctness fixes for the underlying TDD data pipeline.


1. TDD (Total Daily Dose) calculation for Loop

Displays the rolling 24-hour total daily dose in the info table for Loop users. TDD is computed from the treatments endpoint only (not device status) to avoid double-counting, then cached to Storage so it is available across views.

Data pipeline

The calculation lives in DeviceStatusLoop and runs whenever treatments or device status are refreshed:

  • Basal: reconstructed from the scheduled basal profile, filling gaps between actual temp-basal records. A guard prevents gap-fill before the first treatment entry, avoiding spurious additions at the tail.
  • Bolus: summed directly from treatments (normal + SMB).
  • Race condition guard: Treatments.swift now posts a .treatmentsUpdated notification that DeviceStatusLoop observes, ensuring TDD is not computed until both datasets are available.

Fixes included

Fix Description
Blank on first load Wait for .treatmentsUpdated before first calculation
Basal gap before first entry Guard the retroactive gap-fill against the first treatment timestamp
Crash on empty basalData Nil-check basalData.first before accessing it

2. Basal Variability chart

A new chart in the Statistics view that plots scheduled vs. actual basal delivery over the selected date range, making it easy to see how often and by how much Loop is adjusting basal.

New files

File Purpose
LoopFollow/Stats/BasalVariability/BasalVariabilityCalculator.swift Aggregates scheduled and actual basal by hour
LoopFollow/Stats/BasalVariability/BasalVariabilityDataPoint.swift Data model
LoopFollow/Stats/BasalVariability/BasalVariabilityGraphView.swift SwiftUI chart view
LoopFollow/Stats/BasalVariability/BasalVariabilityView.swift Container view with legend
LoopFollow/Stats/BasalVariability/BasalVariabilityViewModel.swift Drives the chart from AggregatedStatsViewModel

The chart is gated behind Loop being the active closed-loop system and is inserted into AggregatedStatsView between the TIR section and the existing tables.


Files changed

File Change
LoopFollow/Controllers/Nightscout/DeviceStatus.swift Route TDD reads through treatments only
LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift Full TDD calculation with basal reconstruction
LoopFollow/Controllers/Nightscout/Treatments.swift Post .treatmentsUpdated notification
LoopFollow/Stats/AggregatedStatsView.swift Wire Basal Variability chart into Statistics
LoopFollow/Stats/AggregatedStatsViewModel.swift Expose BasalVariabilityViewModel
LoopFollow/Stats/BasalVariability/ New — five files (see above)

- Add TDD (Total Daily Dose) calculation for Loop from device status and treatments,
  including basal gap fill, race condition handling, and crash fix on empty basalData
- Add Basal Variability chart to Statistics view
- Fix TDD double-count: compute only from treatments, not device status
- Fix TDD blank on first load due to device status / treatments race condition

https://claude.ai/code/session_01DGCES9o6CLpeAmfe7tMoR6
@MtlPhil MtlPhil marked this pull request as draft May 6, 2026 22:17
@MtlPhil MtlPhil marked this pull request as ready for review May 6, 2026 22:18
@MtlPhil
Copy link
Copy Markdown
Contributor Author

MtlPhil commented May 6, 2026

Corrected lint errors.

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.

2 participants