Skip to main content

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โ€‹

  1. Read the Executive Summary below
  2. Review the Implementation Plan section in the main design doc
  3. Check the Effort Estimates (10-14 hours total)

For Architects / Tech Leadsโ€‹

  1. Start with Architecture Comparison document
  2. Review the Data Flow Diagrams in Quick Reference
  3. Read the Design Goals and Key Considerations in main design

For Developersโ€‹

  1. Start with Quick Reference Guide
  2. Copy the Code Snippets as starting templates
  3. Follow the Implementation Steps (4 phases)
  4. Use the Testing Checklist for validation

For QA / Testersโ€‹

  1. Review the Testing Strategy in main design
  2. Use the Testing Checklist in Quick Reference
  3. 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 DeviceChart component 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 GetDeviceTagData endpoint to DeviceController.cs
  • Add GetDeviceTagInfo endpoint to DeviceController.cs
  • Add request/response DTOs
  • Test endpoints with Postman
  • Verify data format matches design

Phase 2: Backend (FastAPI) - 3-4 hoursโ€‹

  • Add DeviceTagData Pydantic model to main.py
  • Add DeviceTagInfo Pydantic model to main.py
  • Add get_device_tag_graph_data endpoint to main.py
  • Add DEVICE_CONFIG to config.py
  • Test with sample device data
  • Verify predictions are generated correctly

Phase 3: Frontend (React) - 3-4 hoursโ€‹

  • Modify DeviceChart.jsx to accept showPredictions prop
  • Add conditional API fetching logic (FastAPI vs ASP.NET)
  • Add forecast data styling (dashed line, opacity)
  • Update EsriCustomPopup.jsx to include PredictionButton
  • Pass showPredictions state to DeviceChart
  • Add FASTAPI_ENDPOINTS.GetDeviceTagGraphData to 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โ€‹

  1. โœ… Predictions work for monthly consumption data
  2. โœ… Predictions work for quarterly consumption data
  3. โœ… Predictions work for yearly consumption data
  4. โœ… Toggle predictions on/off works correctly
  5. โœ… Chart styling distinguishes historical vs. forecast
  6. โœ… Loading states display correctly
  7. โœ… Error handling works (404, 500, etc.)
  8. โœ… Works with single tag (one axis)
  9. โœ… Works with dual tags (two axes)

Edge Casesโ€‹

  1. โš ๏ธ Device with no data (empty result)
  2. โš ๏ธ Device with only 1-2 data points (below minimum)
  3. โš ๏ธ Device with gaps in data (missing months)
  4. โš ๏ธ Device with anomalous data (spikes, zeros)
  5. โš ๏ธ Very large datasets (> 100 points)
  6. โš ๏ธ Invalid device ID (404 response)
  7. โš ๏ธ Invalid tag ID (404 response)

Performance Testsโ€‹

  1. ๐Ÿš€ Response time < 2 seconds
  2. ๐Ÿš€ Memory usage reasonable (< 50MB)
  3. ๐Ÿš€ Concurrent requests handled correctly
  4. ๐Ÿš€ Large datasets (1000+ points) handled efficiently

๐Ÿ› Troubleshooting Guideโ€‹

Issue: Predictions Not Showingโ€‹

Symptoms: Button toggles but chart doesn't update

Debug Steps:

  1. Check browser console for errors
  2. Verify showPredictions prop is being passed
  3. Check FastAPI endpoint is responding (Network tab)
  4. Verify isForecast flag is present in response
  5. Check chart data transformation logic

Issue: 404 from ASP.NET APIโ€‹

Symptoms: Error message "Device not found"

Debug Steps:

  1. Verify device exists in database
  2. Verify tag exists and belongs to device
  3. Check DeviceTagId foreign key relationship
  4. 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:

  1. Check data volume (how many points?)
  2. Add date range filters to limit data
  3. Check database indexes on UnixTime
  4. Consider caching predictions
  5. Profile FastAPI with timing logs

๐Ÿ“š Additional Resourcesโ€‹

  • 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โ€‹


๐Ÿ”„ Revision Historyโ€‹

DateVersionAuthorChanges
2025-10-231.0AI AssistantInitial design documentation suite created

๐Ÿ“ง Contact & Supportโ€‹

For questions or clarifications about this design:

  1. Review the three design documents in order
  2. Check the troubleshooting guide
  3. Reference existing code examples
  4. Consult with the development team

โœ… Next Stepsโ€‹

  1. Review all three design documents
  2. Discuss with team (architects, developers, QA)
  3. Estimate effort and schedule implementation
  4. Implement following the 4-phase plan
  5. Test thoroughly using provided checklists
  6. Deploy to staging environment first
  7. Monitor performance and user feedback
  8. 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โ€‹

DocumentPurposeLengthAudience
Main DesignComplete specification13,000+ wordsArchitects, PMs
Quick ReferenceImplementation guide7,000+ wordsDevelopers, QA
ComparisonArchitecture analysis10,000+ wordsArchitects, Tech Leads
This READMEOverview & navigation2,500+ wordsEveryone

Total Documentation: ~32,500 words / ~65 pages


Last Updated: October 23, 2025