Skip to content

Conversation

@codebymini
Copy link
Collaborator

Add Advanced Statistics View

Summary

This PR introduces a comprehensive statistics view that provides detailed glucose and treatment analytics across multiple time periods (1, 14, and 30 days).

Features Added

📊 Core Statistics

  • Aggregated Stats View: Central dashboard displaying key metrics
    • GMI (Glucose Management Indicator) with toggle to eHbA1c
    • Average glucose
    • Standard deviation with toggle to CV (Coefficient of Variation)
    • Total daily dose (insulin)
    • Average bolus and carbs
    • Basal comparison (programmed vs. actual delivered)

📈 Ambulatory Glucose Profile (AGP)

  • Visualizes glucose percentiles (5th, 25th, 50th/median, 75th, 95th) across a 24-hour period
  • Helps identify patterns and glucose variability throughout the day
  • Interactive graph with hour-by-hour breakdown

⏱️ Time in Range (TIR)

  • Period-based analysis (Night, Morning, Day, Evening, Average)
  • Categorizes glucose levels:
    • Very Low (<54 mg/dL)
    • Low (54-70 mg/dL)
    • In Range (70-180 mg/dL or 70-140 mg/dL in tight mode)
    • High (180-250 mg/dL)
    • Very High (>250 mg/dL)
  • Toggle between standard TIR and TITR (Time in Tight Range)
  • Stacked bar chart visualization

🎯 Glucose Risk Index (GRI)

  • Calculates and visualizes glucose variability risk
  • Risk grid showing classification zones
  • Comprehensive risk assessment metrics

Technical Implementation

New Components

  • AggregatedStatsView.swift - Main statistics container
  • AGPCalculator.swift, AGPView.swift, AGPViewModel.swift - AGP implementation
  • TIRCalculator.swift, TIRView.swift, TIRViewModel.swift - TIR implementation
  • GRICalculator.swift, GRIView.swift, GRIViewModel.swift - GRI implementation
  • SimpleStatsViewModel.swift - Basic stats calculations
  • StatsDataService.swift - Data management service
  • StatsDataFetcher.swift - Historical data fetching (up to 30 days)
  • ChartContainerView.swift - Chart wrapper utilities

Data Management

  • Added separate stats data storage arrays in MainViewController:
    • statsBGData - Blood glucose readings
    • statsBolusData - Bolus entries
    • statsSMBData - SMB entries
    • statsCarbData - Carb entries
    • statsBasalData - Basal rate data
  • Supports up to 30 days of historical data
  • Automatic data fetching with progress tracking

User Preferences

Added new storage preferences:

  • showGMI - Toggle between GMI and eHbA1c display
  • showStdDev - Toggle between Standard Deviation and CV
  • showTITR - Toggle between TIR (180 mg/dL) and TITR (140 mg/dL)

UI Integration

  • Added "Statistics" menu item to More Menu
  • Accessible from Settings menu
  • Full-screen modal presentation with navigation
  • Supports dark mode
  • Period selector (1 day, 14 days, 30 days)
  • Pull-to-refresh functionality

Bug Fixes

  • Fixed string formatting in MainViewController+updateStats.swift (removed duplicate % symbols in format strings)

Changes to Existing Files

  • SettingsMenuView.swift - Added wrapper for accessing statistics
  • MoreMenuViewController.swift - Added statistics menu entry
  • MainViewController.swift - Added stats data storage arrays
  • Storage.swift - Added statistics display preferences
  • project.pbxproj - Added Stats folder and file references

@codebymini codebymini linked an issue Nov 22, 2025 that may be closed by this pull request
@marionbarker marionbarker requested a review from bjorkert January 30, 2026 14:31
@marionbarker
Copy link
Collaborator

marionbarker commented Jan 30, 2026

Test

All tests are using a Loop URL for Nightscout.

Tested on a test phone viewing my personal URL (so plenty of data) using WiFi only
Tested on my personal phone - access to WiFi or cell
Note that my WiFi was acting up - so saw some issues that might not be seen with a good connection.

Summary:

  • rethink how one accesses the new features
  • add a message if access to the URL is slow or non-existent
  • the total daily dose is missing some data, my theory is perhaps the scheduled basal amount is not included

See notes below.

Notes

Initially

  • my statistics page has no data (and I'm reading from my actual NS site - but might be a internet issue)
  • my treatments page is fully populated - this is very nice

Organization

  • do we really want to add 2 additional items for the More menu (. . .)
  • would it be better to add Statistics and Treatments as rows in a new section on the Settings screen?
  • I set up the Tab Settings so all the optional choices (Alarms, Remote, Nightscout) are selected as More Menu and took a screenshot
  • Later - when testing my personal phone (which has Nightscout hidden so there is no More Menu), I had to modify Tab Settings to enable the More menu to get access to Treatments and Statistics
IMG_8712

Details and Screenshots

My internet is offline (tap on Treatments and it just spins for a long time, then says "no recent treatments"

  • there should be a warning that it can't connect to NS server (I saw an error message in Xcode debug log)
  • rebooted the router
  • now treatment loads (screenshot - left of composite below)
  • statistics screen initially is 14 day and shows a blank (screenshot - right of composite below)
  • statistics 1 day loaded after a long time
  • statistics 14 days loaded after an extremely long time
  • some of the statistics on the 14 day do not match what Nightscout shows on its report
    • avg total daily dose 26.43 (LF), 33.2 (NS)
    • avg daily bolus close, but avg daily basal rate is very much too low (is it ignoring scheduled basal and only adding up temp basal values?)
pr487_composite_treatment_blank_stats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Statistics view

4 participants