# dmins06/petrotwin- architecture

> Architecture diagram of the GitHub repository dmins06/petrotwin-, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dmins06/petrotwin-
- Repository: https://github.com/dmins06/petrotwin-

## Overview

PetroTwin is a Vietnamese fuel-market monitoring and next-adjustment forecasting platform. Its user workflow runs from market observations through a versioned forecast service to a dashboard showing prices, scenarios, and explanations; published actuals later support reconciliation. Separate automated collection and monitoring branches are documented capabilities. Several links are supported by the README or source-index references; unsampled implementation details are represented at directory level rather than inferred as specific calls.

Main entry points: [`backend/app/service.py`](https://github.com/dmins06/petrotwin-/blob/main/backend/app/service.py) (Forecast service), [`frontend/src/components/PetroTwinWorkspace.tsx`](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/PetroTwinWorkspace.tsx) (Dashboard workspace), [`src/petrotwin/models/packaging/inference.py`](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/packaging/inference.py) (Forecast inference), [`backend/app/main.py`](https://github.com/dmins06/petrotwin-/blob/main/backend/app/main.py) (Forecast API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_collection["Data collection"]
  node_detector["Change detection<br/>[change_detector.py]"]
  node_collectors["Market collectors"]
  node_data_pipeline["Validation and publication<br/>[pipeline.py]"]
end

subgraph group_forecast["Forecasting"]
  node_feature_pipeline["Feature engineering<br/>[pipeline.py]"]
  node_model_package["Verified model package<br/>[loader.py]"]
  node_inference["Forecast inference<br/>[inference.py]"]
  node_scenario_engine["Market scenarios<br/>[scenario_engine.py]"]
  node_explanations["Model explanations"]
end

subgraph group_service["API and persistence"]
  node_market_store[("TiDB market data<br/>[tidb.py]")]
  node_api["Forecast API<br/>[main.py]"]
  node_service_logic["Forecast service<br/>[service.py]"]
end

subgraph group_dashboard["Dashboard"]
  node_web_api["Frontend API client<br/>[api.ts]"]
  node_workspace["Dashboard workspace"]
  node_charts["Market charts<br/>[MarketCharts.tsx]"]
  node_price_chart["Forecast chart<br/>[PriceChart.tsx]"]
  node_explanation_panel["XAI panel<br/>[XaiPanel.tsx]"]
  node_monitoring_views["Monitoring views"]
end

subgraph group_monitoring["Monitoring and evaluation"]
  node_forecast_store[("Forecast log<br/>[forecast_log.py]")]
  node_reconciler["Actual reconciliation<br/>[reconciler.py]"]
  node_mlops["Forecast monitoring<br/>[mlops.py]"]
  node_evaluation["Model evaluation<br/>[model_benchmark.py]"]
end

node_sources(("Market sources"))
node_api_actor(("Dashboard user"))

node_sources -->|"fingerprints changes"| node_detector
node_sources -->|"provides observations"| node_collectors
node_detector -->|"dispatches updates"| node_collectors
node_collectors -->|"normalizes data"| node_data_pipeline
node_data_pipeline -->|"publishes observations"| node_market_store
node_market_store -->|"reads market data"| node_service_logic
node_service_logic -.->|"assembles forecast inputs"| node_feature_pipeline
node_service_logic -->|"loads active package"| node_model_package
node_model_package -->|"provides verified models"| node_inference
node_service_logic -->|"requests predictions"| node_inference
node_inference -->|"runs scenarios"| node_scenario_engine
node_inference -->|"produces explanations"| node_explanations
node_api_actor -->|"requests forecasts"| node_api
node_api -->|"routes requests"| node_service_logic
node_service_logic -->|"logs forecasts"| node_forecast_store
node_api -->|"serves API data"| node_web_api
node_web_api -->|"fetches dashboard data"| node_workspace
node_workspace -->|"renders market views"| node_charts
node_workspace -->|"renders forecast series"| node_price_chart
node_workspace -->|"renders explanations"| node_explanation_panel
node_workspace -->|"renders monitoring"| node_monitoring_views
node_forecast_store -->|"supplies pending forecasts"| node_reconciler
node_reconciler -.->|"matches actual prices"| node_market_store
node_reconciler -->|"updates monitoring"| node_mlops
node_evaluation -.->|"benchmarks models"| node_feature_pipeline

click node_detector "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/collectors/change_detector.py"
click node_collectors "https://github.com/dmins06/petrotwin-/tree/main/src/petrotwin/collectors"
click node_data_pipeline "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/data/pipeline.py"
click node_market_store "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/db/tidb.py"
click node_feature_pipeline "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/features/pipeline.py"
click node_model_package "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/packaging/loader.py"
click node_inference "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/packaging/inference.py"
click node_scenario_engine "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/scenarios/scenario_engine.py"
click node_explanations "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/explainability/explanation_layer.py"
click node_api "https://github.com/dmins06/petrotwin-/blob/main/backend/app/main.py"
click node_service_logic "https://github.com/dmins06/petrotwin-/blob/main/backend/app/service.py"
click node_forecast_store "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/forecast_log.py"
click node_web_api "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/lib/api.ts"
click node_workspace "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/PetroTwinWorkspace.tsx"
click node_charts "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/MarketCharts.tsx"
click node_price_chart "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/PriceChart.tsx"
click node_explanation_panel "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/XaiPanel.tsx"
click node_monitoring_views "https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/MonitoringTables.tsx"
click node_reconciler "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/reconciler.py"
click node_mlops "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/mlops.py"
click node_evaluation "https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/evaluation/model_benchmark.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_detector,node_collectors,node_data_pipeline,node_api_actor toneBlue
class node_feature_pipeline,node_model_package,node_inference,node_scenario_engine,node_explanations toneAmber
class node_market_store,node_api,node_service_logic toneMint
class node_web_api,node_workspace,node_charts,node_price_chart,node_explanation_panel,node_monitoring_views toneRose
class node_forecast_store,node_reconciler,node_mlops,node_evaluation,node_sources toneIndigo
```

## Components

### Data collection

- [Change detection](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/collectors/change_detector.py): `src/petrotwin/collectors/change_detector.py`
- [Market collectors](https://github.com/dmins06/petrotwin-/tree/main/src/petrotwin/collectors): `src/petrotwin/collectors`
- [Validation and publication](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/data/pipeline.py): `src/petrotwin/data/pipeline.py`

### Forecasting

- [Feature engineering](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/features/pipeline.py): `src/petrotwin/features/pipeline.py`
- [Verified model package](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/packaging/loader.py): `src/petrotwin/models/packaging/loader.py`
- [Forecast inference](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/packaging/inference.py): `src/petrotwin/models/packaging/inference.py`
- [Market scenarios](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/scenarios/scenario_engine.py): `src/petrotwin/scenarios/scenario_engine.py`
- [Model explanations](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/models/explainability/explanation_layer.py): `src/petrotwin/models/explainability/explanation_layer.py`

### API and persistence

- [TiDB market data](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/db/tidb.py): `src/petrotwin/db/tidb.py`
- [Forecast API](https://github.com/dmins06/petrotwin-/blob/main/backend/app/main.py): `backend/app/main.py`
- [Forecast service](https://github.com/dmins06/petrotwin-/blob/main/backend/app/service.py): `backend/app/service.py`

### Dashboard

- [Frontend API client](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/lib/api.ts): `frontend/src/lib/api.ts`
- [Dashboard workspace](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/PetroTwinWorkspace.tsx): `frontend/src/components/PetroTwinWorkspace.tsx`
- [Market charts](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/MarketCharts.tsx): `frontend/src/components/MarketCharts.tsx`
- [Forecast chart](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/PriceChart.tsx): `frontend/src/components/PriceChart.tsx`
- [XAI panel](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/XaiPanel.tsx): `frontend/src/components/XaiPanel.tsx`
- [Monitoring views](https://github.com/dmins06/petrotwin-/blob/main/frontend/src/components/MonitoringTables.tsx): `frontend/src/components/MonitoringTables.tsx`

### Monitoring and evaluation

- [Forecast log](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/forecast_log.py): `src/petrotwin/monitoring/forecast_log.py`
- [Actual reconciliation](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/reconciler.py): `src/petrotwin/monitoring/reconciler.py`
- [Forecast monitoring](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/monitoring/mlops.py): `src/petrotwin/monitoring/mlops.py`
- [Model evaluation](https://github.com/dmins06/petrotwin-/blob/main/src/petrotwin/evaluation/model_benchmark.py): `src/petrotwin/evaluation/model_benchmark.py`

### Other components

- **Market sources**
- **Dashboard user**

## Connections

- Market sources → Change detection: fingerprints changes (evidence: `README.md`)
- Market sources → Market collectors: provides observations (evidence: `README.md`)
- Change detection → Market collectors: dispatches updates (evidence: `README.md`)
- Market collectors → Validation and publication: normalizes data (evidence: `README.md`)
- Validation and publication → TiDB market data: publishes observations (evidence: `README.md`)
- TiDB market data → Forecast service: reads market data (evidence: `backend/app/service.py`)
- Forecast service → Feature engineering: assembles forecast inputs
- Forecast service → Verified model package: loads active package (evidence: `backend/app/service.py`)
- Verified model package → Forecast inference: provides verified models (evidence: `src/petrotwin/models/packaging/inference.py`)
- Forecast service → Forecast inference: requests predictions (evidence: `backend/app/service.py`)
- Forecast inference → Market scenarios: runs scenarios (evidence: `src/petrotwin/models/packaging/inference.py`)
- Forecast inference → Model explanations: produces explanations (evidence: `src/petrotwin/models/packaging/inference.py`)
- Dashboard user → Forecast API: requests forecasts (evidence: `README.md`)
- Forecast API → Forecast service: routes requests (evidence: `backend/app/main.py`)
- Forecast service → Forecast log: logs forecasts (evidence: `backend/app/service.py`)
- Forecast API → Frontend API client: serves API data (evidence: `README.md`)
- Frontend API client → Dashboard workspace: fetches dashboard data (evidence: `frontend/src/components/PetroTwinWorkspace.tsx`)
- Dashboard workspace → Market charts: renders market views (evidence: `frontend/src/components/PetroTwinWorkspace.tsx`)
- Dashboard workspace → Forecast chart: renders forecast series (evidence: `frontend/src/components/PetroTwinWorkspace.tsx`)
- Dashboard workspace → XAI panel: renders explanations (evidence: `frontend/src/components/PetroTwinWorkspace.tsx`)
- Dashboard workspace → Monitoring views: renders monitoring (evidence: `frontend/src/components/PetroTwinWorkspace.tsx`)
- Forecast log → Actual reconciliation: supplies pending forecasts (evidence: `src/petrotwin/monitoring/reconciler.py`)
- Actual reconciliation → TiDB market data: matches actual prices
- Actual reconciliation → Forecast monitoring: updates monitoring (evidence: `src/petrotwin/monitoring/reconciler.py`)
- Model evaluation → Feature engineering: benchmarks models
