Delete legacy alerts
This commit is contained in:
@@ -9,12 +9,10 @@ from .production_batch_repository import ProductionBatchRepository
|
||||
from .production_schedule_repository import ProductionScheduleRepository
|
||||
from .production_capacity_repository import ProductionCapacityRepository
|
||||
from .quality_check_repository import QualityCheckRepository
|
||||
from .production_alert_repository import ProductionAlertRepository
|
||||
|
||||
__all__ = [
|
||||
"ProductionBatchRepository",
|
||||
"ProductionScheduleRepository",
|
||||
"ProductionCapacityRepository",
|
||||
"QualityCheckRepository",
|
||||
"ProductionAlertRepository"
|
||||
]
|
||||
@@ -1,379 +0,0 @@
|
||||
"""
|
||||
Production Alert Repository
|
||||
Repository for production alert operations
|
||||
"""
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, and_, text, desc, func
|
||||
from datetime import datetime, timedelta, date
|
||||
from uuid import UUID
|
||||
import structlog
|
||||
|
||||
from .base import ProductionBaseRepository
|
||||
from app.models.production import ProductionAlert, AlertSeverity
|
||||
from shared.database.exceptions import DatabaseError, ValidationError
|
||||
from shared.database.transactions import transactional
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
class ProductionAlertRepository(ProductionBaseRepository):
|
||||
"""Repository for production alert operations"""
|
||||
|
||||
def __init__(self, session: AsyncSession, cache_ttl: Optional[int] = 60):
|
||||
# Alerts are very dynamic, very short cache time (1 minute)
|
||||
super().__init__(ProductionAlert, session, cache_ttl)
|
||||
|
||||
@transactional
|
||||
async def create_alert(self, alert_data: Dict[str, Any]) -> ProductionAlert:
|
||||
"""Create a new production alert with validation"""
|
||||
try:
|
||||
# Validate alert data
|
||||
validation_result = self._validate_production_data(
|
||||
alert_data,
|
||||
["tenant_id", "alert_type", "title", "message"]
|
||||
)
|
||||
|
||||
if not validation_result["is_valid"]:
|
||||
raise ValidationError(f"Invalid alert data: {validation_result['errors']}")
|
||||
|
||||
# Set default values
|
||||
if "severity" not in alert_data:
|
||||
alert_data["severity"] = AlertSeverity.MEDIUM
|
||||
if "source_system" not in alert_data:
|
||||
alert_data["source_system"] = "production"
|
||||
if "is_active" not in alert_data:
|
||||
alert_data["is_active"] = True
|
||||
if "is_acknowledged" not in alert_data:
|
||||
alert_data["is_acknowledged"] = False
|
||||
if "is_resolved" not in alert_data:
|
||||
alert_data["is_resolved"] = False
|
||||
|
||||
# Create alert
|
||||
alert = await self.create(alert_data)
|
||||
|
||||
logger.info("Production alert created successfully",
|
||||
alert_id=str(alert.id),
|
||||
alert_type=alert.alert_type,
|
||||
severity=alert.severity.value if alert.severity else None,
|
||||
tenant_id=str(alert.tenant_id))
|
||||
|
||||
return alert
|
||||
|
||||
except ValidationError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Error creating production alert", error=str(e))
|
||||
raise DatabaseError(f"Failed to create production alert: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def get_active_alerts(
|
||||
self,
|
||||
tenant_id: str,
|
||||
severity: Optional[AlertSeverity] = None
|
||||
) -> List[ProductionAlert]:
|
||||
"""Get active production alerts for a tenant"""
|
||||
try:
|
||||
filters = {
|
||||
"tenant_id": tenant_id,
|
||||
"is_active": True,
|
||||
"is_resolved": False
|
||||
}
|
||||
|
||||
if severity:
|
||||
filters["severity"] = severity
|
||||
|
||||
alerts = await self.get_multi(
|
||||
filters=filters,
|
||||
order_by="created_at",
|
||||
order_desc=True
|
||||
)
|
||||
|
||||
logger.info("Retrieved active production alerts",
|
||||
count=len(alerts),
|
||||
severity=severity.value if severity else "all",
|
||||
tenant_id=tenant_id)
|
||||
|
||||
return alerts
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error fetching active alerts", error=str(e))
|
||||
raise DatabaseError(f"Failed to fetch active alerts: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def get_alerts_by_type(
|
||||
self,
|
||||
tenant_id: str,
|
||||
alert_type: str,
|
||||
include_resolved: bool = False
|
||||
) -> List[ProductionAlert]:
|
||||
"""Get production alerts by type"""
|
||||
try:
|
||||
filters = {
|
||||
"tenant_id": tenant_id,
|
||||
"alert_type": alert_type
|
||||
}
|
||||
|
||||
if not include_resolved:
|
||||
filters["is_resolved"] = False
|
||||
|
||||
alerts = await self.get_multi(
|
||||
filters=filters,
|
||||
order_by="created_at",
|
||||
order_desc=True
|
||||
)
|
||||
|
||||
logger.info("Retrieved alerts by type",
|
||||
count=len(alerts),
|
||||
alert_type=alert_type,
|
||||
include_resolved=include_resolved,
|
||||
tenant_id=tenant_id)
|
||||
|
||||
return alerts
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error fetching alerts by type", error=str(e))
|
||||
raise DatabaseError(f"Failed to fetch alerts by type: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def get_alerts_by_batch(
|
||||
self,
|
||||
tenant_id: str,
|
||||
batch_id: str
|
||||
) -> List[ProductionAlert]:
|
||||
"""Get production alerts for a specific batch"""
|
||||
try:
|
||||
alerts = await self.get_multi(
|
||||
filters={
|
||||
"tenant_id": tenant_id,
|
||||
"batch_id": batch_id
|
||||
},
|
||||
order_by="created_at",
|
||||
order_desc=True
|
||||
)
|
||||
|
||||
logger.info("Retrieved alerts by batch",
|
||||
count=len(alerts),
|
||||
batch_id=batch_id,
|
||||
tenant_id=tenant_id)
|
||||
|
||||
return alerts
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error fetching alerts by batch", error=str(e))
|
||||
raise DatabaseError(f"Failed to fetch alerts by batch: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def acknowledge_alert(
|
||||
self,
|
||||
alert_id: UUID,
|
||||
acknowledged_by: str,
|
||||
acknowledgment_notes: Optional[str] = None
|
||||
) -> ProductionAlert:
|
||||
"""Acknowledge a production alert"""
|
||||
try:
|
||||
alert = await self.get(alert_id)
|
||||
if not alert:
|
||||
raise ValidationError(f"Alert {alert_id} not found")
|
||||
|
||||
if alert.is_acknowledged:
|
||||
raise ValidationError("Alert is already acknowledged")
|
||||
|
||||
update_data = {
|
||||
"is_acknowledged": True,
|
||||
"acknowledged_by": acknowledged_by,
|
||||
"acknowledged_at": datetime.utcnow(),
|
||||
"updated_at": datetime.utcnow()
|
||||
}
|
||||
|
||||
if acknowledgment_notes:
|
||||
current_actions = alert.actions_taken or []
|
||||
current_actions.append({
|
||||
"action": "acknowledged",
|
||||
"by": acknowledged_by,
|
||||
"at": datetime.utcnow().isoformat(),
|
||||
"notes": acknowledgment_notes
|
||||
})
|
||||
update_data["actions_taken"] = current_actions
|
||||
|
||||
alert = await self.update(alert_id, update_data)
|
||||
|
||||
logger.info("Acknowledged production alert",
|
||||
alert_id=str(alert_id),
|
||||
acknowledged_by=acknowledged_by)
|
||||
|
||||
return alert
|
||||
|
||||
except ValidationError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Error acknowledging alert", error=str(e))
|
||||
raise DatabaseError(f"Failed to acknowledge alert: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def resolve_alert(
|
||||
self,
|
||||
alert_id: UUID,
|
||||
resolved_by: str,
|
||||
resolution_notes: str
|
||||
) -> ProductionAlert:
|
||||
"""Resolve a production alert"""
|
||||
try:
|
||||
alert = await self.get(alert_id)
|
||||
if not alert:
|
||||
raise ValidationError(f"Alert {alert_id} not found")
|
||||
|
||||
if alert.is_resolved:
|
||||
raise ValidationError("Alert is already resolved")
|
||||
|
||||
update_data = {
|
||||
"is_resolved": True,
|
||||
"is_active": False,
|
||||
"resolved_by": resolved_by,
|
||||
"resolved_at": datetime.utcnow(),
|
||||
"resolution_notes": resolution_notes,
|
||||
"updated_at": datetime.utcnow()
|
||||
}
|
||||
|
||||
# Add to actions taken
|
||||
current_actions = alert.actions_taken or []
|
||||
current_actions.append({
|
||||
"action": "resolved",
|
||||
"by": resolved_by,
|
||||
"at": datetime.utcnow().isoformat(),
|
||||
"notes": resolution_notes
|
||||
})
|
||||
update_data["actions_taken"] = current_actions
|
||||
|
||||
alert = await self.update(alert_id, update_data)
|
||||
|
||||
logger.info("Resolved production alert",
|
||||
alert_id=str(alert_id),
|
||||
resolved_by=resolved_by)
|
||||
|
||||
return alert
|
||||
|
||||
except ValidationError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Error resolving alert", error=str(e))
|
||||
raise DatabaseError(f"Failed to resolve alert: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def get_alert_statistics(
|
||||
self,
|
||||
tenant_id: str,
|
||||
start_date: date,
|
||||
end_date: date
|
||||
) -> Dict[str, Any]:
|
||||
"""Get alert statistics for a tenant and date range"""
|
||||
try:
|
||||
start_datetime = datetime.combine(start_date, datetime.min.time())
|
||||
end_datetime = datetime.combine(end_date, datetime.max.time())
|
||||
|
||||
alerts = await self.get_multi(
|
||||
filters={
|
||||
"tenant_id": tenant_id,
|
||||
"created_at__gte": start_datetime,
|
||||
"created_at__lte": end_datetime
|
||||
}
|
||||
)
|
||||
|
||||
total_alerts = len(alerts)
|
||||
active_alerts = len([a for a in alerts if a.is_active])
|
||||
acknowledged_alerts = len([a for a in alerts if a.is_acknowledged])
|
||||
resolved_alerts = len([a for a in alerts if a.is_resolved])
|
||||
|
||||
# Group by severity
|
||||
by_severity = {}
|
||||
for severity in AlertSeverity:
|
||||
severity_alerts = [a for a in alerts if a.severity == severity]
|
||||
by_severity[severity.value] = {
|
||||
"total": len(severity_alerts),
|
||||
"active": len([a for a in severity_alerts if a.is_active]),
|
||||
"resolved": len([a for a in severity_alerts if a.is_resolved])
|
||||
}
|
||||
|
||||
# Group by alert type
|
||||
by_type = {}
|
||||
for alert in alerts:
|
||||
alert_type = alert.alert_type
|
||||
if alert_type not in by_type:
|
||||
by_type[alert_type] = {
|
||||
"total": 0,
|
||||
"active": 0,
|
||||
"resolved": 0
|
||||
}
|
||||
|
||||
by_type[alert_type]["total"] += 1
|
||||
if alert.is_active:
|
||||
by_type[alert_type]["active"] += 1
|
||||
if alert.is_resolved:
|
||||
by_type[alert_type]["resolved"] += 1
|
||||
|
||||
# Calculate resolution time statistics
|
||||
resolved_with_times = [
|
||||
a for a in alerts
|
||||
if a.is_resolved and a.resolved_at and a.created_at
|
||||
]
|
||||
|
||||
resolution_times = []
|
||||
for alert in resolved_with_times:
|
||||
resolution_time = (alert.resolved_at - alert.created_at).total_seconds() / 3600 # hours
|
||||
resolution_times.append(resolution_time)
|
||||
|
||||
avg_resolution_time = sum(resolution_times) / len(resolution_times) if resolution_times else 0
|
||||
|
||||
return {
|
||||
"period_start": start_date.isoformat(),
|
||||
"period_end": end_date.isoformat(),
|
||||
"total_alerts": total_alerts,
|
||||
"active_alerts": active_alerts,
|
||||
"acknowledged_alerts": acknowledged_alerts,
|
||||
"resolved_alerts": resolved_alerts,
|
||||
"acknowledgment_rate": round((acknowledged_alerts / total_alerts * 100) if total_alerts > 0 else 0, 2),
|
||||
"resolution_rate": round((resolved_alerts / total_alerts * 100) if total_alerts > 0 else 0, 2),
|
||||
"average_resolution_time_hours": round(avg_resolution_time, 2),
|
||||
"by_severity": by_severity,
|
||||
"by_alert_type": by_type,
|
||||
"tenant_id": tenant_id
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error calculating alert statistics", error=str(e))
|
||||
raise DatabaseError(f"Failed to calculate alert statistics: {str(e)}")
|
||||
|
||||
@transactional
|
||||
async def cleanup_old_resolved_alerts(
|
||||
self,
|
||||
tenant_id: str,
|
||||
days_to_keep: int = 30
|
||||
) -> int:
|
||||
"""Clean up old resolved alerts"""
|
||||
try:
|
||||
cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep)
|
||||
|
||||
old_alerts = await self.get_multi(
|
||||
filters={
|
||||
"tenant_id": tenant_id,
|
||||
"is_resolved": True,
|
||||
"resolved_at__lt": cutoff_date
|
||||
}
|
||||
)
|
||||
|
||||
deleted_count = 0
|
||||
for alert in old_alerts:
|
||||
await self.delete(alert.id)
|
||||
deleted_count += 1
|
||||
|
||||
logger.info("Cleaned up old resolved alerts",
|
||||
deleted_count=deleted_count,
|
||||
days_to_keep=days_to_keep,
|
||||
tenant_id=tenant_id)
|
||||
|
||||
return deleted_count
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error cleaning up old alerts", error=str(e))
|
||||
raise DatabaseError(f"Failed to clean up old alerts: {str(e)}")
|
||||
Reference in New Issue
Block a user