add Trends page: GMI trend, overnight/daytime, weekday pattern, bolus, site-age (FR7, FR8)

reporting.py gains overnight_daytime_split, day_of_week_pattern,
bolus_stats, and site_interval_stats. The last two are explicitly scoped
to bolus-only insulin (no basal reconstruction) since that needs the
Nightscout profile, which is the separate profile-tuning feature's
territory — documented as a caveat in both the UI and the design doc.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
2026-07-26 21:06:06 +02:00
co-authored by Claude Sonnet 5
parent 04c5f521fe
commit 9929f44daa
3 changed files with 363 additions and 0 deletions
+118
View File
@@ -0,0 +1,118 @@
import plotly.graph_objects as go
import requests
import streamlit as st
from auth import require_auth
from data_processing import classify_treatments
from nightscout_client import NightscoutConfigError
from reporting import (
bolus_stats,
day_of_week_pattern,
gmi_percent,
overnight_daytime_split,
site_interval_stats,
)
from ui_common import get_colors, load_range_data, select_period_days
st.set_page_config(page_title="Glucose dashboard — Trends", page_icon="📊", layout="wide")
require_auth()
st.title("Trends")
st.caption("Geschat HbA1c (GMI) over tijd en overige statistieken (FR7, FR8).")
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()
colors = get_colors()
events = classify_treatments(treatments)
# --- FR7: geschat HbA1c (GMI) per maand ------------------------------------
st.subheader("Geschat HbA1c (GMI) per maand")
monthly = glucose.copy()
monthly["month"] = monthly["timestamp"].dt.to_period("M").astype(str)
monthly_gmi = monthly.groupby("month")["mmol"].mean().apply(gmi_percent)
if len(monthly_gmi) >= 1:
fig = go.Figure(
go.Bar(x=monthly_gmi.index, y=monthly_gmi.to_numpy(), marker_color=colors["glucose"])
)
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font_color=colors["text"],
height=280,
margin={"l": 10, "r": 10, "t": 10, "b": 10},
yaxis={"title": "GMI (%)", "gridcolor": colors["grid"]},
xaxis={"title": "Maand", "gridcolor": colors["grid"]},
)
st.plotly_chart(fig, width="stretch")
st.caption("GMI is een schatting (Bergenstal et al. 2018), geen labwaarde.")
# --- FR8: nacht vs. overdag --------------------------------------------------
st.subheader("Nacht (00:00-06:00) vs. overdag")
split = overnight_daytime_split(glucose)
col1, col2 = st.columns(2)
col1.metric("Tijd in bereik 's nachts", f"{split['overnight']['in_range']:.0f}%")
col2.metric("Tijd in bereik overdag", f"{split['daytime']['in_range']:.0f}%")
# --- FR8: patroon per weekdag -------------------------------------------------
st.subheader("Patroon per weekdag")
weekday_df = day_of_week_pattern(glucose)
weekday_plot = weekday_df.dropna(subset=["mean_mmol"])
if not weekday_plot.empty:
fig = go.Figure(
go.Bar(x=weekday_plot["weekday"], y=weekday_plot["mean_mmol"], marker_color=colors["glucose"])
)
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font_color=colors["text"],
height=280,
margin={"l": 10, "r": 10, "t": 10, "b": 10},
yaxis={"title": "Gem. glucose (mmol/L)", "gridcolor": colors["grid"]},
xaxis={"gridcolor": colors["grid"]},
)
st.plotly_chart(fig, width="stretch")
# --- FR8: bolus-insuline --------------------------------------------------
st.subheader("Bolus-insuline")
st.caption(
"Alleen bolusinsuline (maaltijd/correctie/combo/SMB) — basaalinsuline vereist "
"profielgegevens en is hier niet in verwerkt (zie docs/design-reporting.md)."
)
bolus = bolus_stats(treatments)
col1, col2 = st.columns(2)
col1.metric("Totaal bolus", f"{bolus['total_units']:.1f} E")
col2.metric("Aantal boluses", bolus["count"])
if bolus["by_type"]:
st.table({"type": list(bolus["by_type"].keys()), "eenheden": list(bolus["by_type"].values())})
# --- FR8: pompsite-leeftijd vs. glucose/insuline ---------------------------
st.subheader("Pompsite-leeftijd vs. glucose en insuline")
st.caption(
"Statistieken per interval tussen opeenvolgende pompwissels — een benadering "
"(zonder basaalinsuline) van of insulinebehoefte samenhangt met de leeftijd "
"van de pompsite."
)
site_stats = site_interval_stats(glucose, treatments, events["site_changes"])
if site_stats.empty:
st.info("Geen pompwissels gevonden in deze periode.")
else:
display = site_stats.copy()
display["start"] = display["start"].dt.strftime("%d-%m-%Y")
display["end"] = display["end"].dt.strftime("%d-%m-%Y")
st.dataframe(display, width="stretch", hide_index=True)
st.caption(f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')}")