Add improvements 2
This commit is contained in:
@@ -43,86 +43,79 @@ class POIFeatureIntegrator:
|
||||
force_refresh: bool = False
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Fetch POI features for tenant location.
|
||||
Fetch POI features for tenant location (optimized for training).
|
||||
|
||||
First checks if POI context exists, if not, triggers detection.
|
||||
First checks if POI context exists. If not, returns None without triggering detection.
|
||||
POI detection should be triggered during tenant registration, not during training.
|
||||
|
||||
Args:
|
||||
tenant_id: Tenant UUID
|
||||
latitude: Bakery latitude
|
||||
longitude: Bakery longitude
|
||||
force_refresh: Force re-detection
|
||||
force_refresh: Force re-detection (only use if POI context already exists)
|
||||
|
||||
Returns:
|
||||
Dictionary with POI features or None if detection fails
|
||||
Dictionary with POI features or None if not available
|
||||
"""
|
||||
try:
|
||||
# Try to get existing POI context first
|
||||
if not force_refresh:
|
||||
existing_context = await self.external_client.get_poi_context(tenant_id)
|
||||
if existing_context:
|
||||
poi_context = existing_context.get("poi_context", {})
|
||||
ml_features = poi_context.get("ml_features", {})
|
||||
existing_context = await self.external_client.get_poi_context(tenant_id)
|
||||
|
||||
# Check if stale
|
||||
is_stale = existing_context.get("is_stale", False)
|
||||
if not is_stale:
|
||||
if existing_context:
|
||||
poi_context = existing_context.get("poi_context", {})
|
||||
ml_features = poi_context.get("ml_features", {})
|
||||
|
||||
# Check if stale and force_refresh is requested
|
||||
is_stale = existing_context.get("is_stale", False)
|
||||
|
||||
if not is_stale or not force_refresh:
|
||||
logger.info(
|
||||
"Using existing POI context",
|
||||
tenant_id=tenant_id,
|
||||
is_stale=is_stale,
|
||||
feature_count=len(ml_features)
|
||||
)
|
||||
return ml_features
|
||||
else:
|
||||
logger.info(
|
||||
"POI context is stale and force_refresh=True, refreshing",
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
# Only refresh if explicitly requested and context exists
|
||||
detection_result = await self.external_client.detect_poi_for_tenant(
|
||||
tenant_id=tenant_id,
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
force_refresh=True
|
||||
)
|
||||
|
||||
if detection_result:
|
||||
poi_context = detection_result.get("poi_context", {})
|
||||
ml_features = poi_context.get("ml_features", {})
|
||||
logger.info(
|
||||
"Using existing POI context",
|
||||
tenant_id=tenant_id
|
||||
"POI refresh completed",
|
||||
tenant_id=tenant_id,
|
||||
feature_count=len(ml_features)
|
||||
)
|
||||
return ml_features
|
||||
else:
|
||||
logger.info(
|
||||
"POI context is stale, refreshing",
|
||||
logger.warning(
|
||||
"POI refresh failed, returning existing features",
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
force_refresh = True
|
||||
else:
|
||||
logger.info(
|
||||
"No existing POI context, will detect",
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
|
||||
# Detect or refresh POIs
|
||||
logger.info(
|
||||
"Detecting POIs for tenant",
|
||||
tenant_id=tenant_id,
|
||||
location=(latitude, longitude)
|
||||
)
|
||||
|
||||
detection_result = await self.external_client.detect_poi_for_tenant(
|
||||
tenant_id=tenant_id,
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
force_refresh=force_refresh
|
||||
)
|
||||
|
||||
if detection_result:
|
||||
poi_context = detection_result.get("poi_context", {})
|
||||
ml_features = poi_context.get("ml_features", {})
|
||||
|
||||
logger.info(
|
||||
"POI detection completed",
|
||||
tenant_id=tenant_id,
|
||||
total_pois=poi_context.get("total_pois_detected", 0),
|
||||
feature_count=len(ml_features)
|
||||
)
|
||||
|
||||
return ml_features
|
||||
return ml_features
|
||||
else:
|
||||
logger.error(
|
||||
"POI detection failed",
|
||||
logger.info(
|
||||
"No existing POI context found - POI detection should be triggered during tenant registration",
|
||||
tenant_id=tenant_id
|
||||
)
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Unexpected error fetching POI features",
|
||||
logger.warning(
|
||||
"Error fetching POI features - returning None",
|
||||
tenant_id=tenant_id,
|
||||
error=str(e),
|
||||
exc_info=True
|
||||
error=str(e)
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user