// 服务器管理共享纯函数/常量 // Logan.Li
import { Badge } from '@/components/ui/badge';
import i18n from '@/i18n';
import type { Server as ServerType } from '@/types';
import type { JsonData } from '@/types/common';
// isRdpServer 判断资产是否应展示 RDP 入口(兼容云导入仅填 os 字段的历史数据) // Logan.Li
export function isRdpServer(server: JsonData): boolean {
const protocol = String(server.protocol || '').toLowerCase();
if (protocol === 'rdp') return true;
const osType = String(server.os_type || server.os || '').toLowerCase();
return osType.includes('windows') || osType.includes('win');
}
// 解析标签:后端 tags 可能是 JSON 字符串(JSON数组/对象)、逗号分隔字符串或数组 // Logan.Li
export function getTags(server: JsonData): string[] {
const tags = server.tags;
if (!tags) return [];
if (Array.isArray(tags)) return tags;
try {
const parsed = JSON.parse(tags);
if (Array.isArray(parsed)) return parsed;
// JSON 对象 → key:value 格式展示(如 project:prod) // Logan.Li
if (typeof parsed === 'object' && parsed !== null) {
return Object.entries(parsed).map(([k, v]) => `${k}:${String(v)}`);
}
return [String(parsed)];
} catch {
return String(tags)
.split(',')
.map((t: string) => t.trim())
.filter(Boolean);
}
}
// 服务器状态徽章 // Logan.Li
export function getStatusBadge(status: ServerType['status']) {
switch (status) {
case 'running':
return (
{i18n.t('server.statusBadge.running')}
);
case 'stopped':
return (
{i18n.t('server.statusBadge.stopped')}
);
case 'maintenance':
return (
{i18n.t('server.statusBadge.maintenance')}
);
case 'offline':
return (
{i18n.t('server.statusBadge.offline')}
);
default:
return null;
}
}
// 使用率进度条颜色(CPU/内存通用)— 绿色<60% 黄色60-80% 红色>80% // Logan.Li
export function getUsageColor(usage: number): string {
return usage > 80 ? 'bg-red-400' : usage > 60 ? 'bg-amber-400' : 'bg-emerald-400';
}