Skip to content

Commit 8a32754

Browse files
committed
Adds the simulations
1 parent b2f0a65 commit 8a32754

18 files changed

+1091
-160
lines changed

.github/workflows/tests.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
python-version: ["3.10", "3.11", "3.12"]
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
cache: pip
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
python -m pip install -r requirements.txt -r requirements-dev.txt
26+
- name: Run tests
27+
run: pytest
28+
- name: Lint code
29+
run: pylint --recursive=y elephant/

README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Its main purpose is the integration into the Green Metrics Tool (GMT) to provide dynamic carbon intensity values.
66

7+
![Elephant UI](Screenshot.png)
8+
79
## Installation
810

911
```bash
@@ -25,11 +27,13 @@ Before running Elephant, you need to configure your data providers:
2527
```bash
2628
# Copy the example configuration
2729
cp config.example.yml config.yml
28-
29-
# Edit config.yml and add your API tokens
30-
# For ElectricityMaps: Replace "your-electricitymaps-api-token-here" with your actual token
3130
```
3231

32+
Edit config.yml and add your API tokens
33+
34+
For ElectricityMaps: Replace "your-electricitymaps-api-token-here" with your actual token. You don't need to modify anything to start collection. We use free endpoints per default.
35+
36+
3337
## Running
3438

3539
You should just be able to do
@@ -49,7 +53,7 @@ You should be able to
4953

5054
```
5155
pip install -r requirements-dev.txt
52-
pylint elephant/**.py
56+
pylint pylint --recursive=y elephant/
5357
pytest tests
5458
```
5559

@@ -66,3 +70,50 @@ python3 -m elephant.database
6670
python3 -m elephant.cron
6771
python3 -m elephant --host 0.0.0.0 --port 8000
6872
```
73+
74+
## API
75+
76+
Once Elephant is running (for example with `docker compose up --build`), the generated docs are available at:
77+
78+
- Swagger UI: `http://localhost:8000/docs`
79+
- ReDoc: `http://localhost:8000/redoc`
80+
81+
### Core endpoints
82+
83+
- `GET /carbon-intensity/current` — Latest carbon intensities for a region. Query params: `region` (ISO 3166-1 alpha-2), optional `update` (force refresh) and `simulation_id` (serve simulated data).
84+
- `GET /carbon-intensity/current/primary` — Latest carbon intensity from the configured primary provider for `region`; also accepts `update` and `simulation_id`.
85+
- `GET /carbon-intensity/history` — Historical values between `startTime` and `endTime` (ISO 8601) for `region`.
86+
- `GET /regions` — List of regions with stored data.
87+
- `GET /health` — Service health, providers, DB record count, and regions.
88+
89+
### Simulation endpoints
90+
91+
Elephant includes a lightweight simulator so you can drive client integrations or demos without live provider data.
92+
93+
- `POST /simulation` with a JSON body `{"carbon_values": [120, [140, 2], [100, null]]}` registers a run and returns a `simulation_id`. Each entry is either a float or a `[value, calls]` pair; `calls` is how often that value is returned before automatically advancing (use `null` to disable auto-advance).
94+
- `GET /simulation/get_carbon?simulation_id=...` returns the current simulated carbon intensity and will auto-advance when call thresholds are met.
95+
- `POST /simulation/next?simulation_id=...` forces the next value.
96+
- `GET /simulation/stats?simulation_id=...` shows the current index and call history for debugging.
97+
98+
You can plug a simulation into the core responses by passing `simulation_id`:
99+
100+
```bash
101+
curl "http://localhost:8000/carbon-intensity/current?region=DE&simulation_id=<simulation_id>"
102+
curl "http://localhost:8000/carbon-intensity/current/primary?region=DE&simulation_id=<simulation_id>"
103+
```
104+
105+
### Electricity Maps compatible endpoints
106+
107+
For Electricity Maps compatible payloads, use the v3 endpoints:
108+
109+
```bash
110+
curl "http://localhost:8000/v3/carbon-intensity/current?zone=DE"
111+
curl "http://localhost:8000/v3/carbon-intensity/history?zone=DE"
112+
```
113+
114+
They mirror the Electricity Maps API schema. To serve simulated data to an Electricity Maps client, send the simulation id as `auth-token`:
115+
116+
```bash
117+
curl -H "auth-token: <simulation_id>" "http://localhost:8000/v3/carbon-intensity/current?zone=DE"
118+
curl -H "auth-token: <simulation_id>" "http://localhost:8000/v3/carbon-intensity/history?zone=DE"
119+
```

Screenshot.png

581 KB
Loading

config.example.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@ database:
88
# Provider configuration
99
providers:
1010
electricitymaps:
11-
enabled: false
1211
api_token: "YOUR TOKEN HERE"
13-
energycharts:
14-
enabled: true
15-
bundesnetzagentur:
16-
enabled: true
1712

1813
# Cron job for fetching data
1914
cron:
2015
interval_seconds: 300 # Default: 5 minutes
2116
sources:
2217
- region: "DE"
2318
provider: "bundesnetzagentur"
24-
- region: "DE"
25-
provider: "electricitymaps"
26-
primary: true
19+
primary: true # If you have multiple sources for the same region, one should be marked as primary so the api knows which to return by default
2720
- region: "DE"
2821
provider: "energycharts"
29-
- region: "FR"
30-
provider: "electricitymaps"
22+
# - region: "DE"
23+
# provider: "bundesnetzagentur_all"
24+
# - region: "DE"
25+
# provider: "electricitymaps"
26+
# primary: true
27+
# - region: "FR"
28+
# provider: "electricitymaps"
3129

3230
# Logging
3331
logging:

0 commit comments

Comments
 (0)