Fix data fetch

This commit is contained in:
Urtzi Alfaro
2025-07-27 19:30:42 +02:00
parent 280d356a51
commit 4684235111
5 changed files with 18 additions and 68 deletions

View File

@@ -28,7 +28,7 @@ router = APIRouter(tags=["traffic"])
traffic_service = TrafficService()
logger = structlog.get_logger()
@router.get("/tenants/{tenant_id}/current", response_model=TrafficDataResponse)
@router.get("/tenants/{tenant_id}/traffic/current", response_model=TrafficDataResponse)
async def get_current_traffic(
latitude: float = Query(..., description="Latitude"),
longitude: float = Query(..., description="Longitude"),
@@ -71,7 +71,7 @@ async def get_current_traffic(
logger.error("Traffic API traceback", traceback=traceback.format_exc())
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@router.get("/tenants/{tenant_id}/historical", response_model=List[TrafficDataResponse])
@router.get("/tenants/{tenant_id}/traffic/historical", response_model=List[TrafficDataResponse])
async def get_historical_traffic(
latitude: float = Query(..., description="Latitude"),
longitude: float = Query(..., description="Longitude"),
@@ -118,7 +118,7 @@ async def get_historical_traffic(
logger.error("Unexpected error in historical traffic API", error=str(e))
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@router.post("/tenants/{tenant_id}/store")
@router.post("/tenants/{tenant_id}/traffic/store")
async def store_traffic_data(
latitude: float = Query(..., description="Latitude"),
longitude: float = Query(..., description="Longitude"),

View File

@@ -23,7 +23,7 @@ from shared.auth.decorators import (
router = APIRouter(tags=["weather"])
logger = structlog.get_logger()
@router.get("/tenants/{tenant_id}/current", response_model=WeatherDataResponse)
@router.get("/tenants/{tenant_id}/wetaher/current", response_model=WeatherDataResponse)
async def get_current_weather(
latitude: float = Query(..., description="Latitude"),
longitude: float = Query(..., description="Longitude"),
@@ -65,7 +65,7 @@ async def get_current_weather(
logger.error("Failed to get current weather", error=str(e))
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@router.get("/tenants/{tenant_id}/forecast", response_model=List[WeatherForecastResponse])
@router.get("/tenants/{tenant_id}/weather/forecast", response_model=List[WeatherForecastResponse])
async def get_weather_forecast(
latitude: float = Query(..., description="Latitude"),
longitude: float = Query(..., description="Longitude"),
@@ -109,7 +109,7 @@ async def get_weather_forecast(
logger.error("Failed to get weather forecast", error=str(e))
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@router.get("/tenants/{tenant_id}/history", response_model=List[WeatherDataResponse])
@router.get("/tenants/{tenant_id}/weather/history", response_model=List[WeatherDataResponse])
async def get_weather_history(
start_date: date = Query(..., description="Start date"),
end_date: date = Query(..., description="End date"),
@@ -125,7 +125,7 @@ async def get_weather_history(
tenant_id=tenant_id)
weather_service = WeatherService()
history = await weather_service.get_weather_history(
history = await weather_service.get_historical_weather(
latitude, longitude, start_date, end_date
)
@@ -135,7 +135,7 @@ async def get_weather_history(
logger.error("Failed to get weather history", error=str(e))
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@router.post("/tenants/{tenant_id}/sync")
@router.post("/tenants/{tenant_id}weather/sync")
async def sync_weather_data(
background_tasks: BackgroundTasks,
force: bool = Query(False, description="Force sync even if recently synced"),

View File

@@ -94,10 +94,14 @@ class DataServiceClient:
headers = service_auth.get_request_headers(tenant_id)
headers["Authorization"] = f"Bearer {token}"
params = {strta date, end date, lat, long }
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(
f"{self.base_url}/api/v1/tenants/{tenant_id}/weather/history",
headers=headers
headers=headers,
params=params,
timeout=30.0
)
if response.status_code == 200:

View File

@@ -176,6 +176,7 @@ class TrainingService:
# Publish completion event
await publish_job_completed(job_id, tenant_id, training_results)
logger.info(f"Training results {training_results}")
logger.info(f"Training job {job_id} completed successfully")
metrics.increment_counter("training_jobs_completed")
@@ -207,17 +208,16 @@ class TrainingService:
# Fetch data
sales_data = await self._fetch_product_sales_data(tenant_id, product_name, request)
weather_data = []
traffic_data = []
if request.include_weather:
await self._update_job_status(db, job_id, "running", 30, "Fetching weather data")
weather_data = await self._fetch_weather_data(tenant_id, request)
weather_data = await self.data_client.fetch_weather_data(tenant_id, request)
if request.include_traffic:
await self._update_job_status(db, job_id, "running", 50, "Fetching traffic data")
traffic_data = await self._fetch_traffic_data(tenant_id, request)
traffic_data = await self.data_client.fetch_traffic_data(tenant_id, request)
# Execute training
await self._update_job_status(db, job_id, "running", 70, f"Training model for {product_name}")
@@ -507,62 +507,6 @@ class TrainingService:
logger.error(f"Error fetching product sales data: {str(e)}")
return []
async def _fetch_weather_data(self, tenant_id: str, request: Any) -> List[Dict]:
"""Fetch weather data from data service"""
try:
async with httpx.AsyncClient() as client:
params = { }
if hasattr(request, 'start_date') and request.start_date:
params["start_date"] = request.start_date.isoformat()
if hasattr(request, 'end_date') and request.end_date:
params["end_date"] = request.end_date.isoformat()
response = await client.get(
f"{settings.DATA_SERVICE_URL}/tenants/{tenant_id}/weather/history",
params=params,
timeout=30.0
)
if response.status_code == 200:
return response.json().get("weather", [])
else:
logger.warning(f"Failed to fetch weather data: {response.status_code}")
return []
except Exception as e:
logger.warning(f"Error fetching weather data: {str(e)}")
return []
async def _fetch_traffic_data(self, tenant_id: str, request: Any) -> List[Dict]:
"""Fetch traffic data from data service"""
try:
async with httpx.AsyncClient() as client:
params = { }
if hasattr(request, 'start_date') and request.start_date:
params["start_date"] = request.start_date.isoformat()
if hasattr(request, 'end_date') and request.end_date:
params["end_date"] = request.end_date.isoformat()
response = await client.get(
f"http://gateway:8000/tenants/{tenant_id}/traffic/historical",
params=params,
timeout=30.0
)
if response.status_code == 200:
return response.json().get("traffic", [])
else:
logger.warning(f"Failed to fetch traffic data: {response.status_code}")
return []
except Exception as e:
logger.warning(f"Error fetching traffic data: {str(e)}")
return []
async def _store_trained_models(self,
db: AsyncSession,
tenant_id: str,

View File

@@ -327,6 +327,8 @@ BAKERY_DATA="{
\"city\": \"Madrid\",
\"postal_code\": \"28001\",
\"phone\": \"+34600123456\",
\"include_weather\": \"True\",
\"include_traffic\": \"True\",
\"latitude\": $MOCK_LATITUDE,
\"longitude\": $MOCK_LONGITUDE
}"