Files
monkey-ai-web/src/pages/Monitoring/components/ChannelMonitorView.jsx
T

237 lines
7.9 KiB
React

/*
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 from 'react';
import {
Card,
Grid,
Typography,
Tag,
Progress,
Empty,
Button,
} from '@/components/ui';
import { IconRefresh, IconAlertCircle } from '@/components/ui/icons';
import { useTranslation } from 'react-i18next';
const { Row, Col } = Grid;
const { Title, Text } = Typography;
const ChannelMonitorView = ({ channelStatus, onRefresh }) => {
const { t } = useTranslation();
// 获取状态显示
const getStatusConfig = (status) => {
switch (status) {
case 1: // 在线
return { color: 'green', text: t('可用'), icon: '🟢' };
case 0: // 离线
return { color: 'red', text: t('不可用'), icon: '🔴' };
case 2: // 维护中
return { color: 'orange', text: t('维护中'), icon: '🟡' };
default:
return { color: 'grey', text: t('未知'), icon: '⚪' };
}
};
// 获取响应速度配置
const getSpeedConfig = (responseTime) => {
if (responseTime < 1000) {
return { level: t('快'), color: 'green', percent: 100, bars: '🟩🟩🟩' };
} else if (responseTime < 3000) {
return { level: t('中等'), color: 'yellow', percent: 66, bars: '🟨🟨' };
} else {
return { level: t('慢'), color: 'red', percent: 33, bars: '🟥' };
}
};
// 渲染迷你趋势图 (Sparkline)
const renderSparkline = (uptime) => {
const width = 100;
const height = 30;
const points = 12;
const data = Array.from({ length: points }, (_, i) => {
// 模拟趋势数据,基于uptime生成波动
const baseValue = uptime * 100;
const variance = (Math.random() - 0.5) * 10;
return Math.max(0, Math.min(100, baseValue + variance));
});
const pathData = data
.map((value, index) => {
const x = (index / (points - 1)) * width;
const y = height - (value / 100) * height;
return `${index === 0 ? 'M' : 'L'} ${x},${y}`;
})
.join(' ');
return (
<svg width={width} height={height} style={{ display: 'block' }}>
<path
d={pathData}
fill='none'
stroke={
uptime > 0.9
? 'var(--ui-color-success)'
: uptime > 0.7
? 'var(--ui-color-warning)'
: 'var(--ui-color-danger)'
}
strokeWidth='2'
/>
</svg>
);
};
if (!channelStatus || channelStatus.length === 0) {
return (
<Empty
image={<IconAlertCircle size='extra-large' />}
title={t('暂无渠道数据')}
description={t('请稍后刷新或联系管理员')}
>
<Button icon={<IconRefresh />} onClick={onRefresh}>
{t('刷新数据')}
</Button>
</Empty>
);
}
return (
<div>
{/* 刷新按钮 */}
<div style={{ marginBottom: 16, textAlign: 'right' }}>
<Button icon={<IconRefresh />} onClick={onRefresh}>
{t('刷新')}
</Button>
</div>
{/* 渠道分组展示 */}
{channelStatus.map((group, groupIndex) => (
<div key={groupIndex} style={{ marginBottom: 32 }}>
<Title heading={4} style={{ marginBottom: 16, color: '#1890ff' }}>
📁 {group.categoryName}
</Title>
<Row gutter={[16, 16]}>
{group.monitors &&
group.monitors.map((channel, index) => {
const statusConfig = getStatusConfig(channel.status);
const speedConfig = getSpeedConfig(channel.response_time || 0);
return (
<Col key={index} xs={24} sm={12} md={8} lg={6}>
<Card
title={
<div
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
}}
>
<span style={{ fontSize: 20 }}>
{statusConfig.icon}
</span>
<Text strong>{channel.name}</Text>
</div>
}
headerStyle={{ borderBottom: '2px solid #f0f0f0' }}
style={{
height: '100%',
border: `2px solid ${
channel.status === 1
? '#52c41a20'
: channel.status === 0
? '#f5222d20'
: '#faad1420'
}`,
}}
styles={{ body: { padding: 16 } }}
>
{/* 1. 渠道状态 */}
<div style={{ marginBottom: 16 }}>
<Text type='tertiary' size='small'>
{t('状态')}
</Text>
<div style={{ marginTop: 4 }}>
<Tag color={statusConfig.color} size='large'>
{statusConfig.text}
</Tag>
</div>
</div>
{/* 2. 响应速度 */}
<div style={{ marginBottom: 16 }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: 4,
}}
>
<Text type='tertiary' size='small'>
{t('响应速度')}
</Text>
<Text strong size='small'>
{speedConfig.level}
</Text>
</div>
<Progress
percent={speedConfig.percent}
stroke={speedConfig.color}
showInfo={false}
style={{ marginBottom: 4 }}
/>
<Text type='tertiary' size='small'>
{channel.response_time || 0}ms
</Text>
</div>
{/* 3. 稳定性 (Sparkline) */}
<div>
<Text type='tertiary' size='small'>
{t('稳定性趋势 (24h)')}
</Text>
<div
style={{
marginTop: 8,
display: 'flex',
alignItems: 'center',
gap: 8,
}}
>
{renderSparkline(channel.uptime || 0)}
<Text type='success' strong>
{((channel.uptime || 0) * 100).toFixed(1)}%
</Text>
</div>
</div>
</Card>
</Col>
);
})}
</Row>
</div>
))}
</div>
);
};
export default ChannelMonitorView;