Fix data fetch 7
This commit is contained in:
@@ -153,7 +153,8 @@ async def get_training_job(
|
||||
job_id: str,
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
||||
training_service: TrainingService = Depends(get_training_service)
|
||||
training_service: TrainingService = Depends(get_training_service),
|
||||
db: AsyncSession = Depends(get_db_session)
|
||||
):
|
||||
"""Get specific training job details"""
|
||||
try:
|
||||
@@ -164,17 +165,24 @@ async def get_training_job(
|
||||
job_id=job_id,
|
||||
tenant_id=tenant_id_str)
|
||||
|
||||
job = await training_service.get_training_job(job_id)
|
||||
job_log = await training_service.get_job_status(db, job_id, tenant_id_str)
|
||||
|
||||
# Verify tenant access
|
||||
if job.tenant_id != tenant_id:
|
||||
if job_log.tenant_id != tenant_id:
|
||||
logger.warning("Unauthorized job access attempt",
|
||||
job_id=job_id,
|
||||
tenant_id=str(tenant_id),
|
||||
job_tenant_id=job.tenant_id)
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
|
||||
return job
|
||||
return TrainingJobResponse(
|
||||
job_id=job_log.job_id,
|
||||
status=TrainingStatus(job_log.status),
|
||||
message=_generate_status_message(job_log.status, job_log.current_step),
|
||||
tenant_id=str(job_log.tenant_id),
|
||||
created_at=job_log.start_time,
|
||||
estimated_duration_minutes=_estimate_duration(job_log.status, job_log.progress)
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
@@ -501,4 +509,31 @@ async def retrain_all_products(
|
||||
logger.error("Failed to start retraining",
|
||||
error=str(e),
|
||||
tenant_id=tenant_id)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start retraining: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start retraining: {str(e)}")
|
||||
|
||||
def _generate_status_message(status: str, current_step: str) -> str:
|
||||
"""Generate appropriate status message"""
|
||||
status_messages = {
|
||||
"pending": "Training job is queued",
|
||||
"running": f"Training in progress: {current_step}",
|
||||
"completed": "Training completed successfully",
|
||||
"failed": "Training failed",
|
||||
"cancelled": "Training was cancelled"
|
||||
}
|
||||
return status_messages.get(status, f"Status: {status}")
|
||||
|
||||
def _estimate_duration(status: str, progress: int) -> int:
|
||||
"""Estimate remaining duration in minutes"""
|
||||
if status == "completed":
|
||||
return 0
|
||||
elif status == "failed" or status == "cancelled":
|
||||
return 0
|
||||
elif status == "pending":
|
||||
return 30 # Default estimate
|
||||
else: # running
|
||||
if progress > 0:
|
||||
# Rough estimate based on progress
|
||||
remaining_progress = 100 - progress
|
||||
return max(1, int((remaining_progress / max(progress, 1)) * 10))
|
||||
else:
|
||||
return 25 # Default for running jobs
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user