Files
glucose-dashboard/pages/3_AGP_Profiel.py
T
willemjan 29d20ca441 fix: Nightscout API silently truncated multi-day fetches to ~3-4 days
_entries_since/_treatments_since requested a single page via `count`,
but Nightscout's /api/v1/entries and /api/v1/treatments ignore a large
`count` and cap the response around ~1150/~700 documents regardless of
how far back the requested period goes. Every report page relying on
fetch_entries_range/fetch_treatments_range (7/14/30/90-day selectors)
was silently only getting the most recent ~3-4 days, no matter the
selected period.

Fixed by paginating with `find[date][$lte]` cursors in a new
_paginated_get(), walking back until the requested `since` boundary is
reached or the server returns a short/empty page (= genuinely no more
data, confirmed by manually paginating against the real instance:
entries go back 50+ days, treatments only ~4 days on this install).

Also added reporting.completeness_warning(), shared by the interactive
pages (via ui_common.render_completeness_warning) and the PDF export,
that distinguishes "a structural gap at the start of the period" (data
wasn't fetched/available) from "scattered gaps below the clinical AGP
coverage threshold" (Danne et al. 2017) — the previous single generic
"X% sensor coverage" message read as a CGM/sensor problem even when
the real cause was this fetch bug.
2026-07-31 14:21:35 +02:00

109 lines
3.1 KiB
Python

import plotly.graph_objects as go
import requests
import streamlit as st
from auth import require_auth
from data_processing import TARGET_HIGH, TARGET_LOW
from nightscout_client import NightscoutConfigError
from reporting import agp_percentile_bands
from ui_common import (
get_colors,
hex_to_rgba,
load_range_data,
render_completeness_warning,
select_period_days,
)
st.set_page_config(page_title="Glucose dashboard — AGP profiel", page_icon="📊", layout="wide")
require_auth()
st.title("AGP profiel")
st.caption(
"Percentielgrafiek over een samengesteld 24-uursbeeld (FR3) — de 5e/25e/50e/75e/"
"95e percentiel van alle metingen op elk tijdstip van de dag in de gekozen periode."
)
start, end, days = select_period_days(default_label="90 dagen")
try:
glucose, _treatments = load_range_data(start, end)
except NightscoutConfigError as exc:
st.error(str(exc))
st.stop()
except (requests.RequestException, ValueError) as exc:
st.error(f"Kon geen data ophalen van Nightscout: {exc}")
st.stop()
if glucose.empty:
st.info("Geen glucosemetingen gevonden in deze periode.")
st.stop()
render_completeness_warning(glucose, start, end)
bands = agp_percentile_bands(glucose, bin_minutes=30)
colors = get_colors()
hours = bands["minute_of_day"] / 60
fig = go.Figure()
fig.add_hrect(y0=TARGET_LOW, y1=TARGET_HIGH, fillcolor=colors["band"], line_width=0, layer="below")
# 5e-95e percentiel als brede, lichte band.
fig.add_trace(
go.Scatter(x=hours, y=bands["p95"], mode="lines", line={"width": 0}, showlegend=False, hoverinfo="skip")
)
fig.add_trace(
go.Scatter(
x=hours,
y=bands["p5"],
mode="lines",
line={"width": 0},
fill="tonexty",
fillcolor=colors["band"],
name="5e-95e percentiel",
hoverinfo="skip",
)
)
# 25e-75e percentiel als smallere, donkerdere band.
fig.add_trace(
go.Scatter(x=hours, y=bands["p75"], mode="lines", line={"width": 0}, showlegend=False, hoverinfo="skip")
)
fig.add_trace(
go.Scatter(
x=hours,
y=bands["p25"],
mode="lines",
line={"width": 0},
fill="tonexty",
fillcolor=hex_to_rgba(colors["glucose"], 0.3),
name="25e-75e percentiel",
hoverinfo="skip",
)
)
# Mediaan.
fig.add_trace(
go.Scatter(
x=hours,
y=bands["p50"],
mode="lines",
line={"color": colors["glucose"], "width": 2},
name="Mediaan",
hovertemplate="%{x:.0f}u · %{y:.1f} mmol/L<extra></extra>",
)
)
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font_color=colors["text"],
legend={"orientation": "h", "yanchor": "bottom", "y": 1.02, "xanchor": "left", "x": 0},
margin={"l": 10, "r": 10, "t": 10, "b": 10},
height=480,
yaxis={"title": "mmol/L", "gridcolor": colors["grid"], "zeroline": False},
xaxis={"title": "Uur van de dag", "gridcolor": colors["grid"], "tickmode": "linear", "dtick": 2},
)
st.plotly_chart(fig, width="stretch")
st.caption(
f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')} ({len(glucose)} metingen)"
)