Files
aiops/frontend/src/components/server/serverHelpers.tsx
T

64 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 服务器管理共享纯函数/常量 // Logan.Li
import { Badge } from '@/components/ui/badge';
import type { Server as ServerType } from '@/types';
// 解析标签:后端 tags 可能是 JSON 字符串(JSON数组/对象)、逗号分隔字符串或数组 // Logan.Li
export function getTags(server: any): 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 (
<Badge variant="outline" className="border-emerald-500/30 text-emerald-400 bg-emerald-500/10">
<div className="w-1.5 h-1.5 rounded-full bg-emerald-400 mr-1.5 animate-pulse" />
运行中
</Badge>
);
case 'stopped':
return (
<Badge variant="outline" className="border-[var(--dg-border)] text-[var(--dg-text-muted)] bg-[var(--dg-bg-tertiary)]">
已停止
</Badge>
);
case 'maintenance':
return (
<Badge variant="outline" className="border-amber-500/30 text-amber-400 bg-amber-500/10">
<div className="w-1.5 h-1.5 rounded-full bg-amber-400 mr-1.5" />
维护中
</Badge>
);
case 'offline':
return (
<Badge variant="outline" className="border-red-500/30 text-red-400 bg-red-500/10">
离线
</Badge>
);
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';
}