/* 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, Badge, Empty, Spin } from '@/components/ui'; import { Activity, Zap } from 'lucide-react'; import { useTranslation } from 'react-i18next'; // 模拟渠道数据 const generateMockChannels = () => { const channelNames = [ 'OpenAI GPT-4', 'Claude 3.5 Sonnet', 'Gemini Pro', 'DeepSeek V3', 'Qwen Plus', 'Moonshot AI', 'GLM-4', 'Doubao Pro', ]; const statuses = ['available', 'available', 'available', 'busy', 'disabled']; const speedLevels = ['fast', 'fast', 'medium', 'slow']; return channelNames.map((name, index) => { const status = statuses[Math.floor(Math.random() * statuses.length)]; const speed = speedLevels[Math.floor(Math.random() * speedLevels.length)]; // 生成稳定性趋势数据(24小时) const stability = Array.from({ length: 24 }, () => status === 'available' ? 85 + Math.random() * 15 // 85-100% : status === 'busy' ? 60 + Math.random() * 25 // 60-85% : 0, ); return { id: index + 1, name, status, speed, speedMs: speed === 'fast' ? 200 + Math.random() * 300 : speed === 'medium' ? 500 + Math.random() * 500 : 1000 + Math.random() * 1000, stability, uptime: status === 'available' ? 99.5 + Math.random() * 0.5 : status === 'busy' ? 95 + Math.random() * 4 : 0, }; }); }; const ChannelMonitor = () => { const { t } = useTranslation(); const [channels, setChannels] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { // 模拟数据加载 setTimeout(() => { setChannels(generateMockChannels()); setLoading(false); }, 800); // 每30秒刷新一次数据 const interval = setInterval(() => { setChannels(generateMockChannels()); }, 30000); return () => clearInterval(interval); }, []); const getStatusConfig = (status) => { const configs = { available: { color: 'var(--ui-color-success)', bgColor: 'rgba(82, 196, 26, 0.1)', text: t('可用'), dotColor: 'success', }, busy: { color: 'var(--ui-color-warning)', bgColor: 'rgba(250, 173, 20, 0.1)', text: t('繁忙'), dotColor: 'warning', }, unavailable: { color: 'var(--ui-color-danger)', bgColor: 'rgba(255, 77, 79, 0.1)', text: t('不可用'), dotColor: 'danger', }, disabled: { color: '#8c8c8c', bgColor: 'rgba(140, 140, 140, 0.1)', text: t('已关闭'), dotColor: 'tertiary', }, }; return configs[status] || configs.disabled; }; const getSpeedConfig = (speed) => { const configs = { fast: { color: 'var(--ui-color-success)', width: '85%', text: t('快'), gradient: 'linear-gradient(90deg, var(--ui-color-success) 0%, #95de64 100%)', }, medium: { color: 'var(--ui-color-warning)', width: '60%', text: t('中等'), gradient: 'linear-gradient(90deg, var(--ui-color-warning) 0%, #ffd666 100%)', }, slow: { color: 'var(--ui-color-danger)', width: '35%', text: t('慢'), gradient: 'linear-gradient(90deg, var(--ui-color-danger) 0%, #ff7875 100%)', }, }; return configs[speed] || configs.medium; }; const renderSparkline = (data) => { if (!data || data.length === 0) return null; const max = Math.max(...data); const min = Math.min(...data); const range = max - min || 1; const points = data .map((value, index) => { const x = (index / (data.length - 1)) * 100; const y = 100 - ((value - min) / range) * 100; return `${x},${y}`; }) .join(' '); const avgStability = data.reduce((a, b) => a + b, 0) / data.length; const lineColor = avgStability >= 95 ? 'var(--ui-color-success)' : avgStability >= 85 ? 'var(--ui-color-warning)' : 'var(--ui-color-danger)'; return ( ); }; if (loading) { return (
); } if (channels.length === 0) { return ; } return (
{channels.map((channel) => { const statusConfig = getStatusConfig(channel.status); const speedConfig = getSpeedConfig(channel.speed); return ( {/* 渠道名称和状态 */}

{channel.name}

{statusConfig.text}
{/* 响应速度 */}
{t('响应速度')} {Math.round(channel.speedMs)}ms
{speedConfig.text}
{/* 稳定性趋势 */}
{t('稳定性')} (24h) = 99 ? 'var(--ui-color-success)' : channel.uptime >= 95 ? 'var(--ui-color-warning)' : 'var(--ui-color-danger)', }} > {channel.uptime.toFixed(2)}%
{renderSparkline(channel.stability)}
); })}
); }; export default ChannelMonitor;