How kaviyathamizhan/energyopt works
Purpose and entry points
EnergyOpt is a full-stack energy-analytics system: it forecasts demand, flags anomalous telemetry, and generates a cost-minimizing 48-hour load schedule. The backend starts at api/main.py (uvicorn api.main:app); the SPA starts at frontend/src/main.jsx and renders the dashboard through frontend/src/App.jsx.
API application and lifecycle
FastAPI owns model loading, CORS, startup buffer warm-up, health reporting, and route registration. Runtime state is initialized from persisted artifacts and recent telemetry rather than a database.
• api/main.py
• api/state.py
• api/schemas.py
The public boundary is JSON REST/OpenAPI. Route separation permits forecast, anomaly, and optimization calls independently, while the unified prediction path orchestrates all stages.
• api/routers/predict.py
• api/routers/anomaly.py
• api/routers/optimize.py
Stateful feature boundary
The only live operational state is an in-memory sliding history buffer. It is seeded from historical CSV data, updated by incoming readings, and derives lag, rolling-window, and temporal features for inference. This makes predictions dependent on process-local history; it is not durable or multi-tenant.
• api/core/buffer.py
• data/processed/recent_history.csv
• api/services/pipeline.py
Forecasting pipeline
The pipeline normalizes engineered features and applies a primary tree forecast model plus lower/upper quantile models to create point demand and a confidence interval. Model/scaler files define a strict artifact compatibility boundary and must align with feature-column metadata.
• api/services/pipeline.py
• artifacts/models/forecast_winner.pkl
• artifacts/models/xgb_q05.pkl
• artifacts/models/xgb_q95.pkl
• artifacts/scalers/main_scaler.pkl
Anomaly guard
Anomaly assessment combines rolling statistical deviation with an Isolation Forest over selected features. It returns anomaly flags/severity alongside forecast results, allowing downstream optimization and UI decisions to expose potentially unreliable telemetry.
• api/routers/anomaly.py
• artifacts/models/isolation_forest.pkl
• artifacts/scalers/anomaly_feature_columns.json
• artifacts/DRIFT_AND_STATE_RULES.md
Optimization stage
Projected demand is passed to a linear-programming scheduler that applies time-of-use pricing and load-shifting constraints, producing an optimized profile, shifted load, and savings. The API-layer optimizer adapts the solver implementation for route/orchestrator use.
• api/core/optimizer.py
• src/optimization/lp_scheduler.py
• api/routers/optimize.py
SciPy/HiGHS is the material optimization runtime; its output depends on the forecast horizon and tariff assumptions rather than external utility services.
Frontend presentation flow
The React/Vite client submits telemetry through a configurable Axios API client, then distributes the unified response across dashboard panels for forecast bands, anomaly state, optimization, and decision summaries.
• frontend/src/services/api.js
• frontend/src/pages/Dashboard.jsx
• frontend/src/components/ForecastChart.jsx
• frontend/src/components/OptimizationPanel.jsx
• frontend/src/components/AnomalyIndicator.jsx
The frontend is deployable on Vercel and the Python API on Render; VITE_API_URL is the cross-deployment boundary. No database, authentication provider, MQTT/Kafka broker, or external telemetry service is present.
Main entry points: main.py FastAPI Application · Dashboard.jsx Dashboard · pipeline.py Forecast Pipeline · predict.py Prediction Routes
Components 22
React Dashboard
- SPA Entry
frontend/src/main.jsx - Dashboard App
frontend/src/App.jsx - Dashboard · Distributes unified results to decision panels.
frontend/src/pages/Dashboard.jsx - API Client · Uses VITE_API_URL as the cross-deployment boundary.
frontend/src/services/api.js - Forecast Chart
frontend/src/components/ForecastChart.jsx - Optimization Panel
frontend/src/components/OptimizationPanel.jsx - Anomaly Indicator
frontend/src/components/AnomalyIndicator.jsx
FastAPI Service
- FastAPI Application · Loads artifacts, configures CORS, warms state, and registers routes.
api/main.py - Request & Response Schemas
api/schemas.py - Prediction Routes · Unified prediction orchestrates forecast, anomaly, and scheduling.
api/routers/predict.py - Anomaly Routes
api/routers/anomaly.py - Optimization Routes
api/routers/optimize.py - Forecast Pipeline · Normalizes features and produces point demand plus confidence bounds.
api/services/pipeline.py
Operational State
- Runtime State
api/state.py - Sliding History Buffer · Process-local, non-durable history deriving lag, rolling, and temporal features.
api/core/buffer.py - Recent History Seed
data/processed/recent_history.csv
Model Artifacts
- Forecast & Quantile Models · Artifact compatibility must match feature-column metadata.
artifacts/models/forecast_winner.pkl - Feature Scaler
artifacts/scalers/main_scaler.pkl - Isolation Forest
artifacts/models/isolation_forest.pkl - Anomaly Feature Rules · Defines selected anomaly features and drift/state rules.
artifacts/scalers/anomaly_feature_columns.json
Optimization Runtime
- API Optimizer Adapter
api/core/optimizer.py - LP Scheduler · Applies tariffs and load-shifting constraints over the forecast horizon.
src/optimization/lp_scheduler.py
Connections 26
- SPA Entry → Dashboard App: renders
- Dashboard App → Dashboard: hosts
- Dashboard → API Client: submits telemetry
- API Client → FastAPI Application: JSON REST / OpenAPI
- FastAPI Application → Request & Response Schemas: validates
- FastAPI Application → Prediction Routes: registers
- FastAPI Application → Anomaly Routes: registers
- FastAPI Application → Optimization Routes: registers
- FastAPI Application → Runtime State: initializes
- Runtime State → Sliding History Buffer: owns
- Recent History Seed → Sliding History Buffer: seeds
- Prediction Routes → Forecast Pipeline: orchestrates
- Prediction Routes → Anomaly Routes: assesses telemetry
- Prediction Routes → API Optimizer Adapter: schedules demand
- Sliding History Buffer → Forecast Pipeline: engineered features
- Forecast Pipeline → Sliding History Buffer: updates readings
- Forecast & Quantile Models → Forecast Pipeline: point and quantile inference
- Feature Scaler → Forecast Pipeline: normalizes features
- Anomaly Routes → Isolation Forest: scores features
- Anomaly Feature Rules → Anomaly Routes: selected features and rules
- API Optimizer Adapter → LP Scheduler: adapts solver
- Forecast Pipeline → API Optimizer Adapter: 48-hour projected demand
- Prediction Routes → Dashboard: unified response
- Dashboard → Forecast Chart: forecast bands
- Dashboard → Optimization Panel: profile and savings
- Dashboard → Anomaly Indicator: flag and severity