[LOOP-5925] Preserve scheduled basal rate in insulin delivery cache#746
Open
ps2 wants to merge 4 commits into
Open
[LOOP-5925] Preserve scheduled basal rate in insulin delivery cache#746ps2 wants to merge 4 commits into
ps2 wants to merge 4 commits into
Conversation
A mutable .basal dose loses its scheduledBasalRate when it round-trips through the PumpEvent Core Data entity, which has no column for it. CachedInsulinDeliveryObject then stores only the delivered total (quantized to 0.05 U increments) and, on read-back with a nil rate, falls back to unit == .units, so the displayed rate becomes delivered_total / elapsed and drifts (e.g. 0.999/1.001 U/hr for a 1.0 U/hr basal) until the dose finalizes. For a .basal the rate is intrinsically the dose's value, so preserve it as scheduledBasalRate in create(from:)/update(from:) when not already set. Display-only (basal net-insulin is always 0); no Core Data migration.
…github.com/tidepool-org/LoopKit into ps2/LOOP-5925/preserve-basal-rate-in-cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Jira: LOOP-5925
Summary
A live (mutable) scheduled basal shows a rate slightly off from the scheduled rate (e.g. 0.999 / 1.001 U/hr for a 1.0 U/hr basal), snapping back to exact once the dose finalizes.
The exact rate is lost because a
.basaldose'sscheduledBasalRateis dropped when it round-trips through thePumpEventCore Data entity (which has no column for it).CachedInsulinDeliveryObjectthen stores only the delivered total quantized to 0.05 U increments (unitsInDeliverableIncrements); on read-back with a nil rate it falls back tounit == .units, so the displayed rate becomesdelivered_total / elapsedand drifts until the duration crosses a 0.05 U boundary. (Note: this is the on-device delivery cache — mutable doses are never written to HealthKit.)For a
.basal(scheduled basal, as opposed to.tempBasal) the rate is intrinsically the dose'svalue, so this preserves it directly:CachedInsulinDeliveryObject.create(from:)/update(from:)now derivescheduledBasalRatefrom the dose's own rate for.basalentries when it isn't already set.scheduledBasalRate(e.g. one a pump manager provides) is still preferred when present.Affects pumps that report explicit scheduled-basal events and aren't served from reservoir data; pumps that rely on LoopKit's basal interpolation were unaffected (those entries already get the rate stamped in
overlayBasal).Test plan
testCreateFromScheduledBasalEntryPreservesRate— a mutable 1.0 U/hr.basalover a duration that would quantize to a drifting total; asserts the cached rate and round-trippeddose.unitsPerHourare exactly 1.0.