Device AI Predictions - Documentation Suite
This documentation suite provides a comprehensive design for adding AI prediction support for Device (water meter) time-series data to the Hydrotrek DWD system.
๐ Documentation Overviewโ
1. Main Design Documentโ
File: device_ai_predictions_design.md (13,000+ words)
The comprehensive design specification covering:
- Current state analysis
- Device data model and relationships
- Complete architecture design
- Backend implementation (FastAPI + ASP.NET)
- Frontend implementation (React)
- API contracts and data formats
- Implementation plan with effort estimates
- Testing strategy and considerations
- Future enhancements
Best for: Understanding the full design, architecture decisions, and detailed specifications.
2. Quick Reference Guideโ
File: device_ai_predictions_quick_reference.md (7,000+ words)
A condensed implementation guide with:
- Key changes summary
- Step-by-step implementation instructions
- Code snippets and templates
- Testing checklist
- Troubleshooting guide
- API examples
- Data flow diagrams
Best for: Developers ready to implement, looking for code examples and step-by-step guidance.
3. Architecture Comparisonโ
File: device_ai_predictions_comparison.md (10,000+ words)
Side-by-side comparison of existing vs. proposed:
- High-level architecture diagrams
- Data model comparisons
- API endpoint comparisons
- Frontend component comparisons
- Prediction pipeline analysis
- Configuration differences
- Performance characteristics
Best for: Understanding how Device predictions fit into the existing system and what's reusable.
๐ฏ Quick Startโ
For Product Owners / Managersโ
- Read the Executive Summary below
- Review the Implementation Plan section in the main design doc
- Check the Effort Estimates (10-14 hours total)
For Architects / Tech Leadsโ
- Start with Architecture Comparison document
- Review the Data Flow Diagrams in Quick Reference
- Read the Design Goals and Key Considerations in main design
For Developersโ
- Start with Quick Reference Guide
- Copy the Code Snippets as starting templates
- Follow the Implementation Steps (4 phases)
- Use the Testing Checklist for validation
For QA / Testersโ
- Review the Testing Strategy in main design
- Use the Testing Checklist in Quick Reference
- Reference the Troubleshooting Guide for common issues
๐ Executive Summaryโ
What We're Buildingโ
Add AI-powered forecasting for water meter consumption data, reusing the existing TSAI prediction infrastructure built for hydraulic model data.
Why It Mattersโ
- Predictive Insights: Forecast future consumption patterns
- Anomaly Detection: Identify unusual consumption early
- Resource Planning: Better predict water demand
- Customer Service: Proactive billing estimates
How It Worksโ
User โ Clicks "AI Predictions" button on Device chart
โ Frontend fetches from FastAPI prediction service
โ FastAPI gets historical data from ASP.NET API
โ TSAI model generates forecasts
โ Chart displays predictions with visual distinction
What's Reusableโ
- โ 70-80% of existing code can be reused
- โ Prediction algorithms (TSAI + scikit-learn)
- โ UI components (PredictionButton)
- โ UX patterns (toggle predictions on/off)
- โ Configuration system
What's Newโ
- โ ASP.NET endpoint:
/api/Device/{deviceId}/tag/{tagId}/data - โ FastAPI endpoint:
/device/{device_id}/tag/{tag_id}/graph-data - โ Enhanced
DeviceChartcomponent to support predictions - โ Device-specific configuration
Effort Estimateโ
- Backend (ASP.NET): 2-3 hours
- Backend (FastAPI): 3-4 hours
- Frontend (React): 3-4 hours
- Testing & Polish: 2-3 hours
- Total: 10-14 hours
Risk Levelโ
Low - High code reusability, well-understood patterns, limited scope.
๐ Key Metricsโ
Code Reusabilityโ
- Prediction Logic: 100% reusable
- UI Components: 100% reusable (PredictionButton)
- API Patterns: 80% reusable (similar structure)
- Chart Components: 60% reusable (needs enhancement)
- Overall: ~70-80% reusable
Implementation Scopeโ
- New Files: 0 (all enhancements to existing files)
- Modified Files: ~5-7 files
- Lines of Code: ~400-600 new LOC
- API Endpoints: 2 new endpoints
Performance Expectationsโ
- Training Time: < 1 second (small datasets)
- Prediction Time: < 0.5 seconds
- Memory Usage: < 10MB per request
- Cache-able: Yes (data changes monthly/quarterly)
๐๏ธ Architecture at a Glanceโ
Data Flowโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Interface โ
โ โข Clicks "AI Predictions" button on Device chart โ
โ โข DeviceChart component re-renders with predictions=true โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Prediction Service (Python) โ
โ โข GET /device/{id}/tag/{tag_id}/graph-data โ
โ โข Fetches raw data from ASP.NET API โ
โ โข Applies TSAI InceptionTimePlus model โ
โ โข Returns data + predictions with isForecast flag โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ASP.NET API (C#) โ
โ โข GET /api/Device/{deviceId}/tag/{tagId}/data โ
โ โข Queries DeviceTagDatum table โ
โ โข Returns [{ unixTime, value }, ...] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PostgreSQL Database โ
โ โข DeviceTagDatum table โ
โ โข Indexed on DeviceTagId, UnixTime โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Component Integrationโ
EsriCustomPopup (Existing)
โโ PredictionButton (Existing, Reused) โ
โโ DeviceChart (Enhanced) โ ๏ธ
โโ showPredictions prop (New) โ
โโ Fetch from FastAPI if predictions=true (New) โ
โโ Fetch from ASP.NET if predictions=false (Existing) โ
โโ Style forecast data differently (New) โ
๐ Implementation Checklistโ
Phase 1: Backend (ASP.NET) - 2-3 hoursโ
- Add
GetDeviceTagDataendpoint toDeviceController.cs - Add
GetDeviceTagInfoendpoint toDeviceController.cs - Add request/response DTOs
- Test endpoints with Postman
- Verify data format matches design
Phase 2: Backend (FastAPI) - 3-4 hoursโ
- Add
DeviceTagDataPydantic model tomain.py - Add
DeviceTagInfoPydantic model tomain.py - Add
get_device_tag_graph_dataendpoint tomain.py - Add
DEVICE_CONFIGtoconfig.py - Test with sample device data
- Verify predictions are generated correctly
Phase 3: Frontend (React) - 3-4 hoursโ
- Modify
DeviceChart.jsxto acceptshowPredictionsprop - Add conditional API fetching logic (FastAPI vs ASP.NET)
- Add forecast data styling (dashed line, opacity)
- Update
EsriCustomPopup.jsxto include PredictionButton - Pass
showPredictionsstate to DeviceChart - Add
FASTAPI_ENDPOINTS.GetDeviceTagGraphDatato constants - Test in browser with real data
Phase 4: Testing & Polish - 2-3 hoursโ
- End-to-end testing with various device types
- Test with different tag types (monthly, quarterly, yearly)
- Test with sparse data (< 10 points)
- Test error handling and edge cases
- Performance testing with large datasets
- UI/UX refinement
- Update documentation
๐งช Testing Scenariosโ
Functional Testsโ
- โ Predictions work for monthly consumption data
- โ Predictions work for quarterly consumption data
- โ Predictions work for yearly consumption data
- โ Toggle predictions on/off works correctly
- โ Chart styling distinguishes historical vs. forecast
- โ Loading states display correctly
- โ Error handling works (404, 500, etc.)
- โ Works with single tag (one axis)
- โ Works with dual tags (two axes)
Edge Casesโ
- โ ๏ธ Device with no data (empty result)
- โ ๏ธ Device with only 1-2 data points (below minimum)
- โ ๏ธ Device with gaps in data (missing months)
- โ ๏ธ Device with anomalous data (spikes, zeros)
- โ ๏ธ Very large datasets (> 100 points)
- โ ๏ธ Invalid device ID (404 response)
- โ ๏ธ Invalid tag ID (404 response)
Performance Testsโ
- ๐ Response time < 2 seconds
- ๐ Memory usage reasonable (< 50MB)
- ๐ Concurrent requests handled correctly
- ๐ Large datasets (1000+ points) handled efficiently
๐ Troubleshooting Guideโ
Issue: Predictions Not Showingโ
Symptoms: Button toggles but chart doesn't update
Debug Steps:
- Check browser console for errors
- Verify
showPredictionsprop is being passed - Check FastAPI endpoint is responding (Network tab)
- Verify
isForecastflag is present in response - Check chart data transformation logic
Issue: 404 from ASP.NET APIโ
Symptoms: Error message "Device not found"
Debug Steps:
- Verify device exists in database
- Verify tag exists and belongs to device
- Check DeviceTagId foreign key relationship
- Test endpoint directly in Postman
Issue: Poor Prediction Qualityโ
Symptoms: Predictions look unrealistic
Root Causes:
- Insufficient data (< 10 points)
- High data variance (seasonal spikes)
- Anomalous data (errors in readings)
Solutions:
- Increase minimum data points threshold
- Use simpler fallback model (moving average)
- Clean data before prediction
- Adjust forecast percentage (lower = more conservative)
Issue: Slow Performanceโ
Symptoms: Predictions take > 5 seconds
Debug Steps:
- Check data volume (how many points?)
- Add date range filters to limit data
- Check database indexes on UnixTime
- Consider caching predictions
- Profile FastAPI with timing logs
๐ Additional Resourcesโ
Related Documentationโ
- Azure Configuration:
azure_configuration_quick_reference.md - Blob Storage:
blob_storage_phases_1-4_complete.md - Database Schema: See efcorelibraries/EN2PostgresContext
Code Referencesโ
- Existing Predictions:
dwd-api-fastapi/main.py(lines 532-662) - Prediction Button:
dwd-frontend-react/src/components/PredictionButton.jsx - Forecast Chart Example:
dwd-frontend-react/src/components/ForecastChart.jsx - Device Controller:
dwd-api-aspnet/DwdApiAspNet/Controllers/DeviceController.cs - Device Chart:
dwd-frontend-react/src/components/DeviceChart.jsx
External Resourcesโ
- TSAI Documentation: https://timeseriesai.github.io/tsai/
- Scikit-learn: https://scikit-learn.org/stable/modules/linear_model.html
- FastAPI: https://fastapi.tiangolo.com/
- ApexCharts: https://apexcharts.com/ (used by GqcLineChart)
๐ Revision Historyโ
| Date | Version | Author | Changes |
|---|---|---|---|
| 2025-10-23 | 1.0 | AI Assistant | Initial design documentation suite created |
๐ง Contact & Supportโ
For questions or clarifications about this design:
- Review the three design documents in order
- Check the troubleshooting guide
- Reference existing code examples
- Consult with the development team
โ Next Stepsโ
- Review all three design documents
- Discuss with team (architects, developers, QA)
- Estimate effort and schedule implementation
- Implement following the 4-phase plan
- Test thoroughly using provided checklists
- Deploy to staging environment first
- Monitor performance and user feedback
- Iterate based on results
๐ Expected Outcomesโ
Upon successful implementation, users will be able to:
โ View AI predictions for any device's consumption data โ Toggle predictions on/off with a single button click โ See forecast data visually distinguished from historical data โ Get reliable forecasts using industry-standard ML algorithms (TSAI) โ Experience consistent UX with existing prediction features โ Benefit from fast performance (< 2 second response times)
This will provide valuable insights for:
- Water utility planners
- Operations teams
- Customer service representatives
- Data analysts
- Customers (future potential)
๐ Document Indexโ
| Document | Purpose | Length | Audience |
|---|---|---|---|
| Main Design | Complete specification | 13,000+ words | Architects, PMs |
| Quick Reference | Implementation guide | 7,000+ words | Developers, QA |
| Comparison | Architecture analysis | 10,000+ words | Architects, Tech Leads |
| This README | Overview & navigation | 2,500+ words | Everyone |
Total Documentation: ~32,500 words / ~65 pages
Last Updated: October 23, 2025