Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,787 @@
|
||||
/*
|
||||
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, useCallback, useMemo } from 'react';
|
||||
import {
|
||||
Empty,
|
||||
Spin,
|
||||
Table,
|
||||
Button,
|
||||
Modal,
|
||||
Tooltip,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
Activity,
|
||||
RefreshCw,
|
||||
Server,
|
||||
ChevronRight,
|
||||
Zap,
|
||||
Clock,
|
||||
TrendingUp,
|
||||
} from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, showError } from '../../helpers';
|
||||
import { isAdmin } from '../../helpers/utils';
|
||||
|
||||
// 条形码状态组件 - 美化版
|
||||
const StatusBars = ({ dataPoints = [], loading = false, t }) => {
|
||||
// 补全或截取数据以适应展示 (展示最近60个点)
|
||||
// 注意:useMemo 必须在任何 early return 之前调用,保证 hook 调用顺序稳定。
|
||||
const displayCount = 60;
|
||||
const normalizedData = useMemo(() => {
|
||||
if (!dataPoints || dataPoints.length === 0) {
|
||||
return Array(displayCount).fill(null);
|
||||
}
|
||||
// 如果数据点过多,取最后 displayCount 个
|
||||
if (dataPoints.length > displayCount) {
|
||||
return dataPoints.slice(-displayCount);
|
||||
}
|
||||
// 如果不足,前面补 null
|
||||
const pad = Array(displayCount - dataPoints.length).fill(null);
|
||||
return [...pad, ...dataPoints];
|
||||
}, [dataPoints]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className='flex gap-[2px] items-center h-8 opacity-50 animate-pulse'>
|
||||
{Array.from({ length: 60 }).map((_, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className='w-[6px] h-4 bg-gray-200 dark:bg-gray-700 rounded-sm'
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='flex flex-col w-fit'>
|
||||
<div className='flex gap-[2px] items-end h-8'>
|
||||
{normalizedData.map((point, i) => {
|
||||
if (!point) {
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className='w-[6px] h-2 bg-gray-100 dark:bg-gray-800 rounded-sm'
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const isHealthy = point.uptime_percent >= 90;
|
||||
const isWarning =
|
||||
point.uptime_percent >= 50 && point.uptime_percent < 90;
|
||||
|
||||
let bgColor = '#ef4444'; // red-500
|
||||
let glowColor = 'rgba(239, 68, 68, 0.3)';
|
||||
if (isHealthy) {
|
||||
bgColor = '#10b981'; // emerald-500
|
||||
glowColor = 'rgba(16, 185, 129, 0.3)';
|
||||
} else if (isWarning) {
|
||||
bgColor = '#f59e0b'; // amber-500
|
||||
glowColor = 'rgba(245, 158, 11, 0.3)';
|
||||
}
|
||||
|
||||
const height = isHealthy ? '20px' : '16px';
|
||||
|
||||
return (
|
||||
<Tooltip
|
||||
key={i}
|
||||
content={
|
||||
<div className='text-xs p-1'>
|
||||
<div style={{ color: bgColor }} className='font-semibold'>
|
||||
{isHealthy ? t('正常') : t('异常')} (
|
||||
{point.uptime_percent?.toFixed(1)}%)
|
||||
</div>
|
||||
<div className='text-gray-400 mt-1'>
|
||||
{point.timestamp
|
||||
? new Date(point.timestamp * 1000).toLocaleString()
|
||||
: '-'}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div
|
||||
className='w-[6px] rounded-sm transition-all duration-200 hover:scale-y-125 hover:opacity-80 cursor-pointer'
|
||||
style={{
|
||||
backgroundColor: bgColor,
|
||||
height: height,
|
||||
boxShadow: `0 0 6px ${glowColor}`,
|
||||
}}
|
||||
/>
|
||||
</Tooltip>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<div className='flex justify-between w-full text-[10px] text-gray-400 dark:text-gray-500 mt-1.5 font-medium'>
|
||||
<span>1h</span>
|
||||
<span>{t('现在')}</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 模型测试单元格组件
|
||||
const TestModelCell = ({ modelName, groupName, t }) => {
|
||||
const [testing, setTesting] = useState(false);
|
||||
const [result, setResult] = useState(null);
|
||||
|
||||
const handleTest = async (e) => {
|
||||
e.stopPropagation();
|
||||
setTesting(true);
|
||||
setResult(null);
|
||||
|
||||
try {
|
||||
const res = await API.post(
|
||||
'/api/monitor/test/model',
|
||||
{
|
||||
model: modelName,
|
||||
group: groupName,
|
||||
},
|
||||
{
|
||||
timeout: 60000,
|
||||
skipErrorHandler: true,
|
||||
},
|
||||
);
|
||||
|
||||
const { success, message, time } = res.data;
|
||||
const timeMs = Math.round((time || 0) * 1000);
|
||||
|
||||
if (success) {
|
||||
setResult({ success: true, time: timeMs });
|
||||
} else {
|
||||
setResult({
|
||||
success: false,
|
||||
time: timeMs,
|
||||
error: message || t('测试失败'),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg =
|
||||
error.response?.data?.message || error.message || t('测试失败');
|
||||
setResult({ success: false, time: 0, error: errorMsg });
|
||||
} finally {
|
||||
setTesting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className='flex items-center gap-3'>
|
||||
<Button
|
||||
size='small'
|
||||
type='tertiary'
|
||||
theme='light'
|
||||
loading={testing}
|
||||
onClick={handleTest}
|
||||
className='!bg-gradient-to-r !from-emerald-50 !to-teal-50 !border !border-emerald-200 hover:!border-emerald-300 !rounded-lg'
|
||||
style={{ color: '#047857' }}
|
||||
>
|
||||
<Zap size={12} className='mr-1' style={{ color: '#047857' }} />
|
||||
{testing ? t('测试中') : t('测试')}
|
||||
</Button>
|
||||
{result && (
|
||||
<span
|
||||
className={`text-xs font-medium ${result.success ? 'text-emerald-600 dark:text-emerald-400' : 'text-red-500 dark:text-red-400'}`}
|
||||
>
|
||||
{result.success ? (
|
||||
<span className='flex items-center gap-1 px-2 py-0.5 bg-emerald-50 dark:bg-emerald-900/30 rounded-full'>
|
||||
<span>✓</span>
|
||||
<span>{result.time}ms</span>
|
||||
</span>
|
||||
) : (
|
||||
<Tooltip content={result.error}>
|
||||
<span className='flex items-center gap-1 px-2 py-0.5 bg-red-50 dark:bg-red-900/30 rounded-full cursor-help'>
|
||||
<span>✗</span>
|
||||
{result.time > 0 && <span>{result.time}ms</span>}
|
||||
</span>
|
||||
</Tooltip>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const GroupMonitor = () => {
|
||||
const { t } = useTranslation();
|
||||
const [groups, setGroups] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [selectedGroup, setSelectedGroup] = useState(null);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
|
||||
const [lastUpdateTime, setLastUpdateTime] = useState(null);
|
||||
const [nextRefreshTime, setNextRefreshTime] = useState(null);
|
||||
const [timeRemaining, setTimeRemaining] = useState(0);
|
||||
|
||||
const [listHistoryData, setListHistoryData] = useState({});
|
||||
|
||||
const adminUser = isAdmin();
|
||||
|
||||
const formatDateTime = (date) => {
|
||||
if (!date) return '';
|
||||
const pad = (n) => n.toString().padStart(2, '0');
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||
};
|
||||
|
||||
const fetchGroupData = useCallback(
|
||||
async (showRefreshIndicator = false) => {
|
||||
if (showRefreshIndicator) {
|
||||
setRefreshing(true);
|
||||
} else {
|
||||
setLoading(true);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await API.get('/api/monitor/groups');
|
||||
if (res.data.success) {
|
||||
const newGroups = res.data.data || [];
|
||||
setGroups(newGroups);
|
||||
|
||||
const now = new Date();
|
||||
setLastUpdateTime(now);
|
||||
const next = new Date(now.getTime() + 60000);
|
||||
setNextRefreshTime(next);
|
||||
setTimeRemaining(60);
|
||||
|
||||
const newHistoryData = {};
|
||||
newGroups.forEach((group) => {
|
||||
if (group.data_points) {
|
||||
newHistoryData[group.name] = { data_points: group.data_points };
|
||||
}
|
||||
});
|
||||
setListHistoryData(newHistoryData);
|
||||
} else {
|
||||
showError(res.data.message || t('获取数据失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
showError(error.message || t('网络请求失败'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
setRefreshing(false);
|
||||
}
|
||||
},
|
||||
[t],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
fetchGroupData();
|
||||
const interval = setInterval(() => {
|
||||
fetchGroupData(true);
|
||||
}, 60000);
|
||||
return () => clearInterval(interval);
|
||||
}, [fetchGroupData]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!nextRefreshTime) return;
|
||||
|
||||
const timer = setInterval(() => {
|
||||
const now = new Date();
|
||||
const diff = Math.max(0, Math.ceil((nextRefreshTime - now) / 1000));
|
||||
setTimeRemaining(diff);
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, [nextRefreshTime]);
|
||||
|
||||
const handleGroupClick = (group) => {
|
||||
if (!adminUser) return;
|
||||
setSelectedGroup(group);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
const handleCloseModal = () => {
|
||||
setModalVisible(false);
|
||||
};
|
||||
|
||||
const handleAfterClose = () => {
|
||||
setSelectedGroup(null);
|
||||
};
|
||||
|
||||
const getUptimeColor = (uptime) => {
|
||||
const val = Number(uptime) || 0;
|
||||
if (val >= 95) return '#10b981';
|
||||
if (val >= 80) return '#f59e0b';
|
||||
return '#ef4444';
|
||||
};
|
||||
|
||||
const getUptimeGradient = (uptime) => {
|
||||
const val = Number(uptime) || 0;
|
||||
if (val >= 95) return 'from-emerald-500 to-teal-500';
|
||||
if (val >= 80) return 'from-amber-500 to-orange-500';
|
||||
return 'from-red-500 to-rose-500';
|
||||
};
|
||||
|
||||
const modelColumns = useMemo(
|
||||
() => [
|
||||
{
|
||||
title: t('模型'),
|
||||
dataIndex: 'model',
|
||||
key: 'model',
|
||||
render: (text) => (
|
||||
<div className='font-medium text-gray-700 dark:text-gray-200 flex items-center gap-2'>
|
||||
<div className='w-2 h-2 rounded-full bg-gradient-to-r from-emerald-400 to-teal-400' />
|
||||
{text}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('可用率'),
|
||||
dataIndex: 'uptime_percent',
|
||||
key: 'uptime_percent',
|
||||
render: (value) => (
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className='w-16 h-1.5 bg-gray-100 dark:bg-gray-700 rounded-full overflow-hidden'>
|
||||
<div
|
||||
className={`h-full rounded-full bg-gradient-to-r ${getUptimeGradient(value)}`}
|
||||
style={{ width: `${Math.min(100, value || 0)}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span
|
||||
className='text-sm font-medium'
|
||||
style={{ color: getUptimeColor(value) }}
|
||||
>
|
||||
{(value || 0).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
),
|
||||
sorter: (a, b) => (a.uptime_percent || 0) - (b.uptime_percent || 0),
|
||||
},
|
||||
{
|
||||
title: t('可用/总数'),
|
||||
key: 'count',
|
||||
render: (_, record) => (
|
||||
<span className='text-gray-600 dark:text-gray-300 font-medium'>
|
||||
<span className='text-emerald-600 dark:text-emerald-400'>
|
||||
{record.available_count}
|
||||
</span>
|
||||
<span className='text-gray-300 dark:text-gray-600 mx-1'>/</span>
|
||||
<span>{record.total_count}</span>
|
||||
</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('平均响应'),
|
||||
dataIndex: 'avg_response_time',
|
||||
key: 'avg_response_time',
|
||||
render: (value) =>
|
||||
value > 0 ? (
|
||||
<div className='flex items-center gap-1.5'>
|
||||
<Clock size={12} className='text-gray-400' />
|
||||
<span
|
||||
className={`font-medium ${value > 2000 ? 'text-orange-500' : 'text-gray-600 dark:text-gray-300'}`}
|
||||
>
|
||||
{value}ms
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<span className='text-gray-300 dark:text-gray-600'>-</span>
|
||||
),
|
||||
sorter: (a, b) => a.avg_response_time - b.avg_response_time,
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
key: 'action',
|
||||
width: 200,
|
||||
render: (_, record) => (
|
||||
<TestModelCell
|
||||
modelName={record.model}
|
||||
groupName={selectedGroup?.name || ''}
|
||||
t={t}
|
||||
/>
|
||||
),
|
||||
},
|
||||
],
|
||||
[t, selectedGroup],
|
||||
);
|
||||
|
||||
const modalDataSource = useMemo(() => {
|
||||
if (!selectedGroup?.model_stats) return [];
|
||||
return selectedGroup.model_stats.filter(
|
||||
(item) => item && typeof item === 'object',
|
||||
);
|
||||
}, [selectedGroup?.model_stats]);
|
||||
|
||||
const modalPagination = useMemo(
|
||||
() => ({
|
||||
pageSize: 10,
|
||||
size: 'small',
|
||||
showSizeChanger: false,
|
||||
hideOnSinglePage: true,
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className='flex flex-col items-center justify-center py-20'>
|
||||
<div className='relative'>
|
||||
<div className='absolute inset-0 bg-emerald-400/30 rounded-full blur-xl animate-pulse' />
|
||||
<Spin size='large' />
|
||||
</div>
|
||||
<p className='mt-4 text-gray-500 dark:text-gray-400'>
|
||||
{t('加载中...')}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (groups.length === 0) {
|
||||
return (
|
||||
<div className='py-16'>
|
||||
<Empty description={t('暂无分组数据')} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='group-monitor'>
|
||||
{/* 分组列表 */}
|
||||
<div className='space-y-3'>
|
||||
{groups.map((group, index) => {
|
||||
const history = listHistoryData[group.name];
|
||||
const availableModelCount =
|
||||
group.model_stats?.filter((m) => m.available_count > 0).length || 0;
|
||||
const totalModelCount = group.model_stats?.length || 0;
|
||||
const uptimePercent = group.uptime_percent || 0;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={group.name}
|
||||
className={`group relative bg-white/70 dark:bg-gray-800/70 backdrop-blur-sm rounded-xl border border-gray-100 dark:border-gray-700/50 transition-all duration-300 overflow-hidden ${adminUser ? 'hover:border-emerald-200 dark:hover:border-emerald-700/50 hover:shadow-lg hover:shadow-emerald-500/5 cursor-pointer' : ''}`}
|
||||
onClick={adminUser ? () => handleGroupClick(group) : undefined}
|
||||
>
|
||||
{/* 左侧状态指示条 */}
|
||||
<div
|
||||
className={`absolute left-0 top-0 bottom-0 w-1 bg-gradient-to-b ${getUptimeGradient(uptimePercent)}`}
|
||||
/>
|
||||
|
||||
{/* 桌面端布局 */}
|
||||
<div className='hidden md:flex items-center px-6 py-5 pl-8'>
|
||||
{/* 分组名称 */}
|
||||
<div className='w-[180px] shrink-0 pr-4'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div
|
||||
className={`w-10 h-10 rounded-xl bg-gradient-to-br ${getUptimeGradient(uptimePercent)} flex items-center justify-center shadow-lg`}
|
||||
>
|
||||
<Server className='w-5 h-5 text-white' />
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<div
|
||||
className='text-base font-bold text-gray-800 dark:text-gray-100 truncate group-hover:text-emerald-600 dark:group-hover:text-emerald-400 transition-colors'
|
||||
title={group.name}
|
||||
>
|
||||
{group.name}
|
||||
</div>
|
||||
<div className='text-xs text-gray-400 dark:text-gray-500 mt-0.5'>
|
||||
{t('服务分组')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 可用率 */}
|
||||
<div className='w-[120px] shrink-0 flex justify-center'>
|
||||
<div className='relative'>
|
||||
<div
|
||||
className={`px-4 py-2 rounded-xl text-white text-sm font-bold shadow-lg bg-gradient-to-r ${getUptimeGradient(uptimePercent)}`}
|
||||
>
|
||||
{uptimePercent.toFixed(2)}%
|
||||
</div>
|
||||
{uptimePercent >= 95 && (
|
||||
<div className='absolute -top-1 -right-1 w-3 h-3 bg-yellow-400 rounded-full animate-pulse shadow-lg' />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 模型数量 */}
|
||||
<div className='w-[120px] shrink-0 flex flex-col items-center justify-center'>
|
||||
<div className='flex items-baseline gap-1'>
|
||||
<span
|
||||
className={`text-xl font-bold ${availableModelCount > 0 ? 'text-gray-800 dark:text-gray-100' : 'text-red-500'}`}
|
||||
>
|
||||
{availableModelCount}
|
||||
</span>
|
||||
<span className='text-gray-300 dark:text-gray-600 text-sm'>
|
||||
/
|
||||
</span>
|
||||
<span className='text-gray-500 dark:text-gray-400 text-sm font-medium'>
|
||||
{totalModelCount}
|
||||
</span>
|
||||
</div>
|
||||
<div className='text-xs text-gray-400 dark:text-gray-500 mt-0.5'>
|
||||
{t('模型')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 趋势图 */}
|
||||
<div className='flex-1 flex justify-center px-6'>
|
||||
<StatusBars
|
||||
dataPoints={history?.data_points}
|
||||
loading={!history}
|
||||
t={t}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 查看按钮 - 仅管理员可见 */}
|
||||
{adminUser && (
|
||||
<div className='w-[100px] shrink-0 flex justify-end'>
|
||||
<button
|
||||
className='flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-emerald-600 dark:text-emerald-400 bg-emerald-50 dark:bg-emerald-900/30 hover:bg-emerald-100 dark:hover:bg-emerald-900/50 rounded-lg transition-all group-hover:translate-x-1'
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleGroupClick(group);
|
||||
}}
|
||||
>
|
||||
{t('详情')}
|
||||
<ChevronRight
|
||||
size={16}
|
||||
className='transition-transform group-hover:translate-x-0.5'
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 移动端布局 */}
|
||||
<div className='flex md:hidden flex-col px-4 py-4 pl-6 gap-3'>
|
||||
<div className='flex items-center justify-between'>
|
||||
<div className='flex items-center gap-3 min-w-0 flex-1'>
|
||||
<div
|
||||
className={`w-9 h-9 shrink-0 rounded-lg bg-gradient-to-br ${getUptimeGradient(uptimePercent)} flex items-center justify-center shadow-md`}
|
||||
>
|
||||
<Server className='w-4 h-4 text-white' />
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<div
|
||||
className='text-sm font-bold text-gray-800 dark:text-gray-100 truncate'
|
||||
title={group.name}
|
||||
>
|
||||
{group.name}
|
||||
</div>
|
||||
<div className='text-[11px] text-gray-400 dark:text-gray-500'>
|
||||
{t('服务分组')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className='flex items-center gap-3 shrink-0'>
|
||||
<div className='relative'>
|
||||
<div
|
||||
className={`px-3 py-1.5 rounded-lg text-white text-xs font-bold shadow-md bg-gradient-to-r ${getUptimeGradient(uptimePercent)}`}
|
||||
>
|
||||
{uptimePercent.toFixed(2)}%
|
||||
</div>
|
||||
</div>
|
||||
<div className='text-center'>
|
||||
<div className='flex items-baseline gap-0.5'>
|
||||
<span
|
||||
className={`text-sm font-bold ${availableModelCount > 0 ? 'text-gray-800 dark:text-gray-100' : 'text-red-500'}`}
|
||||
>
|
||||
{availableModelCount}
|
||||
</span>
|
||||
<span className='text-gray-300 dark:text-gray-600 text-xs'>
|
||||
/
|
||||
</span>
|
||||
<span className='text-gray-500 dark:text-gray-400 text-xs'>
|
||||
{totalModelCount}
|
||||
</span>
|
||||
</div>
|
||||
<div className='text-[10px] text-gray-400 dark:text-gray-500'>
|
||||
{t('模型')}
|
||||
</div>
|
||||
</div>
|
||||
{adminUser && (
|
||||
<ChevronRight size={16} className='text-gray-400' />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{/* 移动端趋势图 */}
|
||||
<div className='overflow-x-auto -mx-2 px-2'>
|
||||
<StatusBars
|
||||
dataPoints={history?.data_points}
|
||||
loading={!history}
|
||||
t={t}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* 刷新信息 */}
|
||||
{lastUpdateTime && (
|
||||
<div className='mt-8 flex flex-col items-center justify-center gap-3'>
|
||||
<div className='flex items-center gap-4 px-6 py-3 bg-white/50 dark:bg-gray-800/50 backdrop-blur-sm rounded-full border border-gray-100 dark:border-gray-700/50'>
|
||||
<div className='flex items-center gap-2 text-gray-500 dark:text-gray-400 text-sm'>
|
||||
<Clock size={14} />
|
||||
<span>
|
||||
{t('更新时间')}: {formatDateTime(lastUpdateTime)}
|
||||
</span>
|
||||
</div>
|
||||
<div className='w-px h-4 bg-gray-200 dark:bg-gray-700' />
|
||||
<div className='flex items-center gap-2 text-emerald-600 dark:text-emerald-400 text-sm font-medium'>
|
||||
<div className='relative'>
|
||||
<svg
|
||||
className='w-5 h-5 transform -rotate-90'
|
||||
viewBox='0 0 24 24'
|
||||
>
|
||||
<circle
|
||||
cx='12'
|
||||
cy='12'
|
||||
r='10'
|
||||
fill='none'
|
||||
stroke='currentColor'
|
||||
strokeWidth='2'
|
||||
opacity='0.2'
|
||||
/>
|
||||
<circle
|
||||
cx='12'
|
||||
cy='12'
|
||||
r='10'
|
||||
fill='none'
|
||||
stroke='currentColor'
|
||||
strokeWidth='2'
|
||||
strokeDasharray={62.83}
|
||||
strokeDashoffset={62.83 * (1 - timeRemaining / 60)}
|
||||
strokeLinecap='round'
|
||||
className='transition-all duration-1000'
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<span>
|
||||
{Math.floor(timeRemaining / 60)
|
||||
.toString()
|
||||
.padStart(2, '0')}
|
||||
:
|
||||
{Math.floor(timeRemaining % 60)
|
||||
.toString()
|
||||
.padStart(2, '0')}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
icon={
|
||||
<RefreshCw
|
||||
className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`}
|
||||
/>
|
||||
}
|
||||
onClick={() => fetchGroupData(true)}
|
||||
loading={refreshing}
|
||||
type='tertiary'
|
||||
theme='borderless'
|
||||
className='!text-gray-500 hover:!text-emerald-600 dark:!text-gray-400 dark:hover:!text-emerald-400'
|
||||
>
|
||||
{t('立即刷新')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 详情弹窗 */}
|
||||
<Modal
|
||||
visible={modalVisible}
|
||||
onCancel={handleCloseModal}
|
||||
afterClose={handleAfterClose}
|
||||
footer={null}
|
||||
width={950}
|
||||
title={null}
|
||||
bodyStyle={{ padding: '0' }}
|
||||
centered
|
||||
className='group-monitor-modal'
|
||||
>
|
||||
{selectedGroup && (
|
||||
<div className='bg-gradient-to-br from-white to-gray-50/50 dark:from-gray-800 dark:to-gray-900/50 rounded-xl overflow-hidden'>
|
||||
{/* Header */}
|
||||
<div className='relative px-8 py-8 border-b border-gray-100 dark:border-gray-700 overflow-hidden'>
|
||||
{/* 装饰背景 */}
|
||||
<div className='absolute inset-0 bg-gradient-to-r from-emerald-500/5 via-teal-500/5 to-cyan-500/5' />
|
||||
<div className='absolute top-0 right-0 w-64 h-64 bg-gradient-to-br from-emerald-400/10 to-transparent rounded-full blur-3xl' />
|
||||
|
||||
<div className='relative flex items-center justify-between'>
|
||||
<div className='flex items-center gap-5'>
|
||||
<div className='relative group'>
|
||||
<div className='absolute inset-0 bg-gradient-to-br from-emerald-500 to-teal-600 rounded-2xl blur-lg opacity-50' />
|
||||
<div
|
||||
className={`relative w-16 h-16 rounded-2xl bg-gradient-to-br ${getUptimeGradient(selectedGroup.uptime_percent)} flex items-center justify-center shadow-xl`}
|
||||
>
|
||||
<Server className='w-8 h-8 text-white' />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h2 className='text-2xl font-bold text-gray-900 dark:text-white'>
|
||||
{selectedGroup.name}
|
||||
</h2>
|
||||
<div className='text-gray-500 dark:text-gray-400 text-sm mt-1 flex items-center gap-2'>
|
||||
<Activity size={14} />
|
||||
{t('分组监控详情')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex gap-4'>
|
||||
<div className='px-6 py-4 bg-white/80 dark:bg-gray-800/80 backdrop-blur-sm rounded-2xl border border-gray-100 dark:border-gray-700 shadow-lg min-w-[120px]'>
|
||||
<div className='flex items-center gap-1.5 text-xs text-gray-500 dark:text-gray-400 mb-2'>
|
||||
<TrendingUp size={12} />
|
||||
{t('可用率')}
|
||||
</div>
|
||||
<div
|
||||
className={`text-2xl font-bold bg-clip-text text-transparent bg-gradient-to-r ${getUptimeGradient(selectedGroup.uptime_percent)}`}
|
||||
>
|
||||
{(selectedGroup.uptime_percent || 0).toFixed(2)}%
|
||||
</div>
|
||||
</div>
|
||||
<div className='px-6 py-4 bg-white/80 dark:bg-gray-800/80 backdrop-blur-sm rounded-2xl border border-gray-100 dark:border-gray-700 shadow-lg min-w-[120px]'>
|
||||
<div className='flex items-center gap-1.5 text-xs text-gray-500 dark:text-gray-400 mb-2'>
|
||||
<Server size={12} />
|
||||
{t('模型数量')}
|
||||
</div>
|
||||
<div className='text-2xl font-bold text-gray-800 dark:text-white'>
|
||||
{selectedGroup.models?.length || 0}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Table Content */}
|
||||
<div className='p-6 sm:p-8'>
|
||||
<div className='bg-white dark:bg-gray-800 rounded-xl border border-gray-100 dark:border-gray-700 shadow-sm overflow-hidden'>
|
||||
<Table
|
||||
columns={modelColumns}
|
||||
dataSource={modalDataSource}
|
||||
pagination={modalPagination}
|
||||
size='middle'
|
||||
rowKey='model'
|
||||
emptyText={
|
||||
<Empty
|
||||
description={t('暂无模型数据')}
|
||||
imageStyle={{ height: 48 }}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default GroupMonitor;
|
||||
@@ -0,0 +1,883 @@
|
||||
/*
|
||||
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, useCallback } from 'react';
|
||||
import {
|
||||
Card,
|
||||
Row,
|
||||
Col,
|
||||
Empty,
|
||||
Spin,
|
||||
Tag,
|
||||
Table,
|
||||
Progress,
|
||||
Button,
|
||||
DatePicker,
|
||||
Space,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
Activity,
|
||||
Clock,
|
||||
Server,
|
||||
Zap,
|
||||
AlertTriangle,
|
||||
CheckCircle,
|
||||
XCircle,
|
||||
RefreshCw,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
} from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, showError } from '../../helpers';
|
||||
|
||||
const GroupMonitorAdmin = () => {
|
||||
const { t } = useTranslation();
|
||||
const [groups, setGroups] = useState([]);
|
||||
const [overview, setOverview] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [expandedGroups, setExpandedGroups] = useState({});
|
||||
const [historyData, setHistoryData] = useState({});
|
||||
const [historyLoading, setHistoryLoading] = useState({});
|
||||
const [dateRange, setDateRange] = useState({});
|
||||
|
||||
// 获取分组监控数据
|
||||
const fetchGroupData = useCallback(
|
||||
async (showRefreshIndicator = false) => {
|
||||
if (showRefreshIndicator) {
|
||||
setRefreshing(true);
|
||||
} else {
|
||||
setLoading(true);
|
||||
}
|
||||
|
||||
try {
|
||||
const [groupsRes, overviewRes] = await Promise.all([
|
||||
API.get('/api/monitor/groups/admin'),
|
||||
API.get('/api/monitor/groups/overview'),
|
||||
]);
|
||||
|
||||
if (groupsRes.data.success) {
|
||||
setGroups(groupsRes.data.data || []);
|
||||
} else {
|
||||
showError(groupsRes.data.message || t('获取分组数据失败'));
|
||||
}
|
||||
|
||||
if (overviewRes.data.success) {
|
||||
setOverview(overviewRes.data.data);
|
||||
}
|
||||
} catch (error) {
|
||||
showError(error.message || t('网络请求失败'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
setRefreshing(false);
|
||||
}
|
||||
},
|
||||
[t],
|
||||
);
|
||||
|
||||
// 获取历史趋势数据
|
||||
const fetchHistoryData = useCallback(
|
||||
async (groupName, startTime, endTime) => {
|
||||
setHistoryLoading((prev) => ({ ...prev, [groupName]: true }));
|
||||
|
||||
try {
|
||||
let url = `/api/monitor/groups/${encodeURIComponent(groupName)}/history`;
|
||||
if (startTime && endTime) {
|
||||
url += `?start_time=${Math.floor(startTime.getTime() / 1000)}&end_time=${Math.floor(endTime.getTime() / 1000)}`;
|
||||
} else {
|
||||
url += '?period=24h';
|
||||
}
|
||||
|
||||
const res = await API.get(url);
|
||||
if (res.data.success) {
|
||||
setHistoryData((prev) => ({ ...prev, [groupName]: res.data.data }));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取历史数据失败:', error);
|
||||
} finally {
|
||||
setHistoryLoading((prev) => ({ ...prev, [groupName]: false }));
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
fetchGroupData();
|
||||
|
||||
// 每30秒刷新一次数据
|
||||
const interval = setInterval(() => {
|
||||
fetchGroupData(true);
|
||||
}, 30000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [fetchGroupData]);
|
||||
|
||||
// 切换分组展开状态
|
||||
const toggleGroupExpand = (groupName) => {
|
||||
setExpandedGroups((prev) => {
|
||||
const newState = { ...prev, [groupName]: !prev[groupName] };
|
||||
if (newState[groupName] && !historyData[groupName]) {
|
||||
fetchHistoryData(groupName);
|
||||
}
|
||||
return newState;
|
||||
});
|
||||
};
|
||||
|
||||
// 处理日期范围变更
|
||||
const handleDateRangeChange = (groupName, dates) => {
|
||||
setDateRange((prev) => ({ ...prev, [groupName]: dates }));
|
||||
if (dates && dates.length === 2) {
|
||||
fetchHistoryData(groupName, dates[0], dates[1]);
|
||||
}
|
||||
};
|
||||
|
||||
// 格式化时间戳
|
||||
const formatTime = (timestamp) => {
|
||||
if (!timestamp) return '-';
|
||||
const date = new Date(timestamp * 1000);
|
||||
return date.toLocaleString('zh-CN', {
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
};
|
||||
|
||||
// 格式化相对时间
|
||||
const formatRelativeTime = (timestamp) => {
|
||||
if (!timestamp) return '-';
|
||||
const now = Date.now() / 1000;
|
||||
const diff = now - timestamp;
|
||||
|
||||
if (diff < 60) return t('刚刚');
|
||||
if (diff < 3600) return `${Math.floor(diff / 60)} ${t('分钟前')}`;
|
||||
if (diff < 86400) return `${Math.floor(diff / 3600)} ${t('小时前')}`;
|
||||
return `${Math.floor(diff / 86400)} ${t('天前')}`;
|
||||
};
|
||||
|
||||
// 获取健康度颜色
|
||||
const getHealthColor = (score) => {
|
||||
if (score >= 90) return 'var(--semi-color-success)';
|
||||
if (score >= 70) return 'var(--semi-color-warning)';
|
||||
return 'var(--semi-color-danger)';
|
||||
};
|
||||
|
||||
// 获取状态配置
|
||||
const getStatusConfig = (status) => {
|
||||
switch (status) {
|
||||
case 'healthy':
|
||||
return {
|
||||
color: 'green',
|
||||
icon: <CheckCircle className='w-4 h-4' />,
|
||||
text: t('健康'),
|
||||
};
|
||||
case 'warning':
|
||||
return {
|
||||
color: 'orange',
|
||||
icon: <AlertTriangle className='w-4 h-4' />,
|
||||
text: t('警告'),
|
||||
};
|
||||
case 'critical':
|
||||
return {
|
||||
color: 'red',
|
||||
icon: <XCircle className='w-4 h-4' />,
|
||||
text: t('严重'),
|
||||
};
|
||||
default:
|
||||
return { color: 'grey', icon: null, text: t('未知') };
|
||||
}
|
||||
};
|
||||
|
||||
// 获取渠道状态配置
|
||||
const getChannelStatusConfig = (status) => {
|
||||
switch (status) {
|
||||
case 1:
|
||||
return { color: 'green', text: t('启用') };
|
||||
case 2:
|
||||
return { color: 'grey', text: t('手动禁用') };
|
||||
case 3:
|
||||
return { color: 'orange', text: t('自动禁用') };
|
||||
default:
|
||||
return { color: 'grey', text: t('未知') };
|
||||
}
|
||||
};
|
||||
|
||||
// 渲染趋势图
|
||||
const renderTrendChart = (dataPoints, height = 80) => {
|
||||
if (!dataPoints || dataPoints.length === 0) return null;
|
||||
|
||||
const uptimeData = dataPoints.map((p) => p.uptime_percent);
|
||||
const max = Math.max(...uptimeData, 100);
|
||||
const min = Math.min(...uptimeData, 0);
|
||||
const range = max - min || 1;
|
||||
|
||||
const points = uptimeData
|
||||
.map((value, index) => {
|
||||
const x = (index / (uptimeData.length - 1)) * 100;
|
||||
const y = 100 - ((value - min) / range) * 100;
|
||||
return `${x},${y}`;
|
||||
})
|
||||
.join(' ');
|
||||
|
||||
const avgUptime = uptimeData.reduce((a, b) => a + b, 0) / uptimeData.length;
|
||||
const lineColor = getHealthColor(avgUptime);
|
||||
|
||||
return (
|
||||
<svg
|
||||
width='100%'
|
||||
height={height}
|
||||
viewBox='0 0 100 100'
|
||||
preserveAspectRatio='none'
|
||||
style={{ display: 'block' }}
|
||||
>
|
||||
<defs>
|
||||
<linearGradient
|
||||
id={`admin-gradient-${avgUptime}`}
|
||||
x1='0%'
|
||||
y1='0%'
|
||||
x2='0%'
|
||||
y2='100%'
|
||||
>
|
||||
<stop
|
||||
offset='0%'
|
||||
style={{ stopColor: lineColor, stopOpacity: 0.3 }}
|
||||
/>
|
||||
<stop
|
||||
offset='100%'
|
||||
style={{ stopColor: lineColor, stopOpacity: 0.05 }}
|
||||
/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<polyline
|
||||
fill={`url(#admin-gradient-${avgUptime})`}
|
||||
stroke='none'
|
||||
points={`0,100 ${points} 100,100`}
|
||||
/>
|
||||
<polyline
|
||||
fill='none'
|
||||
stroke={lineColor}
|
||||
strokeWidth='2'
|
||||
points={points}
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
// 渠道列定义
|
||||
const channelColumns = [
|
||||
{
|
||||
title: t('渠道名称'),
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
render: (text, record) => (
|
||||
<div className='flex items-center gap-2'>
|
||||
<Server className='w-4 h-4 text-gray-400' />
|
||||
<span className='font-medium'>{text}</span>
|
||||
{record.priority > 0 && (
|
||||
<Tag size='small' color='blue'>
|
||||
P{record.priority}
|
||||
</Tag>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'status',
|
||||
key: 'status',
|
||||
width: 100,
|
||||
render: (status) => {
|
||||
const config = getChannelStatusConfig(status);
|
||||
return <Tag color={config.color}>{config.text}</Tag>;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('响应时间'),
|
||||
dataIndex: 'response_time',
|
||||
key: 'response_time',
|
||||
width: 120,
|
||||
render: (time) => {
|
||||
if (!time) return '-';
|
||||
let color = 'green';
|
||||
if (time > 3000) color = 'red';
|
||||
else if (time > 1000) color = 'orange';
|
||||
return (
|
||||
<span
|
||||
style={{
|
||||
color:
|
||||
color === 'green'
|
||||
? 'var(--semi-color-success)'
|
||||
: color === 'orange'
|
||||
? 'var(--semi-color-warning)'
|
||||
: 'var(--semi-color-danger)',
|
||||
}}
|
||||
>
|
||||
<Zap className='w-3 h-3 inline mr-1' />
|
||||
{time}ms
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('最后测试'),
|
||||
dataIndex: 'test_time',
|
||||
key: 'test_time',
|
||||
width: 120,
|
||||
render: (time) => (
|
||||
<span className='text-gray-500 text-sm'>
|
||||
{formatRelativeTime(time)}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('支持模型'),
|
||||
dataIndex: 'models',
|
||||
key: 'models',
|
||||
render: (models) => (
|
||||
<div className='flex flex-wrap gap-1'>
|
||||
{models?.slice(0, 3).map((m, i) => (
|
||||
<Tag key={i} size='small'>
|
||||
{m}
|
||||
</Tag>
|
||||
))}
|
||||
{models?.length > 3 && (
|
||||
<Tag size='small' color='grey'>
|
||||
+{models.length - 3}
|
||||
</Tag>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 模型统计列定义
|
||||
const modelColumns = [
|
||||
{
|
||||
title: t('模型'),
|
||||
dataIndex: 'model',
|
||||
key: 'model',
|
||||
render: (text) => <Tag>{text}</Tag>,
|
||||
},
|
||||
{
|
||||
title: t('可用率'),
|
||||
dataIndex: 'uptime_percent',
|
||||
key: 'uptime_percent',
|
||||
render: (value) => (
|
||||
<Progress
|
||||
percent={value}
|
||||
size='small'
|
||||
style={{ width: 100 }}
|
||||
stroke={getHealthColor(value)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('可用/总数'),
|
||||
key: 'count',
|
||||
render: (_, record) => `${record.available_count}/${record.total_count}`,
|
||||
},
|
||||
{
|
||||
title: t('平均响应'),
|
||||
dataIndex: 'avg_response_time',
|
||||
key: 'avg_response_time',
|
||||
render: (value) => (value > 0 ? `${value}ms` : '-'),
|
||||
},
|
||||
];
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div style={{ textAlign: 'center', padding: '60px 0' }}>
|
||||
<Spin size='large' />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='group-monitor-admin'>
|
||||
<style>
|
||||
{`
|
||||
.overview-card {
|
||||
transition: all 0.3s;
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.overview-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.group-admin-card {
|
||||
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
border: 1px solid var(--semi-color-border);
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.group-admin-card:hover {
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.health-ring {
|
||||
position: relative;
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
}
|
||||
|
||||
.health-ring-bg {
|
||||
fill: none;
|
||||
stroke: var(--semi-color-fill-1);
|
||||
stroke-width: 8;
|
||||
}
|
||||
|
||||
.health-ring-progress {
|
||||
fill: none;
|
||||
stroke-width: 8;
|
||||
stroke-linecap: round;
|
||||
transform: rotate(-90deg);
|
||||
transform-origin: 50% 50%;
|
||||
transition: stroke-dasharray 0.5s ease;
|
||||
}
|
||||
|
||||
.health-text {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.stat-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.stat-box {
|
||||
padding: 12px;
|
||||
background: var(--semi-color-fill-0);
|
||||
border-radius: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-box-value {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
color: var(--semi-color-text-0);
|
||||
}
|
||||
|
||||
.stat-box-label {
|
||||
font-size: 12px;
|
||||
color: var(--semi-color-text-2);
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.trend-section {
|
||||
background: var(--semi-color-fill-0);
|
||||
border-radius: 8px;
|
||||
padding: 16px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
`}
|
||||
</style>
|
||||
|
||||
{/* 概览卡片 */}
|
||||
{overview && (
|
||||
<Row gutter={[16, 16]} className='mb-6'>
|
||||
<Col xs={12} sm={6}>
|
||||
<Card
|
||||
className='overview-card'
|
||||
bodyStyle={{ padding: '20px', textAlign: 'center' }}
|
||||
>
|
||||
<div className='text-3xl font-bold text-blue-500'>
|
||||
{overview.total_groups}
|
||||
</div>
|
||||
<div className='text-gray-500 mt-1'>{t('分组总数')}</div>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={12} sm={6}>
|
||||
<Card
|
||||
className='overview-card'
|
||||
bodyStyle={{ padding: '20px', textAlign: 'center' }}
|
||||
>
|
||||
<div className='text-3xl font-bold text-green-500'>
|
||||
{overview.healthy_groups}
|
||||
</div>
|
||||
<div className='text-gray-500 mt-1'>{t('健康分组')}</div>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={12} sm={6}>
|
||||
<Card
|
||||
className='overview-card'
|
||||
bodyStyle={{ padding: '20px', textAlign: 'center' }}
|
||||
>
|
||||
<div className='text-3xl font-bold text-orange-500'>
|
||||
{overview.warning_groups}
|
||||
</div>
|
||||
<div className='text-gray-500 mt-1'>{t('警告分组')}</div>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={12} sm={6}>
|
||||
<Card
|
||||
className='overview-card'
|
||||
bodyStyle={{ padding: '20px', textAlign: 'center' }}
|
||||
>
|
||||
<div
|
||||
className='text-3xl font-bold'
|
||||
style={{ color: getHealthColor(overview.average_uptime) }}
|
||||
>
|
||||
{overview.average_uptime.toFixed(1)}%
|
||||
</div>
|
||||
<div className='text-gray-500 mt-1'>{t('平均可用率')}</div>
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
)}
|
||||
|
||||
{/* 刷新按钮 */}
|
||||
<div className='flex justify-end mb-4'>
|
||||
<Button
|
||||
icon={
|
||||
<RefreshCw
|
||||
className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`}
|
||||
/>
|
||||
}
|
||||
onClick={() => fetchGroupData(true)}
|
||||
loading={refreshing}
|
||||
>
|
||||
{t('刷新')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{groups.length === 0 ? (
|
||||
<Empty description={t('暂无分组数据')} />
|
||||
) : (
|
||||
<Row gutter={[16, 16]}>
|
||||
{groups.map((group) => {
|
||||
const isExpanded = expandedGroups[group.name];
|
||||
const statusConfig = getStatusConfig(group.status);
|
||||
const groupHistory = historyData[group.name];
|
||||
const isHistoryLoading = historyLoading[group.name];
|
||||
|
||||
return (
|
||||
<Col key={group.name} xs={24}>
|
||||
<Card className='group-admin-card' bodyStyle={{ padding: 0 }}>
|
||||
{/* 分组头部 */}
|
||||
<div
|
||||
className='p-4 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors'
|
||||
onClick={() => toggleGroupExpand(group.name)}
|
||||
>
|
||||
<div className='flex items-center justify-between'>
|
||||
<div className='flex items-center gap-4'>
|
||||
{/* 健康度环形图 */}
|
||||
<div className='health-ring'>
|
||||
<svg viewBox='0 0 100 100'>
|
||||
<circle
|
||||
className='health-ring-bg'
|
||||
cx='50'
|
||||
cy='50'
|
||||
r='40'
|
||||
/>
|
||||
<circle
|
||||
className='health-ring-progress'
|
||||
cx='50'
|
||||
cy='50'
|
||||
r='40'
|
||||
stroke={getHealthColor(group.health_score)}
|
||||
strokeDasharray={`${group.health_score * 2.51} 251`}
|
||||
/>
|
||||
</svg>
|
||||
<span
|
||||
className='health-text'
|
||||
style={{
|
||||
color: getHealthColor(group.health_score),
|
||||
}}
|
||||
>
|
||||
{Math.round(group.health_score)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<h3 className='text-xl font-semibold m-0'>
|
||||
{group.name}
|
||||
</h3>
|
||||
<Tag color={statusConfig.color}>
|
||||
{statusConfig.icon}
|
||||
<span className='ml-1'>{statusConfig.text}</span>
|
||||
</Tag>
|
||||
</div>
|
||||
<div className='flex items-center gap-4 mt-2 text-sm text-gray-500'>
|
||||
<span>
|
||||
<Clock className='w-4 h-4 inline mr-1' />
|
||||
{t('最后测试')}:{' '}
|
||||
{formatRelativeTime(group.last_test_time)}
|
||||
</span>
|
||||
<span>
|
||||
<Server className='w-4 h-4 inline mr-1' />
|
||||
{group.available_count}/{group.total_channels}{' '}
|
||||
{t('可用')}
|
||||
</span>
|
||||
{group.auto_disabled_count > 0 && (
|
||||
<span className='text-orange-500'>
|
||||
<AlertTriangle className='w-4 h-4 inline mr-1' />
|
||||
{group.auto_disabled_count} {t('自动禁用')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex items-center gap-6'>
|
||||
{/* 快速统计 */}
|
||||
<div className='hidden md:flex items-center gap-6'>
|
||||
<div className='text-center'>
|
||||
<div
|
||||
className='text-2xl font-bold'
|
||||
style={{
|
||||
color: getHealthColor(group.uptime_percent),
|
||||
}}
|
||||
>
|
||||
{group.uptime_percent.toFixed(1)}%
|
||||
</div>
|
||||
<div className='text-xs text-gray-500'>
|
||||
{t('可用率')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='text-center'>
|
||||
<div className='text-2xl font-bold text-blue-500'>
|
||||
{group.avg_response_time || '-'}
|
||||
<span className='text-sm font-normal'>ms</span>
|
||||
</div>
|
||||
<div className='text-xs text-gray-500'>
|
||||
{t('平均响应')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='text-center'>
|
||||
<div className='text-2xl font-bold text-brand'>
|
||||
{group.models?.length || 0}
|
||||
</div>
|
||||
<div className='text-xs text-gray-500'>
|
||||
{t('模型数')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isExpanded ? (
|
||||
<ChevronUp className='w-6 h-6 text-gray-400' />
|
||||
) : (
|
||||
<ChevronDown className='w-6 h-6 text-gray-400' />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 展开详情 */}
|
||||
{isExpanded && (
|
||||
<div className='border-t border-gray-200 dark:border-gray-700'>
|
||||
{/* 详细统计 */}
|
||||
<div className='p-4 bg-gray-50 dark:bg-gray-800'>
|
||||
<div className='stat-grid'>
|
||||
<div className='stat-box'>
|
||||
<div
|
||||
className='stat-box-value'
|
||||
style={{
|
||||
color: getHealthColor(group.uptime_percent),
|
||||
}}
|
||||
>
|
||||
{group.uptime_percent.toFixed(1)}%
|
||||
</div>
|
||||
<div className='stat-box-label'>{t('可用率')}</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-green-500'>
|
||||
{group.available_count}
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('可用渠道')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-gray-500'>
|
||||
{group.disabled_count}
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('禁用渠道')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-orange-500'>
|
||||
{group.auto_disabled_count}
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('自动禁用')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-blue-500'>
|
||||
{group.avg_response_time || '-'}ms
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('平均响应')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-red-500'>
|
||||
{group.max_response_time || '-'}ms
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('最慢响应')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-green-500'>
|
||||
{group.min_response_time || '-'}ms
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('最快响应')}
|
||||
</div>
|
||||
</div>
|
||||
<div className='stat-box'>
|
||||
<div className='stat-box-value text-brand'>
|
||||
{group.models?.length || 0}
|
||||
</div>
|
||||
<div className='stat-box-label'>
|
||||
{t('支持模型')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 时间选择器 */}
|
||||
<div className='flex items-center gap-4 mt-4'>
|
||||
<span className='text-sm font-medium'>
|
||||
{t('趋势时间范围')}:
|
||||
</span>
|
||||
<DatePicker
|
||||
type='dateTimeRange'
|
||||
style={{ width: 320 }}
|
||||
value={dateRange[group.name]}
|
||||
onChange={(dates) =>
|
||||
handleDateRangeChange(group.name, dates)
|
||||
}
|
||||
placeholder={[t('开始时间'), t('结束时间')]}
|
||||
/>
|
||||
<Space>
|
||||
<Button
|
||||
size='small'
|
||||
onClick={() => {
|
||||
const now = new Date();
|
||||
const start = new Date(
|
||||
now.getTime() - 24 * 60 * 60 * 1000,
|
||||
);
|
||||
handleDateRangeChange(group.name, [start, now]);
|
||||
}}
|
||||
>
|
||||
24h
|
||||
</Button>
|
||||
<Button
|
||||
size='small'
|
||||
onClick={() => {
|
||||
const now = new Date();
|
||||
const start = new Date(
|
||||
now.getTime() - 7 * 24 * 60 * 60 * 1000,
|
||||
);
|
||||
handleDateRangeChange(group.name, [start, now]);
|
||||
}}
|
||||
>
|
||||
7d
|
||||
</Button>
|
||||
<Button
|
||||
size='small'
|
||||
onClick={() => {
|
||||
const now = new Date();
|
||||
const start = new Date(
|
||||
now.getTime() - 30 * 24 * 60 * 60 * 1000,
|
||||
);
|
||||
handleDateRangeChange(group.name, [start, now]);
|
||||
}}
|
||||
>
|
||||
30d
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
|
||||
{/* 趋势图 */}
|
||||
{isHistoryLoading ? (
|
||||
<div className='text-center py-4'>
|
||||
<Spin />
|
||||
</div>
|
||||
) : (
|
||||
groupHistory?.data_points && (
|
||||
<div className='trend-section'>
|
||||
<div className='flex items-center justify-between mb-2'>
|
||||
<span className='text-sm font-medium'>
|
||||
<Activity className='w-4 h-4 inline mr-1' />
|
||||
{t('可用率趋势')}
|
||||
</span>
|
||||
<span className='text-xs text-gray-500'>
|
||||
{formatTime(groupHistory.start_time)} -{' '}
|
||||
{formatTime(groupHistory.end_time)}
|
||||
</span>
|
||||
</div>
|
||||
{renderTrendChart(groupHistory.data_points, 100)}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 模型统计 */}
|
||||
<div className='p-4 border-t border-gray-200 dark:border-gray-700'>
|
||||
<h4 className='text-sm font-semibold mb-3'>
|
||||
{t('模型可用性统计')}
|
||||
</h4>
|
||||
<Table
|
||||
columns={modelColumns}
|
||||
dataSource={group.model_stats || []}
|
||||
pagination={false}
|
||||
size='small'
|
||||
rowKey='model'
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 渠道列表 */}
|
||||
<div className='p-4 border-t border-gray-200 dark:border-gray-700'>
|
||||
<h4 className='text-sm font-semibold mb-3'>
|
||||
{t('渠道详情')}
|
||||
</h4>
|
||||
<Table
|
||||
columns={channelColumns}
|
||||
dataSource={group.channels || []}
|
||||
pagination={{ pageSize: 10 }}
|
||||
size='small'
|
||||
rowKey='id'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</Col>
|
||||
);
|
||||
})}
|
||||
</Row>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default GroupMonitorAdmin;
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
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 { Progress } from '@douyinfe/semi-ui';
|
||||
import { Clock } from 'lucide-react';
|
||||
import { getServerURL } from '../../helpers/config';
|
||||
|
||||
export const RefreshCountdown = ({ interval, onComplete }) => {
|
||||
const [timeLeft, setTimeLeft] = useState(interval);
|
||||
const [percentage, setPercentage] = useState(100);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeLeft(interval);
|
||||
setPercentage(100);
|
||||
}, [interval]);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setInterval(() => {
|
||||
setTimeLeft((prev) => {
|
||||
if (prev <= 1) {
|
||||
onComplete && onComplete();
|
||||
return interval;
|
||||
}
|
||||
return prev - 1;
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, [interval, onComplete]);
|
||||
|
||||
useEffect(() => {
|
||||
setPercentage((timeLeft / interval) * 100);
|
||||
}, [timeLeft, interval]);
|
||||
|
||||
const formatTime = (seconds) => {
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = seconds % 60;
|
||||
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className='flex items-center gap-2 text-sm text-gray-600'>
|
||||
<Clock className='w-4 h-4' />
|
||||
<span>{formatTime(timeLeft)}</span>
|
||||
<Progress
|
||||
percent={percentage}
|
||||
showInfo={false}
|
||||
stroke='var(--semi-color-primary)'
|
||||
size='small'
|
||||
style={{ width: '60px' }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 动态配置Hook
|
||||
export const useMonitorConfig = () => {
|
||||
const [config, setConfig] = useState({
|
||||
refresh_interval: 60,
|
||||
enable_auto_refresh: true,
|
||||
});
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchConfig = async () => {
|
||||
try {
|
||||
const res = await fetch(`${getServerURL()}/api/monitor/config`, {
|
||||
credentials: getServerURL() ? 'include' : 'same-origin',
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success) {
|
||||
setConfig(data.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load monitor config:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchConfig();
|
||||
}, []);
|
||||
|
||||
return { config, loading };
|
||||
};
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
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 { BarChart3, Sparkles } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import GroupMonitor from './GroupMonitor';
|
||||
import { API } from '../../helpers';
|
||||
import { MarkdownRenderer } from '../../components/common/markdown/MarkdownRenderer';
|
||||
|
||||
const ServiceDashboard = () => {
|
||||
const { t } = useTranslation();
|
||||
const [description, setDescription] = useState(
|
||||
t('实时监控各分组服务可用性与性能指标'),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const loadConfig = async () => {
|
||||
try {
|
||||
const res = await API.get('/api/monitor/description');
|
||||
if (res.data.success && res.data.data?.description) {
|
||||
setDescription(res.data.data.description);
|
||||
}
|
||||
} catch (error) {
|
||||
// 使用默认描述
|
||||
}
|
||||
};
|
||||
loadConfig();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className='min-h-screen pt-[88px] px-2 md:px-6 pb-8 relative overflow-x-hidden'>
|
||||
{/* 页面背景由父级 UserCenter shell 的 PageBackground 统一提供(走 bg-glow- 主题变量)。
|
||||
* 本页仅保留语义性的绿色状态元素(健康指示点 / 状态卡),不再硬编码整页绿色晕染。 */}
|
||||
|
||||
<div className='max-w-[1600px] mx-auto relative'>
|
||||
{/* 页面标题区域 */}
|
||||
<div className='mb-8'>
|
||||
<div className='flex items-start gap-4'>
|
||||
{/* 图标容器 - 带动画效果 */}
|
||||
<div className='relative group'>
|
||||
<div className='absolute inset-0 bg-gradient-to-br from-emerald-500 to-teal-600 rounded-2xl blur-lg opacity-50 group-hover:opacity-70 transition-opacity' />
|
||||
<div className='relative w-14 h-14 rounded-2xl bg-gradient-to-br from-emerald-500 to-teal-600 flex items-center justify-center shadow-lg shadow-emerald-500/25'>
|
||||
<BarChart3 className='w-7 h-7 text-white' />
|
||||
<Sparkles className='absolute -top-1 -right-1 w-4 h-4 text-yellow-300 animate-pulse' />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex-1'>
|
||||
<h1 className='text-2xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-emerald-600 via-teal-600 to-cyan-600 dark:from-emerald-400 dark:via-teal-400 dark:to-cyan-400'>
|
||||
{t('服务大盘')}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{/* 状态指示器 */}
|
||||
<div className='hidden lg:flex items-center gap-3 px-4 py-2 bg-white/60 dark:bg-gray-800/60 backdrop-blur-sm rounded-full border border-emerald-100 dark:border-gray-700'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<span className='relative flex h-2.5 w-2.5'>
|
||||
<span className='animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75'></span>
|
||||
<span className='relative inline-flex rounded-full h-2.5 w-2.5 bg-emerald-500'></span>
|
||||
</span>
|
||||
<span className='text-sm text-gray-600 dark:text-gray-300 font-medium'>
|
||||
{t('实时监控中')}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 服务描述区域 */}
|
||||
{description &&
|
||||
description !== t('实时监控各分组服务可用性与性能指标') && (
|
||||
<div className='relative mb-6'>
|
||||
<div className='relative app-card p-6 service-dashboard-description'>
|
||||
<MarkdownRenderer content={description} fontSize={14} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 分组监控内容 - 玻璃态卡片(统一 .app-card,去掉原双层叠加) */}
|
||||
<div className='relative'>
|
||||
<div className='relative app-card p-6'>
|
||||
<GroupMonitor />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServiceDashboard;
|
||||
Reference in New Issue
Block a user