使用 i18n.t() 全局方式(避免 hook 定位问题): - 288 目标文件中 135 个成功转换 - 2228 处中文替换为 i18n.t() 调用 - 1006 个新翻译 key 添加到 zh-CN.json + en-US.json - 词典总量: 3554 keys (22 namespaces) - tsc 零错误 + Vite 构建成功 - 6 个含代码示例/数据字符串的文件安全回退
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
// k8s-event-utils — K8s Event 字段归一化与展示 // Logan.Li
|
||
import { elapsedSeconds, formatDuration, formatK8sTime } from './k8s-yaml-utils';
|
||
import type { JsonData } from '@/types/common';
|
||
import i18n from '@/i18n';
|
||
|
||
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: JsonData): 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 i18n.t('task.cronJob.time.justNow');
|
||
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) };
|
||
} |