diff --git a/CHANGELOG b/CHANGELOG index 945548cb..b992eb9d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,7 @@ * Roam app: Add a date-picker for navigating to a daily page * Clear notification popups when they're dismissed on the phone, and avoid showing some notifications that are spurious * Add depth effects to some modals that didn't have them +* Nightscout: Add a missed-bolus detection feature ### Bug fixes diff --git a/app/apps/nightscout/index.ts b/app/apps/nightscout/index.ts index 3d30251c..87e3c960 100644 --- a/app/apps/nightscout/index.ts +++ b/app/apps/nightscout/index.ts @@ -1,14 +1,18 @@ import { inProcessTrayWidget, type AppDefinition } from "../app-definition"; import { createNightscoutAppWindow, startNightscoutTrayIcon, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app"; +import { startMissedBolusMonitor } from "./missed-bolus-monitor"; const nightscoutApp: AppDefinition = { appId: "nightscout", title: "Nightscout", icon: "nightscout", - // Selected in the switcher, the tray readout shows the app's window. - boot: (ctx) => startNightscoutTrayIcon( - inProcessTrayWidget(ctx, NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow), - ), + boot: (ctx) => { + // Selected in the switcher, the tray readout shows the app's window. + startNightscoutTrayIcon( + inProcessTrayWidget(ctx, NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow), + ); + startMissedBolusMonitor(ctx); + }, launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow), }; diff --git a/app/apps/nightscout/missed-bolus-detector.ts b/app/apps/nightscout/missed-bolus-detector.ts new file mode 100644 index 00000000..37a8a985 --- /dev/null +++ b/app/apps/nightscout/missed-bolus-detector.ts @@ -0,0 +1,336 @@ +/** + * Missed-bolus detector: given a recent window of CGM readings and + * treatments, decide whether glucose is rising the way an unannounced meal + * does, with no meal bolus, manual bolus or carb entry to account for it. + * + * Deliberately self-contained (no imports; plain data in, plain data out) so + * it can move to the server side of Nightscout unchanged, and so it can be + * iterated on against recorded history with + * scripts/nightscout-missed-bolus-replay.cjs. The Faceclaw side (when to run + * it, cooldown, the glasses notification) is in missed-bolus-monitor.ts. + * + * Glucose is mg/dL throughout: Nightscout stores sgv in mg/dL whatever the + * site's display units. + */ + +export type GlucoseReading = { timeMs: number; mgdl: number }; + +/** + * - meal: a meal/snack/wizard bolus, or any bolus entered together with carbs. + * - manual: a bolus explicitly marked user-initiated (e.g. Loop's `automatic: false`). + * - automatic: a bolus the loop issued (SMB, automatic bolus). + * - ambiguous: not marked either way. OpenAPS on a Medtronic pump uploads its + * SMBs as plain "Correction Bolus" records, indistinguishable from a manual + * correction except by size; `ambiguousBolusMinUnits` decides. + */ +export type BolusKind = "meal" | "manual" | "automatic" | "ambiguous"; +export type BolusEvent = { timeMs: number; units: number; kind: BolusKind }; +export type CarbEntry = { timeMs: number; grams: number }; + +export type MissedBolusInput = { + /** The time to classify as of. Readings and treatments after it are ignored. */ + nowMs: number; + glucose: readonly GlucoseReading[]; + boluses: readonly BolusEvent[]; + carbs: readonly CarbEntry[]; +}; + +export type MissedBolusOptions = { + /** The latest reading must be at most this old, or there is nothing current to judge. */ + maxReadingAgeMs: number; + /** How far back from the latest reading to look for where the rise started. */ + lookbackMs: number; + /** A gap between readings longer than this ends the window (the CGM dropped out). */ + maxGapMs: number; + /** Recent span the rate of rise is fitted over (least squares). */ + slopeWindowMs: number; + /** Fewest readings the rate fit needs. */ + minSlopeReadings: number; + /** Rise from the window's lowest reading to the latest. */ + minRiseMgdl: number; + /** The latest reading must be at least this high. */ + minGlucoseMgdl: number; + /** Rises from below this are taken as recovering from a low (rescue carbs go unbolused). */ + hypoNadirMgdl: number; + /** The rise must have lasted at least this long, to rule out sensor noise. */ + minRiseDurationMs: number; + /** Rate of rise over the slope window, mg/dL per minute. */ + minRateMgdlPerMin: number; + /** A bolus or carb entry this long before the rise began still accounts for it (pre-bolusing). */ + preBolusWindowMs: number; + /** Smallest meal or manual bolus that accounts for a rise. */ + minManualBolusUnits: number; + /** Smallest unmarked bolus taken as manual rather than an SMB. */ + ambiguousBolusMinUnits: number; + /** Whether a carb entry with no bolus accounts for a rise (the user evidently knows they ate). */ + carbsCountAsAnnouncement: boolean; +}; + +export const DEFAULT_MISSED_BOLUS_OPTIONS: Readonly = { + maxReadingAgeMs: 12 * 60_000, + lookbackMs: 90 * 60_000, + maxGapMs: 16 * 60_000, + slopeWindowMs: 20 * 60_000, + minSlopeReadings: 3, + minRiseMgdl: 40, + minGlucoseMgdl: 140, + hypoNadirMgdl: 70, + minRiseDurationMs: 20 * 60_000, + minRateMgdlPerMin: 1.5, + preBolusWindowMs: 60 * 60_000, + minManualBolusUnits: 0.5, + ambiguousBolusMinUnits: 1.5, + carbsCountAsAnnouncement: true, +}; + +export type GlucoseRise = { + /** When the rise started: the lowest reading in the window (the latest, if tied). */ + onsetMs: number; + nadirMgdl: number; + latestMs: number; + latestMgdl: number; + riseMgdl: number; + /** Least-squares rate over the slope window, mg/dL per minute. */ + rateMgdlPerMin: number; +}; + +export type MissedBolusClassification = "missed-bolus" | "no-alert" | "insufficient-data"; + +export type MissedBolusResult = { + classification: MissedBolusClassification; + /** One-line explanation, for logs and the replay tool. */ + reason: string; + /** The rise that was judged, when there was enough data to measure one. */ + rise: GlucoseRise | null; +}; + +export function detectMissedBolus( + input: MissedBolusInput, + overrides: Partial = {}, +): MissedBolusResult { + const options: MissedBolusOptions = { ...DEFAULT_MISSED_BOLUS_OPTIONS, ...overrides }; + const { nowMs } = input; + const readings = usableReadings(input.glucose, nowMs); + const latest = readings[readings.length - 1]; + if (!latest) return insufficient("no glucose readings"); + if (nowMs - latest.timeMs > options.maxReadingAgeMs) { + return insufficient(`latest reading is ${formatMinutes(nowMs - latest.timeMs)} old`); + } + + // The contiguous run of readings ending at the latest, within the lookback. + const windowStartMs = latest.timeMs - options.lookbackMs; + let first = readings.length - 1; + while (first > 0) { + const previous = readings[first - 1]!; + if (previous.timeMs < windowStartMs || readings[first]!.timeMs - previous.timeMs > options.maxGapMs) break; + first--; + } + const window = readings.slice(first); + let nadir = window[0]!; + for (const reading of window) { + if (reading.mgdl <= nadir.mgdl) nadir = reading; + } + + const slopeStartMs = Math.max(nadir.timeMs, latest.timeMs - options.slopeWindowMs); + const slopeReadings = window.filter((reading) => reading.timeMs >= slopeStartMs); + const rise: GlucoseRise = { + onsetMs: nadir.timeMs, + nadirMgdl: nadir.mgdl, + latestMs: latest.timeMs, + latestMgdl: latest.mgdl, + riseMgdl: latest.mgdl - nadir.mgdl, + rateMgdlPerMin: slopeReadings.length >= 2 ? slopePerMinute(slopeReadings) : 0, + }; + const noAlert = (reason: string): MissedBolusResult => ({ classification: "no-alert", reason, rise }); + + if (rise.riseMgdl < options.minRiseMgdl) { + return noAlert(`rise of ${rise.riseMgdl} mg/dL is under ${options.minRiseMgdl}`); + } + if (latest.mgdl < options.minGlucoseMgdl) { + return noAlert(`glucose ${latest.mgdl} mg/dL is under ${options.minGlucoseMgdl}`); + } + if (nadir.mgdl < options.hypoNadirMgdl) { + return noAlert(`rising from a low of ${nadir.mgdl} mg/dL`); + } + if (latest.timeMs - nadir.timeMs < options.minRiseDurationMs) { + return noAlert(`rise has lasted only ${formatMinutes(latest.timeMs - nadir.timeMs)}`); + } + if (slopeReadings.length < options.minSlopeReadings) { + return { classification: "insufficient-data", reason: `only ${slopeReadings.length} readings to fit a rate`, rise }; + } + if (rise.rateMgdlPerMin < options.minRateMgdlPerMin) { + return noAlert(`rising ${formatRate(rise.rateMgdlPerMin)}, under ${formatRate(options.minRateMgdlPerMin)}`); + } + + const coverStartMs = nadir.timeMs - options.preBolusWindowMs; + const inCoverWindow = (timeMs: number) => timeMs >= coverStartMs && timeMs <= nowMs; + const bolus = latestMatching(input.boluses, (event) => inCoverWindow(event.timeMs) && accountsForRise(event, options)); + if (bolus) { + return noAlert(`${formatUnits(bolus.units)} U ${bolus.kind} bolus ${formatMinutes(nowMs - bolus.timeMs)} ago`); + } + if (options.carbsCountAsAnnouncement) { + const carbs = latestMatching(input.carbs, (entry) => inCoverWindow(entry.timeMs) && entry.grams > 0); + if (carbs) return noAlert(`${carbs.grams} g carbs entered ${formatMinutes(nowMs - carbs.timeMs)} ago`); + } + return { + classification: "missed-bolus", + reason: `up ${rise.riseMgdl} mg/dL in ${formatMinutes(latest.timeMs - nadir.timeMs)}, rising ${formatRate(rise.rateMgdlPerMin)}, no bolus`, + rise, + }; +} + +function insufficient(reason: string): MissedBolusResult { + return { classification: "insufficient-data", reason, rise: null }; +} + +function accountsForRise(bolus: BolusEvent, options: MissedBolusOptions): boolean { + switch (bolus.kind) { + case "meal": + case "manual": + return bolus.units >= options.minManualBolusUnits; + case "ambiguous": + return bolus.units >= Math.max(options.minManualBolusUnits, options.ambiguousBolusMinUnits); + case "automatic": + return false; + } +} + +/** + * Finite, in-range readings at or before now, oldest first, one per + * timestamp. Dexcom reports 39 for "LOW" and smaller values as error codes. + */ +function usableReadings(glucose: readonly GlucoseReading[], nowMs: number): GlucoseReading[] { + const sorted = glucose + .filter((reading) => Number.isFinite(reading.timeMs) && Number.isFinite(reading.mgdl) + && reading.mgdl >= 39 && reading.mgdl <= 600 && reading.timeMs <= nowMs) + .slice() + .sort((a, b) => a.timeMs - b.timeMs); + return sorted.filter((reading, index) => index === 0 || reading.timeMs !== sorted[index - 1]!.timeMs); +} + +/** Least-squares slope through the readings, in mg/dL per minute. */ +function slopePerMinute(readings: readonly GlucoseReading[]): number { + const t0 = readings[0]!.timeMs; + const n = readings.length; + let sumX = 0, sumY = 0, sumXY = 0, sumXX = 0; + for (const reading of readings) { + const x = (reading.timeMs - t0) / 60_000; + sumX += x; + sumY += reading.mgdl; + sumXY += x * reading.mgdl; + sumXX += x * x; + } + const denominator = n * sumXX - sumX * sumX; + return denominator > 0 ? (n * sumXY - sumX * sumY) / denominator : 0; +} + +function latestMatching(events: readonly T[], predicate: (event: T) => boolean): T | null { + let found: T | null = null; + for (const event of events) { + if (predicate(event) && (!found || event.timeMs > found.timeMs)) found = event; + } + return found; +} + +function formatMinutes(ms: number): string { + return `${Math.round(ms / 60_000)} min`; +} + +function formatRate(mgdlPerMin: number): string { + return `${mgdlPerMin.toFixed(1)} mg/dL/min`; +} + +function formatUnits(units: number): string { + return `${Math.round(units * 100) / 100}`; +} + +// --------------------------------------------------------------------------- +// Nightscout record normalization, shared by the Faceclaw bridge and a +// server-side port. + +/** The fields of a Nightscout treatment record the detector reads. */ +export type NightscoutTreatmentRecord = { + eventType?: unknown; + created_at?: unknown; + timestamp?: unknown; + insulin?: unknown; + carbs?: unknown; + automatic?: unknown; + isSMB?: unknown; + type?: unknown; + wizard?: unknown; +}; + +/** The fields of a Nightscout entry record the detector reads. */ +export type NightscoutEntryRecord = { date?: unknown; sgv?: unknown }; + +const MEAL_BOLUS_EVENT_TYPES = new Set(["Meal Bolus", "Snack Bolus", "Bolus Wizard", "Combo Bolus"]); + +/** + * How a treatment's insulin was given. Uploaders mark automatic boluses + * differently: Loop sets `automatic`, AndroidAPS `isSMB`, Trio/OpenAPS + * `type: "SMB"` or eventType "SMB". Meal boluses carry carbs or a wizard + * record, or a meal-type eventType. + */ +export function bolusKindFromTreatment(treatment: NightscoutTreatmentRecord): BolusKind { + const eventType = typeof treatment.eventType === "string" ? treatment.eventType : ""; + if (treatment.automatic === true || treatment.isSMB === true || treatment.type === "SMB" || eventType === "SMB") { + return "automatic"; + } + if (MEAL_BOLUS_EVENT_TYPES.has(eventType) || positiveNumber(treatment.carbs) !== null + || (typeof treatment.wizard === "object" && treatment.wizard !== null)) { + return "meal"; + } + if (treatment.automatic === false || treatment.isSMB === false) return "manual"; + return "ambiguous"; +} + +/** Boluses and carb entries from raw Nightscout treatment records, oldest first. */ +export function normalizeNightscoutTreatments(treatments: readonly NightscoutTreatmentRecord[]): { + boluses: BolusEvent[]; + carbs: CarbEntry[]; +} { + const boluses: BolusEvent[] = []; + const carbs: CarbEntry[] = []; + for (const treatment of treatments) { + const timeMs = treatmentTimeMs(treatment); + if (timeMs === null) continue; + const units = positiveNumber(treatment.insulin); + if (units !== null) boluses.push({ timeMs, units, kind: bolusKindFromTreatment(treatment) }); + const grams = positiveNumber(treatment.carbs); + if (grams !== null) carbs.push({ timeMs, grams }); + } + boluses.sort((a, b) => a.timeMs - b.timeMs); + carbs.sort((a, b) => a.timeMs - b.timeMs); + return { boluses, carbs }; +} + +/** Glucose readings from raw Nightscout sgv entries, oldest first. */ +export function normalizeNightscoutEntries(entries: readonly NightscoutEntryRecord[]): GlucoseReading[] { + const readings: GlucoseReading[] = []; + for (const entry of entries) { + const timeMs = Number(entry.date); + const mgdl = Number(entry.sgv); + if (Number.isFinite(timeMs) && timeMs > 0 && Number.isFinite(mgdl)) readings.push({ timeMs, mgdl }); + } + return readings.sort((a, b) => a.timeMs - b.timeMs); +} + +function treatmentTimeMs(treatment: NightscoutTreatmentRecord): number | null { + for (const value of [treatment.created_at, treatment.timestamp]) { + if (typeof value === "number" && Number.isFinite(value) && value > 0) return value; + if (typeof value === "string" && value.trim()) { + const numeric = Number(value); + const parsed = Number.isFinite(numeric) ? numeric : Date.parse(value); + if (Number.isFinite(parsed) && parsed > 0) return parsed; + } + } + return null; +} + +function positiveNumber(value: unknown): number | null { + if (typeof value !== "number" && typeof value !== "string") return null; + if (typeof value === "string" && !value.trim()) return null; + const numeric = Number(value); + return Number.isFinite(numeric) && numeric > 0 ? numeric : null; +} diff --git a/app/apps/nightscout/missed-bolus-monitor.ts b/app/apps/nightscout/missed-bolus-monitor.ts new file mode 100644 index 00000000..1a64641b --- /dev/null +++ b/app/apps/nightscout/missed-bolus-monitor.ts @@ -0,0 +1,73 @@ +import { type AppContext } from "../app-definition"; +import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; +import { getStringSetting, setStringSetting } from "../../native/settings-store"; +import { nightscoutMissedBolusAlertsSetting, nightscoutMissedBolusCooldownSetting } from "../../ui/dashboard-settings"; +import { shell } from "../../ui/shell/shell"; +import { detectMissedBolus, type MissedBolusInput } from "./missed-bolus-detector"; +import { MissedBolusNotificationLayer } from "./missed-bolus-notification"; + +/** When the last alert was shown, persisted so a restart doesn't reset the cooldown. */ +const LAST_ALERT_STORAGE_KEY = "integrations.nightscout.missedBolusLastAlertMs"; + +let started = false; +let lastInputKey = ""; +let lastSummary = ""; + +/** + * Runs the missed-bolus detector each time a Nightscout sync brings new + * readings or treatments, and shows a glasses notification when it fires + * outside the cooldown. Shell-lifetime, started from the app's boot hook. + */ +export function startMissedBolusMonitor(ctx: AppContext): void { + if (started) return; + started = true; + nightscoutBridge.onStateChange((state) => checkForMissedBolus(state, ctx)); +} + +function checkForMissedBolus(state: NightscoutState, ctx: AppContext): void { + if (!nightscoutMissedBolusAlertsSetting.get() || !state.available) return; + const key = inputKey(state); + if (key === lastInputKey) return; + lastInputKey = key; + + const nowMs = Date.now(); + const result = detectMissedBolus(detectorInput(state, nowMs)); + const summary = `${result.classification}: ${result.reason}`; + if (summary !== lastSummary) { + lastSummary = summary; + console.log(`missed-bolus detector: ${summary}`); + } + if (result.classification !== "missed-bolus" || !result.rise) return; + + const lastAlertMs = Number(getStringSetting(LAST_ALERT_STORAGE_KEY, "0")) || 0; + const cooldownMs = Number(nightscoutMissedBolusCooldownSetting.get()) * 60_000; + // A last-alert time in the future means the clock moved back; don't let it block alerts. + if (lastAlertMs <= nowMs && nowMs - lastAlertMs < cooldownMs) return; + setStringSetting(LAST_ALERT_STORAGE_KEY, String(nowMs)); + ctx.appendLog(`missed-bolus alert: ${result.reason}`); + const rise = result.rise; + shell.postAppNotification({ + key: `nightscout-missed-bolus:${nowMs}`, + createLayer: (close) => new MissedBolusNotificationLayer(rise, nowMs, close, () => { + void ctx.launchApp("nightscout"); + }), + }); +} + +/** Changes whenever a sync brings a new reading or a treatment the detector reads. */ +function inputKey(state: NightscoutState): string { + return [ + state.latest?.timestampMs ?? 0, + state.boluses.map((bolus) => bolus.timestampMs).join(","), + state.carbs.map((entry) => entry.timestampMs).join(","), + ].join("|"); +} + +function detectorInput(state: NightscoutState, nowMs: number): MissedBolusInput { + return { + nowMs, + glucose: state.history.map((point) => ({ timeMs: point.timestampMs, mgdl: point.sgv })), + boluses: state.boluses.map((bolus) => ({ timeMs: bolus.timestampMs, units: bolus.insulin, kind: bolus.kind })), + carbs: state.carbs.map((entry) => ({ timeMs: entry.timestampMs, grams: entry.carbs })), + }; +} diff --git a/app/apps/nightscout/missed-bolus-notification.ts b/app/apps/nightscout/missed-bolus-notification.ts new file mode 100644 index 00000000..3b09ecf0 --- /dev/null +++ b/app/apps/nightscout/missed-bolus-notification.ts @@ -0,0 +1,111 @@ +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage } from "../../graphics/image"; +import { renderIcon } from "../../graphics/icons"; +import { wrapText } from "../../graphics/textwrap"; +import { formatRelativeTime } from "../../util/date-util"; +import { nightscoutBridge } from "../../native/nightscout-bridge"; +import { type InputEvent } from "../../ui/gestures"; +import { type Layer, type LayerContext } from "../../ui/layers"; +import { lineStep } from "../../ui/metrics"; +import { + NOTIFICATION_CONTENT_X, + createNotificationActionMenu, + drawNotificationHeader, + notificationActionMenuBox, + notificationContentWidth, +} from "../../ui/notifications"; +import { type GlucoseRise } from "./missed-bolus-detector"; +import { + drawDirectionIndicator, + drawNightscoutGraph, + drawNightscoutValueStrikeThrough, + formatDelta, + isNightscoutPointStale, +} from "./nightscout"; + +const ICON_SIZE = 24; +const GRAPH_GAP = 6; +const GRAPH_BOTTOM_MARGIN = 12; +const MIN_GRAPH_HEIGHT = 32; + +type ActionItem = { label: string; run: () => void }; + +/** + * The missed-bolus alert, laid out like an Android notification's detail view + * (header, text, right-hand action column), with the live glucose readout and + * 2-hour graph below the text. + */ +export class MissedBolusNotificationLayer implements Layer { + private readonly menu = createNotificationActionMenu(); + + constructor( + private readonly rise: GlucoseRise, + private readonly postedAtMs: number, + private readonly close: () => void, + openNightscout: () => void, + ) { + this.menu.setItems([ + { label: "Dismiss", run: close }, + { label: "Open Nightscout", run: () => { + close(); + openNightscout(); + } }, + ]); + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const step = lineStep(font); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const contentX = NOTIFICATION_CONTENT_X; + const contentWidth = notificationContentWidth(width); + const nowMs = Date.now(); + + let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`); + image.drawText(font, contentX, y, "Missed bolus?", 230); + y += step; + const minutes = Math.round((this.rise.latestMs - this.rise.onsetMs) / 60_000); + const body = `Glucose rose ${this.rise.riseMgdl} mg/dL in ${minutes} min with no bolus or carbs entered.`; + for (const line of wrapText(font, body, contentWidth)) { + image.drawText(font, contentX, y, line, 190); + y += step; + } + + // Live readout and graph: the modal repaints with each shell render, so + // these follow new readings (and a bolus, once entered) while it is up. + const nightscout = nightscoutBridge.snapshot(); + const latest = nightscout.latest; + if (latest) { + const value = `${latest.sgv}`; + image.drawText(font, contentX, y, value, 230); + if (isNightscoutPointStale(latest, nowMs)) { + drawNightscoutValueStrikeThrough(image, contentX, y + (font.lineHeight >> 1), font.measureText(value)); + } + let x = contentX + font.measureText(`${value} `); + x += drawDirectionIndicator(image, font, x, y, nightscout.direction, 200); + x += font.measureText(" "); + const iob = nightscout.iob === null ? "" : ` IOB ${nightscout.iob.toFixed(2)}`; + image.drawText(font, x, y, `${formatDelta(nightscout.delta)}${iob}`, 180); + y += step; + } + const graphTop = y + GRAPH_GAP; + const graphHeight = height - GRAPH_BOTTOM_MARGIN - graphTop; + if (graphHeight >= MIN_GRAPH_HEIGHT) { + drawNightscoutGraph(image, { x: contentX, y: graphTop, width: contentWidth, height: graphHeight }, nightscout, nowMs, font); + } + + this.menu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused()); + return image; + } + + async handleInput(event: InputEvent): Promise { + if (event.type === "double-click") { + this.close(); + } else if (event.type === "click") { + this.menu.selectedItem?.run(); + } else if (event.type === "scroll-up" || event.type === "scroll-down") { + await this.menu.handleInput(event); + } + } +} diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 675a07f8..8266a31d 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -13,6 +13,8 @@ import { nightscoutBatteryLowSetting, nightscoutMaxLoopAgeSetting, nightscoutAlwaysShowInTopBarSetting, + nightscoutMissedBolusAlertsSetting, + nightscoutMissedBolusCooldownSetting, toggleSettingMenuItem, nightscoutApiTokenSetting, nightscoutSiteUrlSetting, @@ -28,6 +30,7 @@ export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000; const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000; const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000; +/** Draws the trend indicator; returns its width. */ export function drawDirectionIndicator( image: GrayImage, font: UiFont, @@ -35,16 +38,10 @@ export function drawDirectionIndicator( y: number, direction: string, shade: number, -): void { - const label = directionGlyphLabel(font, direction); - if (label) { - image.drawText(font, x, y, label, shade); - return; - } - - if (direction) { - image.drawText(font, x, y, truncateLine(direction, 10), shade); - } +): number { + const label = directionGlyphLabel(font, direction) || (direction ? truncateLine(direction, 10) : ""); + if (label) image.drawText(font, x, y, label, shade); + return font.measureText(label); } /** @@ -425,6 +422,8 @@ export function nightscoutMenuItems(): MenuItem[] { textSettingMenuItem(nightscoutCartridgeLowSetting), textSettingMenuItem(nightscoutBatteryLowSetting), textSettingMenuItem(nightscoutMaxLoopAgeSetting), + toggleSettingMenuItem(nightscoutMissedBolusAlertsSetting), + textSettingMenuItem(nightscoutMissedBolusCooldownSetting), ]); }, }, diff --git a/app/native/nightscout-bridge.ts b/app/native/nightscout-bridge.ts index 9271d7c2..51be4f57 100644 --- a/app/native/nightscout-bridge.ts +++ b/app/native/nightscout-bridge.ts @@ -1,5 +1,6 @@ import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings"; import { fetchTextWithUserAgent } from "../util/http"; +import { bolusKindFromTreatment, type BolusKind } from "../apps/nightscout/missed-bolus-detector"; export type NightscoutPoint = { timestampMs: number; @@ -14,6 +15,8 @@ export type NightscoutCarbEvent = { export type NightscoutBolusEvent = { timestampMs: number; insulin: number; + /** Meal, manual, loop-automatic, or unmarked (see BolusKind). */ + kind: BolusKind; }; export type NightscoutBasalEvent = { @@ -102,10 +105,17 @@ type NightscoutTreatmentResponse = { absolute?: unknown; rate?: unknown; duration?: unknown; + automatic?: unknown; + isSMB?: unknown; + type?: unknown; + wizard?: unknown; }; const NIGHTSCOUT_REFRESH_MS = 60_000; -const NIGHTSCOUT_HISTORY_COUNT = 30; +// Glucose history by time rather than count, so a 1-minute CGM still covers +// the graphs' 2 hours and the missed-bolus detector's 90-minute lookback. +const NIGHTSCOUT_HISTORY_WINDOW_MS = 3 * 60 * 60 * 1000; +const NIGHTSCOUT_HISTORY_MAX_COUNT = 200; const NIGHTSCOUT_TREATMENT_LOOKBACK_MS = 3 * 60 * 60 * 1000; // The v1 treatments API silently adds `created_at >= now - 4 days` to any // query without an explicit date filter (lib/server/query.js enforceDateFilter), @@ -208,13 +218,17 @@ export class NightscoutBridge { this.emit(); return; } + const historyStartMs = nowMs - NIGHTSCOUT_HISTORY_WINDOW_MS; const treatmentStartIso = new Date(nowMs - NIGHTSCOUT_TREATMENT_LOOKBACK_MS).toISOString(); const siteChangeStartIso = new Date(nowMs - NIGHTSCOUT_SITE_CHANGE_LOOKBACK_MS).toISOString(); const tokenQuery = `token=${encodeURIComponent(apiToken)}`; const [entries, deviceStatus, status, treatments, siteChanges] = await Promise.all([ fetchJson( - `${siteUrl}/api/v1/entries.json?count=${NIGHTSCOUT_HISTORY_COUNT}&${tokenQuery}`, - ), + `${siteUrl}/api/v1/entries.json?find%5Bdate%5D%5B%24gte%5D=${historyStartMs}&count=${NIGHTSCOUT_HISTORY_MAX_COUNT}&${tokenQuery}`, + ).then(async (recent) => recent?.length + ? recent + // Nothing in the window: still show the last reading, as stale. + : await fetchJson(`${siteUrl}/api/v1/entries.json?count=1&${tokenQuery}`)), fetchJson( `${siteUrl}/api/v1/devicestatus.json?count=1&${tokenQuery}`, ), @@ -334,7 +348,7 @@ function parseTreatments(treatments: NightscoutTreatmentResponse[], nowMs: numbe const insulinValue = firstFiniteNumber(treatment.insulin); if (insulinValue !== null && insulinValue > 0) { - boluses.push({ timestampMs, insulin: insulinValue }); + boluses.push({ timestampMs, insulin: insulinValue, kind: bolusKindFromTreatment(treatment) }); } const eventType = typeof treatment.eventType === "string" ? treatment.eventType : ""; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 083291fe..7aed28fd 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1176,6 +1176,23 @@ export const nightscoutAlwaysShowInTopBarSetting = new ConfigSettingBoolean({ defaultValue: false, description: "Keep the Nightscout glucose graph and warnings in the top bar even when all Nightscout windows are closed.", }); +export const nightscoutMissedBolusAlertsSetting = new ConfigSettingBoolean({ + id: "nightscout-missed-bolus-alerts", + label: "Missed-bolus alerts", + storageKey: "integrations.nightscout.missedBolusAlerts", + defaultValue: false, + description: "Show a notification on the glasses when glucose rises like an unannounced meal with no bolus or carbs entered. Experimental.", +}); +export const nightscoutMissedBolusCooldownSetting = new ConfigSettingString({ + id: "nightscout-missed-bolus-cooldown-minutes", + label: "Missed-bolus alert cooldown", + storageKey: "integrations.nightscout.missedBolusCooldownMinutes", + defaultValue: "120", + editorTitle: "Missed-bolus alert cooldown (min)", + normalize: normalizeNightscoutThreshold, + formatValue: (value) => `${value} min`, + description: "After a missed-bolus alert, wait at least this many minutes before showing another.", +}); export function loadNightscoutThresholds(): NightscoutThresholds { return { diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index c2c3c57d..268e7030 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -190,22 +190,7 @@ export class NotificationsListLayer implements Layer { */ export class SingleNotificationLayer implements Layer { /** The action column; its items are rebuilt from the live notification on every paint and input. */ - private readonly detailMenu = new Menu({ - wrap: false, - rowGap: DETAIL_MENU_ROW_GAP, - highlight: { radius: 6 }, - getHeight: (item, width) => { - const font = getDefaultSmallFont(); - return detailMenuRowLines(font, item, width).length * lineStep(font) + 8; - }, - draw: ({ image, item, x, y, width, selected }) => { - const font = getDefaultSmallFont(); - const lines = detailMenuRowLines(font, item, width); - for (let line = 0; line < lines.length; line++) { - image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185); - } - }, - }); + private readonly detailMenu = createNotificationActionMenu((item) => item.kind === "disable-source"); private confirmation: { source: AndroidNotification; menu: Menu } | null = null; // The last paint, which a tray-hosted layer repeats once its notification // is gone, until the tray swaps in a neighbour's (see closeUnavailableNotification). @@ -234,7 +219,7 @@ export class SingleNotificationLayer implements Layer { this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin)); drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height); - this.detailMenu.paint(image, detailMenuBox(width, height), ctx.stack.isFocused()); + this.detailMenu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused()); this.lastImage = image; return image; } @@ -432,20 +417,9 @@ function drawDetailContent( width: number, height: number, ): void { - const contentX = DETAIL_CONTENT_X; - const menuX = width - DETAIL_MENU_WIDTH - 24; - const contentWidth = menuX - contentX - 20; - image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220); - // The header row (icon + app name) and the body stack below the title's - // line box, so a large UI font pushes them down instead of overlapping. - const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP; - const headerHeight = Math.max(ICON_SIZE, font.lineHeight); - let appLineX = contentX; - if (icon) { - image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true }); - appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP; - } - image.drawText(font, appLineX, centeredTextY(font, headerTop, headerHeight), `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150); + const contentX = NOTIFICATION_CONTENT_X; + const contentWidth = notificationContentWidth(width); + const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`); const lines: string[] = []; lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth)); @@ -461,7 +435,6 @@ function drawDetailContent( } const step = lineStep(font); - const bodyTop = headerTop + headerHeight + DETAIL_BODY_GAP; const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0); for (let index = 0; index < Math.min(lines.length, maxLines); index++) { const line = lines[index]!; @@ -472,8 +445,63 @@ function drawDetailContent( } } +/** Left edge of a notification detail view's content column. */ +export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X; + +/** Width of a notification detail view's content column, left of the action column. */ +export function notificationContentWidth(width: number): number { + return width - DETAIL_MENU_WIDTH - 24 - NOTIFICATION_CONTENT_X - 20; +} + +/** + * The top of a notification detail view: the "Notification" title, then the + * source's icon and label. Returns the y where the body starts. + */ +export function drawNotificationHeader(image: GrayImage, font: UiFont, icon: GrayImage | null, sourceLabel: string): number { + const contentX = NOTIFICATION_CONTENT_X; + image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220); + // The header row (icon + app name) and the body stack below the title's + // line box, so a large UI font pushes them down instead of overlapping. + const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP; + const headerHeight = Math.max(ICON_SIZE, font.lineHeight); + let sourceX = contentX; + if (icon) { + image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true }); + sourceX = contentX + ICON_SIZE + ICON_TEXT_GAP; + } + image.drawText(font, sourceX, centeredTextY(font, headerTop, headerHeight), sourceLabel, 150); + return headerTop + headerHeight + DETAIL_BODY_GAP; +} + +/** + * A notification detail view's right-hand action column. Rows truncate their + * label to one line, except those `wraps` picks, which wrap it. + */ +export function createNotificationActionMenu(wraps: (item: T) => boolean = () => false): Menu { + const rowLines = (font: UiFont, item: T, rowWidth: number): string[] => { + const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X; + return wraps(item) ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)]; + }; + return new Menu({ + wrap: false, + rowGap: DETAIL_MENU_ROW_GAP, + highlight: { radius: 6 }, + getHeight: (item, width) => { + const font = getDefaultSmallFont(); + return rowLines(font, item, width).length * lineStep(font) + 8; + }, + draw: ({ image, item, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const lines = rowLines(font, item, width); + for (let line = 0; line < lines.length; line++) { + image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185); + } + }, + }); +} + /** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */ -function detailMenuBox(width: number, height: number): MenuBox { +export function notificationActionMenuBox(width: number, height: number): MenuBox { return { x: width - DETAIL_MENU_WIDTH - 32, y: DETAIL_MENU_TOP, @@ -482,12 +510,6 @@ function detailMenuBox(width: number, height: number): MenuBox { }; } -/** An action row's text lines: the disable-source row wraps, the rest truncate to one line. */ -function detailMenuRowLines(font: UiFont, item: DetailMenuItem, rowWidth: number): string[] { - const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X; - return item.kind === "disable-source" ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)]; -} - function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] { return [ { kind: "back", label: "Back" }, diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index 001adcbb..4055eaf3 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -239,8 +239,11 @@ export type TrayWidget = { openWindow: () => { window: ShellWindow; opened: boolean } | null; }; -/** A new-notification popup: the shell modal, and the notification view inside it. */ -type NotificationPopup = { modal: ShellModalLayer; content: SingleNotificationLayer }; +/** + * A new-notification popup: the shell modal, and the notification view inside + * it (null for an app notification, which brings its own layer). + */ +type NotificationPopup = { modal: ShellModalLayer; content: SingleNotificationLayer | null }; /** * The app switcher's selection while it is on a status bar item rather than @@ -282,6 +285,18 @@ export type AssistantActivityEvent = { export type FocusKind = "sidebar" | "window"; +/** + * A notification an app posts itself, rather than one Android posted. It + * opens in the same modal (and waits in the same queue) as Android + * notifications, but is not listed in the Notifications app. + */ +export type AppNotification = { + /** Unique per notification; must not collide with Android notification keys. */ + key: string; + /** The modal's content; `close` takes the modal down. */ + createLayer: (close: () => void) => Layer; +}; + const noopActions: LayerActions = noopLayerActions; /** @@ -437,9 +452,12 @@ class Shell { private readonly trayIcons = new Map(); private activeVoiceLayer: VoiceInputLayer | null = null; private activeKeyboardLayer: KeyboardInputLayer | null = null; + // App-posted notifications that are open or queued, by key. + private readonly appNotifications = new Map(); private readonly notificationModals = new NotificationModalQueue({ open: (notificationKey) => this.pushNotificationModal(notificationKey), isAvailable: (notificationKey) => { + if (this.appNotifications.has(notificationKey)) return true; const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey); return !!notification && shouldShowNotificationOnGlasses(notification.packageName); }, @@ -866,25 +884,44 @@ class Shell { */ notificationRemoved(notificationKey: string): void { const popup = this.notificationModals.withdrawn(notificationKey); - if (popup && !popup.content.outlivesNotification()) { - this.closeNotificationModal(popup); + if (popup && !popup.content?.outlivesNotification()) { + this.closeNotificationModal(popup, notificationKey); } } + /** + * Show an app's own notification the way new Android notifications are + * shown: in the notification modal, waking the screen for it if it was off. + */ + postAppNotification(notification: AppNotification): void { + this.appNotifications.set(notification.key, notification); + const wokeScreen = this.screenOn ? false : this.wake("sidebar"); + this.openNotificationModal(notification.key, wokeScreen); + } + private pushNotificationModal(notificationKey: string): NotificationPopup { - const content = new SingleNotificationLayer(notificationKey, { - origin: "new-notification-modal", - onClose: () => this.closeNotificationModal(popup), - }); + const appNotification = this.appNotifications.get(notificationKey); + const close = () => this.closeNotificationModal(popup, notificationKey); + const content = appNotification + ? null + : new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", onClose: close }); const popup: NotificationPopup = { content, - modal: new ShellModalLayer(content, this.config.actions, () => this.notificationModals.removed(popup, this.screenOn)), + modal: new ShellModalLayer( + appNotification ? appNotification.createLayer(close) : content!, + this.config.actions, + () => { + this.appNotifications.delete(notificationKey); + this.notificationModals.removed(popup, this.screenOn); + }, + ), }; this.stack.push(popup.modal); return popup; } - private closeNotificationModal(popup: NotificationPopup): void { + private closeNotificationModal(popup: NotificationPopup, notificationKey: string): void { + this.appNotifications.delete(notificationKey); // The queue first: it opens the next notification or puts the screen back // to sleep, and then ignores the removal below. remove() rather than a // pop, so a menu opened over the modal doesn't leave it stuck. diff --git a/scripts/nightscout-missed-bolus-replay.cjs b/scripts/nightscout-missed-bolus-replay.cjs new file mode 100644 index 00000000..17b5c24a --- /dev/null +++ b/scripts/nightscout-missed-bolus-replay.cjs @@ -0,0 +1,157 @@ +#!/usr/bin/env node +// Replays the missed-bolus detector (app/apps/nightscout/missed-bolus-detector.ts) +// over recorded Nightscout history, as if it had run after every CGM reading +// with the app's cooldown, and lists where it would have alerted next to the +// meal boluses around them. For tuning the detector without the glasses. +// +// node scripts/nightscout-missed-bolus-replay.cjs --site https://x.herokuapp.com --token TOKEN --days 14 +// node scripts/nightscout-missed-bolus-replay.cjs --site ... --token ... --days 30 --save /tmp/ns +// node scripts/nightscout-missed-bolus-replay.cjs --load /tmp/ns --set minRiseMgdl=50 --set minRateMgdlPerMin=2 +// +// --site/--token default to $NIGHTSCOUT_URL/$NIGHTSCOUT_TOKEN. --save writes +// the fetched entries.json/treatments.json to a directory; --load replays a +// saved directory instead of fetching. --set overrides one detector option +// (see MissedBolusOptions); --cooldown-min sets the alert cooldown (default +// 120, the app's default); --verbose also prints every classification change. + +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const DETECTOR = path.join(__dirname, '../app/apps/nightscout/missed-bolus-detector.ts'); +// Matches the bridge's history window: the detector looks back 90 min, plus slack. +const HISTORY_WINDOW_MS = 3 * 60 * 60_000; +// The app sees a reading at the next minutely sync after its upload. +const SYNC_LAG_MS = 60_000; +// A meal bolus this soon after an alert counts as the alert being answered. +const ANSWER_WINDOW_MS = 3 * 60 * 60_000; + +function loadDetector() { + const source = ts.transpileModule(fs.readFileSync(DETECTOR, 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText; + const exports = {}; + vm.runInNewContext(source, { exports }, { filename: DETECTOR }); + return exports; +} + +function parseArgs(argv) { + const args = { site: process.env.NIGHTSCOUT_URL ?? '', token: process.env.NIGHTSCOUT_TOKEN ?? '', days: 14, cooldownMin: 120, set: {}, verbose: false }; + for (let i = 0; i < argv.length; i++) { + const flag = argv[i]; + const value = () => { + if (i + 1 >= argv.length) throw new Error(`${flag} needs a value`); + return argv[++i]; + }; + if (flag === '--site') args.site = value().replace(/\/+$/, ''); + else if (flag === '--token') args.token = value(); + else if (flag === '--days') args.days = Number(value()); + else if (flag === '--save') args.save = value(); + else if (flag === '--load') args.load = value(); + else if (flag === '--cooldown-min') args.cooldownMin = Number(value()); + else if (flag === '--verbose') args.verbose = true; + else if (flag === '--set') { + const [key, raw] = value().split('='); + args.set[key] = raw === 'true' ? true : raw === 'false' ? false : Number(raw); + } else throw new Error(`unknown argument ${flag}`); + } + return args; +} + +async function fetchJson(url) { + const response = await fetch(url); + if (!response.ok) throw new Error(`HTTP ${response.status} for ${url.replace(/token=[^&]*/, 'token=...')}`); + return response.json(); +} + +async function loadHistory(args) { + if (args.load) { + return { + entries: JSON.parse(fs.readFileSync(path.join(args.load, 'entries.json'), 'utf8')), + treatments: JSON.parse(fs.readFileSync(path.join(args.load, 'treatments.json'), 'utf8')), + }; + } + if (!args.site || !args.token) throw new Error('need --site and --token (or $NIGHTSCOUT_URL/$NIGHTSCOUT_TOKEN), or --load'); + const startMs = Date.now() - args.days * 86_400_000; + const token = `token=${encodeURIComponent(args.token)}`; + // Room for 1-minute CGMs; Nightscout otherwise caps a query at 10 results. + const count = Math.ceil(args.days * 1440 * 1.1); + const [entries, treatments] = await Promise.all([ + fetchJson(`${args.site}/api/v1/entries/sgv.json?find%5Bdate%5D%5B%24gte%5D=${startMs}&count=${count}&${token}`), + fetchJson(`${args.site}/api/v1/treatments.json?find%5Bcreated_at%5D%5B%24gte%5D=${encodeURIComponent(new Date(startMs).toISOString())}&count=${count}&${token}`), + ]); + if (args.save) { + fs.mkdirSync(args.save, { recursive: true }); + fs.writeFileSync(path.join(args.save, 'entries.json'), JSON.stringify(entries)); + fs.writeFileSync(path.join(args.save, 'treatments.json'), JSON.stringify(treatments)); + } + return { entries, treatments }; +} + +function formatTime(ms) { + const date = new Date(ms); + const pad = (n) => String(n).padStart(2, '0'); + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`; +} + +function minutes(ms) { + return `${Math.round(ms / 60_000)} min`; +} + +async function main() { + const args = parseArgs(process.argv.slice(2)); + const detector = loadDetector(); + for (const key of Object.keys(args.set)) { + if (!(key in detector.DEFAULT_MISSED_BOLUS_OPTIONS)) throw new Error(`unknown detector option ${key}`); + } + const { entries, treatments } = await loadHistory(args); + const glucose = detector.normalizeNightscoutEntries(entries); + const { boluses, carbs } = detector.normalizeNightscoutTreatments(treatments); + const mealBoluses = boluses.filter((bolus) => bolus.kind === 'meal'); + if (!glucose.length) throw new Error('no glucose entries'); + + const cooldownMs = args.cooldownMin * 60_000; + const alerts = []; + let lastAlertMs = -Infinity; + let lastSummary = ''; + let first = 0; + for (const reading of glucose) { + const nowMs = reading.timeMs + SYNC_LAG_MS; + while (glucose[first].timeMs < nowMs - HISTORY_WINDOW_MS) first++; + const inWindow = (event) => event.timeMs >= nowMs - HISTORY_WINDOW_MS && event.timeMs <= nowMs; + const result = detector.detectMissedBolus({ + nowMs, + glucose: glucose.slice(first).filter((r) => r.timeMs <= nowMs), + boluses: boluses.filter(inWindow), + carbs: carbs.filter(inWindow), + }, args.set); + const summary = `${result.classification}: ${result.reason}`; + if (args.verbose && summary !== lastSummary) console.log(` ${formatTime(nowMs)} ${reading.mgdl} ${summary}`); + lastSummary = summary; + if (result.classification !== 'missed-bolus' || nowMs - lastAlertMs < cooldownMs) continue; + lastAlertMs = nowMs; + alerts.push({ nowMs, result }); + } + + const spanDays = (glucose[glucose.length - 1].timeMs - glucose[0].timeMs) / 86_400_000; + console.log(`${glucose.length} readings over ${spanDays.toFixed(1)} days; ${mealBoluses.length} meal boluses; ${boluses.filter((b) => b.kind === 'ambiguous').length} unmarked boluses (max ${Math.max(0, ...boluses.filter((b) => b.kind === 'ambiguous').map((b) => b.units))} U)`); + console.log(`options: ${JSON.stringify({ ...detector.DEFAULT_MISSED_BOLUS_OPTIONS, ...args.set })}`); + console.log(`\n${alerts.length} alerts (${(alerts.length / Math.max(spanDays, 1 / 24)).toFixed(2)}/day, cooldown ${args.cooldownMin} min):`); + for (const { nowMs, result } of alerts) { + const answer = mealBoluses.find((bolus) => bolus.timeMs > nowMs && bolus.timeMs - nowMs <= ANSWER_WINDOW_MS); + const note = answer ? `meal bolus ${answer.units} U ${minutes(answer.timeMs - nowMs)} later` : 'no meal bolus within 3 h'; + console.log(` ${formatTime(nowMs)} ${result.rise.latestMgdl} mg/dL ${result.reason} [${note}]`); + } + console.log('\nmeal boluses:'); + for (const bolus of mealBoluses) { + const alert = alerts.find(({ nowMs }) => nowMs < bolus.timeMs && bolus.timeMs - nowMs <= ANSWER_WINDOW_MS); + const meal = carbs.find((entry) => entry.timeMs === bolus.timeMs); + console.log(` ${formatTime(bolus.timeMs)} ${bolus.units} U${meal ? ` ${meal.grams} g` : ''}${alert ? ` [alerted ${minutes(bolus.timeMs - alert.nowMs)} before]` : ''}`); + } +} + +main().catch((error) => { + console.error(error.message ?? error); + process.exit(1); +}); diff --git a/tests/missed-bolus-detector.test.cjs b/tests/missed-bolus-detector.test.cjs new file mode 100644 index 00000000..046a2951 --- /dev/null +++ b/tests/missed-bolus-detector.test.cjs @@ -0,0 +1,137 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js'); + +const MIN = 60_000; +const T0 = 1_800_000_000_000; + +/** + * 5-minute readings: flat at `base` for `flatMin`, then rising `rate` mg/dL + * per minute for `riseMin`. The rise starts at T0 + flatMin. + */ +function series({ base = 100, flatMin = 60, rate = 2.5, riseMin = 40 } = {}) { + const readings = []; + for (let m = 0; m <= flatMin + riseMin; m += 5) { + readings.push({ timeMs: T0 + m * MIN, mgdl: Math.round(base + Math.max(0, m - flatMin) * rate) }); + } + return readings; +} + +/** A detector input one minute after the last reading. */ +function input(glucose, { boluses = [], carbs = [] } = {}) { + return { nowMs: glucose[glucose.length - 1].timeMs + MIN, glucose, boluses, carbs }; +} + +const onset = T0 + 60 * MIN; + +test('an unannounced meal-shaped rise is a missed bolus', () => { + const result = detector.detectMissedBolus(input(series())); + assert.equal(result.classification, 'missed-bolus', result.reason); + assert.equal(result.rise.onsetMs, onset); + assert.equal(result.rise.nadirMgdl, 100); + assert.equal(result.rise.latestMgdl, 200); + assert.equal(result.rise.riseMgdl, 100); + assert.ok(Math.abs(result.rise.rateMgdlPerMin - 2.5) < 0.01); +}); + +test('a meal or manual bolus from the pre-bolus window onward accounts for the rise', () => { + for (const [kind, offsetMin] of [['meal', -50], ['meal', 15], ['manual', 30]]) { + const result = detector.detectMissedBolus(input(series(), { + boluses: [{ timeMs: onset + offsetMin * MIN, units: 5, kind }], + })); + assert.equal(result.classification, 'no-alert', `${kind} at ${offsetMin}: ${result.reason}`); + } + // Too long before the rise began, or too small, doesn't count. + for (const bolus of [{ timeMs: onset - 70 * MIN, units: 5, kind: 'meal' }, { timeMs: onset, units: 0.3, kind: 'meal' }]) { + assert.equal(detector.detectMissedBolus(input(series(), { boluses: [bolus] })).classification, 'missed-bolus'); + } +}); + +test('loop boluses do not count; unmarked ones count only above the SMB size threshold', () => { + const smbs = [0, 10, 20, 30].map((m) => ({ timeMs: onset + m * MIN, units: 1.4, kind: 'ambiguous' })); + smbs.push({ timeMs: onset + 5 * MIN, units: 3, kind: 'automatic' }); + assert.equal(detector.detectMissedBolus(input(series(), { boluses: smbs })).classification, 'missed-bolus'); + const manual = [{ timeMs: onset + 20 * MIN, units: 2, kind: 'ambiguous' }]; + assert.equal(detector.detectMissedBolus(input(series(), { boluses: manual })).classification, 'no-alert'); + assert.equal(detector.detectMissedBolus(input(series(), { boluses: manual }), { ambiguousBolusMinUnits: 2.5 }).classification, 'missed-bolus'); +}); + +test('a carb entry accounts for the rise unless that option is off', () => { + const carbs = [{ timeMs: onset + 10 * MIN, grams: 40 }]; + assert.equal(detector.detectMissedBolus(input(series(), { carbs })).classification, 'no-alert'); + assert.equal(detector.detectMissedBolus(input(series(), { carbs }), { carbsCountAsAnnouncement: false }).classification, 'missed-bolus'); +}); + +test('rises that are small, slow, brief, low, or from a hypo do not alert', () => { + const cases = [ + series({ rate: 0.9, riseMin: 60 }), // slow: under 1.5 mg/dL/min + series({ rate: 3, riseMin: 15 }), // brief: 15 min + series({ base: 80, rate: 1.6, riseMin: 30 }), // ends at 128, under 140 + series({ base: 60, rate: 3, riseMin: 40 }), // recovering from a low + series({ base: 150, rate: 1.5, riseMin: 20 }), // only 30 mg/dL + ]; + for (const glucose of cases) { + const result = detector.detectMissedBolus(input(glucose)); + assert.equal(result.classification, 'no-alert', result.reason); + assert.ok(result.rise); + } +}); + +test('a rise that has peaked and turned down does not alert', () => { + const glucose = series(); + const last = glucose[glucose.length - 1]; + for (let m = 5; m <= 20; m += 5) glucose.push({ timeMs: last.timeMs + m * MIN, mgdl: last.mgdl - m }); + assert.equal(detector.detectMissedBolus(input(glucose)).classification, 'no-alert'); +}); + +test('stale, missing, or gapped data is insufficient', () => { + const glucose = series(); + const stale = { ...input(glucose), nowMs: glucose[glucose.length - 1].timeMs + 20 * MIN }; + assert.equal(detector.detectMissedBolus(stale).classification, 'insufficient-data'); + assert.equal(detector.detectMissedBolus({ nowMs: T0, glucose: [], boluses: [], carbs: [] }).classification, 'insufficient-data'); + // Readings after "now" are ignored rather than used. + const early = { ...input(glucose), nowMs: onset + 11 * MIN }; + assert.notEqual(detector.detectMissedBolus(early).classification, 'missed-bolus'); + // A CGM dropout splits the window: the rise before it doesn't count. + const gapped = series().filter((r) => r.timeMs < onset + 10 * MIN || r.timeMs > onset + 30 * MIN); + assert.notEqual(detector.detectMissedBolus(input(gapped)).classification, 'missed-bolus'); + // Dexcom error codes are dropped; 39 ("LOW") is a real reading. + const withErrors = series().map((r, i) => (i === 3 ? { ...r, mgdl: 5 } : r)); + assert.equal(detector.detectMissedBolus(input(withErrors)).classification, 'missed-bolus'); +}); + +test('bolus kinds from each uploader', () => { + const kind = detector.bolusKindFromTreatment; + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.5, automatic: true }), 'automatic'); // Loop + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.5, automatic: false }), 'manual'); // Loop + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.4, isSMB: true }), 'automatic'); // AndroidAPS + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.4, type: 'SMB' }), 'automatic'); // Trio + assert.equal(kind({ eventType: 'SMB', insulin: 0.4 }), 'automatic'); + assert.equal(kind({ eventType: 'Meal Bolus', insulin: 4 }), 'meal'); + assert.equal(kind({ eventType: 'Bolus', insulin: 4, carbs: 30 }), 'meal'); + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 4, wizard: { carb_input: 0 } }), 'meal'); + // OpenAPS on a Medtronic pump: SMBs and manual corrections look the same. + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.3, carbs: null, enteredBy: 'openaps://medtronic/723' }), 'ambiguous'); +}); + +test('normalizing raw Nightscout records', () => { + const { boluses, carbs } = detector.normalizeNightscoutTreatments([ + { eventType: 'Meal Bolus', created_at: '2026-10-11T23:05:32.000Z', insulin: 10.7, carbs: 75, wizard: {} }, + { eventType: 'Correction Bolus', created_at: '2026-10-11T22:05:00.000Z', insulin: 0.3, carbs: null }, + { eventType: 'Temp Basal', created_at: '2026-10-11T22:00:00.000Z', insulin: null, carbs: null, rate: 1.2 }, + { eventType: 'Carb Correction', timestamp: '2026-10-11T21:00:00.000Z', carbs: '15' }, + { eventType: 'Correction Bolus', insulin: 1 }, // no time + ]); + assert.deepEqual(boluses, [ + { timeMs: Date.parse('2026-10-11T22:05:00.000Z'), units: 0.3, kind: 'ambiguous' }, + { timeMs: Date.parse('2026-10-11T23:05:32.000Z'), units: 10.7, kind: 'meal' }, + ]); + assert.deepEqual(carbs, [ + { timeMs: Date.parse('2026-10-11T21:00:00.000Z'), grams: 15 }, + { timeMs: Date.parse('2026-10-11T23:05:32.000Z'), grams: 75 }, + ]); + assert.deepEqual( + detector.normalizeNightscoutEntries([{ date: 2000, sgv: 120 }, { date: 1000, sgv: 110 }, { date: 3000 }]), + [{ timeMs: 1000, mgdl: 110 }, { timeMs: 2000, mgdl: 120 }], + ); +}); diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index e1deee44..8dcbb916 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -5,6 +5,7 @@ const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); const alerts = require('../.test-build/app/apps/nightscout/nightscout-alerts.js'); +const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js'); const now = 1_800_000_000_000; const limits = { maxCannulaAgeHours: 72, cartridgeLowUnits: 20, batteryLowVoltage: 1.2, maxLoopAgeMinutes: 15 }; const healthy = { cageTimestampMs: now, reservoirUnits: 100, batteryVoltage: 1.5, loopTimestampMs: now }; @@ -206,6 +207,7 @@ test('bridge exposes numeric measurements and treats absent values as unknown', let pump = { reservoir: '10.5', battery: { voltage: '1.15' } }; const module = load('app/native/nightscout-bridge.ts', { '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, '../util/http': { async fetchTextWithUserAgent(url) { const body = url.includes('/entries.') ? [{ date: now, sgv: 100 }] : url.includes('/devicestatus.') ? [{ pump, openaps: { suggested: { mills: now - 60_000 } } }] : url.includes('/status.') ? {} : []; return { ok: true, json: async () => body }; @@ -225,12 +227,44 @@ test('bridge exposes numeric measurements and treats absent values as unknown', assert.equal(bridge.snapshot().reservoirUnits, 0); }); +test('bridge tags bolus kinds and falls back to the last reading when the window is empty', async () => { + const urls = []; + let recent = []; + const module = load('app/native/nightscout-bridge.ts', { + '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, + '../util/http': { async fetchTextWithUserAgent(url) { + urls.push(url); + const iso = new Date(now - 30 * 60_000).toISOString(); + const body = url.includes('/entries.') ? (url.includes('%24gte') ? recent : [{ date: now - 5 * 3_600_000, sgv: 140 }]) + : url.includes('/treatments.') && !url.includes('Site%20Change') ? [ + { eventType: 'Meal Bolus', created_at: iso, insulin: 6, carbs: 50 }, + { eventType: 'Correction Bolus', created_at: iso, insulin: 0.3 }, + { eventType: 'Correction Bolus', created_at: iso, insulin: 0.2, automatic: true }, + ] + : url.includes('/status.') ? {} : []; + return { ok: true, json: async () => body }; + } }, + }, { Date: class extends Date { static now() { return now; } }, console }); + const bridge = new module.NightscoutBridge(); + await bridge.refreshNow(); + assert.deepEqual(Array.from(bridge.snapshot().boluses, b => b.kind).sort(), ['ambiguous', 'automatic', 'meal']); + assert.equal(bridge.snapshot().latest.sgv, 140, 'a stale last reading still shows'); + assert.ok(urls.some(url => url.includes('/entries.json?count=1&'))); + recent = [{ date: now - 60_000, sgv: 120 }]; + urls.length = 0; + await bridge.refreshNow(); + assert.equal(bridge.snapshot().latest.sgv, 120); + assert.ok(!urls.some(url => url.includes('/entries.json?count=1&')), 'no fallback when the window has readings'); +}); + test('polling starts once and stops even while the initial HTTP request is pending', async () => { let resolveHttp, calls = 0, nextTimer = 0; const pending = new Promise(resolve => { resolveHttp = resolve; }); const timers = new Map(); const module = load('app/native/nightscout-bridge.ts', { '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, '../util/http': { async fetchTextWithUserAgent() { calls++; await pending; @@ -248,6 +282,7 @@ test('polling starts once and stops even while the initial HTTP request is pendi assert.equal(timers.size, 0, 'finishing the first refresh must not restart polling'); await bridge.start(); assert.equal(timers.size, 1); - assert.equal(calls, 10); + // Five requests per refresh, plus the last-reading fallback for the empty glucose window. + assert.equal(calls, 12); await bridge.stop(); }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 4e18c26c..843e6875 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -55,6 +55,7 @@ "../app/apps/timer/timer-model.ts", "../app/apps/teleprompter/script-tracker.ts", "../app/apps/nightscout/nightscout-alerts.ts", + "../app/apps/nightscout/missed-bolus-detector.ts", "../app/g2/brightness-curve.ts", "../app/g2/events.ts", "../app/apps/t3code/t3-rpc.ts",