from fastapi import APIRouter, Query, HTTPException, status from schemas.weather import CurrentWeatherMetrics, WeatherHistoryResponse from services.prometheus import prometheus_service router = APIRouter(prefix="/weather", tags=["Weather"]) @router.get( "/current", response_model=CurrentWeatherMetrics, summary="Get current weather metrics", description="Returns live weather metrics from the Grabengasse 7 weather station via Prometheus." ) async def get_current_weather(): try: metrics = await prometheus_service.get_current_metrics() return metrics except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to fetch current weather metrics: {str(e)}" ) @router.get( "/history", response_model=WeatherHistoryResponse, summary="Get weather historical time series data", description="Returns historical time series metrics (temperature, humidity, wind, UV, illuminance) for interactive charts." ) async def get_weather_history( hours: int = Query(24, ge=1, le=10000, description="History duration in hours (1h to 8760h / 12 months)") ): try: # Calculate appropriate step size based on duration if hours <= 24: step = 300 # 5 min intervals (~288 points) elif hours <= 168: step = 1800 # 30 min intervals (~336 points) elif hours <= 720: step = 7200 # 2 hour intervals (~360 points) else: step = 86400 # 24 hour intervals (~365 points for 12 months) history = await prometheus_service.get_history(hours=hours, step=step) return history except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to fetch weather historical data: {str(e)}" )