'use client'; import { SiteHeader, PageInfo } from '@/components/site-header'; import { SidebarInset, SidebarProvider } from '@nice/ui/components/sidebar'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@nice/ui/components/card'; import { Badge } from '@nice/ui/components/badge'; import { Button } from '@nice/ui/components/button'; import { Progress } from '@nice/ui/components/progress'; import { IconServer, IconUsers, IconFiles, IconCloudUpload, IconTrendingUp, IconTrendingDown, IconActivity, IconDatabase, IconCpu, IconShield, IconBell, IconCalendar, IconClock, IconAlertTriangle, IconCircleCheck, IconCircleX, IconRefresh, IconNetwork, IconBrain, IconFolder, } from '@tabler/icons-react'; import { useTRPC } from '@fenghuo/client'; import { useQuery } from '@tanstack/react-query'; import { useEffect } from 'react'; import { SystemStatus } from '@fenghuo/common'; import { useSetPageInfo } from '@/components/providers/dashboard-provider'; // 用户和文件数据的模拟数据(保留,因为这部分数据不在systemStatus中) const additionalStats = { users: { total: 1248, online: 89, newToday: 12, activeThisWeek: 324, growth: '+5.2%', peakOnline: 156, }, files: { total: 45280, size: '2.4TB', uploaded: 156, shared: 89, downloadedToday: 1289, uploadedToday: 234, growth: '+8.1%', }, database: { connections: 45, maxConnections: 200, queryTime: 2.3, status: 'healthy', size: '890MB', backupStatus: 'completed', lastBackup: '4小时前', }, }; const recentActivities = [ { id: 1, user: '张三', action: '上传了文件', target: 'project.pdf', time: '5分钟前', type: 'upload', ip: '192.168.1.100', }, { id: 2, user: '李四', action: '创建了文章', target: 'API设计指南', time: '10分钟前', type: 'create', ip: '192.168.1.101', }, ]; const systemAlerts = [ { id: 1, type: 'warning', level: 'medium', message: '磁盘空间使用率超过80%', time: '1小时前', resolved: false }, { id: 2, type: 'info', level: 'low', message: '系统备份已完成', time: '2小时前', resolved: true }, { id: 3, type: 'success', level: 'low', message: '安全扫描未发现威胁', time: '3小时前', resolved: true }, { id: 4, type: 'error', level: 'high', message: 'Email服务响应异常', time: '4小时前', resolved: false }, ]; function StatsCard({ title, value, description, icon: Icon, trend, trendValue, className = '', status, }: { title: string; value: string | number; description: string; icon: any; trend?: 'up' | 'down'; trendValue?: string; className?: string; status?: 'good' | 'warning' | 'error'; }) { const getStatusColor = () => { switch (status) { case 'good': return 'text-green-500'; case 'warning': return 'text-yellow-500'; case 'error': return 'text-red-500'; default: return 'text-muted-foreground'; } }; return ( {title}
{value}
{description} {trend && trendValue && (
{trend === 'up' ? ( ) : ( )} {trendValue}
)}
); } function SystemStatusCard({ systemStatus, systemStatusLoading, refetchSystemStatus }: { systemStatus?: SystemStatus, systemStatusLoading: boolean, refetchSystemStatus: () => void }) { // 如果没有数据,显示加载状态 if (systemStatusLoading || !systemStatus) { return (
服务器状态
加载中...
正在获取系统监控数据
正在加载系统状态...
); } return (
服务器状态
{systemStatus?.isRunning ? '运行中' : '离线'}
实时系统监控数据
运行时间 {systemStatus?.uptimeFormatted}
进程数 {systemStatus?.processCount}
CPU使用率
{systemStatus?.cpuUsage.toFixed(1)}%
内存使用率
{systemStatus?.memory.usagePercent.toFixed(1)}%
磁盘使用率
{systemStatus?.disk.usagePercent.toFixed(1)}%
{systemStatus?.loadAverage.oneMinute}
1分钟负载
{systemStatus?.loadAverage.fiveMinutes}
5分钟负载
{systemStatus?.loadAverage.fifteenMinutes}
15分钟负载
); } function UserActivityCard() { const getActivityIcon = (type: string) => { switch (type) { case 'upload': return ; case 'create': return ; case 'share': return ; case 'permission': return ; case 'login': return ; case 'download': return ; default: return ; } }; return ( 用户活动 最近用户操作记录
{recentActivities.map((activity) => (
{getActivityIcon(activity.type)}

{activity.user} {activity.action}{' '} {activity.target}

{activity.time}
{activity.ip}
))}
); } function SystemAlertsCard() { const getAlertIcon = (type: string, level: string) => { switch (type) { case 'warning': return ; case 'error': return ; case 'success': return ; case 'info': return ; default: return ; } }; const getLevelBadge = (level: string) => { switch (level) { case 'high': return ; case 'medium': return ; case 'low': return ; default: return 未知; } }; return (
系统警告
{systemAlerts.filter((alert) => !alert.resolved).length}
重要系统消息和警告
{systemAlerts.map((alert) => (
{getAlertIcon(alert.type, alert.level)}

{alert.message}

{getLevelBadge(alert.level)}

{alert.time}

{alert.resolved && ( 已解决 )}
))}
); } function DatabaseStatusCard() { const connectionPercent = Math.round((additionalStats.database.connections / additionalStats.database.maxConnections) * 100); return ( 数据库状态 数据库性能和连接状态
数据库状态 {additionalStats.database.status === 'healthy' ? '健康' : '异常'}
连接数 {additionalStats.database.connections} / {additionalStats.database.maxConnections}
{additionalStats.database.queryTime}ms
平均查询时间
{additionalStats.database.size}
数据库大小
备份状态 {additionalStats.database.backupStatus === 'completed' ? '已完成' : '进行中'}
最后备份: {additionalStats.database.lastBackup}
); } export default function MonitoringPage() { const trpc = useTRPC(); useSetPageInfo({ title: '系统监控', subtitle: '实时监控系统运行状态和用户活动', }) const { data: systemStatus, isLoading: systemStatusLoading, refetch: refetchSystemStatus } = useQuery( trpc.system_monitor.getSystemStatus.queryOptions(), ); return (
{/* 统计卡片 */}
80 ? 'error' : systemStatus.cpuUsage > 60 ? 'warning' : 'good' } />
{/* 详细信息第一行 */}
{/* 详细信息第二行 */}
); }