45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
"""
|
|
REST API for accessing processed electricity price and network data.
|
|
"""
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
from common.utils import database as db
|
|
from common.utils.config_loader import settings
|
|
|
|
app = FastAPI(
|
|
title="Strompreis API",
|
|
description="Provides access to cleaned and joined electricity price, network, and weather data."
|
|
)
|
|
|
|
@app.get("/current-price")
|
|
async def get_current_price():
|
|
"""
|
|
Returns the most recent price record from the Gold (combined) layer.
|
|
"""
|
|
try:
|
|
with db.get_connection() as con:
|
|
res = con.execute(
|
|
"SELECT timestamp, price FROM combined ORDER BY timestamp DESC LIMIT 1"
|
|
).fetchone()
|
|
|
|
if not res:
|
|
raise HTTPException(status_code=404, detail="No data available")
|
|
|
|
return {
|
|
"timestamp": res[0],
|
|
"price_eur_mwh": res[1],
|
|
"price_ct_kwh": round(res[1] / 10, 2)
|
|
}
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
|
|
|
@app.get("/status")
|
|
async def get_status():
|
|
"""Returns basic system configuration and status."""
|
|
return {
|
|
"region": settings.smard.region,
|
|
"database": settings.database.path,
|
|
"status": "operational"
|
|
}
|