Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
+776
View File
@@ -0,0 +1,776 @@
/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useState, useEffect } from 'react';
import { Card, Row, Col, Table, Spin } from '@douyinfe/semi-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',
) => (
<Card
style={{
background:
'linear-gradient(135deg, rgba(24, 144, 255, 0.05) 0%, rgba(24, 144, 255, 0.01) 100%)',
border: '1px solid var(--semi-color-border)',
borderRadius: '12px',
height: '100%',
}}
bodyStyle={{ padding: '24px' }}
>
<div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
<div
style={{
width: '48px',
height: '48px',
borderRadius: '12px',
background: `${color}15`,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
{React.cloneElement(icon, { size: 24, color })}
</div>
<div style={{ flex: 1 }}>
<div
style={{
fontSize: '13px',
color: 'var(--semi-color-text-2)',
marginBottom: '4px',
}}
>
{label}
</div>
<div
style={{
fontSize: '28px',
fontWeight: 700,
color: 'var(--semi-color-text-0)',
lineHeight: 1,
}}
>
{value}
{suffix && (
<span
style={{ fontSize: '16px', fontWeight: 500, marginLeft: '4px' }}
>
{suffix}
</span>
)}
</div>
</div>
</div>
</Card>
);
const renderBarChart = (data, maxValue) => {
return data.slice(0, 12).map((item, index) => {
const percentage = (item.requests / maxValue) * 100;
return (
<div key={index} style={{ marginBottom: '12px' }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: '4px',
fontSize: '12px',
color: 'var(--semi-color-text-2)',
}}
>
<span>{item.hour}</span>
<span style={{ fontWeight: 600 }}>
{item.requests.toLocaleString()}
</span>
</div>
<div
style={{
height: '24px',
background: 'var(--semi-color-fill-0)',
borderRadius: '6px',
overflow: 'hidden',
}}
>
<div
style={{
height: '100%',
width: `${percentage}%`,
background: 'linear-gradient(90deg, #1890ff 0%, #40a9ff 100%)',
borderRadius: '6px',
transition: 'width 0.6s cubic-bezier(0.4, 0, 0.2, 1)',
}}
/>
</div>
</div>
);
});
};
const renderStackedAreaChart = (data) => {
const maxTotal = Math.max(
...data.map((d) => d.promptTokens + d.completionTokens),
);
return (
<div style={{ height: '200px', position: 'relative' }}>
<svg
width='100%'
height='100%'
viewBox='0 0 100 100'
preserveAspectRatio='none'
>
<defs>
<linearGradient
id='promptGradient'
x1='0%'
y1='0%'
x2='0%'
y2='100%'
>
<stop
offset='0%'
style={{ stopColor: '#52c41a', stopOpacity: 0.6 }}
/>
<stop
offset='100%'
style={{ stopColor: '#52c41a', stopOpacity: 0.1 }}
/>
</linearGradient>
<linearGradient
id='completionGradient'
x1='0%'
y1='0%'
x2='0%'
y2='100%'
>
<stop
offset='0%'
style={{ stopColor: '#1890ff', stopOpacity: 0.6 }}
/>
<stop
offset='100%'
style={{ stopColor: '#1890ff', stopOpacity: 0.1 }}
/>
</linearGradient>
</defs>
<polyline
fill='url(#completionGradient)'
stroke='#1890ff'
strokeWidth='0.5'
points={data
.map((d, i) => {
const x = (i / (data.length - 1)) * 100;
const y = 100 - (d.completionTokens / maxTotal) * 100;
return `${x},${y}`;
})
.join(' ')}
/>
<polyline
fill='url(#promptGradient)'
stroke='#52c41a'
strokeWidth='0.5'
points={data
.map((d, i) => {
const x = (i / (data.length - 1)) * 100;
const total = d.promptTokens + d.completionTokens;
const y = 100 - (total / maxTotal) * 100;
return `${x},${y}`;
})
.join(' ')}
/>
</svg>
<div
style={{
position: 'absolute',
top: '8px',
right: '8px',
display: 'flex',
gap: '12px',
fontSize: '12px',
}}
>
<div style={{ display: 'flex', alignItems: 'center', gap: '4px' }}>
<div
style={{
width: '12px',
height: '12px',
background: '#52c41a',
borderRadius: '2px',
}}
/>
<span>Prompt</span>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '4px' }}>
<div
style={{
width: '12px',
height: '12px',
background: '#1890ff',
borderRadius: '2px',
}}
/>
<span>Completion</span>
</div>
</div>
</div>
);
};
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 (
<div>
<svg width='100%' height='200' viewBox='0 0 100 100'>
<circle
cx='50'
cy='50'
r='40'
fill='none'
stroke='var(--semi-color-fill-0)'
strokeWidth='0.5'
/>
{segments.map((segment, index) => (
<path
key={index}
d={segment.d}
fill={segment.color}
opacity='0.85'
/>
))}
<circle cx='50' cy='50' r='20' fill='var(--semi-color-bg-1)' />
</svg>
<div style={{ marginTop: '16px' }}>
{segments.map((segment, index) => (
<div
key={index}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: '8px',
fontSize: '12px',
}}
>
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<div
style={{
width: '12px',
height: '12px',
background: segment.color,
borderRadius: '2px',
}}
/>
<span>{segment.name}</span>
</div>
<span style={{ fontWeight: 600 }}>
${segment.cost.toFixed(2)} ({segment.percentage}%)
</span>
</div>
))}
</div>
</div>
);
};
const renderGaugeChart = (percentage) => {
const color =
percentage >= 90
? 'var(--semi-color-danger)'
: percentage >= 75
? 'var(--semi-color-warning)'
: 'var(--semi-color-success)';
return (
<div style={{ textAlign: 'center', padding: '20px 0' }}>
<svg width='100%' height='120' viewBox='0 0 100 60'>
<path
d='M 10 50 A 40 40 0 0 1 90 50'
fill='none'
stroke='var(--semi-color-fill-0)'
strokeWidth='8'
strokeLinecap='round'
/>
<path
d='M 10 50 A 40 40 0 0 1 90 50'
fill='none'
stroke={color}
strokeWidth='8'
strokeLinecap='round'
strokeDasharray={`${percentage * 1.256} 125.6`}
/>
<text
x='50'
y='45'
textAnchor='middle'
style={{
fontSize: '18px',
fontWeight: 700,
fill: 'var(--semi-color-text-0)',
}}
>
{percentage}%
</text>
</svg>
<div
style={{
marginTop: '8px',
fontSize: '12px',
color: 'var(--semi-color-text-2)',
}}
>
{percentage >= 90 && t('配额即将用尽')}
{percentage >= 75 && percentage < 90 && t('配额使用偏高')}
{percentage < 75 && t('配额正常')}
</div>
</div>
);
};
const renderHorizontalBar = (data) => {
const maxCount = Math.max(...data.map((d) => d.count));
return data.map((item, index) => {
const percentage = (item.count / maxCount) * 100;
return (
<div key={index} style={{ marginBottom: '16px' }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: '6px',
fontSize: '13px',
}}
>
<span style={{ color: 'var(--semi-color-text-1)' }}>
{item.type}
</span>
<span
style={{ fontWeight: 600, color: 'var(--semi-color-text-0)' }}
>
{item.count}
</span>
</div>
<div
style={{
height: '20px',
background: 'var(--semi-color-fill-0)',
borderRadius: '10px',
overflow: 'hidden',
}}
>
<div
style={{
height: '100%',
width: `${percentage}%`,
background: 'linear-gradient(90deg, #ff4d4f 0%, #ff7875 100%)',
borderRadius: '10px',
transition: 'width 0.6s cubic-bezier(0.4, 0, 0.2, 1)',
}}
/>
</div>
</div>
);
});
};
const columns = [
{
title: t('渠道名称'),
dataIndex: 'name',
key: 'name',
width: 180,
render: (text) => <strong>{text}</strong>,
},
{
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) => (
<span
style={{
color:
text > 500
? 'var(--semi-color-danger)'
: 'var(--semi-color-success)',
}}
>
{text}
</span>
),
},
{
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 (
<span
style={{
color:
rate >= 99
? 'var(--semi-color-success)'
: rate >= 95
? 'var(--semi-color-warning)'
: 'var(--semi-color-danger)',
}}
>
{text}%
</span>
);
},
},
];
if (loading) {
return (
<div style={{ textAlign: 'center', padding: '60px 0' }}>
<Spin size='large' />
</div>
);
}
return (
<div className='admin-monitor'>
<style>
{`
.admin-monitor .semi-card {
transition: all 0.3s;
}
.admin-monitor .semi-card:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.chart-card {
border: 1px solid var(--semi-color-border);
borderRadius: 12px;
}
`}
</style>
<Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
<Col xs={24} sm={12} lg={6}>
{renderKPICard(
<TrendingUp />,
t('过去24小时总请求'),
data.kpi.totalRequests.toLocaleString(),
'',
'#1890ff',
)}
</Col>
<Col xs={24} sm={12} lg={6}>
{renderKPICard(
<DollarSign />,
t('今日预估总成本'),
data.kpi.totalCost,
'¥',
'#52c41a',
)}
</Col>
<Col xs={24} sm={12} lg={6}>
{renderKPICard(
<Layers />,
t('当前活跃渠道'),
data.kpi.activeChannels,
'',
'#722ed1',
)}
</Col>
<Col xs={24} sm={12} lg={6}>
{renderKPICard(
<Activity />,
t('平均响应时间'),
data.kpi.avgResponseTime,
'ms',
'#faad14',
)}
</Col>
</Row>
<Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
<Col xs={24} lg={12}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<BarChart3 size={18} />
<span>{t('请求趋势')} (24h)</span>
</div>
}
bodyStyle={{ padding: '20px' }}
>
{renderBarChart(
data.requestTrend,
Math.max(...data.requestTrend.map((d) => d.requests)),
)}
</Card>
</Col>
<Col xs={24} lg={12}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<DollarSign size={18} />
<span>{t('成本分布')}</span>
</div>
}
bodyStyle={{ padding: '20px' }}
>
{renderDonutChart(data.costDistribution)}
</Card>
</Col>
<Col xs={24} lg={12}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<Activity size={18} />
<span>{t('Token消耗趋势')} (24h)</span>
</div>
}
bodyStyle={{ padding: '20px' }}
>
{renderStackedAreaChart(data.tokenTrend)}
</Card>
</Col>
<Col xs={24} lg={12}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<Layers size={18} />
<span>{t('配额使用情况')}</span>
</div>
}
bodyStyle={{ padding: '20px' }}
>
{renderGaugeChart(data.quotaUsage)}
</Card>
</Col>
</Row>
<Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
<Col span={24}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<AlertTriangle size={18} />
<span>{t('错误类型分布')} (24h)</span>
</div>
}
bodyStyle={{ padding: '20px' }}
>
{renderHorizontalBar(data.errorTypes)}
</Card>
</Col>
</Row>
<Row gutter={[16, 16]}>
<Col span={24}>
<Card
className='chart-card'
title={
<div
style={{ display: 'flex', alignItems: 'center', gap: '8px' }}
>
<BarChart3 size={18} />
<span>{t('渠道详细数据')}</span>
</div>
}
bodyStyle={{ padding: 0 }}
>
<Table
columns={columns}
dataSource={data.channelDetails}
pagination={false}
rowKey='id'
/>
</Card>
</Col>
</Row>
</div>
);
};
export default AdminMonitor;