Add more services
This commit is contained in:
715
services/inventory/app/services/dashboard_service.py
Normal file
715
services/inventory/app/services/dashboard_service.py
Normal file
@@ -0,0 +1,715 @@
|
||||
# ================================================================
|
||||
# services/inventory/app/services/dashboard_service.py
|
||||
# ================================================================
|
||||
"""
|
||||
Dashboard Service - Orchestrates data from multiple sources for dashboard views
|
||||
"""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from typing import List, Optional, Dict, Any
|
||||
from uuid import UUID
|
||||
import structlog
|
||||
|
||||
from shared.database.transactions import transactional
|
||||
|
||||
from app.core.config import settings
|
||||
from app.services.inventory_service import InventoryService
|
||||
from app.services.food_safety_service import FoodSafetyService
|
||||
from app.schemas.dashboard import (
|
||||
InventoryDashboardSummary,
|
||||
BusinessModelInsights,
|
||||
InventoryAnalytics,
|
||||
DashboardFilter,
|
||||
AlertsFilter,
|
||||
StockStatusSummary,
|
||||
AlertSummary,
|
||||
RecentActivity
|
||||
)
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
class DashboardService:
|
||||
"""Service for dashboard data aggregation and analytics"""
|
||||
|
||||
def __init__(self, inventory_service: InventoryService, food_safety_service: FoodSafetyService):
|
||||
self.inventory_service = inventory_service
|
||||
self.food_safety_service = food_safety_service
|
||||
|
||||
@transactional
|
||||
async def get_inventory_dashboard_summary(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID,
|
||||
filters: Optional[DashboardFilter] = None
|
||||
) -> InventoryDashboardSummary:
|
||||
"""Get comprehensive inventory dashboard summary"""
|
||||
try:
|
||||
logger.info("Building dashboard summary", tenant_id=str(tenant_id))
|
||||
|
||||
# Get basic inventory metrics
|
||||
inventory_summary = await self.inventory_service.get_inventory_summary(tenant_id)
|
||||
|
||||
# Get food safety metrics
|
||||
food_safety_dashboard = await self.food_safety_service.get_food_safety_dashboard(db, tenant_id)
|
||||
|
||||
# Get business model insights
|
||||
business_model = await self._detect_business_model(db, tenant_id)
|
||||
|
||||
# Get category breakdown
|
||||
stock_by_category = await self._get_stock_by_category(db, tenant_id)
|
||||
|
||||
# Get alerts breakdown
|
||||
alerts_by_severity = await self._get_alerts_by_severity(db, tenant_id)
|
||||
|
||||
# Get movements breakdown
|
||||
movements_by_type = await self._get_movements_by_type(db, tenant_id)
|
||||
|
||||
# Get performance indicators
|
||||
performance_metrics = await self._calculate_performance_indicators(db, tenant_id)
|
||||
|
||||
# Get trending data
|
||||
stock_value_trend = await self._get_stock_value_trend(db, tenant_id, days=30)
|
||||
alert_trend = await self._get_alert_trend(db, tenant_id, days=30)
|
||||
|
||||
# Recent activity
|
||||
recent_activity = await self.get_recent_activity(db, tenant_id, limit=10)
|
||||
|
||||
return InventoryDashboardSummary(
|
||||
# Current inventory metrics
|
||||
total_ingredients=inventory_summary.total_ingredients,
|
||||
active_ingredients=inventory_summary.total_ingredients, # Assuming all are active
|
||||
total_stock_value=inventory_summary.total_stock_value,
|
||||
total_stock_items=await self._get_total_stock_items(db, tenant_id),
|
||||
|
||||
# Stock status breakdown
|
||||
in_stock_items=await self._get_in_stock_count(db, tenant_id),
|
||||
low_stock_items=inventory_summary.low_stock_alerts,
|
||||
out_of_stock_items=inventory_summary.out_of_stock_items,
|
||||
expired_items=inventory_summary.expired_items,
|
||||
expiring_soon_items=inventory_summary.expiring_soon_items,
|
||||
|
||||
# Food safety metrics
|
||||
food_safety_alerts_active=food_safety_dashboard.critical_alerts + food_safety_dashboard.high_risk_items,
|
||||
temperature_violations_today=food_safety_dashboard.temperature_violations_24h,
|
||||
compliance_issues=food_safety_dashboard.non_compliant_items + food_safety_dashboard.pending_review_items,
|
||||
certifications_expiring_soon=food_safety_dashboard.certifications_expiring_soon,
|
||||
|
||||
# Recent activity
|
||||
recent_stock_movements=inventory_summary.recent_movements,
|
||||
recent_purchases=inventory_summary.recent_purchases,
|
||||
recent_waste=inventory_summary.recent_waste,
|
||||
recent_adjustments=0, # Would need to calculate
|
||||
|
||||
# Business model context
|
||||
business_model=business_model.get("model"),
|
||||
business_model_confidence=business_model.get("confidence"),
|
||||
|
||||
# Category breakdown
|
||||
stock_by_category=stock_by_category,
|
||||
alerts_by_severity=alerts_by_severity,
|
||||
movements_by_type=movements_by_type,
|
||||
|
||||
# Performance indicators
|
||||
inventory_turnover_ratio=performance_metrics.get("turnover_ratio"),
|
||||
waste_percentage=performance_metrics.get("waste_percentage"),
|
||||
compliance_score=performance_metrics.get("compliance_score"),
|
||||
cost_per_unit_avg=performance_metrics.get("avg_cost_per_unit"),
|
||||
|
||||
# Trending data
|
||||
stock_value_trend=stock_value_trend,
|
||||
alert_trend=alert_trend
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to build dashboard summary", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_business_model_insights(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID
|
||||
) -> BusinessModelInsights:
|
||||
"""Get business model insights based on inventory patterns"""
|
||||
try:
|
||||
# Get ingredient metrics
|
||||
ingredient_metrics = await self._get_ingredient_metrics(db, tenant_id)
|
||||
|
||||
# Get operational patterns
|
||||
operational_patterns = await self._analyze_operational_patterns(db, tenant_id)
|
||||
|
||||
# Detect business model
|
||||
model_detection = await self._detect_business_model(db, tenant_id)
|
||||
|
||||
# Generate recommendations
|
||||
recommendations = await self._generate_model_recommendations(
|
||||
model_detection["model"],
|
||||
ingredient_metrics,
|
||||
operational_patterns
|
||||
)
|
||||
|
||||
return BusinessModelInsights(
|
||||
detected_model=model_detection["model"],
|
||||
confidence_score=model_detection["confidence"],
|
||||
total_ingredient_types=ingredient_metrics["total_types"],
|
||||
average_stock_per_ingredient=ingredient_metrics["avg_stock"],
|
||||
finished_product_ratio=ingredient_metrics["finished_product_ratio"],
|
||||
supplier_diversity=ingredient_metrics["supplier_count"],
|
||||
order_frequency_pattern=operational_patterns["order_frequency"],
|
||||
seasonal_variation=operational_patterns["seasonal_variation"],
|
||||
bulk_purchasing_indicator=operational_patterns["bulk_indicator"],
|
||||
production_scale_indicator=operational_patterns["scale_indicator"],
|
||||
model_specific_recommendations=recommendations["specific"],
|
||||
optimization_opportunities=recommendations["optimization"]
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get business model insights", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_inventory_analytics(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID,
|
||||
days_back: int = 30
|
||||
) -> InventoryAnalytics:
|
||||
"""Get advanced inventory analytics"""
|
||||
try:
|
||||
# Get turnover analysis
|
||||
turnover_data = await self._analyze_inventory_turnover(db, tenant_id, days_back)
|
||||
|
||||
# Get cost analysis
|
||||
cost_analysis = await self._analyze_costs(db, tenant_id, days_back)
|
||||
|
||||
# Get efficiency metrics
|
||||
efficiency_metrics = await self._calculate_efficiency_metrics(db, tenant_id, days_back)
|
||||
|
||||
# Get quality and safety metrics
|
||||
quality_metrics = await self._calculate_quality_metrics(db, tenant_id, days_back)
|
||||
|
||||
# Get supplier performance
|
||||
supplier_performance = await self._analyze_supplier_performance(db, tenant_id, days_back)
|
||||
|
||||
return InventoryAnalytics(
|
||||
inventory_turnover_rate=turnover_data["turnover_rate"],
|
||||
fast_moving_items=turnover_data["fast_moving"],
|
||||
slow_moving_items=turnover_data["slow_moving"],
|
||||
dead_stock_items=turnover_data["dead_stock"],
|
||||
total_inventory_cost=cost_analysis["total_cost"],
|
||||
cost_by_category=cost_analysis["by_category"],
|
||||
average_unit_cost_trend=cost_analysis["cost_trend"],
|
||||
waste_cost_analysis=cost_analysis["waste_analysis"],
|
||||
stockout_frequency=efficiency_metrics["stockouts"],
|
||||
overstock_frequency=efficiency_metrics["overstocks"],
|
||||
reorder_accuracy=efficiency_metrics["reorder_accuracy"],
|
||||
forecast_accuracy=efficiency_metrics["forecast_accuracy"],
|
||||
quality_incidents_rate=quality_metrics["incidents_rate"],
|
||||
food_safety_score=quality_metrics["safety_score"],
|
||||
compliance_score_by_standard=quality_metrics["compliance_scores"],
|
||||
temperature_compliance_rate=quality_metrics["temperature_compliance"],
|
||||
supplier_performance=supplier_performance["performance"],
|
||||
delivery_reliability=supplier_performance["delivery_reliability"],
|
||||
quality_consistency=supplier_performance["quality_consistency"]
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get inventory analytics", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_stock_status_by_category(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID
|
||||
) -> List[StockStatusSummary]:
|
||||
"""Get stock status breakdown by category"""
|
||||
try:
|
||||
query = """
|
||||
SELECT
|
||||
COALESCE(i.ingredient_category::text, i.product_category::text, 'other') as category,
|
||||
COUNT(DISTINCT i.id) as total_ingredients,
|
||||
COUNT(CASE WHEN s.available_quantity > i.low_stock_threshold THEN 1 END) as in_stock,
|
||||
COUNT(CASE WHEN s.available_quantity <= i.low_stock_threshold AND s.available_quantity > 0 THEN 1 END) as low_stock,
|
||||
COUNT(CASE WHEN COALESCE(s.available_quantity, 0) = 0 THEN 1 END) as out_of_stock,
|
||||
COALESCE(SUM(s.available_quantity * s.unit_cost), 0) as total_value
|
||||
FROM ingredients i
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
ingredient_id,
|
||||
SUM(available_quantity) as available_quantity,
|
||||
AVG(unit_cost) as unit_cost
|
||||
FROM stock
|
||||
WHERE tenant_id = :tenant_id AND is_available = true
|
||||
GROUP BY ingredient_id
|
||||
) s ON i.id = s.ingredient_id
|
||||
WHERE i.tenant_id = :tenant_id AND i.is_active = true
|
||||
GROUP BY category
|
||||
ORDER BY total_value DESC
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
rows = result.fetchall()
|
||||
|
||||
summaries = []
|
||||
total_value = sum(row.total_value for row in rows)
|
||||
|
||||
for row in rows:
|
||||
percentage = (row.total_value / total_value * 100) if total_value > 0 else 0
|
||||
|
||||
summaries.append(StockStatusSummary(
|
||||
category=row.category,
|
||||
total_ingredients=row.total_ingredients,
|
||||
in_stock=row.in_stock,
|
||||
low_stock=row.low_stock,
|
||||
out_of_stock=row.out_of_stock,
|
||||
total_value=Decimal(str(row.total_value)),
|
||||
percentage_of_total=Decimal(str(percentage))
|
||||
))
|
||||
|
||||
return summaries
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get stock status by category", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_alerts_summary(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID,
|
||||
filters: Optional[AlertsFilter] = None
|
||||
) -> List[AlertSummary]:
|
||||
"""Get alerts summary by type and severity"""
|
||||
try:
|
||||
# Build query with filters
|
||||
where_conditions = ["tenant_id = :tenant_id", "status = 'active'"]
|
||||
params = {"tenant_id": tenant_id}
|
||||
|
||||
if filters:
|
||||
if filters.alert_types:
|
||||
where_conditions.append("alert_type = ANY(:alert_types)")
|
||||
params["alert_types"] = filters.alert_types
|
||||
|
||||
if filters.severities:
|
||||
where_conditions.append("severity = ANY(:severities)")
|
||||
params["severities"] = filters.severities
|
||||
|
||||
if filters.date_from:
|
||||
where_conditions.append("created_at >= :date_from")
|
||||
params["date_from"] = filters.date_from
|
||||
|
||||
if filters.date_to:
|
||||
where_conditions.append("created_at <= :date_to")
|
||||
params["date_to"] = filters.date_to
|
||||
|
||||
where_clause = " AND ".join(where_conditions)
|
||||
|
||||
query = f"""
|
||||
SELECT
|
||||
alert_type,
|
||||
severity,
|
||||
COUNT(*) as count,
|
||||
MIN(EXTRACT(EPOCH FROM (NOW() - created_at))/3600)::int as oldest_alert_age_hours,
|
||||
AVG(CASE WHEN resolved_at IS NOT NULL
|
||||
THEN EXTRACT(EPOCH FROM (resolved_at - created_at))/3600
|
||||
ELSE NULL END)::int as avg_resolution_hours
|
||||
FROM food_safety_alerts
|
||||
WHERE {where_clause}
|
||||
GROUP BY alert_type, severity
|
||||
ORDER BY severity DESC, count DESC
|
||||
"""
|
||||
|
||||
result = await db.execute(query, params)
|
||||
rows = result.fetchall()
|
||||
|
||||
return [
|
||||
AlertSummary(
|
||||
alert_type=row.alert_type,
|
||||
severity=row.severity,
|
||||
count=row.count,
|
||||
oldest_alert_age_hours=row.oldest_alert_age_hours,
|
||||
average_resolution_time_hours=row.avg_resolution_hours
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get alerts summary", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_recent_activity(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID,
|
||||
limit: int = 20,
|
||||
activity_types: Optional[List[str]] = None
|
||||
) -> List[RecentActivity]:
|
||||
"""Get recent inventory activity"""
|
||||
try:
|
||||
activities = []
|
||||
|
||||
# Get recent stock movements
|
||||
stock_query = """
|
||||
SELECT
|
||||
'stock_movement' as activity_type,
|
||||
CASE
|
||||
WHEN movement_type = 'purchase' THEN 'Stock added: ' || i.name || ' (' || sm.quantity || ' ' || i.unit_of_measure::text || ')'
|
||||
WHEN movement_type = 'production_use' THEN 'Stock consumed: ' || i.name || ' (' || sm.quantity || ' ' || i.unit_of_measure::text || ')'
|
||||
WHEN movement_type = 'waste' THEN 'Stock wasted: ' || i.name || ' (' || sm.quantity || ' ' || i.unit_of_measure::text || ')'
|
||||
WHEN movement_type = 'adjustment' THEN 'Stock adjusted: ' || i.name || ' (' || sm.quantity || ' ' || i.unit_of_measure::text || ')'
|
||||
ELSE 'Stock movement: ' || i.name
|
||||
END as description,
|
||||
sm.movement_date as timestamp,
|
||||
sm.created_by as user_id,
|
||||
CASE
|
||||
WHEN movement_type = 'waste' THEN 'high'
|
||||
WHEN movement_type = 'adjustment' THEN 'medium'
|
||||
ELSE 'low'
|
||||
END as impact_level,
|
||||
sm.id as entity_id,
|
||||
'stock_movement' as entity_type
|
||||
FROM stock_movements sm
|
||||
JOIN ingredients i ON sm.ingredient_id = i.id
|
||||
WHERE i.tenant_id = :tenant_id
|
||||
ORDER BY sm.movement_date DESC
|
||||
LIMIT :limit
|
||||
"""
|
||||
|
||||
result = await db.execute(stock_query, {"tenant_id": tenant_id, "limit": limit // 2})
|
||||
for row in result.fetchall():
|
||||
activities.append(RecentActivity(
|
||||
activity_type=row.activity_type,
|
||||
description=row.description,
|
||||
timestamp=row.timestamp,
|
||||
impact_level=row.impact_level,
|
||||
entity_id=row.entity_id,
|
||||
entity_type=row.entity_type
|
||||
))
|
||||
|
||||
# Get recent food safety alerts
|
||||
alert_query = """
|
||||
SELECT
|
||||
'food_safety_alert' as activity_type,
|
||||
title as description,
|
||||
created_at as timestamp,
|
||||
created_by as user_id,
|
||||
CASE
|
||||
WHEN severity = 'critical' THEN 'high'
|
||||
WHEN severity = 'high' THEN 'medium'
|
||||
ELSE 'low'
|
||||
END as impact_level,
|
||||
id as entity_id,
|
||||
'food_safety_alert' as entity_type
|
||||
FROM food_safety_alerts
|
||||
WHERE tenant_id = :tenant_id
|
||||
ORDER BY created_at DESC
|
||||
LIMIT :limit
|
||||
"""
|
||||
|
||||
result = await db.execute(alert_query, {"tenant_id": tenant_id, "limit": limit // 2})
|
||||
for row in result.fetchall():
|
||||
activities.append(RecentActivity(
|
||||
activity_type=row.activity_type,
|
||||
description=row.description,
|
||||
timestamp=row.timestamp,
|
||||
impact_level=row.impact_level,
|
||||
entity_id=row.entity_id,
|
||||
entity_type=row.entity_type
|
||||
))
|
||||
|
||||
# Sort by timestamp and limit
|
||||
activities.sort(key=lambda x: x.timestamp, reverse=True)
|
||||
return activities[:limit]
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get recent activity", error=str(e))
|
||||
raise
|
||||
|
||||
async def get_live_metrics(self, db, tenant_id: UUID) -> Dict[str, Any]:
|
||||
"""Get real-time inventory metrics"""
|
||||
try:
|
||||
query = """
|
||||
SELECT
|
||||
COUNT(DISTINCT i.id) as total_ingredients,
|
||||
COUNT(CASE WHEN s.available_quantity > i.low_stock_threshold THEN 1 END) as in_stock,
|
||||
COUNT(CASE WHEN s.available_quantity <= i.low_stock_threshold THEN 1 END) as low_stock,
|
||||
COUNT(CASE WHEN s.available_quantity = 0 THEN 1 END) as out_of_stock,
|
||||
COALESCE(SUM(s.available_quantity * s.unit_cost), 0) as total_value,
|
||||
COUNT(CASE WHEN s.expiration_date < NOW() THEN 1 END) as expired_items,
|
||||
COUNT(CASE WHEN s.expiration_date BETWEEN NOW() AND NOW() + INTERVAL '7 days' THEN 1 END) as expiring_soon
|
||||
FROM ingredients i
|
||||
LEFT JOIN stock s ON i.id = s.ingredient_id AND s.is_available = true
|
||||
WHERE i.tenant_id = :tenant_id AND i.is_active = true
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
metrics = result.fetchone()
|
||||
|
||||
return {
|
||||
"total_ingredients": metrics.total_ingredients,
|
||||
"in_stock": metrics.in_stock,
|
||||
"low_stock": metrics.low_stock,
|
||||
"out_of_stock": metrics.out_of_stock,
|
||||
"total_value": float(metrics.total_value),
|
||||
"expired_items": metrics.expired_items,
|
||||
"expiring_soon": metrics.expiring_soon,
|
||||
"last_updated": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get live metrics", error=str(e))
|
||||
raise
|
||||
|
||||
async def export_dashboard_data(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID,
|
||||
format: str,
|
||||
date_from: Optional[datetime] = None,
|
||||
date_to: Optional[datetime] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Export dashboard data in specified format"""
|
||||
try:
|
||||
# Get dashboard summary
|
||||
summary = await self.get_inventory_dashboard_summary(db, tenant_id)
|
||||
|
||||
# Get analytics
|
||||
analytics = await self.get_inventory_analytics(db, tenant_id)
|
||||
|
||||
export_data = {
|
||||
"export_info": {
|
||||
"generated_at": datetime.now().isoformat(),
|
||||
"tenant_id": str(tenant_id),
|
||||
"format": format,
|
||||
"date_range": {
|
||||
"from": date_from.isoformat() if date_from else None,
|
||||
"to": date_to.isoformat() if date_to else None
|
||||
}
|
||||
},
|
||||
"dashboard_summary": summary.dict(),
|
||||
"analytics": analytics.dict()
|
||||
}
|
||||
|
||||
if format.lower() == "json":
|
||||
return export_data
|
||||
elif format.lower() in ["csv", "excel"]:
|
||||
# For CSV/Excel, flatten the data structure
|
||||
return {
|
||||
"message": f"Export in {format} format would be generated here",
|
||||
"data_preview": export_data
|
||||
}
|
||||
else:
|
||||
raise ValueError(f"Unsupported export format: {format}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to export dashboard data", error=str(e))
|
||||
raise
|
||||
|
||||
# ===== PRIVATE HELPER METHODS =====
|
||||
|
||||
async def _detect_business_model(self, db, tenant_id: UUID) -> Dict[str, Any]:
|
||||
"""Detect business model based on inventory patterns"""
|
||||
try:
|
||||
if not settings.ENABLE_BUSINESS_MODEL_DETECTION:
|
||||
return {"model": "unknown", "confidence": Decimal("0")}
|
||||
|
||||
# Get ingredient metrics
|
||||
query = """
|
||||
SELECT
|
||||
COUNT(*) as total_ingredients,
|
||||
COUNT(CASE WHEN product_type = 'finished_product' THEN 1 END) as finished_products,
|
||||
COUNT(CASE WHEN product_type = 'ingredient' THEN 1 END) as raw_ingredients,
|
||||
COUNT(DISTINCT supplier_name) as supplier_count,
|
||||
AVG(CASE WHEN s.available_quantity IS NOT NULL THEN s.available_quantity ELSE 0 END) as avg_stock_level
|
||||
FROM ingredients i
|
||||
LEFT JOIN (
|
||||
SELECT ingredient_id, SUM(available_quantity) as available_quantity
|
||||
FROM stock WHERE tenant_id = :tenant_id GROUP BY ingredient_id
|
||||
) s ON i.id = s.ingredient_id
|
||||
WHERE i.tenant_id = :tenant_id AND i.is_active = true
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
metrics = result.fetchone()
|
||||
|
||||
# Business model detection logic
|
||||
total_ingredients = metrics.total_ingredients
|
||||
finished_ratio = metrics.finished_products / total_ingredients if total_ingredients > 0 else 0
|
||||
|
||||
if total_ingredients >= settings.CENTRAL_BAKERY_THRESHOLD_INGREDIENTS:
|
||||
if finished_ratio > 0.3: # More than 30% finished products
|
||||
model = "central_bakery"
|
||||
confidence = Decimal("85")
|
||||
else:
|
||||
model = "central_bakery"
|
||||
confidence = Decimal("70")
|
||||
elif total_ingredients <= settings.INDIVIDUAL_BAKERY_THRESHOLD_INGREDIENTS:
|
||||
model = "individual_bakery"
|
||||
confidence = Decimal("80")
|
||||
else:
|
||||
model = "mixed"
|
||||
confidence = Decimal("60")
|
||||
|
||||
return {"model": model, "confidence": confidence}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to detect business model", error=str(e))
|
||||
return {"model": "unknown", "confidence": Decimal("0")}
|
||||
|
||||
async def _get_stock_by_category(self, db, tenant_id: UUID) -> Dict[str, Any]:
|
||||
"""Get stock breakdown by category"""
|
||||
try:
|
||||
query = """
|
||||
SELECT
|
||||
COALESCE(i.ingredient_category::text, i.product_category::text, 'other') as category,
|
||||
COUNT(*) as count,
|
||||
COALESCE(SUM(s.available_quantity * s.unit_cost), 0) as total_value
|
||||
FROM ingredients i
|
||||
LEFT JOIN (
|
||||
SELECT ingredient_id, SUM(available_quantity) as available_quantity, AVG(unit_cost) as unit_cost
|
||||
FROM stock WHERE tenant_id = :tenant_id GROUP BY ingredient_id
|
||||
) s ON i.id = s.ingredient_id
|
||||
WHERE i.tenant_id = :tenant_id AND i.is_active = true
|
||||
GROUP BY category
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
categories = {}
|
||||
|
||||
for row in result.fetchall():
|
||||
categories[row.category] = {
|
||||
"count": row.count,
|
||||
"total_value": float(row.total_value)
|
||||
}
|
||||
|
||||
return categories
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get stock by category", error=str(e))
|
||||
return {}
|
||||
|
||||
async def _get_alerts_by_severity(self, db, tenant_id: UUID) -> Dict[str, int]:
|
||||
"""Get alerts breakdown by severity"""
|
||||
try:
|
||||
query = """
|
||||
SELECT severity, COUNT(*) as count
|
||||
FROM food_safety_alerts
|
||||
WHERE tenant_id = :tenant_id AND status = 'active'
|
||||
GROUP BY severity
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
alerts = {"critical": 0, "high": 0, "medium": 0, "low": 0}
|
||||
|
||||
for row in result.fetchall():
|
||||
alerts[row.severity] = row.count
|
||||
|
||||
return alerts
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get alerts by severity", error=str(e))
|
||||
return {"critical": 0, "high": 0, "medium": 0, "low": 0}
|
||||
|
||||
async def _get_movements_by_type(self, db, tenant_id: UUID) -> Dict[str, int]:
|
||||
"""Get movements breakdown by type"""
|
||||
try:
|
||||
query = """
|
||||
SELECT sm.movement_type, COUNT(*) as count
|
||||
FROM stock_movements sm
|
||||
JOIN ingredients i ON sm.ingredient_id = i.id
|
||||
WHERE i.tenant_id = :tenant_id
|
||||
AND sm.movement_date > NOW() - INTERVAL '7 days'
|
||||
GROUP BY sm.movement_type
|
||||
"""
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
movements = {}
|
||||
|
||||
for row in result.fetchall():
|
||||
movements[row.movement_type] = row.count
|
||||
|
||||
return movements
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get movements by type", error=str(e))
|
||||
return {}
|
||||
|
||||
async def _calculate_performance_indicators(self, db, tenant_id: UUID) -> Dict[str, Decimal]:
|
||||
"""Calculate performance indicators"""
|
||||
try:
|
||||
# This would involve complex calculations
|
||||
# For now, return placeholder values
|
||||
return {
|
||||
"turnover_ratio": Decimal("4.2"),
|
||||
"waste_percentage": Decimal("2.1"),
|
||||
"compliance_score": Decimal("8.5"),
|
||||
"avg_cost_per_unit": Decimal("12.45")
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to calculate performance indicators", error=str(e))
|
||||
return {}
|
||||
|
||||
async def _get_stock_value_trend(self, db, tenant_id: UUID, days: int) -> List[Dict[str, Any]]:
|
||||
"""Get stock value trend over time"""
|
||||
try:
|
||||
# This would track stock value changes over time
|
||||
# For now, return sample data
|
||||
trend_data = []
|
||||
base_date = datetime.now() - timedelta(days=days)
|
||||
|
||||
for i in range(0, days, 7): # Weekly data points
|
||||
trend_data.append({
|
||||
"date": (base_date + timedelta(days=i)).isoformat(),
|
||||
"value": float(Decimal("50000") + Decimal(str(i * 100)))
|
||||
})
|
||||
|
||||
return trend_data
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get stock value trend", error=str(e))
|
||||
return []
|
||||
|
||||
async def _get_alert_trend(self, db, tenant_id: UUID, days: int) -> List[Dict[str, Any]]:
|
||||
"""Get alert trend over time"""
|
||||
try:
|
||||
query = """
|
||||
SELECT
|
||||
DATE(created_at) as alert_date,
|
||||
COUNT(*) as alert_count,
|
||||
COUNT(CASE WHEN severity IN ('high', 'critical') THEN 1 END) as high_severity_count
|
||||
FROM food_safety_alerts
|
||||
WHERE tenant_id = :tenant_id
|
||||
AND created_at > NOW() - INTERVAL '%s days'
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY alert_date
|
||||
""" % days
|
||||
|
||||
result = await db.execute(query, {"tenant_id": tenant_id})
|
||||
|
||||
return [
|
||||
{
|
||||
"date": row.alert_date.isoformat(),
|
||||
"total_alerts": row.alert_count,
|
||||
"high_severity_alerts": row.high_severity_count
|
||||
}
|
||||
for row in result.fetchall()
|
||||
]
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get alert trend", error=str(e))
|
||||
return []
|
||||
|
||||
# Additional helper methods would be implemented here for:
|
||||
# - _get_total_stock_items
|
||||
# - _get_in_stock_count
|
||||
# - _get_ingredient_metrics
|
||||
# - _analyze_operational_patterns
|
||||
# - _generate_model_recommendations
|
||||
# - _analyze_inventory_turnover
|
||||
# - _analyze_costs
|
||||
# - _calculate_efficiency_metrics
|
||||
# - _calculate_quality_metrics
|
||||
# - _analyze_supplier_performance
|
||||
|
||||
# These are complex analytical methods that would require detailed implementation
|
||||
# based on specific business requirements and data structures
|
||||
633
services/inventory/app/services/food_safety_service.py
Normal file
633
services/inventory/app/services/food_safety_service.py
Normal file
@@ -0,0 +1,633 @@
|
||||
# ================================================================
|
||||
# services/inventory/app/services/food_safety_service.py
|
||||
# ================================================================
|
||||
"""
|
||||
Food Safety Service - Business logic for food safety and compliance
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from typing import List, Optional, Dict, Any
|
||||
from uuid import UUID
|
||||
import structlog
|
||||
|
||||
from shared.notifications.alert_integration import AlertIntegration
|
||||
from shared.database.transactions import transactional
|
||||
|
||||
from app.core.config import settings
|
||||
from app.models.food_safety import (
|
||||
FoodSafetyCompliance,
|
||||
TemperatureLog,
|
||||
FoodSafetyAlert,
|
||||
FoodSafetyStandard,
|
||||
ComplianceStatus,
|
||||
FoodSafetyAlertType
|
||||
)
|
||||
from app.schemas.food_safety import (
|
||||
FoodSafetyComplianceCreate,
|
||||
FoodSafetyComplianceUpdate,
|
||||
FoodSafetyComplianceResponse,
|
||||
TemperatureLogCreate,
|
||||
TemperatureLogResponse,
|
||||
FoodSafetyAlertCreate,
|
||||
FoodSafetyAlertUpdate,
|
||||
FoodSafetyAlertResponse,
|
||||
FoodSafetyMetrics,
|
||||
TemperatureAnalytics
|
||||
)
|
||||
from app.schemas.dashboard import FoodSafetyDashboard, TemperatureMonitoringStatus
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
class FoodSafetyService:
|
||||
"""Service for food safety and compliance operations"""
|
||||
|
||||
def __init__(self):
|
||||
self.alert_integration = AlertIntegration()
|
||||
|
||||
# ===== COMPLIANCE MANAGEMENT =====
|
||||
|
||||
@transactional
|
||||
async def create_compliance_record(
|
||||
self,
|
||||
db,
|
||||
compliance_data: FoodSafetyComplianceCreate,
|
||||
user_id: Optional[UUID] = None
|
||||
) -> FoodSafetyComplianceResponse:
|
||||
"""Create a new food safety compliance record"""
|
||||
try:
|
||||
logger.info("Creating compliance record",
|
||||
ingredient_id=str(compliance_data.ingredient_id),
|
||||
standard=compliance_data.standard)
|
||||
|
||||
# Validate compliance data
|
||||
await self._validate_compliance_data(db, compliance_data)
|
||||
|
||||
# Create compliance record
|
||||
compliance = FoodSafetyCompliance(
|
||||
tenant_id=compliance_data.tenant_id,
|
||||
ingredient_id=compliance_data.ingredient_id,
|
||||
standard=FoodSafetyStandard(compliance_data.standard),
|
||||
compliance_status=ComplianceStatus(compliance_data.compliance_status),
|
||||
certification_number=compliance_data.certification_number,
|
||||
certifying_body=compliance_data.certifying_body,
|
||||
certification_date=compliance_data.certification_date,
|
||||
expiration_date=compliance_data.expiration_date,
|
||||
requirements=compliance_data.requirements,
|
||||
compliance_notes=compliance_data.compliance_notes,
|
||||
documentation_url=compliance_data.documentation_url,
|
||||
last_audit_date=compliance_data.last_audit_date,
|
||||
next_audit_date=compliance_data.next_audit_date,
|
||||
auditor_name=compliance_data.auditor_name,
|
||||
audit_score=compliance_data.audit_score,
|
||||
risk_level=compliance_data.risk_level,
|
||||
risk_factors=compliance_data.risk_factors,
|
||||
mitigation_measures=compliance_data.mitigation_measures,
|
||||
requires_monitoring=compliance_data.requires_monitoring,
|
||||
monitoring_frequency_days=compliance_data.monitoring_frequency_days,
|
||||
created_by=user_id,
|
||||
updated_by=user_id
|
||||
)
|
||||
|
||||
db.add(compliance)
|
||||
await db.flush()
|
||||
await db.refresh(compliance)
|
||||
|
||||
# Check for compliance alerts
|
||||
await self._check_compliance_alerts(db, compliance)
|
||||
|
||||
logger.info("Compliance record created",
|
||||
compliance_id=str(compliance.id))
|
||||
|
||||
return FoodSafetyComplianceResponse(**compliance.to_dict())
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to create compliance record", error=str(e))
|
||||
raise
|
||||
|
||||
@transactional
|
||||
async def update_compliance_record(
|
||||
self,
|
||||
db,
|
||||
compliance_id: UUID,
|
||||
tenant_id: UUID,
|
||||
compliance_data: FoodSafetyComplianceUpdate,
|
||||
user_id: Optional[UUID] = None
|
||||
) -> Optional[FoodSafetyComplianceResponse]:
|
||||
"""Update an existing compliance record"""
|
||||
try:
|
||||
# Get existing compliance record
|
||||
compliance = await db.get(FoodSafetyCompliance, compliance_id)
|
||||
if not compliance or compliance.tenant_id != tenant_id:
|
||||
return None
|
||||
|
||||
# Update fields
|
||||
update_fields = compliance_data.dict(exclude_unset=True)
|
||||
for field, value in update_fields.items():
|
||||
if hasattr(compliance, field):
|
||||
if field in ['compliance_status'] and value:
|
||||
setattr(compliance, field, ComplianceStatus(value))
|
||||
else:
|
||||
setattr(compliance, field, value)
|
||||
|
||||
compliance.updated_by = user_id
|
||||
|
||||
await db.flush()
|
||||
await db.refresh(compliance)
|
||||
|
||||
# Check for compliance alerts after update
|
||||
await self._check_compliance_alerts(db, compliance)
|
||||
|
||||
logger.info("Compliance record updated",
|
||||
compliance_id=str(compliance.id))
|
||||
|
||||
return FoodSafetyComplianceResponse(**compliance.to_dict())
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to update compliance record",
|
||||
compliance_id=str(compliance_id),
|
||||
error=str(e))
|
||||
raise
|
||||
|
||||
# ===== TEMPERATURE MONITORING =====
|
||||
|
||||
@transactional
|
||||
async def log_temperature(
|
||||
self,
|
||||
db,
|
||||
temp_data: TemperatureLogCreate,
|
||||
user_id: Optional[UUID] = None
|
||||
) -> TemperatureLogResponse:
|
||||
"""Log a temperature reading"""
|
||||
try:
|
||||
# Determine if temperature is within range
|
||||
is_within_range = self._is_temperature_within_range(
|
||||
temp_data.temperature_celsius,
|
||||
temp_data.target_temperature_min,
|
||||
temp_data.target_temperature_max,
|
||||
temp_data.storage_location
|
||||
)
|
||||
|
||||
# Create temperature log
|
||||
temp_log = TemperatureLog(
|
||||
tenant_id=temp_data.tenant_id,
|
||||
storage_location=temp_data.storage_location,
|
||||
warehouse_zone=temp_data.warehouse_zone,
|
||||
equipment_id=temp_data.equipment_id,
|
||||
temperature_celsius=temp_data.temperature_celsius,
|
||||
humidity_percentage=temp_data.humidity_percentage,
|
||||
target_temperature_min=temp_data.target_temperature_min,
|
||||
target_temperature_max=temp_data.target_temperature_max,
|
||||
is_within_range=is_within_range,
|
||||
alert_triggered=not is_within_range,
|
||||
measurement_method=temp_data.measurement_method,
|
||||
device_id=temp_data.device_id,
|
||||
calibration_date=temp_data.calibration_date,
|
||||
recorded_by=user_id
|
||||
)
|
||||
|
||||
db.add(temp_log)
|
||||
await db.flush()
|
||||
await db.refresh(temp_log)
|
||||
|
||||
# Create alert if temperature is out of range
|
||||
if not is_within_range:
|
||||
await self._create_temperature_alert(db, temp_log)
|
||||
|
||||
logger.info("Temperature logged",
|
||||
location=temp_data.storage_location,
|
||||
temperature=temp_data.temperature_celsius,
|
||||
within_range=is_within_range)
|
||||
|
||||
return TemperatureLogResponse(**temp_log.to_dict())
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to log temperature", error=str(e))
|
||||
raise
|
||||
|
||||
@transactional
|
||||
async def bulk_log_temperatures(
|
||||
self,
|
||||
db,
|
||||
temp_readings: List[TemperatureLogCreate],
|
||||
user_id: Optional[UUID] = None
|
||||
) -> List[TemperatureLogResponse]:
|
||||
"""Bulk log temperature readings"""
|
||||
try:
|
||||
results = []
|
||||
alerts_to_create = []
|
||||
|
||||
for temp_data in temp_readings:
|
||||
# Determine if temperature is within range
|
||||
is_within_range = self._is_temperature_within_range(
|
||||
temp_data.temperature_celsius,
|
||||
temp_data.target_temperature_min,
|
||||
temp_data.target_temperature_max,
|
||||
temp_data.storage_location
|
||||
)
|
||||
|
||||
# Create temperature log
|
||||
temp_log = TemperatureLog(
|
||||
tenant_id=temp_data.tenant_id,
|
||||
storage_location=temp_data.storage_location,
|
||||
warehouse_zone=temp_data.warehouse_zone,
|
||||
equipment_id=temp_data.equipment_id,
|
||||
temperature_celsius=temp_data.temperature_celsius,
|
||||
humidity_percentage=temp_data.humidity_percentage,
|
||||
target_temperature_min=temp_data.target_temperature_min,
|
||||
target_temperature_max=temp_data.target_temperature_max,
|
||||
is_within_range=is_within_range,
|
||||
alert_triggered=not is_within_range,
|
||||
measurement_method=temp_data.measurement_method,
|
||||
device_id=temp_data.device_id,
|
||||
calibration_date=temp_data.calibration_date,
|
||||
recorded_by=user_id
|
||||
)
|
||||
|
||||
db.add(temp_log)
|
||||
|
||||
if not is_within_range:
|
||||
alerts_to_create.append(temp_log)
|
||||
|
||||
results.append(TemperatureLogResponse(**temp_log.to_dict()))
|
||||
|
||||
await db.flush()
|
||||
|
||||
# Create alerts for out-of-range temperatures
|
||||
for temp_log in alerts_to_create:
|
||||
await self._create_temperature_alert(db, temp_log)
|
||||
|
||||
logger.info("Bulk temperature logging completed",
|
||||
count=len(temp_readings),
|
||||
violations=len(alerts_to_create))
|
||||
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to bulk log temperatures", error=str(e))
|
||||
raise
|
||||
|
||||
# ===== ALERT MANAGEMENT =====
|
||||
|
||||
@transactional
|
||||
async def create_food_safety_alert(
|
||||
self,
|
||||
db,
|
||||
alert_data: FoodSafetyAlertCreate,
|
||||
user_id: Optional[UUID] = None
|
||||
) -> FoodSafetyAlertResponse:
|
||||
"""Create a food safety alert"""
|
||||
try:
|
||||
alert = FoodSafetyAlert(
|
||||
tenant_id=alert_data.tenant_id,
|
||||
alert_code=alert_data.alert_code,
|
||||
alert_type=FoodSafetyAlertType(alert_data.alert_type),
|
||||
severity=alert_data.severity,
|
||||
risk_level=alert_data.risk_level,
|
||||
source_entity_type=alert_data.source_entity_type,
|
||||
source_entity_id=alert_data.source_entity_id,
|
||||
ingredient_id=alert_data.ingredient_id,
|
||||
stock_id=alert_data.stock_id,
|
||||
title=alert_data.title,
|
||||
description=alert_data.description,
|
||||
detailed_message=alert_data.detailed_message,
|
||||
regulatory_requirement=alert_data.regulatory_requirement,
|
||||
compliance_standard=FoodSafetyStandard(alert_data.compliance_standard) if alert_data.compliance_standard else None,
|
||||
regulatory_action_required=alert_data.regulatory_action_required,
|
||||
trigger_condition=alert_data.trigger_condition,
|
||||
threshold_value=alert_data.threshold_value,
|
||||
actual_value=alert_data.actual_value,
|
||||
alert_data=alert_data.alert_data,
|
||||
environmental_factors=alert_data.environmental_factors,
|
||||
affected_products=alert_data.affected_products,
|
||||
public_health_risk=alert_data.public_health_risk,
|
||||
business_impact=alert_data.business_impact,
|
||||
estimated_loss=alert_data.estimated_loss,
|
||||
first_occurred_at=datetime.now(),
|
||||
last_occurred_at=datetime.now(),
|
||||
created_by=user_id
|
||||
)
|
||||
|
||||
db.add(alert)
|
||||
await db.flush()
|
||||
await db.refresh(alert)
|
||||
|
||||
# Send notifications
|
||||
await self._send_alert_notifications(alert)
|
||||
|
||||
logger.info("Food safety alert created",
|
||||
alert_id=str(alert.id),
|
||||
alert_type=alert_data.alert_type,
|
||||
severity=alert_data.severity)
|
||||
|
||||
return FoodSafetyAlertResponse(**alert.to_dict())
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to create food safety alert", error=str(e))
|
||||
raise
|
||||
|
||||
# ===== DASHBOARD AND ANALYTICS =====
|
||||
|
||||
async def get_food_safety_dashboard(
|
||||
self,
|
||||
db,
|
||||
tenant_id: UUID
|
||||
) -> FoodSafetyDashboard:
|
||||
"""Get food safety dashboard data"""
|
||||
try:
|
||||
# Get compliance overview
|
||||
compliance_query = """
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(CASE WHEN compliance_status = 'compliant' THEN 1 END) as compliant,
|
||||
COUNT(CASE WHEN compliance_status = 'non_compliant' THEN 1 END) as non_compliant,
|
||||
COUNT(CASE WHEN compliance_status = 'pending_review' THEN 1 END) as pending_review
|
||||
FROM food_safety_compliance
|
||||
WHERE tenant_id = :tenant_id AND is_active = true
|
||||
"""
|
||||
|
||||
compliance_result = await db.execute(compliance_query, {"tenant_id": tenant_id})
|
||||
compliance_stats = compliance_result.fetchone()
|
||||
|
||||
total_compliance = compliance_stats.total or 0
|
||||
compliant_items = compliance_stats.compliant or 0
|
||||
compliance_percentage = (compliant_items / total_compliance * 100) if total_compliance > 0 else 0
|
||||
|
||||
# Get temperature monitoring status
|
||||
temp_query = """
|
||||
SELECT
|
||||
COUNT(DISTINCT equipment_id) as sensors_online,
|
||||
COUNT(CASE WHEN NOT is_within_range AND recorded_at > NOW() - INTERVAL '24 hours' THEN 1 END) as violations_24h
|
||||
FROM temperature_logs
|
||||
WHERE tenant_id = :tenant_id AND recorded_at > NOW() - INTERVAL '1 hour'
|
||||
"""
|
||||
|
||||
temp_result = await db.execute(temp_query, {"tenant_id": tenant_id})
|
||||
temp_stats = temp_result.fetchone()
|
||||
|
||||
# Get expiration tracking
|
||||
expiration_query = """
|
||||
SELECT
|
||||
COUNT(CASE WHEN expiration_date::date = CURRENT_DATE THEN 1 END) as expiring_today,
|
||||
COUNT(CASE WHEN expiration_date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '7 days' THEN 1 END) as expiring_week,
|
||||
COUNT(CASE WHEN expiration_date < CURRENT_DATE AND is_available THEN 1 END) as expired_requiring_action
|
||||
FROM stock s
|
||||
JOIN ingredients i ON s.ingredient_id = i.id
|
||||
WHERE i.tenant_id = :tenant_id AND s.is_available = true
|
||||
"""
|
||||
|
||||
expiration_result = await db.execute(expiration_query, {"tenant_id": tenant_id})
|
||||
expiration_stats = expiration_result.fetchone()
|
||||
|
||||
# Get alert counts
|
||||
alert_query = """
|
||||
SELECT
|
||||
COUNT(CASE WHEN severity = 'high' OR severity = 'critical' THEN 1 END) as high_risk,
|
||||
COUNT(CASE WHEN severity = 'critical' THEN 1 END) as critical,
|
||||
COUNT(CASE WHEN regulatory_action_required AND NOT resolved_at THEN 1 END) as regulatory_pending
|
||||
FROM food_safety_alerts
|
||||
WHERE tenant_id = :tenant_id AND status = 'active'
|
||||
"""
|
||||
|
||||
alert_result = await db.execute(alert_query, {"tenant_id": tenant_id})
|
||||
alert_stats = alert_result.fetchone()
|
||||
|
||||
return FoodSafetyDashboard(
|
||||
total_compliance_items=total_compliance,
|
||||
compliant_items=compliant_items,
|
||||
non_compliant_items=compliance_stats.non_compliant or 0,
|
||||
pending_review_items=compliance_stats.pending_review or 0,
|
||||
compliance_percentage=Decimal(str(compliance_percentage)),
|
||||
temperature_sensors_online=temp_stats.sensors_online or 0,
|
||||
temperature_sensors_total=temp_stats.sensors_online or 0, # Would need actual count
|
||||
temperature_violations_24h=temp_stats.violations_24h or 0,
|
||||
current_temperature_status="normal", # Would need to calculate
|
||||
items_expiring_today=expiration_stats.expiring_today or 0,
|
||||
items_expiring_this_week=expiration_stats.expiring_week or 0,
|
||||
expired_items_requiring_action=expiration_stats.expired_requiring_action or 0,
|
||||
upcoming_audits=0, # Would need to calculate
|
||||
overdue_audits=0, # Would need to calculate
|
||||
certifications_valid=compliant_items,
|
||||
certifications_expiring_soon=0, # Would need to calculate
|
||||
high_risk_items=alert_stats.high_risk or 0,
|
||||
critical_alerts=alert_stats.critical or 0,
|
||||
regulatory_notifications_pending=alert_stats.regulatory_pending or 0,
|
||||
recent_safety_incidents=[] # Would need to get recent incidents
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get food safety dashboard", error=str(e))
|
||||
raise
|
||||
|
||||
# ===== PRIVATE HELPER METHODS =====
|
||||
|
||||
async def _validate_compliance_data(self, db, compliance_data: FoodSafetyComplianceCreate):
|
||||
"""Validate compliance data for business rules"""
|
||||
# Check if ingredient exists
|
||||
ingredient_query = "SELECT id FROM ingredients WHERE id = :ingredient_id AND tenant_id = :tenant_id"
|
||||
result = await db.execute(ingredient_query, {
|
||||
"ingredient_id": compliance_data.ingredient_id,
|
||||
"tenant_id": compliance_data.tenant_id
|
||||
})
|
||||
|
||||
if not result.fetchone():
|
||||
raise ValueError("Ingredient not found")
|
||||
|
||||
# Validate standard
|
||||
try:
|
||||
FoodSafetyStandard(compliance_data.standard)
|
||||
except ValueError:
|
||||
raise ValueError(f"Invalid food safety standard: {compliance_data.standard}")
|
||||
|
||||
# Validate compliance status
|
||||
try:
|
||||
ComplianceStatus(compliance_data.compliance_status)
|
||||
except ValueError:
|
||||
raise ValueError(f"Invalid compliance status: {compliance_data.compliance_status}")
|
||||
|
||||
def _is_temperature_within_range(
|
||||
self,
|
||||
temperature: float,
|
||||
target_min: Optional[float],
|
||||
target_max: Optional[float],
|
||||
location: str
|
||||
) -> bool:
|
||||
"""Check if temperature is within acceptable range"""
|
||||
# Use target ranges if provided, otherwise use default ranges
|
||||
if target_min is not None and target_max is not None:
|
||||
return target_min <= temperature <= target_max
|
||||
|
||||
# Default ranges based on location type
|
||||
if "freezer" in location.lower():
|
||||
return settings.FREEZER_TEMP_MIN <= temperature <= settings.FREEZER_TEMP_MAX
|
||||
elif "refrigerat" in location.lower() or "fridge" in location.lower():
|
||||
return settings.REFRIGERATION_TEMP_MIN <= temperature <= settings.REFRIGERATION_TEMP_MAX
|
||||
else:
|
||||
return settings.ROOM_TEMP_MIN <= temperature <= settings.ROOM_TEMP_MAX
|
||||
|
||||
async def _create_temperature_alert(self, db, temp_log: TemperatureLog):
|
||||
"""Create an alert for temperature violation"""
|
||||
try:
|
||||
alert_code = f"TEMP-{uuid.uuid4().hex[:8].upper()}"
|
||||
|
||||
# Determine severity based on deviation
|
||||
target_min = temp_log.target_temperature_min or 0
|
||||
target_max = temp_log.target_temperature_max or 25
|
||||
deviation = max(
|
||||
abs(temp_log.temperature_celsius - target_min),
|
||||
abs(temp_log.temperature_celsius - target_max)
|
||||
)
|
||||
|
||||
if deviation > 10:
|
||||
severity = "critical"
|
||||
elif deviation > 5:
|
||||
severity = "high"
|
||||
else:
|
||||
severity = "medium"
|
||||
|
||||
alert = FoodSafetyAlert(
|
||||
tenant_id=temp_log.tenant_id,
|
||||
alert_code=alert_code,
|
||||
alert_type=FoodSafetyAlertType.TEMPERATURE_VIOLATION,
|
||||
severity=severity,
|
||||
risk_level="high" if severity == "critical" else "medium",
|
||||
source_entity_type="temperature_log",
|
||||
source_entity_id=temp_log.id,
|
||||
title=f"Temperature violation in {temp_log.storage_location}",
|
||||
description=f"Temperature reading of {temp_log.temperature_celsius}°C is outside acceptable range",
|
||||
regulatory_action_required=severity == "critical",
|
||||
trigger_condition="temperature_out_of_range",
|
||||
threshold_value=target_max,
|
||||
actual_value=temp_log.temperature_celsius,
|
||||
alert_data={
|
||||
"location": temp_log.storage_location,
|
||||
"equipment_id": temp_log.equipment_id,
|
||||
"target_range": f"{target_min}°C - {target_max}°C"
|
||||
},
|
||||
environmental_factors={
|
||||
"temperature": temp_log.temperature_celsius,
|
||||
"humidity": temp_log.humidity_percentage
|
||||
},
|
||||
first_occurred_at=datetime.now(),
|
||||
last_occurred_at=datetime.now()
|
||||
)
|
||||
|
||||
db.add(alert)
|
||||
await db.flush()
|
||||
|
||||
# Send notifications
|
||||
await self._send_alert_notifications(alert)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to create temperature alert", error=str(e))
|
||||
|
||||
async def _check_compliance_alerts(self, db, compliance: FoodSafetyCompliance):
|
||||
"""Check for compliance-related alerts"""
|
||||
try:
|
||||
alerts_to_create = []
|
||||
|
||||
# Check for expiring certifications
|
||||
if compliance.expiration_date:
|
||||
days_to_expiry = (compliance.expiration_date - datetime.now()).days
|
||||
if days_to_expiry <= settings.CERTIFICATION_EXPIRY_WARNING_DAYS:
|
||||
alert_code = f"CERT-{uuid.uuid4().hex[:8].upper()}"
|
||||
severity = "critical" if days_to_expiry <= 7 else "high"
|
||||
|
||||
alert = FoodSafetyAlert(
|
||||
tenant_id=compliance.tenant_id,
|
||||
alert_code=alert_code,
|
||||
alert_type=FoodSafetyAlertType.CERTIFICATION_EXPIRY,
|
||||
severity=severity,
|
||||
risk_level="high",
|
||||
source_entity_type="compliance",
|
||||
source_entity_id=compliance.id,
|
||||
ingredient_id=compliance.ingredient_id,
|
||||
title=f"Certification expiring soon - {compliance.standard.value}",
|
||||
description=f"Certification expires in {days_to_expiry} days",
|
||||
regulatory_action_required=True,
|
||||
compliance_standard=compliance.standard,
|
||||
first_occurred_at=datetime.now(),
|
||||
last_occurred_at=datetime.now()
|
||||
)
|
||||
alerts_to_create.append(alert)
|
||||
|
||||
# Check for overdue audits
|
||||
if compliance.next_audit_date and compliance.next_audit_date < datetime.now():
|
||||
alert_code = f"AUDIT-{uuid.uuid4().hex[:8].upper()}"
|
||||
|
||||
alert = FoodSafetyAlert(
|
||||
tenant_id=compliance.tenant_id,
|
||||
alert_code=alert_code,
|
||||
alert_type=FoodSafetyAlertType.CERTIFICATION_EXPIRY,
|
||||
severity="high",
|
||||
risk_level="medium",
|
||||
source_entity_type="compliance",
|
||||
source_entity_id=compliance.id,
|
||||
ingredient_id=compliance.ingredient_id,
|
||||
title=f"Audit overdue - {compliance.standard.value}",
|
||||
description="Scheduled audit is overdue",
|
||||
regulatory_action_required=True,
|
||||
compliance_standard=compliance.standard,
|
||||
first_occurred_at=datetime.now(),
|
||||
last_occurred_at=datetime.now()
|
||||
)
|
||||
alerts_to_create.append(alert)
|
||||
|
||||
# Add alerts to database
|
||||
for alert in alerts_to_create:
|
||||
db.add(alert)
|
||||
|
||||
if alerts_to_create:
|
||||
await db.flush()
|
||||
|
||||
# Send notifications
|
||||
for alert in alerts_to_create:
|
||||
await self._send_alert_notifications(alert)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to check compliance alerts", error=str(e))
|
||||
|
||||
async def _send_alert_notifications(self, alert: FoodSafetyAlert):
|
||||
"""Send notifications for food safety alerts"""
|
||||
try:
|
||||
if not settings.ENABLE_EMAIL_ALERTS:
|
||||
return
|
||||
|
||||
# Determine notification methods based on severity
|
||||
notification_methods = ["dashboard"]
|
||||
|
||||
if alert.severity in ["high", "critical"]:
|
||||
notification_methods.extend(["email"])
|
||||
|
||||
if settings.ENABLE_SMS_ALERTS and alert.severity == "critical":
|
||||
notification_methods.append("sms")
|
||||
|
||||
if settings.ENABLE_WHATSAPP_ALERTS and alert.public_health_risk:
|
||||
notification_methods.append("whatsapp")
|
||||
|
||||
# Send notification via notification service
|
||||
if self.notification_client:
|
||||
await self.notification_client.send_alert(
|
||||
str(alert.tenant_id),
|
||||
{
|
||||
"alert_id": str(alert.id),
|
||||
"alert_type": alert.alert_type.value,
|
||||
"severity": alert.severity,
|
||||
"title": alert.title,
|
||||
"description": alert.description,
|
||||
"methods": notification_methods,
|
||||
"regulatory_action_required": alert.regulatory_action_required,
|
||||
"public_health_risk": alert.public_health_risk
|
||||
}
|
||||
)
|
||||
|
||||
# Update alert with notification status
|
||||
alert.notification_sent = True
|
||||
alert.notification_methods = notification_methods
|
||||
|
||||
except Exception as e:
|
||||
logger.warning("Failed to send alert notifications",
|
||||
alert_id=str(alert.id),
|
||||
error=str(e))
|
||||
Reference in New Issue
Block a user