import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Brain, TrendingUp, TrendingDown, Activity, AlertTriangle, Play, Sparkles } from 'lucide-react'; import { formatDistanceToNow } from 'date-fns'; import type { AnomalyDetection } from '@/hooks/admin/useAnomalyDetection'; import { useRunAnomalyDetection } from '@/hooks/admin/useAnomalyDetection'; interface AnomalyDetectionPanelProps { anomalies?: AnomalyDetection[]; isLoading: boolean; } const ANOMALY_TYPE_CONFIG = { spike: { icon: TrendingUp, label: 'Spike', color: 'text-orange-500' }, drop: { icon: TrendingDown, label: 'Drop', color: 'text-blue-500' }, trend_change: { icon: Activity, label: 'Trend Change', color: 'text-purple-500' }, outlier: { icon: AlertTriangle, label: 'Outlier', color: 'text-yellow-500' }, pattern_break: { icon: Activity, label: 'Pattern Break', color: 'text-red-500' }, }; const SEVERITY_CONFIG = { critical: { badge: 'destructive', label: 'Critical' }, high: { badge: 'default', label: 'High' }, medium: { badge: 'secondary', label: 'Medium' }, low: { badge: 'outline', label: 'Low' }, }; export function AnomalyDetectionPanel({ anomalies, isLoading }: AnomalyDetectionPanelProps) { const runDetection = useRunAnomalyDetection(); const handleRunDetection = () => { runDetection.mutate(); }; if (isLoading) { return ( ML Anomaly Detection Loading anomaly data...
); } const recentAnomalies = anomalies?.slice(0, 5) || []; return ( ML Anomaly Detection
{anomalies && anomalies.length > 0 && ( {anomalies.length} detected (24h) )}
Statistical ML algorithms detecting unusual patterns in metrics
{recentAnomalies.length === 0 ? (

No anomalies detected in last 24 hours

ML models are monitoring metrics continuously

) : ( <> {recentAnomalies.map((anomaly) => { const typeConfig = ANOMALY_TYPE_CONFIG[anomaly.anomaly_type]; const severityConfig = SEVERITY_CONFIG[anomaly.severity]; const TypeIcon = typeConfig.icon; return (
{severityConfig.label} {typeConfig.label} {anomaly.metric_name.replace(/_/g, ' ')} {anomaly.alert_created && ( Alert Created )}
Baseline: {anomaly.baseline_value.toFixed(2)} Detected: {anomaly.anomaly_value.toFixed(2)} {anomaly.deviation_score.toFixed(2)}σ
Algorithm: {anomaly.detection_algorithm.replace(/_/g, ' ')} Confidence: {(anomaly.confidence_score * 100).toFixed(0)}% Detected {formatDistanceToNow(new Date(anomaly.detected_at), { addSuffix: true })}
); })} {anomalies && anomalies.length > 5 && (
+ {anomalies.length - 5} more anomalies
)} )}
); }