Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -800,14 +800,21 @@ final class LoopDataManager {
insulinEffect.filterDateRange(startDate, endDate)
)

// Ensure we're not repeating effects
if let lastEffect = insulinCounteractionEffects.last {
guard startDate >= lastEffect.endDate else {
return
}
}

// Compare that retrospective, insulin-driven prediction to the actual glucose change to
// calculate the effect of all insulin counteraction
guard let lastGlucose = prediction.last else { return }
let glucoseUnit = HKUnit.milligramsPerDeciliter()
let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second())
let discrepancy = change.end.quantity.doubleValue(for: glucoseUnit) - lastGlucose.quantity.doubleValue(for: glucoseUnit) // mg/dL
let averageVelocity = HKQuantity(unit: velocityUnit, doubleValue: discrepancy / change.end.endDate.timeIntervalSince(change.start.endDate))
let effect = GlucoseEffectVelocity(startDate: change.start.startDate, endDate: change.end.startDate, quantity: averageVelocity)
let effect = GlucoseEffectVelocity(startDate: startDate, endDate: change.end.startDate, quantity: averageVelocity)

insulinCounteractionEffects.append(effect)
// For now, only keep the last 24 hours of values
Expand Down