New alert service

This commit is contained in:
Urtzi Alfaro
2025-12-05 20:07:01 +01:00
parent 1fe3a73549
commit 667e6e0404
393 changed files with 26002 additions and 61033 deletions

View File

@@ -65,9 +65,10 @@ DELIVERY_WEEKDAYS = [0, 2, 4] # Monday, Wednesday, Friday
async def seed_distribution_history(db: AsyncSession):
"""
Seed 30 days of historical distribution data (routes + shipments)
Seed 30 days of distribution data (routes + shipments) centered around BASE_REFERENCE_DATE
Creates delivery routes for Mon/Wed/Fri pattern going back 30 days from BASE_REFERENCE_DATE
Creates delivery routes for Mon/Wed/Fri pattern spanning from 15 days before to 15 days after BASE_REFERENCE_DATE.
This ensures data exists for today when BASE_REFERENCE_DATE is set to the current date.
"""
logger.info("=" * 80)
logger.info("🚚 Starting Demo Distribution History Seeding")
@@ -75,15 +76,18 @@ async def seed_distribution_history(db: AsyncSession):
logger.info(f"Parent Tenant: {DEMO_TENANT_ENTERPRISE_CHAIN} (Obrador Madrid)")
logger.info(f"Child Tenants: {len(CHILD_TENANTS)}")
logger.info(f"Delivery Pattern: Mon/Wed/Fri (3x per week)")
logger.info(f"History: 30 days from {BASE_REFERENCE_DATE}")
logger.info(f"Date Range: {(BASE_REFERENCE_DATE - timedelta(days=15)).strftime('%Y-%m-%d')} to {(BASE_REFERENCE_DATE + timedelta(days=15)).strftime('%Y-%m-%d')}")
logger.info(f"Reference Date (today): {BASE_REFERENCE_DATE.strftime('%Y-%m-%d')}")
logger.info("")
routes_created = 0
shipments_created = 0
# Generate 30 days of historical routes (working backwards from BASE_REFERENCE_DATE)
for days_ago in range(30, 0, -1):
delivery_date = BASE_REFERENCE_DATE - timedelta(days=days_ago)
# Generate 30 days of routes centered around BASE_REFERENCE_DATE (-15 to +15 days)
# This ensures we have past data, current data, and future data
# Range is inclusive of start, exclusive of end, so -15 to 16 gives -15..15
for days_offset in range(-15, 16): # -15 to +15 = 31 days total
delivery_date = BASE_REFERENCE_DATE + timedelta(days=days_offset)
# Only create routes for Mon/Wed/Fri
if delivery_date.weekday() not in DELIVERY_WEEKDAYS:
@@ -117,6 +121,11 @@ async def seed_distribution_history(db: AsyncSession):
{"stop": 3, "tenant_id": str(DEMO_TENANT_CHILD_3), "location": "Valencia Ruzafa"}
]
# Determine status based on whether the date is in the past or future
# Past routes are completed, today and future routes are planned
is_past = delivery_date < BASE_REFERENCE_DATE
route_status = DeliveryRouteStatus.completed if is_past else DeliveryRouteStatus.planned
route = DeliveryRoute(
id=uuid.uuid4(),
tenant_id=DEMO_TENANT_ENTERPRISE_CHAIN,
@@ -125,7 +134,7 @@ async def seed_distribution_history(db: AsyncSession):
total_distance_km=Decimal(str(round(total_distance_km, 2))),
estimated_duration_minutes=estimated_duration_minutes,
route_sequence=route_sequence,
status=DeliveryRouteStatus.completed if days_ago > 1 else DeliveryRouteStatus.planned, # Recent routes are planned, old ones completed
status=route_status,
driver_id=uuid.uuid4(), # Use a random UUID for the driver_id
vehicle_id=f"VEH-{random.choice(['001', '002', '003'])}",
created_at=delivery_date - timedelta(days=1), # Routes created day before
@@ -144,6 +153,9 @@ async def seed_distribution_history(db: AsyncSession):
shipment_number = f"DEMOSHP-{delivery_date.strftime('%Y%m%d')}-{child_name.split()[0].upper()[:3]}"
# Determine shipment status based on date
shipment_status = ShipmentStatus.delivered if is_past else ShipmentStatus.pending
shipment = Shipment(
id=uuid.uuid4(),
tenant_id=DEMO_TENANT_ENTERPRISE_CHAIN,
@@ -151,7 +163,7 @@ async def seed_distribution_history(db: AsyncSession):
child_tenant_id=child_tenant_id,
shipment_number=shipment_number,
shipment_date=delivery_date,
status=ShipmentStatus.delivered if days_ago > 1 else ShipmentStatus.pending,
status=shipment_status,
total_weight_kg=Decimal(str(round(shipment_weight, 2))),
delivery_route_id=route.id,
delivery_notes=f"Entrega regular a {child_name}",