Fix data fetch 7

This commit is contained in:
Urtzi Alfaro
2025-07-27 22:58:18 +02:00
parent 0201b428e5
commit 946015b80c
8 changed files with 138 additions and 100 deletions

View File

@@ -11,7 +11,7 @@ class DataServiceClient:
def __init__(self):
self.base_url = settings.API_GATEWAY_URL
self.timeout = 30.0
self.timeout = 2000.0
async def fetch_sales_data(
self,
@@ -196,7 +196,8 @@ class DataServiceClient:
logger.info(f"Traffic request payload: {payload}", tenant_id=tenant_id)
# Make POST request via gateway
async with httpx.AsyncClient(timeout=self.timeout) as client:
timeout_config = httpx.Timeout(connect=30.0, read=self.timeout, write=30.0, pool=30.0)
async with httpx.AsyncClient(timeout=timeout_config) as client:
response = await client.post(
f"{self.base_url}/api/v1/tenants/{tenant_id}/traffic/historical",
headers=headers,

View File

@@ -535,79 +535,6 @@ class TrainingService:
logger.error(f"Failed to update job status: {str(e)}")
await db.rollback()
async def _fetch_sales_data(self,
tenant_id: str,
request: Any,
limit: Optional[int] = None) -> List[Dict]:
"""Fetch sales data from data service"""
try:
# Call data service to get sales data
async with httpx.AsyncClient() as client:
params = {}
headers = {
"X-Tenant-ID": tenant_id
}
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()
if limit:
params["limit"] = limit
response = await client.get(
f"{settings.DATA_SERVICE_URL}/api/v1/tenants/{tenant_id}/sales",
params=params,
headers=headers,
timeout=30.0
)
if response.status_code == 200:
return response.json().get("sales", [])
else:
logger.error(f"Failed to fetch sales data: {response.status_code}")
return []
except Exception as e:
logger.error(f"Error fetching sales data: {str(e)}")
return []
async def _fetch_product_sales_data(self,
tenant_id: str,
product_name: str,
request: Any) -> List[Dict]:
"""Fetch sales data for a specific product"""
try:
async with httpx.AsyncClient() as client:
params = {
"tenant_id": tenant_id,
"product_name": product_name
}
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}/api/sales/product/{product_name}",
params=params,
timeout=30.0
)
if response.status_code == 200:
return response.json().get("sales", [])
else:
logger.error(f"Failed to fetch product sales data: {response.status_code}")
return []
except Exception as e:
logger.error(f"Error fetching product sales data: {str(e)}")
return []
async def _store_trained_models(self,
db: AsyncSession,
tenant_id: str,