|
| 1 | +package io.aaps.copilot.service |
| 2 | + |
| 3 | +import io.aaps.copilot.data.repository.LocalNightscoutV3Store |
| 4 | +import java.util.Collections |
| 5 | + |
| 6 | +enum class AapsV3ReportedBasalStatus { |
| 7 | + UNCHECKED, OBSERVED_WINDOW, OBSERVED_WITH_GAPS, NOT_OBSERVED, PROFILE_UNAVAILABLE, |
| 8 | + INPUT_UNAVAILABLE, REVISION_MISMATCH, INVALID, LATE_EVIDENCE, CONFLICTING, UNSUPPORTED, LIMIT_REACHED |
| 9 | +} |
| 10 | + |
| 11 | +internal sealed class AapsV3BasalRate { |
| 12 | + data class Absolute(val unitsPerHour: Double) : AapsV3BasalRate() |
| 13 | + data class PercentAdjustment(val percent: Double) : AapsV3BasalRate() |
| 14 | + data class ExtendedAddition(val unitsPerHour: Double) : AapsV3BasalRate() |
| 15 | + final override fun toString() = "AapsV3BasalRate(redacted)" |
| 16 | +} |
| 17 | + |
| 18 | +internal data class AapsV3ReportedBasalSegment( |
| 19 | + val startMs: Long, val endMs: Long, val rate: AapsV3BasalRate, |
| 20 | + val source: AapsV3InsulinObservation, val profile: AapsV3EffectiveProfileObservation |
| 21 | +) { |
| 22 | + /** A reported component, never independently confirmed delivery or total emulated basal. */ |
| 23 | + val reportedComponentUnits: Double? get() = when (rate) { |
| 24 | + is AapsV3BasalRate.Absolute -> rate.unitsPerHour * ((endMs - startMs) / 3_600_000.0) |
| 25 | + is AapsV3BasalRate.ExtendedAddition -> rate.unitsPerHour * ((endMs - startMs) / 3_600_000.0) |
| 26 | + is AapsV3BasalRate.PercentAdjustment -> null |
| 27 | + } |
| 28 | + override fun toString() = "AapsV3ReportedBasalSegment(redacted)" |
| 29 | +} |
| 30 | + |
| 31 | +internal data class AapsV3BasalGap(val startMs: Long, val endMs: Long) { |
| 32 | + override fun toString() = "AapsV3BasalGap(redacted)" |
| 33 | +} |
| 34 | + |
| 35 | +internal class AapsV3ReportedBasalRead( |
| 36 | + val status: AapsV3ReportedBasalStatus, |
| 37 | + val retainedRevision: Long?, |
| 38 | + segments: List<AapsV3ReportedBasalSegment> = emptyList(), |
| 39 | + gaps: List<AapsV3BasalGap> = emptyList() |
| 40 | +) { |
| 41 | + val segments: List<AapsV3ReportedBasalSegment> = Collections.unmodifiableList(segments.toList()) |
| 42 | + val gaps: List<AapsV3BasalGap> = Collections.unmodifiableList(gaps.toList()) |
| 43 | + val upstreamCompletenessVerified: Boolean get() = false |
| 44 | + override fun toString() = "AapsV3ReportedBasalRead(status=$status, redacted)" |
| 45 | +} |
| 46 | + |
| 47 | +/** Reported intervals only. Missing records are not normal basal and profile timezone is not a clock receipt. */ |
| 48 | +internal object AapsV3ReportedBasalProjector { |
| 49 | + private data class Span(val startMs: Long, val endMs: Long, val rate: AapsV3BasalRate, val source: AapsV3InsulinObservation) |
| 50 | + |
| 51 | + fun read(fromMs: Long, untilMs: Long, knownAtMs: Long, profiles: AapsV3ProfileHistoryRead, |
| 52 | + insulin: AapsV3InsulinRead): AapsV3ReportedBasalRead { |
| 53 | + val revision = profiles.retainedRevision |
| 54 | + fun unavailable(status: AapsV3ReportedBasalStatus) = AapsV3ReportedBasalRead(status, revision) |
| 55 | + if (fromMs !in 1 until untilMs || untilMs > knownAtMs || knownAtMs > LocalNightscoutV3Store.MAX_TIME) |
| 56 | + return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 57 | + if (untilMs - fromMs > 172_800_000L) return unavailable(AapsV3ReportedBasalStatus.LIMIT_REACHED) |
| 58 | + if (profiles.status != AapsV3ObservedProfileStatus.OBSERVED) |
| 59 | + return unavailable(AapsV3ReportedBasalStatus.PROFILE_UNAVAILABLE) |
| 60 | + if (insulin.status !in setOf(AapsV3ObservedInsulinStatus.OBSERVED, |
| 61 | + AapsV3ObservedInsulinStatus.PARTIAL_CONTEXT, AapsV3ObservedInsulinStatus.NOT_OBSERVED)) |
| 62 | + return unavailable(AapsV3ReportedBasalStatus.INPUT_UNAVAILABLE) |
| 63 | + if (revision == null || revision != insulin.retainedRevision) |
| 64 | + return unavailable(AapsV3ReportedBasalStatus.REVISION_MISMATCH) |
| 65 | + if (revision > knownAtMs || insulin.records.any { it.availableAtMs > knownAtMs }) |
| 66 | + return unavailable(AapsV3ReportedBasalStatus.LATE_EVIDENCE) |
| 67 | + if (profiles.intervals.size > 2880 || insulin.records.size > 10_000) |
| 68 | + return unavailable(AapsV3ReportedBasalStatus.LIMIT_REACHED) |
| 69 | + val history = profiles.intervals |
| 70 | + if (history.isEmpty() || history.first().startMs != fromMs || history.last().endMs != untilMs || |
| 71 | + history.zipWithNext().any { (a, b) -> a.endMs != b.startMs } || history.any { |
| 72 | + it.startMs >= it.endMs || it.observation.eventAtMs > it.startMs || |
| 73 | + it.observation.createdRevision !in 1..it.observation.modifiedRevision || |
| 74 | + it.observation.modifiedRevision > revision |
| 75 | + } || insulin.records.any { it.identifier.isBlank() || it.createdRevision !in 1..it.modifiedRevision || |
| 76 | + it.modifiedRevision > revision || it.eventAtMs !in 1..LocalNightscoutV3Store.MAX_TIME }) |
| 77 | + return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 78 | + if (insulin.records.map { it.identifier }.distinct().size != insulin.records.size) |
| 79 | + return unavailable(AapsV3ReportedBasalStatus.CONFLICTING) |
| 80 | + val pumpKeys = insulin.records.mapNotNull { it.pumpRecordIdentity } |
| 81 | + if (pumpKeys.distinct().size != pumpKeys.size) return unavailable(AapsV3ReportedBasalStatus.CONFLICTING) |
| 82 | + val spans = mutableListOf<Span>() |
| 83 | + for (record in insulin.records) { |
| 84 | + if (record.invalidated) continue |
| 85 | + val duration: Long |
| 86 | + val rate: AapsV3BasalRate |
| 87 | + when (val dose = record.dose) { |
| 88 | + is AapsV3InsulinDose.Bolus -> { |
| 89 | + if (dose.basalInsulin) return unavailable(AapsV3ReportedBasalStatus.UNSUPPORTED) |
| 90 | + continue |
| 91 | + } |
| 92 | + is AapsV3InsulinDose.TempBasal -> { |
| 93 | + if (dose.type == AapsV3TempBasalType.FAKE_EXTENDED) return unavailable(AapsV3ReportedBasalStatus.UNSUPPORTED) |
| 94 | + duration = dose.durationMs |
| 95 | + val absolute = dose.absoluteUnitsPerHour |
| 96 | + val percent = dose.percentAdjustment |
| 97 | + if ((absolute != null) == (percent != null) || absolute?.let { !it.isFinite() || it < 0 } == true || |
| 98 | + percent?.let { !it.isFinite() || it < -100 } == true) |
| 99 | + return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 100 | + rate = absolute?.let(AapsV3BasalRate::Absolute) ?: AapsV3BasalRate.PercentAdjustment(checkNotNull(percent)) |
| 101 | + } |
| 102 | + is AapsV3InsulinDose.Extended -> { |
| 103 | + if (!dose.emulatingTempBasal) continue |
| 104 | + if (dose.durationMs == 0L && dose.units != 0.0) return unavailable(AapsV3ReportedBasalStatus.UNSUPPORTED) |
| 105 | + if (!dose.units.isFinite() || dose.units < 0) return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 106 | + duration = dose.durationMs |
| 107 | + if (duration == 0L) continue |
| 108 | + val extra = dose.units / (duration / 3_600_000.0) |
| 109 | + if (!extra.isFinite()) return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 110 | + rate = AapsV3BasalRate.ExtendedAddition(extra) |
| 111 | + } |
| 112 | + } |
| 113 | + if (duration < 0 || record.eventAtMs > LocalNightscoutV3Store.MAX_TIME - duration) |
| 114 | + return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 115 | + val start = maxOf(fromMs, record.eventAtMs) |
| 116 | + val end = minOf(untilMs, record.eventAtMs + duration) |
| 117 | + if (start < end) spans += Span(start, end, rate, record) |
| 118 | + } |
| 119 | + spans.sortBy { it.startMs } |
| 120 | + if (spans.zipWithNext().any { (a, b) -> a.endMs > b.startMs }) |
| 121 | + return unavailable(AapsV3ReportedBasalStatus.CONFLICTING) |
| 122 | + val segments = mutableListOf<AapsV3ReportedBasalSegment>() |
| 123 | + var profileIndex = 0 |
| 124 | + for (span in spans) { |
| 125 | + var start = span.startMs |
| 126 | + while (start < span.endMs) { |
| 127 | + while (history[profileIndex].endMs <= start) profileIndex++ |
| 128 | + val profile = history[profileIndex] |
| 129 | + val end = minOf(profile.endMs, span.endMs) |
| 130 | + val segment = AapsV3ReportedBasalSegment(start, end, span.rate, span.source, profile.observation) |
| 131 | + if (segment.reportedComponentUnits?.isFinite() == false) |
| 132 | + return unavailable(AapsV3ReportedBasalStatus.INVALID) |
| 133 | + segments += segment |
| 134 | + if (segments.size > 2880) return unavailable(AapsV3ReportedBasalStatus.LIMIT_REACHED) |
| 135 | + start = end |
| 136 | + } |
| 137 | + } |
| 138 | + val gaps = mutableListOf<AapsV3BasalGap>() |
| 139 | + var end = fromMs |
| 140 | + for (segment in segments) { |
| 141 | + if (end < segment.startMs) gaps += AapsV3BasalGap(end, segment.startMs) |
| 142 | + end = segment.endMs |
| 143 | + } |
| 144 | + if (end < untilMs) gaps += AapsV3BasalGap(end, untilMs) |
| 145 | + val status = when { |
| 146 | + segments.isEmpty() -> AapsV3ReportedBasalStatus.NOT_OBSERVED |
| 147 | + gaps.isEmpty() -> AapsV3ReportedBasalStatus.OBSERVED_WINDOW |
| 148 | + else -> AapsV3ReportedBasalStatus.OBSERVED_WITH_GAPS |
| 149 | + } |
| 150 | + return AapsV3ReportedBasalRead(status, revision, segments, gaps) |
| 151 | + } |
| 152 | +} |
0 commit comments