File size: 24,733 Bytes
f5c6b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/**
 * Admin Dashboard Component
 * Comprehensive monitoring and management interface for Medical AI Platform
 * 
 * Features:
 * - Real-time system health monitoring
 * - Pipeline statistics and performance metrics
 * - Model performance tracking
 * - Cache management
 * - Alert management
 * - Review queue management
 * - Compliance overview
 */

import React, { useState, useEffect } from 'react';
import { Activity, AlertCircle, BarChart3, Database, GitBranch, Shield, Clock, TrendingUp } from 'lucide-react';

interface DashboardData {
  status: string;
  timestamp: string;
  system: {
    uptime_seconds: number;
    uptime_human: string;
    error_rate: number;
    total_requests: number;
    error_threshold: number;
    status: string;
  };
  pipeline: {
    total_jobs_processed: number;
    completed_jobs: number;
    failed_jobs: number;
    processing_jobs: number;
    success_rate: number;
  };
  models: {
    total_registered: number;
    performance: Record<string, ModelPerformance>;
  };
  synthesis: {
    total_syntheses: number;
    avg_confidence: number;
    requiring_review: number;
    avg_processing_time_ms: number;
  };
  cache: {
    total_entries: number;
    hits: number;
    misses: number;
    hit_rate: number;
    evictions: number;
    memory_usage_mb: number;
    avg_retrieval_time_ms: number;
    cache_efficiency: number;
  };
  alerts: {
    active_count: number;
    critical_count: number;
    recent: Alert[];
  };
  compliance: {
    hipaa_compliant: boolean;
    gdpr_compliant: boolean;
    audit_logging_active: boolean;
    phi_removal_active: boolean;
    encryption_enabled: boolean;
  };
  components: Record<string, string>;
}

interface ModelPerformance {
  version: string;
  total_inferences: number;
  avg_latency_ms: number;
  error_rate: number;
  last_used: string;
}

interface Alert {
  alert_id: string;
  level: string;
  message: string;
  category: string;
  timestamp: string;
  resolved: boolean;
  details: Record<string, any>;
}

const AdminDashboard: React.FC = () => {
  const [dashboardData, setDashboardData] = useState<DashboardData | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [activeTab, setActiveTab] = useState<'overview' | 'models' | 'cache' | 'alerts'>('overview');
  const [autoRefresh, setAutoRefresh] = useState(true);

  // Fetch dashboard data
  const fetchDashboard = async () => {
    try {
      const response = await fetch('/health/dashboard');
      if (!response.ok) {
        throw new Error('Failed to fetch dashboard data');
      }
      const data = await response.json();
      setDashboardData(data);
      setError(null);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Unknown error');
    } finally {
      setLoading(false);
    }
  };

  // Initial load and auto-refresh
  useEffect(() => {
    fetchDashboard();
    
    if (autoRefresh) {
      const interval = setInterval(fetchDashboard, 10000); // Refresh every 10 seconds
      return () => clearInterval(interval);
    }
  }, [autoRefresh]);

  // Status badge component
  const StatusBadge: React.FC<{ status: string }> = ({ status }) => {
    const colors = {
      operational: 'bg-green-100 text-green-800',
      healthy: 'bg-green-100 text-green-800',
      degraded: 'bg-yellow-100 text-yellow-800',
      critical: 'bg-red-100 text-red-800',
      ready: 'bg-blue-100 text-blue-800',
      active: 'bg-blue-100 text-blue-800'
    };
    
    const color = colors[status.toLowerCase() as keyof typeof colors] || 'bg-gray-100 text-gray-800';
    
    return (
      <span className={`px-2 py-1 rounded-full text-xs font-medium ${color}`}>
        {status.toUpperCase()}
      </span>
    );
  };

  // Metric card component
  const MetricCard: React.FC<{
    title: string;
    value: string | number;
    subtitle?: string;
    icon: React.ReactNode;
    status?: 'good' | 'warning' | 'critical';
  }> = ({ title, value, subtitle, icon, status }) => {
    const statusColors = {
      good: 'border-green-200 bg-green-50',
      warning: 'border-yellow-200 bg-yellow-50',
      critical: 'border-red-200 bg-red-50'
    };
    
    const borderColor = status ? statusColors[status] : 'border-gray-200 bg-white';
    
    return (
      <div className={`p-4 rounded-lg border-2 ${borderColor}`}>
        <div className="flex items-center justify-between mb-2">
          <span className="text-sm font-medium text-gray-600">{title}</span>
          <div className="text-gray-400">{icon}</div>
        </div>
        <div className="text-2xl font-bold text-gray-900">{value}</div>
        {subtitle && <div className="text-xs text-gray-500 mt-1">{subtitle}</div>}
      </div>
    );
  };

  // Resolve alert
  const resolveAlert = async (alertId: string) => {
    try {
      const response = await fetch(`/admin/alerts/${alertId}/resolve`, {
        method: 'POST'
      });
      
      if (response.ok) {
        fetchDashboard(); // Refresh data
      }
    } catch (err) {
      console.error('Failed to resolve alert:', err);
    }
  };

  // Clear cache
  const clearCache = async () => {
    if (!confirm('Are you sure you want to clear all cache entries? This may temporarily impact performance.')) {
      return;
    }
    
    try {
      const response = await fetch('/admin/cache/clear', {
        method: 'POST'
      });
      
      if (response.ok) {
        alert('Cache cleared successfully');
        fetchDashboard();
      }
    } catch (err) {
      alert('Failed to clear cache');
    }
  };

  if (loading) {
    return (
      <div className="flex items-center justify-center min-h-screen">
        <div className="text-center">
          <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"></div>
          <p className="text-gray-600">Loading dashboard...</p>
        </div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="flex items-center justify-center min-h-screen">
        <div className="text-center">
          <AlertCircle className="h-12 w-12 text-red-500 mx-auto mb-4" />
          <p className="text-gray-600">Error: {error}</p>
          <button
            onClick={fetchDashboard}
            className="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
          >
            Retry
          </button>
        </div>
      </div>
    );
  }

  if (!dashboardData) {
    return null;
  }

  return (
    <div className="min-h-screen bg-gray-50">
      {/* Header */}
      <div className="bg-white border-b border-gray-200">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
          <div className="flex items-center justify-between">
            <div>
              <h1 className="text-2xl font-bold text-gray-900">Medical AI Platform - Admin Dashboard</h1>
              <p className="text-sm text-gray-500 mt-1">
                Real-time monitoring and system management
              </p>
            </div>
            <div className="flex items-center space-x-4">
              <StatusBadge status={dashboardData.status} />
              <label className="flex items-center space-x-2 text-sm text-gray-600">
                <input
                  type="checkbox"
                  checked={autoRefresh}
                  onChange={(e) => setAutoRefresh(e.target.checked)}
                  className="rounded"
                />
                <span>Auto-refresh</span>
              </label>
              <button
                onClick={fetchDashboard}
                className="px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm"
              >
                Refresh Now
              </button>
            </div>
          </div>
          
          {/* Tabs */}
          <div className="mt-4 flex space-x-4 border-b border-gray-200">
            {['overview', 'models', 'cache', 'alerts'].map((tab) => (
              <button
                key={tab}
                onClick={() => setActiveTab(tab as any)}
                className={`px-4 py-2 font-medium text-sm border-b-2 transition-colors ${
                  activeTab === tab
                    ? 'border-blue-600 text-blue-600'
                    : 'border-transparent text-gray-500 hover:text-gray-700'
                }`}
              >
                {tab.charAt(0).toUpperCase() + tab.slice(1)}
              </button>
            ))}
          </div>
        </div>
      </div>

      {/* Content */}
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
        {/* Overview Tab */}
        {activeTab === 'overview' && (
          <div className="space-y-6">
            {/* System Status Grid */}
            <div>
              <h2 className="text-lg font-semibold text-gray-900 mb-4">System Status</h2>
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <MetricCard
                  title="Uptime"
                  value={dashboardData.system.uptime_human}
                  subtitle={`${dashboardData.system.uptime_seconds.toLocaleString()}s`}
                  icon={<Clock className="h-5 w-5" />}
                  status="good"
                />
                <MetricCard
                  title="Error Rate"
                  value={`${(dashboardData.system.error_rate * 100).toFixed(2)}%`}
                  subtitle={`Threshold: ${(dashboardData.system.error_threshold * 100).toFixed(0)}%`}
                  icon={<AlertCircle className="h-5 w-5" />}
                  status={dashboardData.system.error_rate > dashboardData.system.error_threshold ? 'critical' : 'good'}
                />
                <MetricCard
                  title="Total Requests"
                  value={dashboardData.system.total_requests.toLocaleString()}
                  icon={<Activity className="h-5 w-5" />}
                />
                <MetricCard
                  title="Active Alerts"
                  value={dashboardData.alerts.active_count}
                  subtitle={`${dashboardData.alerts.critical_count} critical`}
                  icon={<AlertCircle className="h-5 w-5" />}
                  status={dashboardData.alerts.critical_count > 0 ? 'critical' : dashboardData.alerts.active_count > 0 ? 'warning' : 'good'}
                />
              </div>
            </div>

            {/* Pipeline Statistics */}
            <div>
              <h2 className="text-lg font-semibold text-gray-900 mb-4">Pipeline Statistics</h2>
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <MetricCard
                  title="Total Jobs"
                  value={dashboardData.pipeline.total_jobs_processed.toLocaleString()}
                  icon={<BarChart3 className="h-5 w-5" />}
                />
                <MetricCard
                  title="Completed"
                  value={dashboardData.pipeline.completed_jobs.toLocaleString()}
                  icon={<TrendingUp className="h-5 w-5" />}
                  status="good"
                />
                <MetricCard
                  title="Failed"
                  value={dashboardData.pipeline.failed_jobs.toLocaleString()}
                  icon={<AlertCircle className="h-5 w-5" />}
                  status={dashboardData.pipeline.failed_jobs > 0 ? 'warning' : 'good'}
                />
                <MetricCard
                  title="Success Rate"
                  value={`${(dashboardData.pipeline.success_rate * 100).toFixed(1)}%`}
                  icon={<Activity className="h-5 w-5" />}
                  status={dashboardData.pipeline.success_rate > 0.95 ? 'good' : dashboardData.pipeline.success_rate > 0.85 ? 'warning' : 'critical'}
                />
              </div>
            </div>

            {/* Cache & Synthesis */}
            <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
              {/* Cache Statistics */}
              <div className="bg-white p-6 rounded-lg border border-gray-200">
                <div className="flex items-center justify-between mb-4">
                  <h3 className="text-lg font-semibold text-gray-900">Cache Performance</h3>
                  <Database className="h-5 w-5 text-gray-400" />
                </div>
                <div className="space-y-3">
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Hit Rate</span>
                    <span className="text-sm font-medium">{(dashboardData.cache.hit_rate * 100).toFixed(1)}%</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Entries</span>
                    <span className="text-sm font-medium">{dashboardData.cache.total_entries.toLocaleString()}</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Memory Usage</span>
                    <span className="text-sm font-medium">{dashboardData.cache.memory_usage_mb.toFixed(1)} MB</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Avg Retrieval</span>
                    <span className="text-sm font-medium">{dashboardData.cache.avg_retrieval_time_ms.toFixed(2)} ms</span>
                  </div>
                </div>
              </div>

              {/* Synthesis Statistics */}
              <div className="bg-white p-6 rounded-lg border border-gray-200">
                <div className="flex items-center justify-between mb-4">
                  <h3 className="text-lg font-semibold text-gray-900">Clinical Synthesis</h3>
                  <GitBranch className="h-5 w-5 text-gray-400" />
                </div>
                <div className="space-y-3">
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Total Syntheses</span>
                    <span className="text-sm font-medium">{dashboardData.synthesis.total_syntheses.toLocaleString()}</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Avg Confidence</span>
                    <span className="text-sm font-medium">{(dashboardData.synthesis.avg_confidence * 100).toFixed(1)}%</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Requiring Review</span>
                    <span className="text-sm font-medium">{dashboardData.synthesis.requiring_review.toLocaleString()}</span>
                  </div>
                  <div className="flex justify-between">
                    <span className="text-sm text-gray-600">Avg Processing</span>
                    <span className="text-sm font-medium">{dashboardData.synthesis.avg_processing_time_ms.toFixed(0)} ms</span>
                  </div>
                </div>
              </div>
            </div>

            {/* Compliance Status */}
            <div className="bg-white p-6 rounded-lg border border-gray-200">
              <div className="flex items-center justify-between mb-4">
                <h3 className="text-lg font-semibold text-gray-900">Compliance Status</h3>
                <Shield className="h-5 w-5 text-gray-400" />
              </div>
              <div className="grid grid-cols-2 md:grid-cols-5 gap-4">
                {Object.entries(dashboardData.compliance).map(([key, value]) => (
                  <div key={key} className="text-center">
                    <div className={`text-2xl mb-1 ${value ? 'text-green-600' : 'text-red-600'}`}>
                      {value ? '✓' : '✗'}
                    </div>
                    <div className="text-xs text-gray-600">
                      {key.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase())}
                    </div>
                  </div>
                ))}
              </div>
            </div>
          </div>
        )}

        {/* Models Tab */}
        {activeTab === 'models' && (
          <div>
            <h2 className="text-lg font-semibold text-gray-900 mb-4">Model Performance</h2>
            <div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
              <table className="min-w-full divide-y divide-gray-200">
                <thead className="bg-gray-50">
                  <tr>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Model ID</th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Version</th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Inferences</th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Avg Latency</th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Error Rate</th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Last Used</th>
                  </tr>
                </thead>
                <tbody className="bg-white divide-y divide-gray-200">
                  {Object.entries(dashboardData.models.performance).map(([modelId, perf]) => (
                    <tr key={modelId}>
                      <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{modelId}</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{perf.version}</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{perf.total_inferences.toLocaleString()}</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{perf.avg_latency_ms.toFixed(1)} ms</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{(perf.error_rate * 100).toFixed(2)}%</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{perf.last_used}</td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          </div>
        )}

        {/* Cache Tab */}
        {activeTab === 'cache' && (
          <div>
            <div className="flex items-center justify-between mb-4">
              <h2 className="text-lg font-semibold text-gray-900">Cache Management</h2>
              <button
                onClick={clearCache}
                className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 text-sm"
              >
                Clear Cache
              </button>
            </div>
            
            <div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
              <MetricCard
                title="Cache Entries"
                value={dashboardData.cache.total_entries.toLocaleString()}
                icon={<Database className="h-5 w-5" />}
              />
              <MetricCard
                title="Hit Rate"
                value={`${(dashboardData.cache.hit_rate * 100).toFixed(1)}%`}
                subtitle={`${dashboardData.cache.hits} hits, ${dashboardData.cache.misses} misses`}
                icon={<Activity className="h-5 w-5" />}
                status={dashboardData.cache.hit_rate > 0.7 ? 'good' : dashboardData.cache.hit_rate > 0.4 ? 'warning' : 'critical'}
              />
              <MetricCard
                title="Memory Usage"
                value={`${dashboardData.cache.memory_usage_mb.toFixed(1)} MB`}
                subtitle={`${dashboardData.cache.evictions} evictions`}
                icon={<BarChart3 className="h-5 w-5" />}
              />
            </div>
            
            <div className="bg-white p-6 rounded-lg border border-gray-200">
              <h3 className="text-md font-semibold text-gray-900 mb-4">Cache Performance Analysis</h3>
              <div className="space-y-2">
                {dashboardData.cache.hit_rate < 0.5 && (
                  <div className="p-3 bg-yellow-50 border border-yellow-200 rounded">
                    <p className="text-sm text-yellow-800">
                      ⚠ Low cache hit rate ({(dashboardData.cache.hit_rate * 100).toFixed(1)}%). 
                      Consider increasing cache size or TTL.
                    </p>
                  </div>
                )}
                {dashboardData.cache.hit_rate >= 0.8 && (
                  <div className="p-3 bg-green-50 border border-green-200 rounded">
                    <p className="text-sm text-green-800">
                      ✓ Excellent cache hit rate ({(dashboardData.cache.hit_rate * 100).toFixed(1)}%). 
                      Cache performing optimally.
                    </p>
                  </div>
                )}
                <div className="p-3 bg-blue-50 border border-blue-200 rounded">
                  <p className="text-sm text-blue-800">
                    Cache efficiency: {dashboardData.cache.cache_efficiency.toFixed(1)}%
                  </p>
                </div>
              </div>
            </div>
          </div>
        )}

        {/* Alerts Tab */}
        {activeTab === 'alerts' && (
          <div>
            <h2 className="text-lg font-semibold text-gray-900 mb-4">
              Active Alerts ({dashboardData.alerts.active_count})
            </h2>
            {dashboardData.alerts.recent.length === 0 ? (
              <div className="bg-white p-12 rounded-lg border border-gray-200 text-center">
                <AlertCircle className="h-12 w-12 text-gray-300 mx-auto mb-4" />
                <p className="text-gray-500">No active alerts</p>
              </div>
            ) : (
              <div className="space-y-3">
                {dashboardData.alerts.recent.map((alert) => (
                  <div
                    key={alert.alert_id}
                    className={`p-4 rounded-lg border-2 ${
                      alert.level === 'critical'
                        ? 'border-red-200 bg-red-50'
                        : alert.level === 'error'
                        ? 'border-orange-200 bg-orange-50'
                        : alert.level === 'warning'
                        ? 'border-yellow-200 bg-yellow-50'
                        : 'border-blue-200 bg-blue-50'
                    }`}
                  >
                    <div className="flex items-start justify-between">
                      <div className="flex-1">
                        <div className="flex items-center space-x-2 mb-2">
                          <span className={`px-2 py-1 rounded text-xs font-medium ${
                            alert.level === 'critical'
                              ? 'bg-red-200 text-red-800'
                              : alert.level === 'error'
                              ? 'bg-orange-200 text-orange-800'
                              : alert.level === 'warning'
                              ? 'bg-yellow-200 text-yellow-800'
                              : 'bg-blue-200 text-blue-800'
                          }`}>
                            {alert.level.toUpperCase()}
                          </span>
                          <span className="text-xs text-gray-500">{alert.category}</span>
                          <span className="text-xs text-gray-400">
                            {new Date(alert.timestamp).toLocaleString()}
                          </span>
                        </div>
                        <p className="text-sm font-medium text-gray-900">{alert.message}</p>
                        {Object.keys(alert.details).length > 0 && (
                          <pre className="mt-2 text-xs text-gray-600 bg-white/50 p-2 rounded">
                            {JSON.stringify(alert.details, null, 2)}
                          </pre>
                        )}
                      </div>
                      {!alert.resolved && (
                        <button
                          onClick={() => resolveAlert(alert.alert_id)}
                          className="ml-4 px-3 py-1 bg-white border border-gray-300 text-gray-700 rounded hover:bg-gray-50 text-sm"
                        >
                          Resolve
                        </button>
                      )}
                    </div>
                  </div>
                ))}
              </div>
            )}
          </div>
        )}
      </div>
    </div>
  );
};

export default AdminDashboard;