demo seed change

This commit is contained in:
Urtzi Alfaro
2025-12-13 23:57:54 +01:00
parent f3688dfb04
commit ff830a3415
299 changed files with 20328 additions and 19485 deletions

View File

@@ -13,6 +13,7 @@ from typing import Optional
import os
from decimal import Decimal
import sys
import json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
@@ -24,7 +25,7 @@ from app.models.sales import SalesData
from app.core.config import settings
logger = structlog.get_logger()
router = APIRouter(prefix="/internal/demo", tags=["internal"])
router = APIRouter()
# Base demo tenant IDs
DEMO_TENANT_PROFESSIONAL = "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"
@@ -38,7 +39,7 @@ def verify_internal_api_key(x_internal_api_key: Optional[str] = Header(None)):
return True
@router.post("/clone")
@router.post("/internal/demo/clone")
async def clone_demo_data(
base_tenant_id: str,
virtual_tenant_id: str,
@@ -102,46 +103,71 @@ async def clone_demo_data(
"sales_records": 0,
}
# Clone Sales Data
result = await db.execute(
select(SalesData).where(SalesData.tenant_id == base_uuid)
)
base_sales = result.scalars().all()
# Load seed data from JSON files instead of cloning from database
try:
from shared.utils.seed_data_paths import get_seed_data_path
if demo_account_type == "professional":
json_file = get_seed_data_path("professional", "09-sales.json")
elif demo_account_type == "enterprise":
json_file = get_seed_data_path("enterprise", "09-sales.json")
else:
raise ValueError(f"Invalid demo account type: {demo_account_type}")
except ImportError:
# Fallback to original path
seed_data_dir = Path(__file__).parent.parent.parent.parent / "infrastructure" / "seed-data"
if demo_account_type == "professional":
json_file = seed_data_dir / "professional" / "09-sales.json"
elif demo_account_type == "enterprise":
json_file = seed_data_dir / "enterprise" / "parent" / "09-sales.json"
else:
raise ValueError(f"Invalid demo account type: {demo_account_type}")
if not json_file.exists():
raise HTTPException(
status_code=404,
detail=f"Seed data file not found: {json_file}"
)
# Load JSON data
with open(json_file, 'r', encoding='utf-8') as f:
seed_data = json.load(f)
logger.info(
"Found sales records to clone",
count=len(base_sales),
base_tenant=str(base_uuid)
"Loaded sales seed data",
sales_records=len(seed_data.get('sales_data', []))
)
for sale in base_sales:
# Load Sales Data from seed data
for sale_data in seed_data.get('sales_data', []):
# Adjust date using the shared utility
adjusted_date = adjust_date_for_demo(
sale.date,
datetime.fromisoformat(sale_data['sale_date'].replace('Z', '+00:00')),
session_time,
BASE_REFERENCE_DATE
) if sale.date else None
) if sale_data.get('sale_date') else None
# Create new sales record with adjusted date
new_sale = SalesData(
id=uuid.uuid4(),
tenant_id=virtual_uuid,
date=adjusted_date,
inventory_product_id=sale.inventory_product_id, # Keep same product refs
quantity_sold=sale.quantity_sold,
unit_price=sale.unit_price,
revenue=sale.revenue,
cost_of_goods=sale.cost_of_goods,
discount_applied=sale.discount_applied,
location_id=sale.location_id,
sales_channel=sale.sales_channel,
source="demo_clone", # Mark as cloned
is_validated=sale.is_validated,
validation_notes=sale.validation_notes,
notes=sale.notes,
weather_condition=sale.weather_condition,
is_holiday=sale.is_holiday,
is_weekend=sale.is_weekend,
inventory_product_id=sale_data.get('product_id'), # Use product_id from seed data
quantity_sold=sale_data.get('quantity_sold', 0.0),
unit_price=sale_data.get('unit_price', 0.0),
revenue=sale_data.get('total_revenue', 0.0),
cost_of_goods=sale_data.get('cost_of_goods', 0.0),
discount_applied=sale_data.get('discount_applied', 0.0),
location_id=sale_data.get('location_id'),
sales_channel=sale_data.get('sales_channel', 'IN_STORE'),
source="demo_seed", # Mark as seeded
is_validated=sale_data.get('is_validated', True),
validation_notes=sale_data.get('validation_notes'),
notes=sale_data.get('notes'),
weather_condition=sale_data.get('weather_condition'),
is_holiday=sale_data.get('is_holiday', False),
is_weekend=sale_data.get('is_weekend', False),
created_at=session_time,
updated_at=session_time
)

View File

@@ -10,7 +10,7 @@ from app.core.database import database_manager
from shared.service_base import StandardFastAPIService
# Import API routers
from app.api import sales_records, sales_operations, analytics, internal_demo, audit, batch
from app.api import sales_records, sales_operations, analytics, audit, batch, internal_demo
class SalesService(StandardFastAPIService):
@@ -151,4 +151,4 @@ service.add_router(batch.router)
service.add_router(sales_records.router)
service.add_router(sales_operations.router)
service.add_router(analytics.router)
service.add_router(internal_demo.router)
service.add_router(internal_demo.router, tags=["internal-demo"])

View File

@@ -1,349 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Sales Seeding Script for Sales Service
Creates realistic historical sales data for demo template tenants
This script runs as a Kubernetes init job inside the sales-service container.
It populates the template tenants with historical sales data.
Usage:
python /app/scripts/demo/seed_demo_sales.py
Environment Variables Required:
SALES_DATABASE_URL - PostgreSQL connection string for sales database
INVENTORY_DATABASE_URL - PostgreSQL connection string for inventory database (to lookup products)
DEMO_MODE - Set to 'production' for production seeding
LOG_LEVEL - Logging level (default: INFO)
"""
import asyncio
import uuid
import sys
import os
from datetime import datetime, timezone, timedelta
from pathlib import Path
import random
from decimal import Decimal
# Add app to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select, text
import structlog
from app.models.sales import SalesData
# Configure logging
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.dev.ConsoleRenderer()
]
)
logger = structlog.get_logger()
# Fixed Demo Tenant IDs (must match tenant service)
DEMO_TENANT_PROFESSIONAL = uuid.UUID("a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6")
# Hardcoded product IDs from ingredientes_es.json (finished products)
PRODUCT_IDS = {
"PRO-BAG-001": "20000000-0000-0000-0000-000000000001", # Baguette Tradicional
"PRO-CRO-001": "20000000-0000-0000-0000-000000000002", # Croissant de Mantequilla
"PRO-PUE-001": "20000000-0000-0000-0000-000000000003", # Pan de Pueblo
"PRO-NAP-001": "20000000-0000-0000-0000-000000000004", # Napolitana de Chocolate
}
# Sample product SKUs and their typical sales patterns
SAN_PABLO_PRODUCTS = [
{"sku": "PRO-BAG-001", "name": "Baguette Tradicional", "avg_qty": 80, "variance": 15, "price": 1.20},
{"sku": "PRO-CRO-001", "name": "Croissant de Mantequilla", "avg_qty": 50, "variance": 10, "price": 1.50},
{"sku": "PRO-PUE-001", "name": "Pan de Pueblo", "avg_qty": 20, "variance": 5, "price": 3.50},
{"sku": "PRO-NAP-001", "name": "Napolitana de Chocolate", "avg_qty": 35, "variance": 8, "price": 1.80},
]
LA_ESPIGA_PRODUCTS = [
{"sku": "PRO-BAG-001", "name": "Baguette Tradicional", "avg_qty": 500, "variance": 80, "price": 0.90},
{"sku": "PRO-CRO-001", "name": "Croissant de Mantequilla", "avg_qty": 300, "variance": 50, "price": 1.10},
{"sku": "PRO-PUE-001", "name": "Pan de Pueblo", "avg_qty": 100, "variance": 20, "price": 2.80},
{"sku": "PRO-NAP-001", "name": "Napolitana de Chocolate", "avg_qty": 200, "variance": 40, "price": 1.40},
]
def get_product_by_sku(tenant_id: uuid.UUID, sku: str, product_name: str):
"""
Get tenant-specific product ID using hardcoded base IDs (no database lookup needed)
Args:
tenant_id: Tenant UUID
sku: Product SKU code
product_name: Product name
Returns:
Tuple of (product_id, product_name) or (None, None) if not found
"""
if sku not in PRODUCT_IDS:
return None, None
# Generate tenant-specific product ID (same as inventory seed script)
base_product_id = uuid.UUID(PRODUCT_IDS[sku])
tenant_int = int(tenant_id.hex, 16)
product_id = uuid.UUID(int=tenant_int ^ int(base_product_id.hex, 16))
return product_id, product_name
async def seed_sales_for_tenant(
sales_db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
product_patterns: list,
days_of_history: int = 90
) -> dict:
"""
Seed sales data for a specific tenant
Args:
sales_db: Sales database session
tenant_id: UUID of the tenant
tenant_name: Name of the tenant (for logging)
product_patterns: List of product sales patterns
days_of_history: Number of days of historical data to generate
Returns:
Dict with seeding statistics
"""
logger.info("" * 80)
logger.info(f"Seeding sales data for: {tenant_name}")
logger.info(f"Tenant ID: {tenant_id}")
logger.info(f"Days of history: {days_of_history}")
logger.info("" * 80)
created_sales = 0
skipped_sales = 0
# Generate sales data for each day
for days_ago in range(days_of_history, 0, -1):
sale_date = datetime.now(timezone.utc) - timedelta(days=days_ago)
# Skip some random days to simulate closures
if random.random() < 0.05: # 5% chance of being closed
continue
# For each product, generate sales
for product_pattern in product_patterns:
sku = product_pattern["sku"]
product_name = product_pattern["name"]
# Get tenant-specific product ID using hardcoded base IDs
product_id, product_name = get_product_by_sku(tenant_id, sku, product_name)
if not product_id:
logger.warning(f" ⚠️ Product not found: {sku}")
continue
# Check if sales record already exists
result = await sales_db.execute(
select(SalesData).where(
SalesData.tenant_id == tenant_id,
SalesData.inventory_product_id == product_id,
SalesData.date == sale_date
)
)
existing = result.scalars().first()
if existing:
skipped_sales += 1
continue
# Calculate sales quantity with variance
avg_qty = product_pattern["avg_qty"]
variance = product_pattern["variance"]
# Add weekly patterns (weekends sell more)
weekday = sale_date.weekday()
if weekday in [5, 6]: # Saturday, Sunday
multiplier = random.uniform(1.2, 1.5)
else:
multiplier = random.uniform(0.8, 1.2)
quantity = max(0, int((avg_qty + random.uniform(-variance, variance)) * multiplier))
if quantity == 0:
continue
# Calculate revenue
unit_price = Decimal(str(product_pattern["price"]))
revenue = Decimal(str(quantity)) * unit_price
# Check if it's a weekend
is_weekend = weekday in [5, 6]
# Create sales record
sales_record = SalesData(
id=uuid.uuid4(),
tenant_id=tenant_id,
inventory_product_id=product_id,
date=sale_date,
quantity_sold=quantity,
revenue=revenue,
unit_price=unit_price,
sales_channel="in_store",
location_id="main",
source="demo_seed",
is_weekend=is_weekend,
created_at=sale_date,
updated_at=sale_date
)
sales_db.add(sales_record)
created_sales += 1
# Commit all changes for this tenant
await sales_db.commit()
logger.info(f" 📊 Created: {created_sales}, Skipped: {skipped_sales}")
logger.info("")
return {
"tenant_id": str(tenant_id),
"tenant_name": tenant_name,
"sales_records_created": created_sales,
"sales_records_skipped": skipped_sales,
"days_of_history": days_of_history
}
async def seed_sales(sales_db: AsyncSession):
"""
Seed sales for all demo template tenants
Args:
sales_db: Sales database session
Returns:
Dict with overall seeding statistics
"""
logger.info("=" * 80)
logger.info("💰 Starting Demo Sales Seeding")
logger.info("=" * 80)
results = []
# Seed for San Pablo (Traditional Bakery) - 30 days of history (optimized for fast demo loading)
logger.info("")
result_san_pablo = await seed_sales_for_tenant(
sales_db,
DEMO_TENANT_PROFESSIONAL,
"Panadería Professional Bakery",
SAN_PABLO_PRODUCTS,
days_of_history=30
)
results.append(result_san_pablo)
# Calculate totals
total_sales = sum(r["sales_records_created"] for r in results)
total_skipped = sum(r["sales_records_skipped"] for r in results)
logger.info("=" * 80)
logger.info("✅ Demo Sales Seeding Completed")
logger.info("=" * 80)
return {
"service": "sales",
"tenants_seeded": len(results),
"total_sales_created": total_sales,
"total_skipped": total_skipped,
"results": results
}
async def main():
"""Main execution function"""
logger.info("Demo Sales Seeding Script Starting")
logger.info("Mode: %s", os.getenv("DEMO_MODE", "development"))
logger.info("Log Level: %s", os.getenv("LOG_LEVEL", "INFO"))
# Get database URL from environment
sales_database_url = os.getenv("SALES_DATABASE_URL") or os.getenv("DATABASE_URL")
if not sales_database_url:
logger.error("❌ SALES_DATABASE_URL or DATABASE_URL environment variable must be set")
return 1
# Convert to async URLs if needed
if sales_database_url.startswith("postgresql://"):
sales_database_url = sales_database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
logger.info("Connecting to sales database")
# Create engine and session
sales_engine = create_async_engine(
sales_database_url,
echo=False,
pool_pre_ping=True,
pool_size=5,
max_overflow=10
)
sales_session_maker = sessionmaker(
sales_engine,
class_=AsyncSession,
expire_on_commit=False
)
try:
async with sales_session_maker() as sales_session:
result = await seed_sales(sales_session)
logger.info("")
logger.info("📊 Seeding Summary:")
logger.info(f" ✅ Tenants seeded: {result['tenants_seeded']}")
logger.info(f" ✅ Sales records created: {result['total_sales_created']}")
logger.info(f" ⏭️ Skipped: {result['total_skipped']}")
logger.info("")
# Print per-tenant details
for tenant_result in result['results']:
logger.info(
f" {tenant_result['tenant_name']}: "
f"{tenant_result['sales_records_created']} sales records "
f"({tenant_result['days_of_history']} days)"
)
logger.info("")
logger.info("🎉 Success! Sales history is ready for cloning.")
logger.info("")
logger.info("Sales data includes:")
logger.info(" • 30 days of historical sales (optimized for demo performance)")
logger.info(" • 4 product types per tenant")
logger.info(" • Realistic weekly patterns (higher on weekends)")
logger.info(" • Random variance and occasional closures")
logger.info("")
logger.info("Next steps:")
logger.info(" 1. Run seed jobs for other services (orders, production, etc.)")
logger.info(" 2. Verify sales data in database")
logger.info(" 3. Test demo session creation with sales cloning")
logger.info("")
return 0
except Exception as e:
logger.error("=" * 80)
logger.error("❌ Demo Sales Seeding Failed")
logger.error("=" * 80)
logger.error("Error: %s", str(e))
logger.error("", exc_info=True)
return 1
finally:
await sales_engine.dispose()
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)

View File

@@ -1,381 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demo Retail Sales Seeding Script for Sales Service
Creates realistic historical sales data for child retail outlets
This script runs as a Kubernetes init job inside the sales-service container.
It populates child retail tenants with 30 days of sales history.
Usage:
python /app/scripts/demo/seed_demo_sales_retail.py
Environment Variables Required:
SALES_DATABASE_URL - PostgreSQL connection string for sales database
DEMO_MODE - Set to 'production' for production seeding
LOG_LEVEL - Logging level (default: INFO)
"""
import asyncio
import uuid
import sys
import os
from datetime import datetime, timezone, timedelta
from pathlib import Path
import random
from decimal import Decimal
# Add app to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
# Add shared to path for demo utilities
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent.parent))
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
import structlog
from shared.utils.demo_dates import BASE_REFERENCE_DATE
from app.models.sales import SalesData
# Configure logging
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.dev.ConsoleRenderer()
]
)
logger = structlog.get_logger()
# Fixed Demo Tenant IDs (must match tenant service)
DEMO_TENANT_CHILD_1 = uuid.UUID("d4e5f6a7-b8c9-40d1-e2f3-a4b5c6d7e8f9") # Madrid Centro
DEMO_TENANT_CHILD_2 = uuid.UUID("e5f6a7b8-c9d0-41e2-f3a4-b5c6d7e8f9a0") # Barcelona Gràcia
DEMO_TENANT_CHILD_3 = uuid.UUID("f6a7b8c9-d0e1-42f3-a4b5-c6d7e8f9a0b1") # Valencia Ruzafa
# Hardcoded product IDs from ingredientes_es.json (finished products)
PRODUCT_IDS = {
"PRO-BAG-001": "20000000-0000-0000-0000-000000000001", # Baguette Tradicional
"PRO-CRO-001": "20000000-0000-0000-0000-000000000002", # Croissant de Mantequilla
"PRO-PUE-001": "20000000-0000-0000-0000-000000000003", # Pan de Pueblo
"PRO-NAP-001": "20000000-0000-0000-0000-000000000004", # Napolitana de Chocolate
}
# Retail sales patterns for each store
# Madrid Centro - Large urban store, high traffic
MADRID_CENTRO_PRODUCTS = [
{"sku": "PRO-BAG-001", "name": "Baguette Tradicional", "avg_qty": 120, "variance": 20, "price": 1.30},
{"sku": "PRO-CRO-001", "name": "Croissant de Mantequilla", "avg_qty": 80, "variance": 15, "price": 1.60},
{"sku": "PRO-PUE-001", "name": "Pan de Pueblo", "avg_qty": 35, "variance": 8, "price": 3.80},
{"sku": "PRO-NAP-001", "name": "Napolitana de Chocolate", "avg_qty": 60, "variance": 12, "price": 1.90},
]
# Barcelona Gràcia - Medium neighborhood store
BARCELONA_GRACIA_PRODUCTS = [
{"sku": "PRO-BAG-001", "name": "Baguette Tradicional", "avg_qty": 90, "variance": 15, "price": 1.25},
{"sku": "PRO-CRO-001", "name": "Croissant de Mantequilla", "avg_qty": 60, "variance": 12, "price": 1.55},
{"sku": "PRO-PUE-001", "name": "Pan de Pueblo", "avg_qty": 25, "variance": 6, "price": 3.70},
{"sku": "PRO-NAP-001", "name": "Napolitana de Chocolate", "avg_qty": 45, "variance": 10, "price": 1.85},
]
# Valencia Ruzafa - Smaller boutique store
VALENCIA_RUZAFA_PRODUCTS = [
{"sku": "PRO-BAG-001", "name": "Baguette Tradicional", "avg_qty": 70, "variance": 12, "price": 1.20},
{"sku": "PRO-CRO-001", "name": "Croissant de Mantequilla", "avg_qty": 45, "variance": 10, "price": 1.50},
{"sku": "PRO-PUE-001", "name": "Pan de Pueblo", "avg_qty": 20, "variance": 5, "price": 3.60},
{"sku": "PRO-NAP-001", "name": "Napolitana de Chocolate", "avg_qty": 35, "variance": 8, "price": 1.80},
]
# Child tenant configurations
CHILD_TENANTS = [
(DEMO_TENANT_CHILD_1, "Madrid Centro", MADRID_CENTRO_PRODUCTS),
(DEMO_TENANT_CHILD_2, "Barcelona Gràcia", BARCELONA_GRACIA_PRODUCTS),
(DEMO_TENANT_CHILD_3, "Valencia Ruzafa", VALENCIA_RUZAFA_PRODUCTS)
]
def get_product_by_sku(tenant_id: uuid.UUID, sku: str, product_name: str):
"""
Get tenant-specific product ID using XOR transformation
Args:
tenant_id: Tenant UUID
sku: Product SKU code
product_name: Product name
Returns:
Tuple of (product_id, product_name) or (None, None) if not found
"""
if sku not in PRODUCT_IDS:
return None, None
# Generate tenant-specific product ID using XOR (same as inventory seed script)
base_product_id = uuid.UUID(PRODUCT_IDS[sku])
tenant_int = int(tenant_id.hex, 16)
product_id = uuid.UUID(int=tenant_int ^ int(base_product_id.hex, 16))
return product_id, product_name
async def seed_retail_sales_for_tenant(
db: AsyncSession,
tenant_id: uuid.UUID,
tenant_name: str,
product_patterns: list,
days_of_history: int = 30
) -> dict:
"""
Seed retail sales data for a specific child tenant
Args:
db: Database session
tenant_id: UUID of the child tenant
tenant_name: Name of the tenant (for logging)
product_patterns: List of product sales patterns
days_of_history: Number of days of historical data to generate (default: 30)
Returns:
Dict with seeding statistics
"""
logger.info("" * 80)
logger.info(f"Seeding retail sales data for: {tenant_name}")
logger.info(f"Tenant ID: {tenant_id}")
logger.info(f"Days of history: {days_of_history}")
logger.info("" * 80)
created_sales = 0
skipped_sales = 0
# Generate sales data for each day (working backwards from BASE_REFERENCE_DATE)
for days_ago in range(days_of_history, 0, -1):
sale_date = BASE_REFERENCE_DATE - timedelta(days=days_ago)
# Skip some random days to simulate closures/holidays (3% chance)
if random.random() < 0.03:
continue
# For each product, generate sales
for product_pattern in product_patterns:
sku = product_pattern["sku"]
product_name = product_pattern["name"]
# Get tenant-specific product ID using XOR transformation
product_id, product_name = get_product_by_sku(tenant_id, sku, product_name)
if not product_id:
logger.warning(f" ⚠️ Product not found: {sku}")
continue
# Check if sales record already exists
result = await db.execute(
select(SalesData).where(
SalesData.tenant_id == tenant_id,
SalesData.inventory_product_id == product_id,
SalesData.date == sale_date
)
)
existing = result.scalars().first()
if existing:
skipped_sales += 1
continue
# Calculate sales quantity with realistic variance
avg_qty = product_pattern["avg_qty"]
variance = product_pattern["variance"]
# Add weekly patterns (weekends sell more for bakeries)
weekday = sale_date.weekday()
if weekday in [5, 6]: # Saturday, Sunday
multiplier = random.uniform(1.3, 1.6) # 30-60% more sales on weekends
elif weekday == 4: # Friday
multiplier = random.uniform(1.1, 1.3) # 10-30% more on Fridays
else: # Weekdays
multiplier = random.uniform(0.85, 1.15)
quantity = max(0, int((avg_qty + random.uniform(-variance, variance)) * multiplier))
if quantity == 0:
continue
# Calculate revenue
unit_price = Decimal(str(product_pattern["price"]))
revenue = Decimal(str(quantity)) * unit_price
# Determine if weekend
is_weekend = weekday in [5, 6]
# Create sales record
sales_record = SalesData(
id=uuid.uuid4(),
tenant_id=tenant_id,
inventory_product_id=product_id,
date=sale_date,
quantity_sold=quantity,
revenue=revenue,
unit_price=unit_price,
sales_channel="in_store", # Retail outlets primarily use in-store sales
location_id="main", # Single location per retail outlet
source="demo_seed",
is_weekend=is_weekend,
created_at=sale_date,
updated_at=sale_date
)
db.add(sales_record)
created_sales += 1
logger.debug(
f"{sale_date.strftime('%Y-%m-%d')}: {product_name} - "
f"{quantity} units @ €{unit_price} = €{revenue:.2f}"
)
# Commit all changes for this tenant
await db.commit()
logger.info(f" 📊 Sales records created: {created_sales}, Skipped: {skipped_sales}")
logger.info("")
return {
"tenant_id": str(tenant_id),
"tenant_name": tenant_name,
"sales_created": created_sales,
"sales_skipped": skipped_sales,
"days_of_history": days_of_history
}
async def seed_retail_sales(db: AsyncSession):
"""
Seed retail sales for all child tenant templates
Args:
db: Database session
Returns:
Dict with overall seeding statistics
"""
logger.info("=" * 80)
logger.info("💰 Starting Demo Retail Sales Seeding")
logger.info("=" * 80)
logger.info("Creating 30 days of sales history for retail outlets")
logger.info("")
results = []
# Seed for each child retail outlet
for child_tenant_id, child_tenant_name, product_patterns in CHILD_TENANTS:
logger.info("")
result = await seed_retail_sales_for_tenant(
db,
child_tenant_id,
f"{child_tenant_name} (Retail Outlet)",
product_patterns,
days_of_history=30 # 30 days of sales history
)
results.append(result)
# Calculate totals
total_sales = sum(r["sales_created"] for r in results)
total_skipped = sum(r["sales_skipped"] for r in results)
logger.info("=" * 80)
logger.info("✅ Demo Retail Sales Seeding Completed")
logger.info("=" * 80)
return {
"service": "sales_retail",
"tenants_seeded": len(results),
"total_sales_created": total_sales,
"total_skipped": total_skipped,
"results": results
}
async def main():
"""Main execution function"""
logger.info("Demo Retail Sales Seeding Script Starting")
logger.info("Mode: %s", os.getenv("DEMO_MODE", "development"))
logger.info("Log Level: %s", os.getenv("LOG_LEVEL", "INFO"))
# Get database URL from environment
database_url = os.getenv("SALES_DATABASE_URL") or os.getenv("DATABASE_URL")
if not database_url:
logger.error("❌ SALES_DATABASE_URL or DATABASE_URL environment variable must be set")
return 1
# Convert to async URL if needed
if database_url.startswith("postgresql://"):
database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
logger.info("Connecting to sales database")
# Create engine and session
engine = create_async_engine(
database_url,
echo=False,
pool_pre_ping=True,
pool_size=5,
max_overflow=10
)
async_session = sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False
)
try:
async with async_session() as session:
result = await seed_retail_sales(session)
logger.info("")
logger.info("📊 Retail Sales Seeding Summary:")
logger.info(f" ✅ Retail outlets seeded: {result['tenants_seeded']}")
logger.info(f" ✅ Total sales records: {result['total_sales_created']}")
logger.info(f" ⏭️ Total skipped: {result['total_skipped']}")
logger.info("")
# Print per-tenant details
for tenant_result in result['results']:
logger.info(
f" {tenant_result['tenant_name']}: "
f"{tenant_result['sales_created']} sales records"
)
logger.info("")
logger.info("🎉 Success! Retail sales history is ready for cloning.")
logger.info("")
logger.info("Sales characteristics:")
logger.info(" ✓ 30 days of historical data")
logger.info(" ✓ Weekend sales boost (30-60% higher)")
logger.info(" ✓ Friday pre-weekend surge (10-30% higher)")
logger.info(" ✓ Realistic variance per product")
logger.info(" ✓ Store-specific pricing and volumes")
logger.info("")
logger.info("Next steps:")
logger.info(" 1. Seed customer data")
logger.info(" 2. Seed retail orders (internal transfers from parent)")
logger.info(" 3. Test forecasting with retail sales data")
logger.info("")
return 0
except Exception as e:
logger.error("=" * 80)
logger.error("❌ Demo Retail Sales Seeding Failed")
logger.error("=" * 80)
logger.error("Error: %s", str(e))
logger.error("", exc_info=True)
return 1
finally:
await engine.dispose()
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)