Improve the dahboard 4
This commit is contained in:
@@ -9,7 +9,7 @@ import structlog
|
||||
|
||||
from app.models.weather import WeatherData, WeatherForecast
|
||||
from app.external.aemet import AEMETClient
|
||||
from app.schemas.weather import WeatherDataResponse, WeatherForecastResponse
|
||||
from app.schemas.weather import WeatherDataResponse, WeatherForecastResponse, HourlyForecastResponse
|
||||
from app.repositories.weather_repository import WeatherRepository
|
||||
|
||||
logger = structlog.get_logger()
|
||||
@@ -79,6 +79,48 @@ class WeatherService:
|
||||
logger.error("Failed to get weather forecast", error=str(e), lat=latitude, lon=longitude)
|
||||
return []
|
||||
|
||||
async def get_hourly_forecast(self, latitude: float, longitude: float, hours: int = 48) -> List[HourlyForecastResponse]:
|
||||
"""Get hourly weather forecast for location"""
|
||||
try:
|
||||
logger.debug("Getting hourly weather forecast", lat=latitude, lon=longitude, hours=hours)
|
||||
hourly_data = await self.aemet_client.get_hourly_forecast(latitude, longitude, hours)
|
||||
|
||||
if hourly_data:
|
||||
logger.debug("Hourly forecast data received", count=len(hourly_data))
|
||||
# Validate each hourly forecast item before creating response
|
||||
valid_forecasts = []
|
||||
for item in hourly_data:
|
||||
try:
|
||||
if isinstance(item, dict):
|
||||
# Ensure required fields are present
|
||||
hourly_item = {
|
||||
"forecast_datetime": item.get("forecast_datetime", datetime.now()),
|
||||
"generated_at": item.get("generated_at", datetime.now()),
|
||||
"temperature": float(item.get("temperature", 15.0)),
|
||||
"precipitation": float(item.get("precipitation", 0.0)),
|
||||
"humidity": float(item.get("humidity", 50.0)),
|
||||
"wind_speed": float(item.get("wind_speed", 10.0)),
|
||||
"description": str(item.get("description", "Variable")),
|
||||
"source": str(item.get("source", "unknown")),
|
||||
"hour": int(item.get("hour", 0))
|
||||
}
|
||||
valid_forecasts.append(HourlyForecastResponse(**hourly_item))
|
||||
else:
|
||||
logger.warning("Invalid hourly forecast item type", item_type=type(item))
|
||||
except Exception as item_error:
|
||||
logger.warning("Error processing hourly forecast item", error=str(item_error), item=item)
|
||||
continue
|
||||
|
||||
logger.debug("Valid hourly forecasts processed", count=len(valid_forecasts))
|
||||
return valid_forecasts
|
||||
else:
|
||||
logger.warning("No hourly forecast data received from AEMET client")
|
||||
return []
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get hourly weather forecast", error=str(e), lat=latitude, lon=longitude)
|
||||
return []
|
||||
|
||||
async def get_historical_weather(self,
|
||||
latitude: float,
|
||||
longitude: float,
|
||||
|
||||
Reference in New Issue
Block a user