# saeidshahriari/etrm-data-platform architecture

> Architecture diagram of the GitHub repository saeidshahriari/etrm-data-platform, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/saeidshahriari/etrm-data-platform
- Repository: https://github.com/saeidshahriari/etrm-data-platform

## Overview

#### Purpose and entry points
ETRM Data Platform is a secured batch lakehouse for European electricity prices and synthetic energy trades. It lands raw market data, produces medallion Parquet datasets, calculates risk/P&L, flags suspicious trades, and exposes results through a dashboard and MCP agent. Primary operational entry points are `Makefile`, Airflow DAG `dags/etrm_pipeline_dag.py`, local runner `main.py`, Streamlit `dashboard/app.py`, and MCP server `agent/etrm_mcp_server.py`.

#### Orchestration and runtime boundary
Apache Airflow coordinates the manually triggered `etrm_medallion_pipeline`, including security checkpoints and local-mode Spark execution. DAG task code must defer heavy imports to task execution, separating scheduler parsing from data-processing dependencies.
- `dags/etrm_pipeline_dag.py`
- `Dockerfile`
- `docker-compose.yml`

Runtime is Python 3.12, Java 17, Airflow 3/Celery, and PySpark 4. Spark transformations run local-mode inside an Airflow worker; standalone Spark services remain deployment infrastructure rather than the primary execution backend.

#### Secure ingestion and source boundary
Market prices originate from the external SMARD API; trades are generated synthetically and stored in PostgreSQL. Secure ingestion scans and validates records before landing raw Bronze data. Invalid rows are quarantined; batches exceeding the failure threshold are rejected, preventing poisoned data from advancing.
- `src/ingestion/secure_ingest.py`
- `src/ingestion/fetch_market_data.py`
- `src/ingestion/generate_trades.py`

Secrets/configuration are Vault-backed, with HashiCorp Vault KV v2 used in development mode.
- `src/config.py`
- `src/ingestion/vault_client.py`
- `config/config.yaml`

#### Data-security and quality gate
The live-data boundary combines custom injection/malware-style scanning with schema, domain-range, and statistical poisoning checks. It protects downstream Spark, analytics, and AI surfaces from SQL/script/path/prompt injection and anomalous-but-in-range values.
- `src/security/scanner.py`
- `src/quality/schemas.py`
- `src/quality/gates.py`

This is distinct from repository and infrastructure security scanning in GitHub Actions and on-demand DAST.
- `.github/workflows/security.yml`
- `security/run_dast.sh`

#### Medallion processing and stores
Bronze preserves raw API payloads; Silver creates cleaned, typed market-price and trade datasets; Gold produces business-ready P&L, counterparty exposure, and portfolio summaries. Parquet is the lake format on a shared local mount. PostgreSQL 16 serves application trade storage and separate Airflow metadata storage.
- `src/processing/bronze_to_silver.py`
- `src/processing/silver_to_gold.py`
- `docker-compose.yml`

#### ML surveillance stage
The pipeline derives trade features, trains an unsupervised Isolation Forest, records experiments through MLflow, and writes scored/flagged trade outputs for analyst consumption. Features connect trade behavior to market context, including price deviation, volume, notional, contract duration, and concentration.
- `src/ml/features.py`
- `src/ml/train_anomaly_model.py`
- `src/ml/score_trades.py`

#### Consumption interfaces
The Streamlit dashboard reads current lakehouse/risk and surveillance outputs for live operational views. The MCP agent uses DuckDB to query Parquet in place. Agent queries are deliberately constrained to read-only `SELECT`/`WITH` statements and whitelisted tables, making the agent a controlled analytics boundary rather than a write interface.
- `dashboard/app.py`
- `agent/etrm_mcp_server.py`

Main entry points: [`src/ingestion/secure_ingest.py`](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/secure_ingest.py) (Secure ingestion), [`src/processing/silver_to_gold.py`](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/silver_to_gold.py) (Gold Parquet), [`src/security/scanner.py`](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/security/scanner.py) (Security & quality gate), [`src/processing/bronze_to_silver.py`](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/bronze_to_silver.py) (Silver Parquet)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Orchestration"]
  node_makefile["Makefile<br/>operations entrypoint"]
  node_local_runner["Local runner<br/>pipeline entrypoint<br/>[main.py]"]
  node_airflow_dag{{"Medallion pipeline DAG<br/>Airflow orchestration"}}
  node_airflow_worker["Airflow worker"]
end

subgraph group_ingestion["Secure ingestion"]
  node_vault[("Vault KV v2<br/>secrets store<br/>[vault_client.py]")]
  node_market_api(("SMARD API<br/>external market source"))
  node_trade_generator["Synthetic trade generator<br/>data generator<br/>[generate_trades.py]"]
  node_trade_postgres[("Trade PostgreSQL<br/>application trade store<br/>[docker-compose.yml]")]
  node_secure_ingest["Secure ingestion<br/>landing service<br/>[secure_ingest.py]"]
  node_data_gate{{"Security &amp; quality gate<br/>validation boundary<br/>[scanner.py]"}}
  node_quarantine[("Quarantine<br/>invalid-record store<br/>[secure_ingest.py]")]
end

subgraph group_lakehouse["Lakehouse &amp; analytics"]
  node_bronze[("Bronze Parquet<br/>raw lake dataset")]
  node_silver[("Silver Parquet<br/>cleaned lake dataset")]
  node_gold[("Gold Parquet<br/>business lake dataset<br/>[silver_to_gold.py]")]
end

subgraph group_surveillance["ML surveillance"]
  node_feature_engineering["Trade features<br/>feature pipeline<br/>[features.py]"]
  node_anomaly_training["Isolation Forest training<br/>ML training"]
  node_trade_scoring["Trade scoring<br/>ML inference<br/>[score_trades.py]"]
end

subgraph group_consumption["Consumption"]
  node_dashboard["Operations dashboard<br/>Streamlit UI<br/>[app.py]"]
  node_mcp_agent{{"MCP analytics agent<br/>read-only query interface<br/>[etrm_mcp_server.py]"}}
end

node_makefile -->|"operates"| node_airflow_dag
node_local_runner -->|"runs locally"| node_airflow_dag
node_airflow_dag -->|"dispatches tasks"| node_airflow_worker
node_vault -.->|"supplies secrets"| node_secure_ingest
node_market_api -->|"market prices"| node_secure_ingest
node_trade_generator -->|"stores trades"| node_trade_postgres
node_trade_postgres -->|"trade records"| node_secure_ingest
node_airflow_worker -->|"executes"| node_secure_ingest
node_secure_ingest -->|"scans and validates"| node_data_gate
node_data_gate -->|"accepted records"| node_bronze
node_data_gate -->|"invalid records"| node_quarantine
node_bronze -->|"clean and type"| node_silver
node_silver -->|"derive business metrics"| node_gold
node_silver -->|"trades and prices"| node_feature_engineering
node_feature_engineering -->|"feature set"| node_anomaly_training
node_anomaly_training -->|"trained model"| node_trade_scoring
node_feature_engineering -->|"scoring features"| node_trade_scoring
node_trade_scoring -->|"flagged trade outputs"| node_gold
node_gold -->|"risk and surveillance views"| node_dashboard
node_gold -->|"Parquet analytics"| node_mcp_agent

click node_makefile "https://github.com/saeidshahriari/etrm-data-platform/blob/main/Makefile"
click node_local_runner "https://github.com/saeidshahriari/etrm-data-platform/blob/main/main.py"
click node_airflow_dag "https://github.com/saeidshahriari/etrm-data-platform/blob/main/dags/etrm_pipeline_dag.py"
click node_airflow_worker "https://github.com/saeidshahriari/etrm-data-platform/blob/main/Dockerfile"
click node_vault "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/vault_client.py"
click node_market_api "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/fetch_market_data.py"
click node_trade_generator "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/generate_trades.py"
click node_trade_postgres "https://github.com/saeidshahriari/etrm-data-platform/blob/main/docker-compose.yml"
click node_secure_ingest "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/secure_ingest.py"
click node_data_gate "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/security/scanner.py"
click node_bronze "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/bronze_to_silver.py"
click node_quarantine "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/secure_ingest.py"
click node_silver "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/bronze_to_silver.py"
click node_gold "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/silver_to_gold.py"
click node_feature_engineering "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/features.py"
click node_anomaly_training "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/train_anomaly_model.py"
click node_trade_scoring "https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/score_trades.py"
click node_dashboard "https://github.com/saeidshahriari/etrm-data-platform/blob/main/dashboard/app.py"
click node_mcp_agent "https://github.com/saeidshahriari/etrm-data-platform/blob/main/agent/etrm_mcp_server.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_makefile,node_local_runner,node_airflow_dag,node_airflow_worker toneBlue
class node_vault,node_market_api,node_trade_generator,node_trade_postgres,node_secure_ingest,node_data_gate,node_quarantine toneAmber
class node_bronze,node_silver,node_gold toneMint
class node_feature_engineering,node_anomaly_training,node_trade_scoring toneRose
class node_dashboard,node_mcp_agent toneIndigo
```

## Components

### Orchestration

- [Makefile](https://github.com/saeidshahriari/etrm-data-platform/blob/main/Makefile): operations entrypoint · `Makefile`
- [Local runner](https://github.com/saeidshahriari/etrm-data-platform/blob/main/main.py): pipeline entrypoint · `main.py`
- [Medallion pipeline DAG](https://github.com/saeidshahriari/etrm-data-platform/blob/main/dags/etrm_pipeline_dag.py): Airflow orchestration · Manually triggered pipeline with security checkpoints and worker-run Spark tasks. · `dags/etrm_pipeline_dag.py`
- [Airflow worker](https://github.com/saeidshahriari/etrm-data-platform/blob/main/Dockerfile): Python / local Spark runtime · Heavy processing imports occur only during task execution. · `Dockerfile`

### Secure ingestion

- [Vault KV v2](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/vault_client.py): secrets store · Development secrets and configuration source. · `src/ingestion/vault_client.py`
- [SMARD API](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/fetch_market_data.py): external market source · `src/ingestion/fetch_market_data.py`
- [Synthetic trade generator](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/generate_trades.py): data generator · `src/ingestion/generate_trades.py`
- [Trade PostgreSQL](https://github.com/saeidshahriari/etrm-data-platform/blob/main/docker-compose.yml): application trade store · `docker-compose.yml`
- [Secure ingestion](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/secure_ingest.py): landing service · Rejects batches over the invalid-row threshold before Bronze landing. · `src/ingestion/secure_ingest.py`
- [Security & quality gate](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/security/scanner.py): validation boundary · Scans injection and malware patterns plus schema, range, and poisoning anomalies. · `src/security/scanner.py`
- [Quarantine](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ingestion/secure_ingest.py): invalid-record store · `src/ingestion/secure_ingest.py`

### Lakehouse & analytics

- [Bronze Parquet](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/bronze_to_silver.py): raw lake dataset · Preserves raw market payloads. · `src/processing/bronze_to_silver.py`
- [Silver Parquet](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/bronze_to_silver.py): cleaned lake dataset · Typed market-price and trade datasets. · `src/processing/bronze_to_silver.py`
- [Gold Parquet](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/processing/silver_to_gold.py): business lake dataset · P&L, counterparty exposure, and portfolio summaries. · `src/processing/silver_to_gold.py`

### ML surveillance

- [Trade features](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/features.py): feature pipeline · Combines trade behavior with market context. · `src/ml/features.py`
- [Isolation Forest training](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/train_anomaly_model.py): ML training · Records unsupervised anomaly experiments in MLflow. · `src/ml/train_anomaly_model.py`
- [Trade scoring](https://github.com/saeidshahriari/etrm-data-platform/blob/main/src/ml/score_trades.py): ML inference · Writes scored and flagged trade outputs. · `src/ml/score_trades.py`

### Consumption

- [Operations dashboard](https://github.com/saeidshahriari/etrm-data-platform/blob/main/dashboard/app.py): Streamlit UI · `dashboard/app.py`
- [MCP analytics agent](https://github.com/saeidshahriari/etrm-data-platform/blob/main/agent/etrm_mcp_server.py): read-only query interface · Uses DuckDB over whitelisted Parquet tables and SELECT/WITH queries only. · `agent/etrm_mcp_server.py`

## Connections

- Makefile → Medallion pipeline DAG: operates
- Local runner → Medallion pipeline DAG: runs locally
- Medallion pipeline DAG → Airflow worker: dispatches tasks
- Vault KV v2 → Secure ingestion: supplies secrets
- SMARD API → Secure ingestion: market prices
- Synthetic trade generator → Trade PostgreSQL: stores trades
- Trade PostgreSQL → Secure ingestion: trade records
- Airflow worker → Secure ingestion: executes
- Secure ingestion → Security & quality gate: scans and validates
- Security & quality gate → Bronze Parquet: accepted records
- Security & quality gate → Quarantine: invalid records
- Bronze Parquet → Silver Parquet: clean and type
- Silver Parquet → Gold Parquet: derive business metrics
- Silver Parquet → Trade features: trades and prices
- Trade features → Isolation Forest training: feature set
- Isolation Forest training → Trade scoring: trained model
- Trade features → Trade scoring: scoring features
- Trade scoring → Gold Parquet: flagged trade outputs
- Gold Parquet → Operations dashboard: risk and surveillance views
- Gold Parquet → MCP analytics agent: Parquet analytics
