import React, { useState, useMemo } from 'react'; import { useSource, useAppState, useAction } from './sdk'; import { Badge, Metric, Glass, Icon } from './components'; import { LineChart, Line, BarChart, Bar, PieChart, Pie, Cell, AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend } from 'recharts'; import { TrendingUp, Activity, AlertTriangle, CheckCircle, Zap, Shield, BarChart3, Clock, Star, Brain, GitBranch, Target } from 'lucide-react'; // MOCK DATA - ML Monitoring realistic data const MOCK_MODELS = [ { id: 'mdl-001', name: 'Customer Churn Predictor', version: 'v2.3.1', accuracy: 94.2, precision: 92.8, recall: 91.5, f1: 92.1, drift_score: 0.12, status: 'healthy', last_trained: '2024-01-10', predictions_24h: 12450 }, { id: 'mdl-002', name: 'Fraud Detection Engine', version: 'v1.8.4', accuracy: 98.7, precision: 97.2, recall: 96.8, f1: 97.0, drift_score: 0.08, status: 'healthy', last_trained: '2024-01-12', predictions_24h: 45200 }, { id: 'mdl-003', name: 'Recommendation System', version: 'v3.1.0', accuracy: 87.3, precision: 85.1, recall: 88.9, f1: 86.9, drift_score: 0.34, status: 'warning', last_trained: '2024-01-05', predictions_24h: 89300 }, { id: 'mdl-004', name: 'Sentiment Analyzer', version: 'v2.0.2', accuracy: 91.5, precision: 90.2, recall: 89.7, f1: 89.9, drift_score: 0.52, status: 'critical', last_trained: '2023-12-28', predictions_24h: 23100 }, ]; const MOCK_ACCURACY_HISTORY = [ { date: '2024-01-01', accuracy: 93.5, precision: 91.2, recall: 90.8 }, { date: '2024-01-03', accuracy: 94.1, precision: 92.0, recall: 91.2 }, { date: '2024-01-05', accuracy: 93.8, precision: 91.8, recall: 91.5 }, { date: '2024-01-07', accuracy: 94.5, precision: 92.5, recall: 92.0 }, { date: '2024-01-09', accuracy: 94.2, precision: 92.8, recall: 91.5 }, { date: '2024-01-11', accuracy: 94.8, precision: 93.1, recall: 92.3 }, { date: '2024-01-13', accuracy: 94.2, precision: 92.8, recall: 91.5 }, ]; const MOCK_PREDICTION_DISTRIBUTION = [ { label: 'Class A', value: 35, color: '#8b5cf6' }, { label: 'Class B', value: 28, color: '#06b6d4' }, { label: 'Class C', value: 22, color: '#10b981' }, { label: 'Class D', value: 15, color: '#f59e0b' }, ]; const MOCK_DRIFT_METRICS = [ { feature: 'user_age', drift: 0.15, threshold: 0.25, status: 'normal' }, { feature: 'transaction_amount', drift: 0.42, threshold: 0.25, status: 'high' }, { feature: 'session_duration', drift: 0.18, threshold: 0.25, status: 'normal' }, { feature: 'device_type', drift: 0.31, threshold: 0.25, status: 'elevated' }, { feature: 'location_region', drift: 0.09, threshold: 0.25, status: 'normal' }, { feature: 'purchase_frequency', drift: 0.28, threshold: 0.25, status: 'elevated' }, ]; export default function App() { const [activeTab, setActiveTab] = useState('overview'); const [selectedModel, setSelectedModel] = useState(null); const { data, loading, error, refetch } = useSource('/api/framework/connections/app/ml-model-monitoring/data', 30000); const dispatch = useAction(); // Use live data if available, otherwise mock data const models = useMemo(() => { const liveModels = data?.providers?.ml_service?.data?.models || []; return liveModels.length > 0 ? liveModels : MOCK_MODELS; }, [data]); const accuracyHistory = useMemo(() => { const liveHistory = data?.providers?.ml_service?.data?.history || []; return liveHistory.length > 0 ? liveHistory : MOCK_ACCURACY_HISTORY; }, [data]); const predictionDist = useMemo(() => { const liveDist = data?.providers?.ml_service?.data?.distribution || []; return liveDist.length > 0 ? liveDist : MOCK_PREDICTION_DISTRIBUTION; }, [data]); const driftMetrics = useMemo(() => { const liveDrift = data?.providers?.ml_service?.data?.drift || []; return liveDrift.length > 0 ? liveDrift : MOCK_DRIFT_METRICS; }, [data]); const avgAccuracy = useMemo(() => { const sum = models.reduce((acc, m) => acc + (m.accuracy || 0), 0); return models.length > 0 ? (sum / models.length).toFixed(1) : '0'; }, [models]); const avgDrift = useMemo(() => { const sum = models.reduce((acc, m) => acc + (m.drift_score || 0), 0); return models.length > 0 ? (sum / models.length).toFixed(2) : '0'; }, [models]); const totalPredictions = useMemo(() => { return models.reduce((acc, m) => acc + (m.predictions_24h || 0), 0).toLocaleString(); }, [models]); const healthyCount = models.filter(m => m.status === 'healthy').length; const warningCount = models.filter(m => m.status === 'warning').length; const criticalCount = models.filter(m => m.status === 'critical').length; const getStatusBadge = (status: string) => { const variants: any = { healthy: { variant: 'positive' as const, icon: 'check-circle' }, warning: { variant: 'neutral' as const, icon: 'alert-triangle' }, critical: { variant: 'negative' as const, icon: 'alert-circle' }, }; const config = variants[status] || variants.healthy; return ; }; const getDriftStatus = (drift: number, threshold: number) => { if (drift < threshold * 0.5) return { color: '#10b981', label: 'Normal' }; if (drift < threshold) return { color: '#f59e0b', label: 'Elevated' }; return { color: '#ef4444', label: 'High' }; }; return (
{/* Header */}

ML Model Monitoring

Real-time model performance & drift detection

{/* Tabs */}
{[ { id: 'overview', label: 'Overview', icon: 'bar-chart-3' }, { id: 'models', label: 'Models', icon: 'git-branch' }, { id: 'drift', label: 'Drift Analysis', icon: 'trending-up' }, { id: 'predictions', label: 'Predictions', icon: 'target' }, ].map(tab => ( ))}
{/* Error Banner - Inline, not full page */} {error && (
Connection issue: {error}. Using cached data.
)}
{/* OVERVIEW TAB */} {activeTab === 'overview' && ( <> {/* KPI Row */}
0.3 ? 'down' : 'up'} trendLabel={parseFloat(avgDrift) > 0.3 ? 'Action needed' : 'Within threshold'} />
{/* Status Summary */}

Model Health

Healthy {healthyCount}
Warning {warningCount}
Critical {criticalCount}

Accuracy Trend

d.slice(5)} />
{/* Models Table */}

Active Models

{models.map((model: any) => ( setSelectedModel(model)} > ))}
Model Version Accuracy Drift Status Predictions (24h)
{model.name}
{model.id}
{model.version}
{model.accuracy}%
0.3 ? 'text-red-400' : 'text-slate-300'}`}> {model.drift_score.toFixed(2)} {getStatusBadge(model.status)} {model.predictions_24h.toLocaleString()}
)} {/* MODELS TAB */} {activeTab === 'models' && (
{models.map((model: any) => ( setSelectedModel(model)} >

{model.name}

{model.id} • {model.version}

{getStatusBadge(model.status)}
Accuracy
{model.accuracy}%
F1 Score
{model.f1}
Precision
{model.precision}
Recall
{model.recall}
Last trained: {model.last_trained} {model.predictions_24h.toLocaleString()} predictions
))}
)} {/* DRIFT TAB */} {activeTab === 'drift' && ( <>

Feature Drift Analysis

{driftMetrics.map((metric: any, i: number) => { const status = getDriftStatus(metric.drift, metric.threshold); return (
{metric.feature}
{metric.drift.toFixed(2)}
Drift score
Threshold: {metric.threshold}
metric.threshold ? 'text-red-400' : 'text-emerald-400'}`}> {metric.drift > metric.threshold ? '⚠️ Exceeded' : '✓ Within limits'}
); })}
)} {/* PREDICTIONS TAB */} {activeTab === 'predictions' && ( <>

Prediction Distribution

{predictionDist.map((entry: any, index: number) => ( ))}
{predictionDist.map((item: any, i: number) => (
{item.label}
{item.value}%
))}

Prediction Volume (24h)

[value.toLocaleString(), 'Predictions']} />
)}
{/* Model Detail Modal */} {selectedModel && (
setSelectedModel(null)} > e.stopPropagation()} >

{selectedModel.name}

{selectedModel.id} • Version {selectedModel.version}

{getStatusBadge(selectedModel.status)}
Accuracy
{selectedModel.accuracy}%
Precision
{selectedModel.precision}
Recall
{selectedModel.recall}
F1 Score
{selectedModel.f1}
Drift Score
0.3 ? 'text-red-400' : 'text-emerald-400'}`}> {selectedModel.drift_score.toFixed(2)}
{selectedModel.drift_score > 0.3 ? '⚠️ Requires attention' : '✓ Within acceptable range'}
Predictions (24h)
{selectedModel.predictions_24h.toLocaleString()}
+12.5% from yesterday
Training History
Last trained: {selectedModel.last_trained}
Next scheduled: 2024-01-20
)}
); }