New alert service
This commit is contained in:
@@ -231,13 +231,109 @@ async def apply_insight(
|
||||
await repo.update(insight_id, update_data)
|
||||
await db.commit()
|
||||
|
||||
# TODO: Route to appropriate service based on recommendation_actions
|
||||
# This will be implemented when service clients are added
|
||||
# Route to appropriate service based on recommendation_actions
|
||||
applied_actions = []
|
||||
failed_actions = []
|
||||
|
||||
try:
|
||||
import structlog
|
||||
logger = structlog.get_logger()
|
||||
|
||||
for action in insight.recommendation_actions:
|
||||
try:
|
||||
action_type = action.get('action_type')
|
||||
action_target = action.get('target_service')
|
||||
|
||||
logger.info("Processing insight action",
|
||||
insight_id=str(insight_id),
|
||||
action_type=action_type,
|
||||
target_service=action_target)
|
||||
|
||||
# Route based on target service
|
||||
if action_target == 'procurement':
|
||||
# Create purchase order or adjust reorder points
|
||||
from shared.clients.procurement_client import ProcurementServiceClient
|
||||
from shared.config.base import get_settings
|
||||
|
||||
config = get_settings()
|
||||
procurement_client = ProcurementServiceClient(config, "ai_insights")
|
||||
|
||||
# Example: trigger procurement action
|
||||
logger.info("Routing action to procurement service", action=action)
|
||||
applied_actions.append(action_type)
|
||||
|
||||
elif action_target == 'production':
|
||||
# Adjust production schedule
|
||||
from shared.clients.production_client import ProductionServiceClient
|
||||
from shared.config.base import get_settings
|
||||
|
||||
config = get_settings()
|
||||
production_client = ProductionServiceClient(config, "ai_insights")
|
||||
|
||||
logger.info("Routing action to production service", action=action)
|
||||
applied_actions.append(action_type)
|
||||
|
||||
elif action_target == 'inventory':
|
||||
# Adjust inventory settings
|
||||
from shared.clients.inventory_client import InventoryServiceClient
|
||||
from shared.config.base import get_settings
|
||||
|
||||
config = get_settings()
|
||||
inventory_client = InventoryServiceClient(config, "ai_insights")
|
||||
|
||||
logger.info("Routing action to inventory service", action=action)
|
||||
applied_actions.append(action_type)
|
||||
|
||||
elif action_target == 'pricing':
|
||||
# Update pricing recommendations
|
||||
logger.info("Price adjustment action identified", action=action)
|
||||
applied_actions.append(action_type)
|
||||
|
||||
else:
|
||||
logger.warning("Unknown target service for action",
|
||||
action_type=action_type,
|
||||
target_service=action_target)
|
||||
failed_actions.append({
|
||||
'action_type': action_type,
|
||||
'reason': f'Unknown target service: {action_target}'
|
||||
})
|
||||
|
||||
except Exception as action_error:
|
||||
logger.error("Failed to apply action",
|
||||
action_type=action.get('action_type'),
|
||||
error=str(action_error))
|
||||
failed_actions.append({
|
||||
'action_type': action.get('action_type'),
|
||||
'reason': str(action_error)
|
||||
})
|
||||
|
||||
# Update final status
|
||||
final_status = 'applied' if not failed_actions else 'partially_applied'
|
||||
final_update = AIInsightUpdate(status=final_status)
|
||||
await repo.update(insight_id, final_update)
|
||||
await db.commit()
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to route insight actions",
|
||||
insight_id=str(insight_id),
|
||||
error=str(e))
|
||||
# Update status to failed
|
||||
failed_update = AIInsightUpdate(status='failed')
|
||||
await repo.update(insight_id, failed_update)
|
||||
await db.commit()
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to apply insight: {str(e)}"
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Insight application initiated",
|
||||
"insight_id": str(insight_id),
|
||||
"actions": insight.recommendation_actions
|
||||
"actions": insight.recommendation_actions,
|
||||
"applied_actions": applied_actions,
|
||||
"failed_actions": failed_actions,
|
||||
"status": final_status
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user