Fix data fetch
This commit is contained in:
@@ -28,7 +28,7 @@ router = APIRouter(tags=["traffic"])
|
|||||||
traffic_service = TrafficService()
|
traffic_service = TrafficService()
|
||||||
logger = structlog.get_logger()
|
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(
|
async def get_current_traffic(
|
||||||
latitude: float = Query(..., description="Latitude"),
|
latitude: float = Query(..., description="Latitude"),
|
||||||
longitude: float = Query(..., description="Longitude"),
|
longitude: float = Query(..., description="Longitude"),
|
||||||
@@ -71,7 +71,7 @@ async def get_current_traffic(
|
|||||||
logger.error("Traffic API traceback", traceback=traceback.format_exc())
|
logger.error("Traffic API traceback", traceback=traceback.format_exc())
|
||||||
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
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(
|
async def get_historical_traffic(
|
||||||
latitude: float = Query(..., description="Latitude"),
|
latitude: float = Query(..., description="Latitude"),
|
||||||
longitude: float = Query(..., description="Longitude"),
|
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))
|
logger.error("Unexpected error in historical traffic API", error=str(e))
|
||||||
raise HTTPException(status_code=500, detail=f"Internal server 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(
|
async def store_traffic_data(
|
||||||
latitude: float = Query(..., description="Latitude"),
|
latitude: float = Query(..., description="Latitude"),
|
||||||
longitude: float = Query(..., description="Longitude"),
|
longitude: float = Query(..., description="Longitude"),
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ from shared.auth.decorators import (
|
|||||||
router = APIRouter(tags=["weather"])
|
router = APIRouter(tags=["weather"])
|
||||||
logger = structlog.get_logger()
|
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(
|
async def get_current_weather(
|
||||||
latitude: float = Query(..., description="Latitude"),
|
latitude: float = Query(..., description="Latitude"),
|
||||||
longitude: float = Query(..., description="Longitude"),
|
longitude: float = Query(..., description="Longitude"),
|
||||||
@@ -65,7 +65,7 @@ async def get_current_weather(
|
|||||||
logger.error("Failed to get current weather", error=str(e))
|
logger.error("Failed to get current weather", error=str(e))
|
||||||
raise HTTPException(status_code=500, detail=f"Internal server 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(
|
async def get_weather_forecast(
|
||||||
latitude: float = Query(..., description="Latitude"),
|
latitude: float = Query(..., description="Latitude"),
|
||||||
longitude: float = Query(..., description="Longitude"),
|
longitude: float = Query(..., description="Longitude"),
|
||||||
@@ -109,7 +109,7 @@ async def get_weather_forecast(
|
|||||||
logger.error("Failed to get weather forecast", error=str(e))
|
logger.error("Failed to get weather forecast", error=str(e))
|
||||||
raise HTTPException(status_code=500, detail=f"Internal server 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(
|
async def get_weather_history(
|
||||||
start_date: date = Query(..., description="Start date"),
|
start_date: date = Query(..., description="Start date"),
|
||||||
end_date: date = Query(..., description="End date"),
|
end_date: date = Query(..., description="End date"),
|
||||||
@@ -125,7 +125,7 @@ async def get_weather_history(
|
|||||||
tenant_id=tenant_id)
|
tenant_id=tenant_id)
|
||||||
|
|
||||||
weather_service = WeatherService()
|
weather_service = WeatherService()
|
||||||
history = await weather_service.get_weather_history(
|
history = await weather_service.get_historical_weather(
|
||||||
latitude, longitude, start_date, end_date
|
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))
|
logger.error("Failed to get weather history", error=str(e))
|
||||||
raise HTTPException(status_code=500, detail=f"Internal server 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(
|
async def sync_weather_data(
|
||||||
background_tasks: BackgroundTasks,
|
background_tasks: BackgroundTasks,
|
||||||
force: bool = Query(False, description="Force sync even if recently synced"),
|
force: bool = Query(False, description="Force sync even if recently synced"),
|
||||||
|
|||||||
@@ -94,10 +94,14 @@ class DataServiceClient:
|
|||||||
headers = service_auth.get_request_headers(tenant_id)
|
headers = service_auth.get_request_headers(tenant_id)
|
||||||
headers["Authorization"] = f"Bearer {token}"
|
headers["Authorization"] = f"Bearer {token}"
|
||||||
|
|
||||||
|
params = {strta date, end date, lat, long }
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
f"{self.base_url}/api/v1/tenants/{tenant_id}/weather/history",
|
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:
|
if response.status_code == 200:
|
||||||
|
|||||||
@@ -176,6 +176,7 @@ class TrainingService:
|
|||||||
# Publish completion event
|
# Publish completion event
|
||||||
await publish_job_completed(job_id, tenant_id, training_results)
|
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")
|
logger.info(f"Training job {job_id} completed successfully")
|
||||||
metrics.increment_counter("training_jobs_completed")
|
metrics.increment_counter("training_jobs_completed")
|
||||||
|
|
||||||
@@ -207,17 +208,16 @@ class TrainingService:
|
|||||||
|
|
||||||
# Fetch data
|
# Fetch data
|
||||||
sales_data = await self._fetch_product_sales_data(tenant_id, product_name, request)
|
sales_data = await self._fetch_product_sales_data(tenant_id, product_name, request)
|
||||||
|
|
||||||
weather_data = []
|
weather_data = []
|
||||||
traffic_data = []
|
traffic_data = []
|
||||||
|
|
||||||
if request.include_weather:
|
if request.include_weather:
|
||||||
await self._update_job_status(db, job_id, "running", 30, "Fetching weather data")
|
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:
|
if request.include_traffic:
|
||||||
await self._update_job_status(db, job_id, "running", 50, "Fetching traffic data")
|
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
|
# Execute training
|
||||||
await self._update_job_status(db, job_id, "running", 70, f"Training model for {product_name}")
|
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)}")
|
logger.error(f"Error fetching product sales data: {str(e)}")
|
||||||
return []
|
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,
|
async def _store_trained_models(self,
|
||||||
db: AsyncSession,
|
db: AsyncSession,
|
||||||
tenant_id: str,
|
tenant_id: str,
|
||||||
|
|||||||
@@ -327,6 +327,8 @@ BAKERY_DATA="{
|
|||||||
\"city\": \"Madrid\",
|
\"city\": \"Madrid\",
|
||||||
\"postal_code\": \"28001\",
|
\"postal_code\": \"28001\",
|
||||||
\"phone\": \"+34600123456\",
|
\"phone\": \"+34600123456\",
|
||||||
|
\"include_weather\": \"True\",
|
||||||
|
\"include_traffic\": \"True\",
|
||||||
\"latitude\": $MOCK_LATITUDE,
|
\"latitude\": $MOCK_LATITUDE,
|
||||||
\"longitude\": $MOCK_LONGITUDE
|
\"longitude\": $MOCK_LONGITUDE
|
||||||
}"
|
}"
|
||||||
|
|||||||
Reference in New Issue
Block a user