Files
aiops/frontend/src/components/agent/agentUtils.ts
T
attacker e544360398 refactor: 拆分AgentMonitor.tsx(672→463行)提取HostDetailDialog+agentUtils子模块
- 新增 agentUtils.ts(31行): isOnline/timeAgo/asList 工具函数
- 新增 HostDetailDialog.tsx(201行): 主机详情弹窗+InfoCell+趋势图
- AgentMonitor.tsx 从 672 行降至 463 行, 远离 700 行硬上限
- 移除未使用的 recharts import
2026-06-16 05:04:13 +08:00

32 lines
1.2 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.
// Agent 监控相关工具函数(从 AgentMonitor.tsx 提取,供多组件共享) // Logan.Li
/** 在线判定:3 分钟内有上报视为在线 */
export function isOnline(lastReport: any): boolean {
if (!lastReport) return false;
const t = new Date(lastReport).getTime();
if (Number.isNaN(t)) return false;
return Date.now() - t < 3 * 60 * 1000;
}
/** 相对时间格式化("3分钟前" / "2小时前") */
export function timeAgo(ts: any): string {
if (!ts) return '--';
const t = new Date(ts).getTime();
if (Number.isNaN(t)) return '--';
const diff = Math.floor((Date.now() - t) / 1000);
if (diff < 60) return `${diff}秒前`;
if (diff < 3600) return `${Math.floor(diff / 60)}分钟前`;
if (diff < 86400) return `${Math.floor(diff / 3600)}小时前`;
return new Date(ts).toLocaleString();
}
/** 兼容多种列表响应包装:数组 / {list} / {items} / {data} */
export function asList(data: any): any[] {
if (!data) return [];
if (Array.isArray(data)) return data;
if (Array.isArray(data.list)) return data.list;
if (Array.isArray(data.items)) return data.items;
if (Array.isArray(data.data)) return data.data;
return [];
}