strompreis/api/main.py

51 lines
1.5 KiB
Python

"""
REST API for accessing processed electricity price and network data.
"""
import sys
from pathlib import Path
from fastapi import FastAPI, HTTPException
# Add project root to sys.path
project_root = str(Path(__file__).parent.parent)
if project_root not in sys.path:
sys.path.append(project_root)
from utils import database as db
from 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 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"
}