Updates
This commit is contained in:
61
ETB-API/incident_intelligence/signals.py
Normal file
61
ETB-API/incident_intelligence/signals.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
Django signals for incident intelligence
|
||||
"""
|
||||
from django.db.models.signals import post_save, pre_save
|
||||
from django.dispatch import receiver
|
||||
from django.utils import timezone
|
||||
import logging
|
||||
|
||||
from .models import Incident
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@receiver(post_save, sender=Incident)
|
||||
def incident_post_save(sender, instance, created, **kwargs):
|
||||
"""
|
||||
Handle incident post-save events
|
||||
"""
|
||||
if created:
|
||||
logger.info(f"New incident created: {instance.id}")
|
||||
|
||||
# Trigger AI processing for new incidents
|
||||
try:
|
||||
from .tasks import process_incident_ai
|
||||
process_incident_ai.delay(instance.id)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to trigger AI processing for incident {instance.id}: {e}")
|
||||
|
||||
# Update resolution time if status changed to resolved
|
||||
if not created and instance.status in ['RESOLVED', 'CLOSED'] and not instance.resolved_at:
|
||||
instance.resolved_at = timezone.now()
|
||||
instance.save(update_fields=['resolved_at'])
|
||||
|
||||
|
||||
@receiver(pre_save, sender=Incident)
|
||||
def incident_pre_save(sender, instance, **kwargs):
|
||||
"""
|
||||
Handle incident pre-save events
|
||||
"""
|
||||
# Auto-assign priority based on severity
|
||||
if instance.severity and not instance.priority:
|
||||
severity_priority_map = {
|
||||
'EMERGENCY': 'P1',
|
||||
'CRITICAL': 'P1',
|
||||
'HIGH': 'P2',
|
||||
'MEDIUM': 'P3',
|
||||
'LOW': 'P4'
|
||||
}
|
||||
instance.priority = severity_priority_map.get(instance.severity, 'P3')
|
||||
|
||||
# Update AI processing flag if key fields changed
|
||||
if instance.pk:
|
||||
try:
|
||||
old_instance = Incident.objects.get(pk=instance.pk)
|
||||
ai_relevant_fields = ['title', 'description', 'free_text', 'category', 'subcategory', 'severity']
|
||||
|
||||
if any(getattr(old_instance, field) != getattr(instance, field) for field in ai_relevant_fields):
|
||||
instance.ai_processed = False
|
||||
instance.ai_processing_error = None
|
||||
except Incident.DoesNotExist:
|
||||
pass
|
||||
Reference in New Issue
Block a user