Improve the dahboard 6

This commit is contained in:
Urtzi Alfaro
2025-08-18 21:14:42 +02:00
parent 954f9a3c3f
commit d6fd53e461
3 changed files with 87 additions and 12 deletions

View File

@@ -51,7 +51,7 @@ async def get_current_weather(
weather = await weather_service.get_current_weather(latitude, longitude)
if not weather:
raise HTTPException(status_code=404, detail="Weather data not available")
raise HTTPException(status_code=503, detail="Weather service temporarily unavailable")
# Publish event
try:
@@ -133,8 +133,10 @@ async def get_weather_forecast(
forecast = await weather_service.get_weather_forecast(request.latitude, request.longitude, request.days)
# Don't return 404 for empty forecast - return empty list with 200 status
if not forecast:
raise HTTPException(status_code=404, detail="Weather forecast not available")
logger.info("Weather forecast unavailable - returning empty list")
return []
# Publish event
try:
@@ -180,11 +182,10 @@ async def get_hourly_weather_forecast(
request.latitude, request.longitude, request.hours
)
# Don't return 404 for empty hourly forecast - return empty list with 200 status
if not hourly_forecast:
raise HTTPException(
status_code=404,
detail="Hourly weather forecast not available. Please check AEMET API configuration."
)
logger.info("Hourly weather forecast unavailable - returning empty list")
return []
# Publish event
try:

View File

@@ -22,7 +22,7 @@ class WeatherService:
self.database_manager = database_manager
async def get_current_weather(self, latitude: float, longitude: float) -> Optional[WeatherDataResponse]:
"""Get current weather for location"""
"""Get current weather for location with graceful failure handling"""
try:
logger.debug("Getting current weather", lat=latitude, lon=longitude)
weather_data = await self.aemet_client.get_current_weather(latitude, longitude)
@@ -31,12 +31,32 @@ class WeatherService:
logger.debug("Weather data received", source=weather_data.get('source'))
return WeatherDataResponse(**weather_data)
else:
logger.warning("No weather data received from AEMET client")
return None
logger.warning("No weather data received from AEMET client - providing service unavailable response")
# Return a response indicating service unavailable rather than None
return WeatherDataResponse(
date=datetime.utcnow().isoformat(),
temperature=None,
precipitation=None,
humidity=None,
wind_speed=None,
pressure=None,
description="Servicio meteorológico temporalmente no disponible",
source="unavailable"
)
except Exception as e:
logger.error("Failed to get current weather", error=str(e), lat=latitude, lon=longitude)
return None
# Return error response rather than None to prevent 404
return WeatherDataResponse(
date=datetime.utcnow().isoformat(),
temperature=None,
precipitation=None,
humidity=None,
wind_speed=None,
pressure=None,
description="Error al obtener datos meteorológicos",
source="error"
)
async def get_weather_forecast(self, latitude: float, longitude: float, days: int = 7) -> List[WeatherForecastResponse]:
"""Get weather forecast for location"""