File size: 21,579 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 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 | /**
* AI API Logs Panel Component
*
* Monitor OpenAI API calls with:
* - Call list with filtering by provider
* - Token usage statistics
* - Response time tracking
* - Error monitoring
*/
import React, { useState, useEffect, useCallback } from 'react';
import {
Zap, RefreshCw, Filter, Clock, Brain, Bot,
Check, XCircle, AlertTriangle, BarChart3, TrendingUp,
Cpu, Activity, Hash
} from 'lucide-react';
import { getToken } from '../../services/api';
const API_BASE = import.meta.env.VITE_API_URL || '';
// Provider badge (OpenAI)
const ProviderBadge = ({ provider }) => {
const config = {
openai: { bg: '#dbeafe', color: '#2563eb', icon: '🤖', label: 'OpenAI' }
};
const { bg, color, icon, label } = config[provider] || config.openai;
return (
<span className="provider-badge" style={{
backgroundColor: bg,
color,
padding: '4px 10px',
borderRadius: '12px',
fontSize: '0.8rem',
fontWeight: '600',
display: 'inline-flex',
alignItems: 'center',
gap: '4px'
}}>
{icon} {label}
</span>
);
};
// Status badge
const StatusBadge = ({ status }) => {
const config = {
success: { bg: '#d1fae5', color: '#059669', icon: <Check size={12} /> },
error: { bg: '#fee2e2', color: '#dc2626', icon: <XCircle size={12} /> },
timeout: { bg: '#fef3c7', color: '#d97706', icon: <Clock size={12} /> }
};
const { bg, color, icon } = config[status] || config.success;
return (
<span className="status-badge" style={{
backgroundColor: bg,
color,
padding: '4px 10px',
borderRadius: '12px',
fontSize: '0.75rem',
fontWeight: '500',
display: 'inline-flex',
alignItems: 'center',
gap: '4px'
}}>
{icon} {status}
</span>
);
};
// Response time indicator
const ResponseTimeIndicator = ({ ms }) => {
if (!ms) return <span style={{ color: '#9ca3af' }}>-</span>;
let color;
if (ms < 1000) color = '#10b981';
else if (ms < 3000) color = '#f59e0b';
else color = '#ef4444';
return (
<span style={{ color, fontWeight: '500' }}>
{ms >= 1000 ? `${(ms / 1000).toFixed(1)}s` : `${ms}ms`}
</span>
);
};
// Token display
const TokenDisplay = ({ tokens }) => {
if (!tokens) return <span style={{ color: '#9ca3af' }}>-</span>;
return (
<span style={{
color: '#8b5cf6',
fontWeight: '500',
display: 'inline-flex',
alignItems: 'center',
gap: '4px'
}}>
<Hash size={12} />
{tokens.toLocaleString()}
</span>
);
};
function AILogsPanel() {
const [logs, setLogs] = useState([]);
const [stats, setStats] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
// Filters
const [provider, setProvider] = useState('');
const [operation, setOperation] = useState('');
const [status, setStatus] = useState('');
const [hours, setHours] = useState(24);
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(),
hours: hours.toString()
});
if (provider) params.append('provider', provider);
if (operation) params.append('operation', operation);
if (status) params.append('status', status);
const response = await fetch(`${API_BASE}/api/admin/ai-logs?${params}`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
if (data.success) {
setLogs(data.logs || []);
setStats(data.stats || null);
}
} else {
throw new Error('Failed to fetch AI logs');
}
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
}, [provider, operation, status, hours, limit]);
useEffect(() => {
fetchLogs();
}, [fetchLogs]);
// Auto-refresh every 10 seconds
useEffect(() => {
if (!autoRefresh) return;
const interval = setInterval(fetchLogs, 10000);
return () => clearInterval(interval);
}, [autoRefresh, fetchLogs]);
// Format timestamp
const formatTime = (isoString) => {
if (!isoString) return '-';
const date = new Date(isoString);
return date.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
};
return (
<div className="ai-logs-panel">
{/* Header */}
<div className="panel-header" style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: '1.5rem'
}}>
<div>
<h2 style={{ margin: 0, display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
<Brain size={24} />
AI API Logs
</h2>
<p style={{ margin: '0.5rem 0 0', color: '#9ca3af', fontSize: '0.9rem' }}>
Monitor OpenAI API calls in real-time
</p>
</div>
<div style={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}>
<label style={{
display: 'flex',
alignItems: 'center',
gap: '0.5rem',
cursor: 'pointer',
fontSize: '0.85rem',
color: '#9ca3af'
}}>
<input
type="checkbox"
checked={autoRefresh}
onChange={(e) => setAutoRefresh(e.target.checked)}
/>
Auto-refresh
</label>
<button onClick={fetchLogs} className="btn-secondary" style={{
display: 'flex',
alignItems: 'center',
gap: '0.5rem',
padding: '0.5rem 1rem'
}}>
<RefreshCw size={16} className={loading ? 'spinning' : ''} />
Refresh
</button>
</div>
</div>
{/* Stats Cards */}
{stats && (
<div className="stats-grid" style={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))',
gap: '1rem',
marginBottom: '1.5rem'
}}>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(59, 130, 246, 0.1)' }}>
<Activity size={20} style={{ color: '#3b82f6' }} />
</div>
<div className="stat-content">
<span className="stat-value">{stats.total_calls?.toLocaleString() || 0}</span>
<span className="stat-label">Total Calls ({hours}h)</span>
</div>
</div>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(59, 130, 246, 0.1)' }}>
<span style={{ fontSize: '1.2rem' }}>🌟</span>
</div>
<div className="stat-content">
<span className="stat-value">{stats.openai_calls?.toLocaleString() || 0}</span>
<span className="stat-label">OpenAI Calls</span>
</div>
</div>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(245, 158, 11, 0.1)' }}>
<span style={{ fontSize: '1.2rem' }}>⚡</span>
</div>
<div className="stat-content">
<span className="stat-value">{stats.total_calls?.toLocaleString() || 0}</span>
<span className="stat-label">Total Calls</span>
</div>
</div>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(16, 185, 129, 0.1)' }}>
<Check size={20} style={{ color: '#10b981' }} />
</div>
<div className="stat-content">
<span className="stat-value">{stats.success_rate || 100}%</span>
<span className="stat-label">Success Rate</span>
</div>
</div>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(139, 92, 246, 0.1)' }}>
<Hash size={20} style={{ color: '#8b5cf6' }} />
</div>
<div className="stat-content">
<span className="stat-value">{stats.total_tokens?.toLocaleString() || 0}</span>
<span className="stat-label">Total Tokens</span>
</div>
</div>
<div className="admin-stat-card">
<div className="stat-icon" style={{ backgroundColor: 'rgba(245, 158, 11, 0.1)' }}>
<Clock size={20} style={{ color: '#f59e0b' }} />
</div>
<div className="stat-content">
<span className="stat-value">{stats.avg_response_time_ms || 0}ms</span>
<span className="stat-label">Avg Response</span>
</div>
</div>
</div>
)}
{/* Filters */}
<div className="filters-bar" style={{
display: 'flex',
gap: '1rem',
marginBottom: '1rem',
flexWrap: 'wrap',
alignItems: 'center'
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
<Filter size={16} style={{ color: '#9ca3af' }} />
<span style={{ color: '#9ca3af', fontSize: '0.9rem' }}>Filters:</span>
</div>
<select
value={provider}
onChange={(e) => setProvider(e.target.value)}
className="filter-select"
style={{
padding: '0.5rem',
borderRadius: '6px',
border: '1px solid rgba(255,255,255,0.1)',
backgroundColor: 'rgba(0,0,0,0.3)',
color: 'white',
fontSize: '0.9rem'
}}
>
<option value="">All Providers</option>
<option value="openai">🤖 OpenAI</option>
</select>
<input
type="text"
placeholder="Operation..."
value={operation}
onChange={(e) => setOperation(e.target.value)}
style={{
padding: '0.5rem',
borderRadius: '6px',
border: '1px solid rgba(255,255,255,0.1)',
backgroundColor: 'rgba(0,0,0,0.3)',
color: 'white',
fontSize: '0.9rem',
width: '150px'
}}
/>
<select
value={status}
onChange={(e) => setStatus(e.target.value)}
className="filter-select"
style={{
padding: '0.5rem',
borderRadius: '6px',
border: '1px solid rgba(255,255,255,0.1)',
backgroundColor: 'rgba(0,0,0,0.3)',
color: 'white',
fontSize: '0.9rem'
}}
>
<option value="">All Status</option>
<option value="success">✓ Success</option>
<option value="error">✗ Error</option>
</select>
<select
value={hours}
onChange={(e) => setHours(Number(e.target.value))}
className="filter-select"
style={{
padding: '0.5rem',
borderRadius: '6px',
border: '1px solid rgba(255,255,255,0.1)',
backgroundColor: 'rgba(0,0,0,0.3)',
color: 'white',
fontSize: '0.9rem'
}}
>
<option value={1}>Last 1 hour</option>
<option value={6}>Last 6 hours</option>
<option value={24}>Last 24 hours</option>
<option value={48}>Last 48 hours</option>
<option value={168}>Last 7 days</option>
</select>
</div>
{/* Error display */}
{error && (
<div style={{
padding: '1rem',
backgroundColor: 'rgba(239, 68, 68, 0.1)',
borderRadius: '8px',
border: '1px solid rgba(239, 68, 68, 0.3)',
color: '#ef4444',
marginBottom: '1rem',
display: 'flex',
alignItems: 'center',
gap: '0.5rem'
}}>
<AlertTriangle size={16} />
{error}
</div>
)}
{/* Logs Table */}
<div className="logs-container" style={{
backgroundColor: 'rgba(0,0,0,0.2)',
borderRadius: '12px',
border: '1px solid rgba(255,255,255,0.05)',
overflow: 'hidden'
}}>
{loading && logs.length === 0 ? (
<div style={{ padding: '3rem', textAlign: 'center', color: '#9ca3af' }}>
<RefreshCw size={24} className="spinning" style={{ marginBottom: '1rem' }} />
<p>Loading AI logs...</p>
</div>
) : logs.length === 0 ? (
<div style={{ padding: '3rem', textAlign: 'center', color: '#9ca3af' }}>
<Brain size={48} style={{ marginBottom: '1rem', opacity: 0.5 }} />
<p>No AI API logs found</p>
<p style={{ fontSize: '0.85rem' }}>AI calls will appear here once made</p>
</div>
) : (
<div style={{ overflowX: 'auto' }}>
<table style={{ width: '100%', borderCollapse: 'collapse' }}>
<thead>
<tr style={{
backgroundColor: 'rgba(255,255,255,0.05)',
borderBottom: '1px solid rgba(255,255,255,0.1)'
}}>
<th style={{ padding: '1rem', textAlign: 'left', color: '#9ca3af', fontWeight: '500' }}>Time</th>
<th style={{ padding: '1rem', textAlign: 'left', color: '#9ca3af', fontWeight: '500' }}>Provider</th>
<th style={{ padding: '1rem', textAlign: 'left', color: '#9ca3af', fontWeight: '500' }}>Operation</th>
<th style={{ padding: '1rem', textAlign: 'left', color: '#9ca3af', fontWeight: '500' }}>Model</th>
<th style={{ padding: '1rem', textAlign: 'center', color: '#9ca3af', fontWeight: '500' }}>Tokens</th>
<th style={{ padding: '1rem', textAlign: 'center', color: '#9ca3af', fontWeight: '500' }}>Response</th>
<th style={{ padding: '1rem', textAlign: 'center', color: '#9ca3af', fontWeight: '500' }}>Status</th>
</tr>
</thead>
<tbody>
{logs.map((log, index) => (
<tr
key={log.id || index}
style={{
borderBottom: '1px solid rgba(255,255,255,0.05)',
transition: 'background-color 0.15s'
}}
onMouseEnter={(e) => e.currentTarget.style.backgroundColor = 'rgba(255,255,255,0.03)'}
onMouseLeave={(e) => e.currentTarget.style.backgroundColor = 'transparent'}
>
<td style={{ padding: '0.75rem 1rem', color: '#9ca3af', fontSize: '0.85rem' }}>
{formatTime(log.created_at)}
</td>
<td style={{ padding: '0.75rem 1rem' }}>
<ProviderBadge provider={log.provider} />
</td>
<td style={{ padding: '0.75rem 1rem', color: '#e5e7eb', fontFamily: 'monospace', fontSize: '0.85rem' }}>
{log.operation || '-'}
</td>
<td style={{ padding: '0.75rem 1rem', color: '#9ca3af', fontSize: '0.85rem' }}>
{log.model || '-'}
</td>
<td style={{ padding: '0.75rem 1rem', textAlign: 'center' }}>
<TokenDisplay tokens={log.total_tokens} />
</td>
<td style={{ padding: '0.75rem 1rem', textAlign: 'center' }}>
<ResponseTimeIndicator ms={log.response_time_ms} />
</td>
<td style={{ padding: '0.75rem 1rem', textAlign: 'center' }}>
<StatusBadge status={log.status} />
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
{/* Footer */}
<div style={{
marginTop: '1rem',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
color: '#9ca3af',
fontSize: '0.85rem'
}}>
<span>Showing {logs.length} logs</span>
<span>Last updated: {new Date().toLocaleTimeString()}</span>
</div>
<style>{`
.spinning {
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
`}</style>
</div>
);
}
export default AILogsPanel;
|