File size: 12,550 Bytes
91be275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * System Health Panel Component

 * 

 * Real-time system monitoring dashboard showing:

 * - CPU usage

 * - Memory usage

 * - Disk usage

 * - Database health

 * - Recent activity metrics

 */

import React, { useState, useEffect, useCallback } from 'react';
import {
    Activity, Cpu, HardDrive, Database, Server, RefreshCw,
    CheckCircle, AlertTriangle, XCircle, Wifi, Clock, Users,
    Zap, TrendingUp, BarChart3
} from 'lucide-react';
import { getToken } from '../../services/api';

const API_BASE = import.meta.env.VITE_API_URL || '';

// Status indicator component
const StatusIndicator = ({ status }) => {
    const getStatusConfig = () => {
        switch (status) {
            case 'healthy':
            case 'connected':
                return { icon: CheckCircle, color: '#10b981', label: 'Healthy' };
            case 'warning':
            case 'degraded':
                return { icon: AlertTriangle, color: '#f59e0b', label: 'Warning' };
            case 'critical':
            case 'error':
            case 'disconnected':
                return { icon: XCircle, color: '#ef4444', label: 'Critical' };
            default:
                return { icon: Activity, color: '#6b7280', label: 'Unknown' };
        }
    };

    const { icon: Icon, color, label } = getStatusConfig();

    return (
        <div className="status-indicator" style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>

            <Icon size={16} color={color} />

            <span style={{ color }}>{label}</span>

        </div>
    );
};

// Metric Card Component
const MetricCard = ({ icon: Icon, title, value, unit, percentage, status, description }) => {
    const getBarColor = (pct) => {
        if (pct < 60) return '#10b981';
        if (pct < 80) return '#f59e0b';
        return '#ef4444';
    };

    return (
        <div className="admin-stat-card metric-card">

            <div className="metric-header">

                <div className="metric-icon">

                    <Icon size={24} />

                </div>

                <StatusIndicator status={status} />

            </div>

            <div className="metric-content">

                <h3>{title}</h3>

                <div className="metric-value">

                    <span className="value">{value}</span>

                    {unit && <span className="unit">{unit}</span>}

                </div>

                {percentage !== undefined && (

                    <div className="progress-bar-container">

                        <div

                            className="progress-bar"

                            style={{

                                width: `${Math.min(percentage, 100)}%`,

                                backgroundColor: getBarColor(percentage)

                            }}

                        />

                    </div>

                )}

                {description && <p className="metric-description">{description}</p>}

            </div>

        </div>
    );
};

// Realtime Stats Row
const RealtimeStats = ({ metrics }) => {
    if (!metrics) return null;

    return (
        <div className="realtime-stats-row">

            <div className="realtime-stat">

                <Zap size={18} className="stat-icon" />

                <span className="stat-value">{metrics.requests_per_hour || 0}</span>

                <span className="stat-label">Requests/hr</span>

            </div>

            <div className="realtime-stat">

                <AlertTriangle size={18} className="stat-icon warning" />

                <span className="stat-value">{metrics.errors_per_hour || 0}</span>

                <span className="stat-label">Errors/hr</span>

            </div>

            <div className="realtime-stat">

                <Users size={18} className="stat-icon" />

                <span className="stat-value">{metrics.active_users || 0}</span>

                <span className="stat-label">Active Users</span>

            </div>

        </div>
    );
};

function SystemHealthPanel() {
    const [health, setHealth] = useState(null);
    const [realtime, setRealtime] = useState(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);
    const [lastUpdate, setLastUpdate] = useState(null);
    const [autoRefresh, setAutoRefresh] = useState(true);

    // Fetch full health data
    const fetchHealth = useCallback(async () => {
        setLoading(true);
        setError(null);
        try {
            const token = getToken();
            const response = await fetch(`${API_BASE}/api/admin/health`, {
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json'
                }
            });

            if (!response.ok) throw new Error('Failed to fetch health data');

            const data = await response.json();
            if (data.success) {
                setHealth(data.health);
                setLastUpdate(new Date());
            }
        } catch (err) {
            setError(err.message);
        } finally {
            setLoading(false);
        }
    }, []);

    // Fetch lightweight realtime metrics
    const fetchRealtime = useCallback(async () => {
        try {
            const token = getToken();
            const response = await fetch(`${API_BASE}/api/admin/health/realtime`, {
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json'
                }
            });

            if (response.ok) {
                const data = await response.json();
                if (data.success) {
                    setRealtime(data.metrics);
                    setLastUpdate(new Date());
                }
            }
        } catch (err) {
            console.error('Realtime fetch error:', err);
        }
    }, []);

    // Initial fetch
    useEffect(() => {
        fetchHealth();
    }, [fetchHealth]);

    // Auto-refresh realtime metrics every 5 seconds
    useEffect(() => {
        if (!autoRefresh) return;

        const interval = setInterval(fetchRealtime, 5000);
        return () => clearInterval(interval);
    }, [autoRefresh, fetchRealtime]);

    if (loading) {
        return (
            <div className="admin-loading">

                <RefreshCw className="spinning" size={40} />

                <p>Loading system health...</p>

            </div>
        );
    }

    if (error) {
        return (
            <div className="admin-error">

                <XCircle size={40} />

                <p>Error: {error}</p>

                <button onClick={fetchHealth}>Retry</button>

            </div>
        );
    }

    const cpu = health?.cpu || {};
    const memory = health?.memory || {};
    const disk = health?.disk || {};
    const database = health?.database || {};
    const overallStatus = realtime?.overall_status || health?.overall_status || 'unknown';

    return (
        <div className="system-health-panel">

            {/* Header */}

            <div className="panel-header">

                <div className="header-left">

                    <Server size={24} />

                    <h2>System Health</h2>

                    <StatusIndicator status={overallStatus} />

                </div>

                <div className="header-right">

                    <label className="auto-refresh-toggle">

                        <input

                            type="checkbox"

                            checked={autoRefresh}

                            onChange={(e) => setAutoRefresh(e.target.checked)}

                        />

                        Auto-refresh

                    </label>

                    <button className="refresh-btn" onClick={fetchHealth} disabled={loading}>

                        <RefreshCw size={16} className={loading ? 'spinning' : ''} />

                        Refresh

                    </button>

                    {lastUpdate && (

                        <span className="last-update">

                            <Clock size={14} />

                            {lastUpdate.toLocaleTimeString()}

                        </span>

                    )}

                </div>

            </div>



            {/* Realtime Stats */}

            <RealtimeStats metrics={realtime} />



            {/* Metric Cards */}

            <div className="metrics-grid">

                <MetricCard

                    icon={Cpu}

                    title="CPU Usage"

                    value={realtime?.cpu_percent ?? cpu.usage_percent ?? 0}

                    unit="%"

                    percentage={realtime?.cpu_percent ?? cpu.usage_percent ?? 0}

                    status={cpu.status || 'unknown'}

                    description={`${cpu.cores || 0} cores available`}

                />

                <MetricCard

                    icon={Activity}

                    title="Memory Usage"

                    value={realtime?.memory_percent ?? memory.usage_percent ?? 0}

                    unit="%"

                    percentage={realtime?.memory_percent ?? memory.usage_percent ?? 0}

                    status={memory.status || 'unknown'}

                    description={`${memory.used_gb?.toFixed(1) || 0} / ${memory.total_gb?.toFixed(1) || 0} GB`}

                />

                <MetricCard

                    icon={HardDrive}

                    title="Disk Usage"

                    value={disk.usage_percent ?? 0}

                    unit="%"

                    percentage={disk.usage_percent ?? 0}

                    status={disk.status || 'unknown'}

                    description={`${disk.used_gb?.toFixed(1) || 0} / ${disk.total_gb?.toFixed(1) || 0} GB`}

                />

                <MetricCard

                    icon={Database}

                    title="Database"

                    value={database.connected ? 'Connected' : 'Disconnected'}

                    status={database.connected ? 'connected' : 'error'}

                    description={database.response_time_ms ? `${database.response_time_ms.toFixed(2)}ms response` : ''}

                />

            </div>



            {/* Database Stats */}

            {database.table_stats && (

                <div className="database-stats">

                    <h3><Database size={18} /> Database Tables</h3>

                    <div className="table-stats-grid">

                        {Object.entries(database.table_stats).map(([table, count]) => (

                            <div key={table} className="table-stat">

                                <span className="table-name">{table}</span>

                                <span className="table-count">{count.toLocaleString()} rows</span>

                            </div>

                        ))}

                    </div>

                </div>

            )}



            {/* Process Info */}

            {health?.process && (

                <div className="process-info">

                    <h3><Activity size={18} /> Process Info</h3>

                    <div className="process-stats">

                        <div className="process-stat">

                            <span className="label">Process Memory</span>

                            <span className="value">{health.process.memory_mb?.toFixed(1)} MB</span>

                        </div>

                        <div className="process-stat">

                            <span className="label">CPU Usage</span>

                            <span className="value">{health.process.cpu_percent?.toFixed(1)}%</span>

                        </div>

                        <div className="process-stat">

                            <span className="label">Threads</span>

                            <span className="value">{health.process.threads}</span>

                        </div>

                        <div className="process-stat">

                            <span className="label">Open Files</span>

                            <span className="value">{health.process.open_files}</span>

                        </div>

                    </div>

                </div>

            )}

        </div>
    );
}

export default SystemHealthPanel;