Clean code

This commit is contained in:
Urtzi Alfaro
2025-09-29 19:18:45 +02:00
parent 2712a60a2a
commit d1c83dce74
6 changed files with 0 additions and 308 deletions

61
tests/test_alert_quick.sh Executable file
View File

@@ -0,0 +1,61 @@
#!/bin/bash
# Quick Alert Test Script
# This script sends a single test alert using the working configuration
# Use this for quick testing of the real-time alert system
echo "🚀 Sending quick test alert..."
docker exec bakery-ia-alert-processor-1 python -c "
import aio_pika
import asyncio
import json
import uuid
from datetime import datetime
async def quick_alert():
connection = await aio_pika.connect_robust('amqp://bakery:forecast123@rabbitmq:5672/')
channel = await connection.channel()
exchange = await channel.declare_exchange(
'alerts.exchange',
aio_pika.ExchangeType.TOPIC,
durable=True
)
tenant_id = 'c464fb3e-7af2-46e6-9e43-85318f34199a'
# Quick test alert
alert = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'alert',
'type': 'quick_test',
'severity': 'urgent',
'service': 'quick-test',
'title': '⚡ QUICK TEST - ' + datetime.now().strftime('%H:%M:%S'),
'message': 'Quick test alert sent at ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'actions': ['Check frontend', 'Verify reception'],
'metadata': {'quick_test': True, 'sent_at': datetime.utcnow().isoformat()},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(alert).encode()),
routing_key='alert.urgent.quick-test'
)
print(f'✅ Quick alert sent: {alert[\"title\"]}')
print(f' ID: {alert[\"id\"]}')
print(' Check your frontend for real-time update!')
await connection.close()
asyncio.run(quick_alert())
"
echo "✅ Quick test completed!"
echo "🌐 Check your frontend - the alert should appear immediately in:"
echo " - Notification icon (header)"
echo " - Panel de control (alerts section)"
echo " - Toast notification (if urgent/high priority)"

137
tests/test_alert_working.py Normal file
View File

@@ -0,0 +1,137 @@
#!/usr/bin/env python3
"""
WORKING Alert Test Script
This script successfully sends alerts that reach the frontend via SSE.
Use this for future testing of the real-time alert system.
"""
import aio_pika
import asyncio
import json
import uuid
from datetime import datetime
async def send_test_alerts():
"""Send test alerts and recommendations to the system"""
# Connect to RabbitMQ using the correct credentials
connection = await aio_pika.connect_robust('amqp://bakery:forecast123@rabbitmq:5672/')
channel = await connection.channel()
# Declare the alerts exchange
exchange = await channel.declare_exchange(
'alerts.exchange',
aio_pika.ExchangeType.TOPIC,
durable=True
)
# Use your actual tenant ID (replace if different)
tenant_id = 'c464fb3e-7af2-46e6-9e43-85318f34199a'
print("🚀 Sending test alerts to the system...")
# Test Alert - Urgent
urgent_alert = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'alert',
'type': 'test_alert',
'severity': 'urgent', # Must be lowercase: urgent, high, medium, low
'service': 'test-manual',
'title': '🚨 TEST URGENT ALERT',
'message': 'This is a test urgent alert that should appear in your frontend immediately',
'actions': ['Check frontend dashboard', 'Verify notification icon', 'Confirm real-time reception'],
'metadata': {'test': True, 'manual': True, 'timestamp': datetime.utcnow().isoformat()},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(urgent_alert).encode()),
routing_key='alert.urgent.test-manual'
)
print(f"✅ Sent urgent alert: {urgent_alert['title']}")
# Wait a moment between alerts
await asyncio.sleep(2)
# Test Alert - High Priority
high_alert = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'alert',
'type': 'test_alert_high',
'severity': 'high',
'service': 'test-manual',
'title': '⚠️ TEST HIGH PRIORITY ALERT',
'message': 'This is a high priority test alert for the bakery system',
'actions': ['Review inventory', 'Contact supplier', 'Update stock levels'],
'metadata': {'test': True, 'priority': 'high', 'category': 'inventory'},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(high_alert).encode()),
routing_key='alert.high.test-manual'
)
print(f"✅ Sent high priority alert: {high_alert['title']}")
# Wait a moment
await asyncio.sleep(2)
# Test Recommendation
recommendation = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'recommendation',
'type': 'optimization_suggestion',
'severity': 'medium',
'service': 'test-manual',
'title': '💡 TEST OPTIMIZATION RECOMMENDATION',
'message': 'This is a test recommendation to optimize your bakery operations',
'actions': ['Review suggestion', 'Analyze impact', 'Consider implementation'],
'metadata': {'test': True, 'type': 'optimization', 'potential_savings': '15%'},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(recommendation).encode()),
routing_key='recommendation.medium.test-manual'
)
print(f"✅ Sent recommendation: {recommendation['title']}")
# Wait a moment
await asyncio.sleep(2)
# Test Low Priority Alert
low_alert = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'alert',
'type': 'test_info',
'severity': 'low',
'service': 'test-manual',
'title': ' TEST INFO ALERT',
'message': 'This is a low priority informational alert for testing',
'actions': ['Review when convenient'],
'metadata': {'test': True, 'info': True},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(low_alert).encode()),
routing_key='alert.low.test-manual'
)
print(f"✅ Sent low priority alert: {low_alert['title']}")
await connection.close()
print("\n🎉 All test alerts sent successfully!")
print("\nYou should see these alerts in your frontend:")
print(" 1. In the notification icon (header) - live counter update")
print(" 2. In the panel de control - compact expandable cards")
print(" 3. As real-time toast notifications")
print("\nNote: Urgent and high alerts will show as toast notifications")
if __name__ == "__main__":
# Run the async function
asyncio.run(send_test_alerts())

View File

@@ -0,0 +1,94 @@
#!/usr/bin/env python3
"""
Out of Stock Alert Test
Sends a realistic out of stock alert to test the inventory monitoring system
"""
import aio_pika
import asyncio
import json
import uuid
from datetime import datetime
async def send_out_of_stock_alert():
"""Send a realistic out of stock alert"""
# Connect to RabbitMQ
connection = await aio_pika.connect_robust('amqp://bakery:forecast123@rabbitmq:5672/')
channel = await connection.channel()
# Declare the alerts exchange
exchange = await channel.declare_exchange(
'alerts.exchange',
aio_pika.ExchangeType.TOPIC,
durable=True
)
# Use actual tenant ID
tenant_id = 'c464fb3e-7af2-46e6-9e43-85318f34199a'
print("🚨 Sending out of stock alert...")
# Out of Stock Alert
out_of_stock_alert = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'alert',
'type': 'out_of_stock',
'severity': 'urgent',
'service': 'inventory-service',
'title': '🚨 PRODUCTO AGOTADO: Harina de Trigo',
'message': 'El ingrediente "Harina de Trigo" está completamente agotado. Stock actual: 0 kg. Este ingrediente es crítico para la producción diaria y afecta 8 productos principales.',
'actions': [
'Contactar proveedor inmediatamente',
'Revisar productos afectados',
'Activar stock de emergencia si disponible',
'Notificar al equipo de producción',
'Actualizar previsiones de ventas'
],
'metadata': {
'ingredient_name': 'Harina de Trigo',
'current_stock': 0,
'unit': 'kg',
'minimum_stock': 50,
'critical_ingredient': True,
'affected_products': [
'Pan Francés',
'Croissants',
'Pan Integral',
'Baguettes',
'Pan de Molde',
'Empanadas',
'Pizza',
'Focaccia'
],
'supplier': 'Molinos San Andrés',
'last_order_date': '2024-01-15',
'estimated_restock_time': '24-48 horas',
'impact_level': 'HIGH'
},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(out_of_stock_alert).encode()),
routing_key='alert.urgent.inventory-service'
)
print(f"✅ Out of stock alert sent: {out_of_stock_alert['title']}")
print(f" Alert ID: {out_of_stock_alert['id']}")
print(f" Severity: {out_of_stock_alert['severity'].upper()}")
print(f" Service: {out_of_stock_alert['service']}")
print(f" Affected products: {len(out_of_stock_alert['metadata']['affected_products'])}")
await connection.close()
print("\n🎯 Out of stock alert test completed!")
print("Check your frontend for:")
print(" • Urgent notification in header (red badge)")
print(" • Real-time alert in dashboard panel")
print(" • Toast notification (urgent priority)")
print(" • Expandable card with ingredient details and actions")
if __name__ == "__main__":
asyncio.run(send_out_of_stock_alert())

View File

@@ -0,0 +1,95 @@
#!/usr/bin/env python3
"""
Recommendation Alert Test
Sends a realistic optimization recommendation to test the AI recommendations system
"""
import aio_pika
import asyncio
import json
import uuid
from datetime import datetime
async def send_recommendation_alert():
"""Send a realistic optimization recommendation"""
# Connect to RabbitMQ
connection = await aio_pika.connect_robust('amqp://bakery:forecast123@rabbitmq:5672/')
channel = await connection.channel()
# Declare the alerts exchange
exchange = await channel.declare_exchange(
'alerts.exchange',
aio_pika.ExchangeType.TOPIC,
durable=True
)
# Use actual tenant ID
tenant_id = 'c464fb3e-7af2-46e6-9e43-85318f34199a'
print("💡 Sending optimization recommendation...")
# Optimization Recommendation
recommendation = {
'id': str(uuid.uuid4()),
'tenant_id': tenant_id,
'item_type': 'recommendation',
'type': 'demand_optimization',
'severity': 'medium',
'service': 'ai-forecast-service',
'title': '💡 OPTIMIZACIÓN: Ajustar Producción de Croissants',
'message': 'Nuestro análisis de IA detectó que la demanda de croissants aumenta un 35% los viernes. Se recomienda incrementar la producción para maximizar ventas y reducir desperdicio.',
'actions': [
'Revisar análisis de demanda detallado',
'Ajustar plan de producción para viernes',
'Evaluar capacidad de horno disponible',
'Calcular ROI del incremento propuesto',
'Implementar cambio gradualmente',
'Monitorear resultados por 2 semanas'
],
'metadata': {
'product_name': 'Croissants',
'current_friday_production': 120,
'recommended_friday_production': 162,
'increase_percentage': 35,
'confidence_score': 0.87,
'data_period_analyzed': '3 meses',
'potential_revenue_increase': 280.50,
'currency': 'EUR',
'historical_sellout_rate': 0.95,
'waste_reduction_potential': '15%',
'implementation_effort': 'LOW',
'roi_timeframe': '2 semanas',
'seasonal_factors': [
'Viernes laboral',
'Proximidad fin de semana',
'Horario de desayuno extendido'
],
'risk_level': 'BAJO'
},
'timestamp': datetime.utcnow().isoformat()
}
await exchange.publish(
aio_pika.Message(json.dumps(recommendation).encode()),
routing_key='recommendation.medium.ai-forecast-service'
)
print(f"✅ Recommendation sent: {recommendation['title']}")
print(f" Recommendation ID: {recommendation['id']}")
print(f" Severity: {recommendation['severity'].upper()}")
print(f" Service: {recommendation['service']}")
print(f" Confidence: {recommendation['metadata']['confidence_score']*100:.0f}%")
print(f" Potential revenue increase: €{recommendation['metadata']['potential_revenue_increase']}")
await connection.close()
print("\n🎯 Recommendation test completed!")
print("Check your frontend for:")
print(" • Medium priority notification in header")
print(" • Real-time recommendation in dashboard panel")
print(" • Blue info badge (recommendation type)")
print(" • Expandable card with AI analysis and business impact")
if __name__ == "__main__":
asyncio.run(send_recommendation_alert())