feat(k8s): 事件页补全时间/次数/来源等字段

- 后端解析 source/reportingComponent 与 object_kind/name
- 新增 EventTable 统一展示:最后/首次时间、次数、重复跨度、类型、原因、对象、来源、消息
- Deployment 事件 Tab 与事件中心共用表格组件
This commit is contained in:
Logan.Li committed 2026-06-26 15:13:35 +08:00
1 parent ee2588fb52
commit b2939470f4
8 files changed
+323 -176

No files matched your search

@@ -0,0 +1,77 @@
// k8s-event-utils — K8s Event 字段归一化与展示 // Logan.Li
import { elapsedSeconds, formatDuration, formatK8sTime } from './k8s-yaml-utils';
export interface NormalizedEvent {
name: string;
namespace: string;
type: string;
reason: string;
message: string;
object: string;
objectKind: string;
objectName: string;
source: string;
count: number;
firstTime: string;
lastTime: string;
eventTime: string;
}
/** 后端/API 原始事件 → 统一结构 // Logan.Li */
export function normalizeK8sEvent(raw: Record<string, any>): NormalizedEvent {
const objectKind = raw.object_kind || raw.objectKind || '';
const objectName = raw.object_name || raw.objectName || '';
const object = raw.object
|| (objectKind && objectName ? `${objectKind}/${objectName}` : '')
|| '';
const firstTime = raw.first_time || raw.firstTime || raw.first_timestamp || raw.firstTimestamp || '';
const lastTime = raw.last_time || raw.lastTime || raw.last_timestamp || raw.lastTimestamp || '';
const eventTime = raw.event_time || raw.eventTime || '';
const count = Number(raw.count) > 0 ? Number(raw.count) : 1;
const source = raw.source
|| raw.source_component
|| raw.sourceComponent
|| (raw.source?.component
? `${raw.source.component}${raw.source.host ? `@${raw.source.host}` : ''}`
: '')
|| '-';
return {
name: raw.name || '',
namespace: raw.namespace || '',
type: raw.type || 'Normal',
reason: raw.reason || '-',
message: raw.message || '-',
object: object || '-',
objectKind,
objectName,
source: source || '-',
count,
firstTime: firstTime || eventTime,
lastTime: lastTime || eventTime,
eventTime,
};
}
export function formatEventRelative(ts: string): string {
if (!ts) return '';
const diff = Date.now() - new Date(ts).getTime();
if (diff < 0) return '';
const mins = Math.floor(diff / 60000);
if (mins < 1) return '刚刚';
if (mins < 60) return `${mins}分钟前`;
const hrs = Math.floor(mins / 60);
if (hrs < 24) return `${hrs}小时前`;
return `${Math.floor(hrs / 24)}天前`;
}
/** 重复跨度:首次 → 最后 // Logan.Li */
export function formatEventRepeatSpan(first: string, last: string, count: number): string {
if (count <= 1) return '-';
const span = formatDuration(elapsedSeconds(first, last));
return span === '-' ? `×${count}` : `${span} · ×${count}`;
}
export function formatEventTimeCell(ts: string): { absolute: string; relative: string } {
return { absolute: formatK8sTime(ts), relative: formatEventRelative(ts) };
}