Updates
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
Management command to run anomaly detection
|
||||
"""
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from analytics_predictive_insights.models import PredictiveModel
|
||||
from analytics_predictive_insights.ml.anomaly_detection import AnomalyDetectionService
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Run anomaly detection using active models"""
|
||||
|
||||
help = 'Run anomaly detection using all active anomaly detection models'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--model-id',
|
||||
type=str,
|
||||
help='Run anomaly detection for a specific model ID only'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--time-window',
|
||||
type=int,
|
||||
default=24,
|
||||
help='Time window in hours for anomaly detection (default: 24)'
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Handle the command execution"""
|
||||
model_id = options.get('model_id')
|
||||
time_window = options.get('time_window', 24)
|
||||
|
||||
try:
|
||||
# Initialize anomaly detection service
|
||||
anomaly_service = AnomalyDetectionService()
|
||||
|
||||
self.stdout.write('Starting anomaly detection...')
|
||||
|
||||
# Run anomaly detection
|
||||
total_anomalies = anomaly_service.run_anomaly_detection(model_id)
|
||||
|
||||
if total_anomalies > 0:
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'✓ Detected {total_anomalies} anomalies')
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
self.style.WARNING('⚠ No anomalies detected')
|
||||
)
|
||||
|
||||
# Get summary
|
||||
summary = anomaly_service.get_anomaly_summary(time_window)
|
||||
|
||||
self.stdout.write('\nAnomaly Summary:')
|
||||
self.stdout.write(f' Total anomalies: {summary["total_anomalies"]}')
|
||||
self.stdout.write(f' Critical: {summary["critical_anomalies"]}')
|
||||
self.stdout.write(f' High: {summary["high_anomalies"]}')
|
||||
self.stdout.write(f' Medium: {summary["medium_anomalies"]}')
|
||||
self.stdout.write(f' Low: {summary["low_anomalies"]}')
|
||||
self.stdout.write(f' Unresolved: {summary["unresolved_anomalies"]}')
|
||||
self.stdout.write(f' False positive rate: {summary["false_positive_rate"]:.2f}%')
|
||||
|
||||
except Exception as e:
|
||||
raise CommandError(f'Error running anomaly detection: {str(e)}')
|
||||
Reference in New Issue
Block a user