File size: 11,316 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
/**

 * API Logs Panel Component

 * 

 * Real-time API request monitoring with:

 * - Request list with filtering

 * - Response time statistics

 * - Top endpoints

 * - Status code breakdown

 */

import React, { useState, useEffect, useCallback } from 'react';
import {
    Activity, RefreshCw, Filter, Clock, ArrowUpRight, ArrowDownRight,
    Check, XCircle, AlertTriangle, Zap, BarChart3, TrendingUp
} from 'lucide-react';
import { getToken } from '../../services/api';

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

// HTTP Method badge
const MethodBadge = ({ method }) => {
    const colors = {
        GET: { bg: '#dbeafe', color: '#2563eb' },
        POST: { bg: '#d1fae5', color: '#059669' },
        PUT: { bg: '#fef3c7', color: '#d97706' },
        DELETE: { bg: '#fee2e2', color: '#dc2626' },
        PATCH: { bg: '#ede9fe', color: '#7c3aed' }
    };

    const { bg, color } = colors[method] || { bg: '#f3f4f6', color: '#374151' };

    return (
        <span className="method-badge" style={{ backgroundColor: bg, color }}>

            {method}

        </span>
    );
};

// Status code badge
const StatusBadge = ({ code }) => {
    let bg, color;
    if (code >= 500) {
        bg = '#fee2e2'; color = '#dc2626';
    } else if (code >= 400) {
        bg = '#fef3c7'; color = '#d97706';
    } else if (code >= 300) {
        bg = '#dbeafe'; color = '#2563eb';
    } else {
        bg = '#d1fae5'; color = '#059669';
    }

    return (
        <span className="status-code-badge" style={{ backgroundColor: bg, color }}>

            {code}

        </span>
    );
};

// Response time indicator
const ResponseTimeIndicator = ({ ms }) => {
    let color;
    if (ms < 100) color = '#10b981';
    else if (ms < 500) color = '#f59e0b';
    else color = '#ef4444';

    return (
        <span className="response-time" style={{ color }}>

            {ms.toFixed(0)}ms

        </span>
    );
};

function APILogsPanel() {
    const [logs, setLogs] = useState([]);
    const [stats, setStats] = useState(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);

    // Filters
    const [endpoint, setEndpoint] = useState('');
    const [method, setMethod] = useState('');
    const [statusCode, setStatusCode] = useState('');
    const [limit, setLimit] = useState(100);
    const [autoRefresh, setAutoRefresh] = useState(true);

    // Fetch logs
    const fetchLogs = useCallback(async () => {
        setLoading(true);
        setError(null);
        try {
            const token = getToken();
            const params = new URLSearchParams({ limit: limit.toString() });
            if (endpoint) params.append('endpoint', endpoint);
            if (method) params.append('method', method);
            if (statusCode) params.append('status_code', statusCode);

            const response = await fetch(`${API_BASE}/api/admin/api-logs?${params}`, {
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json'
                }
            });

            if (!response.ok) throw new Error('Failed to fetch API logs');

            const data = await response.json();
            if (data.success) {
                setLogs(data.logs);
                setStats(data.stats);
            }
        } catch (err) {
            setError(err.message);
        } finally {
            setLoading(false);
        }
    }, [endpoint, method, statusCode, limit]);

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

    // Auto-refresh every 10 seconds
    useEffect(() => {
        if (!autoRefresh) return;
        const interval = setInterval(fetchLogs, 10000);
        return () => clearInterval(interval);
    }, [autoRefresh, fetchLogs]);

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

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

                <p>Loading API logs...</p>

            </div>
        );
    }

    return (
        <div className="api-logs-panel">

            {/* Header */}

            <div className="panel-header">

                <div className="header-left">

                    <Activity size={24} />

                    <h2>API Request Logs</h2>

                </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={fetchLogs} disabled={loading}>

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

                        Refresh

                    </button>

                </div>

            </div>



            {/* Stats Cards */}

            {stats && (

                <div className="stats-cards">

                    <div className="stat-card">

                        <Zap size={20} />

                        <div className="stat-content">

                            <span className="stat-value">{stats.total_requests || 0}</span>

                            <span className="stat-label">Total Requests (24h)</span>

                        </div>

                    </div>

                    <div className="stat-card">

                        <Clock size={20} />

                        <div className="stat-content">

                            <span className="stat-value">{stats.avg_response_time?.toFixed(0) || 0}ms</span>

                            <span className="stat-label">Avg Response Time</span>

                        </div>

                    </div>

                    <div className="stat-card success">

                        <Check size={20} />

                        <div className="stat-content">

                            <span className="stat-value">{stats.status_breakdown?.['2xx'] || 0}</span>

                            <span className="stat-label">Successful (2xx)</span>

                        </div>

                    </div>

                    <div className="stat-card error">

                        <XCircle size={20} />

                        <div className="stat-content">

                            <span className="stat-value">

                                {(stats.status_breakdown?.['4xx'] || 0) + (stats.status_breakdown?.['5xx'] || 0)}

                            </span>

                            <span className="stat-label">Errors (4xx/5xx)</span>

                        </div>

                    </div>

                </div>

            )}



            {/* Top Endpoints */}

            {stats?.top_endpoints && stats.top_endpoints.length > 0 && (

                <div className="top-endpoints">

                    <h3><TrendingUp size={18} /> Top Endpoints</h3>

                    <div className="endpoints-list">

                        {stats.top_endpoints.map((ep, idx) => (

                            <div key={idx} className="endpoint-item">

                                <span className="rank">#{idx + 1}</span>

                                <span className="endpoint-path">{ep.endpoint}</span>

                                <span className="endpoint-count">{ep.count} requests</span>

                            </div>

                        ))}

                    </div>

                </div>

            )}



            {/* Filters */}

            <div className="filters-bar">

                <select value={method} onChange={(e) => setMethod(e.target.value)}>

                    <option value="">All Methods</option>

                    <option value="GET">GET</option>

                    <option value="POST">POST</option>

                    <option value="PUT">PUT</option>

                    <option value="DELETE">DELETE</option>

                </select>

                <input

                    type="text"

                    placeholder="Filter by endpoint..."

                    value={endpoint}

                    onChange={(e) => setEndpoint(e.target.value)}

                />

                <select value={statusCode} onChange={(e) => setStatusCode(e.target.value)}>

                    <option value="">All Status Codes</option>

                    <option value="200">200 OK</option>

                    <option value="201">201 Created</option>

                    <option value="400">400 Bad Request</option>

                    <option value="401">401 Unauthorized</option>

                    <option value="403">403 Forbidden</option>

                    <option value="404">404 Not Found</option>

                    <option value="500">500 Internal Error</option>

                </select>

                <select value={limit} onChange={(e) => setLimit(Number(e.target.value))}>

                    <option value={50}>Show 50</option>

                    <option value={100}>Show 100</option>

                    <option value={200}>Show 200</option>

                </select>

            </div>



            {/* Logs Table */}

            <div className="logs-table-container">

                <table className="admin-table">

                    <thead>

                        <tr>

                            <th>Method</th>

                            <th>Endpoint</th>

                            <th>Status</th>

                            <th>Response Time</th>

                            <th>IP Address</th>

                            <th>Time</th>

                        </tr>

                    </thead>

                    <tbody>

                        {logs.length === 0 ? (

                            <tr>

                                <td colSpan={6} className="empty-row">

                                    No API logs found

                                </td>

                            </tr>

                        ) : (

                            logs.map((log) => (

                                <tr key={log.id}>

                                    <td><MethodBadge method={log.method} /></td>

                                    <td className="endpoint-cell mono">{log.endpoint}</td>

                                    <td><StatusBadge code={log.status_code} /></td>

                                    <td><ResponseTimeIndicator ms={log.response_time_ms} /></td>

                                    <td className="mono">{log.ip_address}</td>

                                    <td className="time-cell">

                                        {new Date(log.created_at).toLocaleTimeString()}

                                    </td>

                                </tr>

                            ))

                        )}

                    </tbody>

                </table>

            </div>

        </div>
    );
}

export default APILogsPanel;