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

@@ -27,6 +27,7 @@ from app.schemas.purchase_order_schemas import (
)
from shared.routing import RouteBuilder
from shared.auth.decorators import get_current_user_dep
from app.utils.cache import get_cached, set_cached, make_cache_key
import structlog
logger = structlog.get_logger()
@@ -123,10 +124,11 @@ async def list_purchase_orders(
limit: int = Query(default=50, ge=1, le=100),
supplier_id: Optional[str] = Query(default=None),
status: Optional[str] = Query(default=None),
enrich_supplier: bool = Query(default=True, description="Include supplier details (slower)"),
service: PurchaseOrderService = Depends(get_po_service)
):
"""
List purchase orders with filters
List purchase orders with filters and caching (30s TTL)
Args:
tenant_id: Tenant UUID
@@ -134,20 +136,46 @@ async def list_purchase_orders(
limit: Maximum number of records to return
supplier_id: Filter by supplier ID (optional)
status: Filter by status (optional)
enrich_supplier: Whether to enrich with supplier data (default: True)
Returns:
List of purchase orders
"""
try:
# PERFORMANCE OPTIMIZATION: Cache even with status filter for dashboard queries
# Only skip cache for supplier_id filter and pagination (skip > 0)
cache_key = None
if skip == 0 and supplier_id is None:
cache_key = make_cache_key(
"purchase_orders",
tenant_id,
limit=limit,
status=status, # Include status in cache key
enrich_supplier=enrich_supplier
)
cached_result = await get_cached(cache_key)
if cached_result is not None:
logger.debug("Cache hit for purchase orders", cache_key=cache_key, tenant_id=tenant_id, status=status)
return [PurchaseOrderResponse(**po) for po in cached_result]
# Cache miss - fetch from database
pos = await service.list_purchase_orders(
tenant_id=uuid.UUID(tenant_id),
skip=skip,
limit=limit,
supplier_id=uuid.UUID(supplier_id) if supplier_id else None,
status=status
status=status,
enrich_supplier=enrich_supplier
)
return [PurchaseOrderResponse.model_validate(po) for po in pos]
result = [PurchaseOrderResponse.model_validate(po) for po in pos]
# PERFORMANCE OPTIMIZATION: Cache the result (20s TTL for purchase orders)
if cache_key:
await set_cached(cache_key, [po.model_dump() for po in result], ttl=20)
logger.debug("Cached purchase orders", cache_key=cache_key, ttl=20, tenant_id=tenant_id, status=status)
return result
except Exception as e:
logger.error("Error listing purchase orders", error=str(e), tenant_id=tenant_id)