/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useState, useEffect } from 'react'; import { Card, Row, Col, Table, Spin } from '@/components/ui'; import { TrendingUp, DollarSign, Activity, Layers, BarChart3, AlertTriangle, } from 'lucide-react'; import { useTranslation } from 'react-i18next'; // 模拟管理员监控数据 const generateMockAdminData = () => { const channels = [ 'OpenAI GPT-4', 'Claude 3.5 Sonnet', 'Gemini Pro', 'DeepSeek V3', 'Qwen Plus', ]; // 生成24小时请求趋势 const requestTrend = Array.from({ length: 24 }, (_, i) => ({ hour: `${i}:00`, requests: Math.floor(Math.random() * 1000) + 500, })); // 生成Token消耗趋势 const tokenTrend = Array.from({ length: 12 }, (_, i) => ({ time: `${i * 2}:00`, promptTokens: Math.floor(Math.random() * 50000) + 30000, completionTokens: Math.floor(Math.random() * 40000) + 20000, })); // 成本分布 const costDistribution = channels.map((name) => ({ name, cost: Math.random() * 500 + 100, percentage: 0, })); const totalCost = costDistribution.reduce((sum, item) => sum + item.cost, 0); costDistribution.forEach((item) => { item.percentage = ((item.cost / totalCost) * 100).toFixed(1); }); // 错误类型分布 const errorTypes = [ { type: '速率超限 (Rate Limit)', count: 156 }, { type: '认证失败 (Auth Failed)', count: 89 }, { type: '超时 (Timeout)', count: 67 }, { type: '配额不足 (Quota Exceeded)', count: 45 }, { type: '模型不可用 (Model Unavailable)', count: 23 }, ]; // 渠道详细数据 const channelDetails = channels.map((name, index) => ({ id: index + 1, name, requests: Math.floor(Math.random() * 5000) + 1000, avgResponseTime: Math.floor(Math.random() * 800) + 200, tokens: Math.floor(Math.random() * 100000) + 50000, cost: costDistribution[index].cost.toFixed(2), successRate: (95 + Math.random() * 5).toFixed(2), })); return { kpi: { totalRequests: requestTrend.reduce((sum, item) => sum + item.requests, 0), totalCost: totalCost.toFixed(2), activeChannels: channels.length, avgResponseTime: Math.floor(Math.random() * 300) + 200, }, requestTrend, tokenTrend, costDistribution, errorTypes, channelDetails, quotaUsage: 73, }; }; const AdminMonitor = () => { const { t } = useTranslation(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { setTimeout(() => { setData(generateMockAdminData()); setLoading(false); }, 1000); const interval = setInterval(() => { setData(generateMockAdminData()); }, 30000); return () => clearInterval(interval); }, []); const renderKPICard = ( icon, label, value, suffix = '', color = '#1890ff', ) => (
{React.cloneElement(icon, { size: 24, color })}
{label}
{value} {suffix && ( {suffix} )}
); const renderBarChart = (data, maxValue) => { return data.slice(0, 12).map((item, index) => { const percentage = (item.requests / maxValue) * 100; return (
{item.hour} {item.requests.toLocaleString()}
); }); }; const renderStackedAreaChart = (data) => { const maxTotal = Math.max( ...data.map((d) => d.promptTokens + d.completionTokens), ); return (
{ const x = (i / (data.length - 1)) * 100; const y = 100 - (d.completionTokens / maxTotal) * 100; return `${x},${y}`; }) .join(' ')} /> { const x = (i / (data.length - 1)) * 100; const total = d.promptTokens + d.completionTokens; const y = 100 - (total / maxTotal) * 100; return `${x},${y}`; }) .join(' ')} />
Prompt
Completion
); }; const renderDonutChart = (data) => { const total = data.reduce((sum, item) => sum + item.cost, 0); const colors = ['#1890ff', '#52c41a', '#faad14', '#f5222d', '#722ed1']; let currentAngle = -90; const segments = data.map((item, index) => { const percentage = item.cost / total; const angle = percentage * 360; const startAngle = currentAngle; const endAngle = currentAngle + angle; currentAngle = endAngle; const startRad = (startAngle * Math.PI) / 180; const endRad = (endAngle * Math.PI) / 180; const x1 = 50 + 40 * Math.cos(startRad); const y1 = 50 + 40 * Math.sin(startRad); const x2 = 50 + 40 * Math.cos(endRad); const y2 = 50 + 40 * Math.sin(endRad); const largeArc = angle > 180 ? 1 : 0; return { d: `M 50 50 L ${x1} ${y1} A 40 40 0 ${largeArc} 1 ${x2} ${y2} Z`, color: colors[index % colors.length], ...item, }; }); return (
{segments.map((segment, index) => ( ))}
{segments.map((segment, index) => (
{segment.name}
${segment.cost.toFixed(2)} ({segment.percentage}%)
))}
); }; const renderGaugeChart = (percentage) => { const color = percentage >= 90 ? 'var(--ui-color-danger)' : percentage >= 75 ? 'var(--ui-color-warning)' : 'var(--ui-color-success)'; return (
{percentage}%
{percentage >= 90 && t('配额即将用尽')} {percentage >= 75 && percentage < 90 && t('配额使用偏高')} {percentage < 75 && t('配额正常')}
); }; const renderHorizontalBar = (data) => { const maxCount = Math.max(...data.map((d) => d.count)); return data.map((item, index) => { const percentage = (item.count / maxCount) * 100; return (
{t(item.type)} {item.count}
); }); }; const columns = [ { title: t('渠道名称'), dataIndex: 'name', key: 'name', width: 180, render: (text) => {text}, }, { title: t('请求数'), dataIndex: 'requests', key: 'requests', sorter: (a, b) => a.requests - b.requests, render: (text) => text.toLocaleString(), }, { title: t('平均响应时间 (ms)'), dataIndex: 'avgResponseTime', key: 'avgResponseTime', sorter: (a, b) => a.avgResponseTime - b.avgResponseTime, render: (text) => ( 500 ? 'var(--ui-color-danger)' : 'var(--ui-color-success)', }} > {text} ), }, { title: t('Token消耗'), dataIndex: 'tokens', key: 'tokens', sorter: (a, b) => a.tokens - b.tokens, render: (text) => text.toLocaleString(), }, { title: t('成本 (¥)'), dataIndex: 'cost', key: 'cost', sorter: (a, b) => parseFloat(a.cost) - parseFloat(b.cost), render: (text) => `¥${text}`, }, { title: t('成功率 (%)'), dataIndex: 'successRate', key: 'successRate', sorter: (a, b) => parseFloat(a.successRate) - parseFloat(b.successRate), render: (text) => { const rate = parseFloat(text); return ( = 99 ? 'var(--ui-color-success)' : rate >= 95 ? 'var(--ui-color-warning)' : 'var(--ui-color-danger)', }} > {text}% ); }, }, ]; if (loading) { return (
); } return (
{renderKPICard( , t('过去24小时总请求'), data.kpi.totalRequests.toLocaleString(), '', '#1890ff', )} {renderKPICard( , t('今日预估总成本'), data.kpi.totalCost, '¥', '#52c41a', )} {renderKPICard( , t('当前活跃渠道'), data.kpi.activeChannels, '', '#722ed1', )} {renderKPICard( , t('平均响应时间'), data.kpi.avgResponseTime, 'ms', '#faad14', )} {t('请求趋势')} (24h)
} styles={{ body: { padding: '20px' } }} > {renderBarChart( data.requestTrend, Math.max(...data.requestTrend.map((d) => d.requests)), )} {t('成本分布')}
} styles={{ body: { padding: '20px' } }} > {renderDonutChart(data.costDistribution)} {t('Token消耗趋势')} (24h)
} styles={{ body: { padding: '20px' } }} > {renderStackedAreaChart(data.tokenTrend)} {t('配额使用情况')}
} styles={{ body: { padding: '20px' } }} > {renderGaugeChart(data.quotaUsage)} {t('错误类型分布')} (24h)
} styles={{ body: { padding: '20px' } }} > {renderHorizontalBar(data.errorTypes)} {t('渠道详细数据')}
} styles={{ body: { padding: 0 } }} > ); }; export default AdminMonitor;