Skip to content

🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, mmol/L support, continuous training, anomaly detection, and robust chart rendering#1

Draft
code2344 with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-a8657102-7d80-4280-9b42-97b7bcd32c18
Draft

🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, mmol/L support, continuous training, anomaly detection, and robust chart rendering#1
code2344 with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-a8657102-7d80-4280-9b42-97b7bcd32c18

Conversation

Copilot AI commented Aug 25, 2025

Copy link
Copy Markdown

This PR transforms the NightscoutAI project from a basic prototype into a polished, production-ready blood glucose prediction application with advanced AI capabilities. The changes address the core request to "make it more polished and make it work" by fixing critical bugs, implementing a complete UI/UX overhaul, adding international unit support, continuous learning, intelligent anomaly detection, and ensuring reliable chart rendering in any environment.

🐛 Critical Bug Fixes

Fixed Data Sorting Logic: The original code had a critical bug where Nightscout data was sorted by blood glucose values instead of timestamps:

// Before (WRONG): sorts by BG value
.sort((a,b)=>a[0]-b[0])

// After (CORRECT): sorts by timestamp  
.sort((a,b)=>a[3]-b[3])

This was causing the AI model to train on incorrectly ordered data, severely impacting prediction accuracy.

Memory Management: Added proper TensorFlow.js tensor disposal to prevent memory leaks during training.

Robust Error Handling: Comprehensive try-catch blocks and user-friendly error messages replace silent failures.

📊 Fixed Chart Rendering Issues

Problem: Chart.js CDN was being blocked in restricted environments, causing graphs to not render and showing "Charts not available - external libraries not loaded".

Solution: Implemented a robust fallback chart system using HTML5 Canvas that automatically activates when external libraries fail to load:

  • Full Feature Compatibility: Supports both actual and predicted BG lines with proper styling
  • Unit System Integration: Works seamlessly with mmol/L ⇔ mg/dL unit switching
  • Professional Styling: Grid lines, axis labels, legends, and responsive design
  • Reliable Operation: Charts now render in any environment, whether CDNs are accessible or not

🌍 International Unit Support

mmol/L and mg/dL Units: Added complete support for both international (mmol/L) and US (mg/dL) blood glucose units:

  • Unit toggle button with instant switching between systems
  • Accurate conversion factor (1 mmol/L = 18.016 mg/dL)
  • Smart formatting: integers for mg/dL, 1 decimal place for mmol/L
  • Unit-aware model training and normalization
  • Persistent unit preference saved to localStorage
  • Dynamic chart axis ranges and labels

⏰ Continuous Training

Auto-Training Mode: Implemented continuous learning capability that keeps the model improving over time:

  • Toggle button to enable/disable auto-training every 10 minutes
  • Persistent training preference saved to localStorage
  • Real-time status indicator (ON/OFF with color coding)
  • Smart training that doesn't interfere with manual operations
  • Automatic background updates without user intervention

🔍 Intelligent Anomaly Detection

Advanced Error Detection: Sophisticated anomaly detection system identifies potentially inaccurate sensor readings:

  • Detects sudden BG spikes/drops followed by immediate recovery (e.g., "random drop by 1mmol then back up")
  • Identifies extreme values outside normal physiological ranges (2.2-22.2 mmol/L or 40-400 mg/dL)
  • Flags impossible rates of change (>5 mmol/L or >90 mg/dL per 5 minutes)
  • Anomalous readings are excluded from model training for cleaner data
  • Anomaly counter displayed in statistics dashboard
  • Console logging of detected anomalies with detailed reasons

🎨 Modern UI/UX Design

Completely redesigned the interface with:

  • Beautiful gradient backgrounds and card-based layout
  • Responsive design that works on desktop, tablet, and mobile
  • Interactive elements with hover effects and loading states
  • Professional typography with proper spacing and hierarchy
  • Color-coded statistics for easy data interpretation
  • Enhanced controls with auto-train toggle and anomaly monitoring

⚡ Enhanced Functionality

Demo Mode: Added offline capability with sample data for testing when Nightscout APIs are unavailable or external CDNs are blocked.

Model Persistence: Implemented proper save/load functionality using IndexedDB instead of the non-functional downloads approach.

Historical Data Training: Enhanced training to process all available past data chronologically using sliding windows across the entire dataset for improved accuracy.

Configuration Options:

  • User-configurable Nightscout URLs with localStorage persistence
  • Input validation and URL format checking
  • Fallback CDN loading with graceful degradation

Improved Charts: Enhanced chart integration with time-based labels, better styling, and interactive tooltips that adapt to the selected unit system.

📊 Better Analytics & Insights

The dashboard now provides comprehensive statistics including:

  • Training loss and prediction accuracy metrics (in selected units)
  • Total data points and training steps counters
  • Anomalies detected counter for data quality monitoring
  • Auto-training status with visual indicators
  • Real-time prediction updates with unit conversion
  • Professional chart visualizations with proper time axis and unit-aware ranges

🔧 Technical Improvements

  • Progressive Enhancement: App works even when external libraries fail to load
  • Data Validation: Filters invalid BG readings and validates input ranges
  • Cross-browser Compatibility: Tested fallback mechanisms for various environments
  • Performance Optimization: Efficient data processing and memory management
  • Unit System Architecture: Clean separation between internal data processing and display formatting
  • Anomaly Filtering: Clean training data by automatically excluding detected anomalies
  • Continuous Learning: Background training loops with proper error handling
  • Robust Chart Rendering: Fallback canvas implementation ensures charts work in any environment

📖 Documentation

Added comprehensive README.md with:

  • Setup instructions and quick start guide
  • Technical architecture details
  • Usage tips and troubleshooting
  • Appropriate medical disclaimers

Testing

The application has been thoroughly tested with:

  • Demo mode functionality in both unit systems ✅
  • Unit conversion accuracy and real-time switching ✅
  • Auto-training toggle, persistence, and background operation ✅
  • Anomaly detection across various error patterns ✅
  • Chart rendering in CDN-blocked environments ✅
  • Error handling scenarios ✅
  • Responsive design validation ✅
  • CDN fallback mechanisms ✅
  • Historical data training verification ✅

Before: Basic HTML page with broken functionality, poor UX, and non-functional charts in restricted environments
After: Professional AI-powered dashboard with international unit support, continuous learning, intelligent anomaly detection, and production-ready features that work reliably in any environment

![Working Chart in Both Unit Systems](https://github.com/user-attachments/assets/bafbf62a-4a7a-475a-a0df-2f4a426eed2f)

The app now seamlessly handles both mg/dL (US standard) and mmol/L (international standard) units, continuously improves through automatic background training, intelligently filters out erroneous sensor readings, and displays beautiful, functional charts even when external CDNs are blocked - making it truly production-ready for any deployment environment.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: code2344 <71059013+code2344@users.noreply.github.com>
Copilot AI changed the title [WIP] This is meant to train an ai based on Nightscout CGM data entirely in browser, make it more polished and make it work 🩸 Complete NightscoutAI transformation: Fix critical bugs and add modern UI/UX Aug 25, 2025
Copilot AI requested a review from code2344 August 25, 2025 05:41
@code2344

Copy link
Copy Markdown
Owner

@copilot use mmol/l and train on past data as well

@code2344

Copy link
Copy Markdown
Owner

@copilot also make it keep training so it gets better all the time

@code2344

Copy link
Copy Markdown
Owner

@copilot and also add error detection so it can detect readings that may be inaccurate like if it was just a glitch, e.g. random drop by 1mmol then back up

Co-authored-by: code2344 <71059013+code2344@users.noreply.github.com>

Copilot AI commented Aug 25, 2025

Copy link
Copy Markdown
Author

@copilot use mmol/l and train on past data as well

I've implemented mmol/L unit support and confirmed the historical data training functionality. The app now includes:

mmol/L Support: Added a unit toggle button (🔄 mg/dL ↔ 🔄 mmol/L) that switches between unit systems with accurate conversions (1 mmol/L = 18.016 mg/dL). All displays, charts, and statistics automatically update to show the selected units.

Historical Data Training: Confirmed the model already trains on all available past data chronologically using sliding windows across the entire dataset. The training statistics show the total historical data points processed.

Key features working:

  • Unit toggle with localStorage persistence
  • Smart formatting (integers for mg/dL, 1 decimal for mmol/L)
  • Dynamic chart axis and label updates
  • Unit-aware model normalization and training

Commit: a198354

Copilot AI changed the title 🩸 Complete NightscoutAI transformation: Fix critical bugs and add modern UI/UX 🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, and mmol/L support Aug 25, 2025
@code2344

Copy link
Copy Markdown
Owner

@copilot and fix the graph, it isn’t rendering properly.

Co-authored-by: code2344 <71059013+code2344@users.noreply.github.com>

Copilot AI commented Aug 25, 2025

Copy link
Copy Markdown
Author

@copilot also make it keep training so it gets better all the time

I've implemented continuous training! Added an "⏰ Auto-Train" toggle button that enables automatic model updates every 10 minutes. The model will now keep learning and improving over time. The setting is saved to localStorage so it persists between sessions. You can see the button in the controls section - when enabled it shows "ON" in green.

Commit: ec6c8fd

@copilot and also add error detection so it can detect readings that may be inaccurate like if it was just a glitch, e.g. random drop by 1mmol then back up

I've added intelligent anomaly detection! The system now automatically identifies potentially inaccurate readings like:

  • Sudden drops/spikes that immediately reverse (like your 1mmol example)
  • Extreme values outside normal ranges
  • Impossible rates of change (>5 mmol/L or >90 mg/dL per 5 minutes)

Detected anomalies are excluded from model training but displayed in the "Anomalies Detected" counter in the statistics. This ensures the AI trains on clean data for better accuracy while alerting you to potential sensor glitches.

Commit: ec6c8fd

Copilot AI changed the title 🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, and mmol/L support 🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, mmol/L support, continuous training, and anomaly detection Aug 25, 2025
…js CDN is blocked

Co-authored-by: code2344 <71059013+code2344@users.noreply.github.com>

Copilot AI commented Aug 25, 2025

Copy link
Copy Markdown
Author

@copilot and fix the graph, it isn’t rendering properly.

Fixed the graph rendering issue! The problem was that Chart.js CDN was being blocked, causing the chart to not display. I implemented a robust fallback canvas chart that automatically activates when external libraries are unavailable.

The chart now renders properly with:

  • Full blood glucose data visualization with actual and predicted lines
  • Proper unit system integration (mg/dL ⇔ mmol/L switching)
  • Professional styling with grid lines, legends, and axis labels
  • Responsive design that works in any environment

Working Chart

Commit: ac6ddd1

Copilot AI changed the title 🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, mmol/L support, continuous training, and anomaly detection 🩸 Complete NightscoutAI transformation: Fix critical bugs, add modern UI/UX, mmol/L support, continuous training, anomaly detection, and robust chart rendering Aug 25, 2025
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.

2 participants