Skip to content

Show daily insulin totals and the usual amount by this time of day - #2601

Merged
ps2 merged 1 commit into
next-devfrom
feat/tdd-statistics
Oct 23, 2026
Merged

ps2 merged 1 commit into
next-devfrom
feat/tdd-statistics

Conversation

@ps2

@ps2 ps2 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

People have asked for total daily dose stats in Loop, and some check in the morning whether the day is running ahead of their usual insulin.

Statistics page: new Daily Insulin section

  • Cards:
    • Average per day.
    • Today so far, with "Usually X U by ".
    • Basal, with % of total and the scheduled basal total.
    • Bolus, with % of total and the automatic part.
  • A chart of each calendar day, stacked by basal, automatic bolus and manual bolus, with a dashed line at the average. It follows the 7/14/30/90-day picker. Today stays out of the chart until the day is over.

Insulin Delivery Log

  • Adds "usually X U by " under the total since midnight.

How the usual amount works

  • It is the median of the last 14 days, each measured from midnight to the same clock time. Meals and dawn-phenomenon basal therefore compare like with like, rather than against a straight-line share of the daily average.
  • It needs at least 3 days of history.
  • It stays at the same clock time across daylight saving changes.

Calculation

  • InsulinDeliveryHistory computes all of these from normalized doses. Time not covered by a basal record counts at the scheduled basal rate, as the existing total does.
  • Doses come from the ~90-day on-device cache, so no new storage is needed.
  • Today's total (totalDeliveredToday()) now uses it too. The old calculation trimmed each dose individually and kept the full volume of zero-duration boluses. A dose logged yesterday evening was still in the display fetch window, so it counted toward today. This may account for some of the Loop-higher-than-Apple-Health reports.

Tests: 9 new InsulinDeliveryHistoryTests:

  • the zero-duration bolus case;
  • a bolus spanning midnight;
  • gaps at the scheduled rate and suspends;
  • overlapping basal records;
  • the automatic/manual split;
  • skipping a partially covered first day;
  • the same-clock-time median, including a DST change.

Screenshots are from the simulator with seeded history (the generator overdoses, hence ~64 U/day):

Statistics Insulin Delivery Log
statistics log

People ask for total daily dose stats, and some check in the morning
whether the day is running ahead of their usual insulin. The Statistics
page gains a Daily Insulin section: average per day, basal and bolus
(with the automatic part and the scheduled basal total), today so far,
and a chart of each calendar day stacked by basal, automatic bolus and
manual bolus. The Insulin Delivery Log adds "usually X U by <time>"
under the total since midnight.

The usual amount is the median of the last 14 days, each measured from
midnight to the same clock time, so meals and dawn-phenomenon basal
compare like with like. It needs at least 3 days of history.

InsulinDeliveryHistory computes all of these from normalized doses,
counting uncovered time at the scheduled basal rate. Today's total now
uses it too: the old calculation kept the full volume of any
zero-duration bolus in the fetch window, so a logged dose from
yesterday evening counted toward today.
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