How akshat-rai0/ntpc-nvvn-mp works
1. This repository is a Django-based forecasting system centered on a state-specific prediction pipeline. The runtime is split into three main Django apps: states for domain data, predictions for ingestion/inference/API, and dashboard for the human-facing UI. The project package demand_predictor/ wires these apps together through Django settings, URL routing, and WSGI/ASGI entrypoints.
2. The core business object is “state.” Each state is configured in config/states/*.yaml with a short code, MERIT India demand URL, model path, timezone, fallback demand, and a set of cities with weather weights. That YAML drives registration in the database and determines which external source, model file, and weather locations are used for that state.
3. states/ defines the persistent domain layer. It holds the Django models and admin registration for state metadata and time-series records such as live demand readings and prediction records. This is the main boundary between the business data model and everything else in the system.
4. predictions/services/ is the engine room. merit_client.py fetches live demand from MERIT India, weather.py in utils/ fetches weather from Open-Meteo, time_features.py and lag_store.py derive model inputs, predictor.py runs single-step inference, and forecaster.py handles day-ahead/multi-slot forecasts. registry.py loads YAML state definitions, while csv_sync.py mirrors database changes into CSV files.
5. The model boundary is explicit and file-based. Each state has a trained LightGBM model stored under models/{state_code}/lgbm_final.txt or, for a few states, a pickle artifact. Inference expects a fixed feature vector assembled from weather, calendar, and lagged demand inputs; the system is therefore tightly coupled to these model assets and their feature order.
6. Data flows from external services into the pipeline, then into storage and visualization. On each refresh, the app pulls live demand and weather, computes features, performs prediction, saves both actual and predicted values to SQLite, and syncs them into CSV logs under data/states/{code}/. Historical CSVs in data/ are also used to backfill lag features and bootstrap new states.
7. The scheduling boundary is handled inside predictions/scheduler.py and app startup logic. A background job refreshes active states every five minutes when enabled, so the system can operate continuously without manual intervention. The same refresh workflow is also exposed as a management command for manual execution and operational control.
8. The public API sits in predictions/api/views.py and predictions/urls.py. It exposes state listing plus per-state “today,” “tomorrow,” “forecast,” and “history” endpoints, returning JSON for consumption by the dashboard or external systems. This API reads from the same persistence layer used by the scheduler, so it is not a separate data source.
9. The dashboard in dashboard/ is a presentation layer over the API/database. templates/dashboard/index.html and the static Chart.js frontend render actual vs predicted demand, forecast views, and summary metrics. It depends on the prediction data being current, but it does not own the forecasting logic itself.
10. Persistence is dual-layered. SQLite (db.sqlite3) is the primary system-of-record for Django models, while CSV exports provide portability, backup, and offline analysis. The CSV layer is not authoritative; it is a synchronized projection of database state.
11. Operational tooling is first-class. Management commands under predictions/management/commands/ support seeding state definitions, refreshing demand, and importing historical demand. The standalone prediction.py script is a lightweight CLI entrypoint for running a single-state prediction without the full server stack.
12. The repository also contains a substantial data/science workspace under data/ with notebooks and intermediate CSVs. This area is separate from the application runtime: it supports model development, feature engineering, and data preparation, while the deployed app consumes only the curated model files, YAML configs, and runtime logs.
13. In architectural terms, the main dependency direction is: external services and historical data → feature engineering and model inference → SQLite/CSV persistence → API and dashboard. The key boundaries are between state configuration, prediction services, persistence, and presentation, with Django acting as the integration framework across all of them.
Main entry points: predictions predictions app · db.sqlite3 SQLite DB · predictor.py Predictor · demand_predictor demand_predictor
Components 22
Django project
Framework wiring and entrypoints
- demand_predictor · Settings, URL routing, ASGI/WSGI integration
demand_predictor
Domain data
State metadata and persisted demand records
- states app · State configuration and persistent demand/prediction data
states - State models · State metadata and time-series records
states/models.py - State admin · Admin registration for domain objects
states/admin.py - SQLite DB · System-of-record for persisted Django state and prediction records
db.sqlite3
Prediction engine
State loading, feature building, inference, forecasting, and sync
- predictions app · Ingestion, inference, scheduling, API, and commands
predictions - Registry · Loads YAML state definitions and runtime metadata
predictions/services/registry.py - MERIT client · Fetches live demand from MERIT India
predictions/services/merit_client.py - Weather fetch · Pulls weather inputs from Open-Meteo
utils/weather.py - Features · Calendar, lag, and time features for inference
utils/time_features.py - Lag store · Backfills and persists lagged demand inputs
utils/lag_store.py - Predictor · Runs model inference for one state and slot
predictions/services/predictor.py - Forecaster · Builds day-ahead and multi-step forecasts
predictions/services/forecaster.py - CSV sync · Mirrors database changes into state CSV logs
predictions/services/csv_sync.py
Presentation
API and dashboard surfaces
Runtime assets
Config, models, and historical data used by the app
- State YAML · Per-state sources, model paths, timezone, fallback demand, and city weights
config/states - Model files · State-specific LightGBM and some pickle artifacts
models - Historical data · CSV logs and notebooks for training, backfill, and analysis
data
Operations
Scheduling and manual execution entrypoints
- Scheduler · Periodic refresh of active states
predictions/scheduler.py - Commands · Seed, refresh, and import operational commands
predictions/management/commands - prediction.py · Standalone single-state prediction runner
prediction.py
Connections 26
- demand_predictor → states app: registers · Project settings and routing include the states app
- demand_predictor → predictions app: registers · Project settings and routing include the predictions app
- demand_predictor → Dashboard: serves · Project URLs expose the dashboard presentation layer
- states app → State models: defines · Persistent state and time-series data model
- states app → State admin: publishes · Registers domain objects in Django admin
- State models → SQLite DB: persists · State metadata and readings are stored in SQLite
- predictions app → Registry: loads · Bootstraps state definitions from YAML configuration
- Registry → State YAML: reads · Parses per-state configuration files
- predictions app → MERIT client: fetches · Pulls live demand from MERIT India
- predictions app → Weather fetch: fetches · Retrieves weather features for configured cities
- predictions app → Features: builds · Derives feature vectors for model input
- Features → Lag store: uses · Consumes lagged demand history for feature creation
- Lag store → Historical data: bootstraps · Uses historical CSVs as backfill source
- Predictor → Model files: loads · Loads the per-state trained artifact
- Predictor → Features: consumes · Uses engineered feature vectors for inference
- Forecaster → Predictor: orchestrates · Builds repeated predictions across future slots
- predictions app → SQLite DB: writes · Stores actuals and predictions in the primary database
- CSV sync → SQLite DB: reads · Projects database state into CSV logs
- CSV sync → Historical data: syncs · Maintains per-state CSV outputs under data/states
- Scheduler → predictions app: triggers · Background refresh workflow runs the service pipeline
- Commands → predictions app: invokes · Management commands reuse the same refresh and import flows
- prediction.py → Predictor: runs · Standalone CLI invokes single-state prediction
- API views → SQLite DB: reads · API reads persisted demand and forecast records
- API views → predictions app: exposes · JSON endpoints surface prediction data to consumers
- Dashboard → API views: consumes · Frontend renders charts from API data
- Dashboard → SQLite DB: reads · Dashboard can also read persisted summaries directly