This commit is contained in:
Iliyan Angelov
2025-09-19 11:58:53 +03:00
parent 306b20e24a
commit 6b247e5b9f
11423 changed files with 1500615 additions and 778 deletions

View File

@@ -0,0 +1,186 @@
"""
Management command to set up the incident-centric chat system
"""
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone
from ...models import ChatBot, WarRoom
from incident_intelligence.models import Incident
User = get_user_model()
class Command(BaseCommand):
help = 'Set up the incident-centric chat system with default bots and configurations'
def add_arguments(self, parser):
parser.add_argument(
'--create-bots',
action='store_true',
help='Create default AI assistant bots',
)
parser.add_argument(
'--create-war-rooms',
action='store_true',
help='Create war rooms for existing incidents',
)
parser.add_argument(
'--force',
action='store_true',
help='Force recreation of existing bots and war rooms',
)
def handle(self, *args, **options):
self.stdout.write(
self.style.SUCCESS('Setting up incident-centric chat system...')
)
if options['create_bots']:
self.create_default_bots(options['force'])
if options['create_war_rooms']:
self.create_war_rooms_for_incidents(options['force'])
self.stdout.write(
self.style.SUCCESS('Chat system setup completed successfully!')
)
def create_default_bots(self, force=False):
"""Create default AI assistant bots"""
self.stdout.write('Creating default AI assistant bots...')
bots_config = [
{
'name': 'Incident Assistant',
'bot_type': 'INCIDENT_ASSISTANT',
'description': 'AI assistant for incident management and response guidance',
'auto_respond': True,
'response_triggers': ['help', 'assist', 'guidance', 'incident', 'problem']
},
{
'name': 'Knowledge Bot',
'bot_type': 'KNOWLEDGE_BOT',
'description': 'AI assistant for knowledge base queries and documentation',
'auto_respond': False,
'response_triggers': ['how', 'what', 'where', 'documentation', 'knowledge']
},
{
'name': 'Automation Bot',
'bot_type': 'AUTOMATION_BOT',
'description': 'AI assistant for automation and runbook execution',
'auto_respond': False,
'response_triggers': ['runbook', 'automation', 'execute', 'playbook']
},
{
'name': 'Compliance Bot',
'bot_type': 'COMPLIANCE_BOT',
'description': 'AI assistant for compliance and audit requirements',
'auto_respond': False,
'response_triggers': ['compliance', 'audit', 'policy', 'retention']
}
]
for bot_config in bots_config:
bot, created = ChatBot.objects.get_or_create(
name=bot_config['name'],
defaults={
'bot_type': bot_config['bot_type'],
'description': bot_config['description'],
'is_active': True,
'auto_respond': bot_config['auto_respond'],
'response_triggers': bot_config['response_triggers']
}
)
if created:
self.stdout.write(
self.style.SUCCESS(f'Created bot: {bot.name}')
)
elif force:
bot.bot_type = bot_config['bot_type']
bot.description = bot_config['description']
bot.auto_respond = bot_config['auto_respond']
bot.response_triggers = bot_config['response_triggers']
bot.save()
self.stdout.write(
self.style.WARNING(f'Updated bot: {bot.name}')
)
else:
self.stdout.write(
self.style.WARNING(f'Bot already exists: {bot.name}')
)
def create_war_rooms_for_incidents(self, force=False):
"""Create war rooms for existing incidents"""
self.stdout.write('Creating war rooms for existing incidents...')
incidents = Incident.objects.all()
created_count = 0
updated_count = 0
for incident in incidents:
war_room, created = WarRoom.objects.get_or_create(
incident=incident,
defaults={
'name': f"Incident {incident.id} - {incident.title[:50]}",
'description': f"War room for incident: {incident.title}",
'created_by': incident.reporter,
'privacy_level': 'PRIVATE',
'status': 'ACTIVE'
}
)
if created:
# Add incident reporter and assignee to war room
if incident.reporter:
war_room.add_participant(incident.reporter)
if incident.assigned_to:
war_room.add_participant(incident.assigned_to)
created_count += 1
self.stdout.write(
self.style.SUCCESS(f'Created war room for incident: {incident.title}')
)
elif force:
war_room.name = f"Incident {incident.id} - {incident.title[:50]}"
war_room.description = f"War room for incident: {incident.title}"
war_room.save()
updated_count += 1
self.stdout.write(
self.style.WARNING(f'Updated war room for incident: {incident.title}')
)
self.stdout.write(
self.style.SUCCESS(
f'Created {created_count} new war rooms, updated {updated_count} existing war rooms'
)
)
def create_sample_data(self):
"""Create sample data for testing"""
self.stdout.write('Creating sample data...')
# Create a sample incident if none exist
if not Incident.objects.exists():
sample_incident = Incident.objects.create(
title='Sample Database Connection Issue',
description='Database connection timeout affecting user authentication',
severity='HIGH',
status='OPEN',
category='Database',
subcategory='Connection'
)
# Create war room for sample incident
WarRoom.objects.create(
incident=sample_incident,
name=f"Incident {sample_incident.id} - {sample_incident.title}",
description=f"War room for incident: {sample_incident.title}",
privacy_level='PRIVATE',
status='ACTIVE'
)
self.stdout.write(
self.style.SUCCESS('Created sample incident and war room')
)