Files
hose-use/src/domains/platform/workflow/watch/formatter.ts
T

52 lines
2.6 KiB
TypeScript

import type { WorkflowWatchEvent } from "./types.js";
/**
* 将事件格式化为带时间戳的清晰控制台文本行
*/
export function formatWatchEvent(event: WorkflowWatchEvent): string {
const timeStr = new Date(event.timestamp).toTimeString().slice(0, 8);
const tag = `[${timeStr}]`;
switch (event.type) {
case "pipeline_watch_start":
return `${tag} 🔍 开始监听工作流 [${event.pipelineLabel || event.pipelineId}] 运行动态 (按 Ctrl+C 退出)...`;
case "job_detected":
return `${tag} 🚀 捕获新运行任务 Job [${event.jobId}] (${event.runType || "RUN"}, 状态: ${event.status || "RUNNING"})`;
case "task_started":
return ` ⏳ [${event.taskType}] ${event.name || event.executionId}${event.label ? ` (${event.label})` : ""} 开始运行...${event.loopPath ? ` [循环: ${event.loopPath}]` : ""}`;
case "task_completed":
return ` ✓ [${event.taskType}] ${event.name || event.executionId}${event.label ? ` (${event.label})` : ""} COMPLETED (${event.durationMs ?? 0}ms)${event.loopPath ? ` [循环: ${event.loopPath}]` : ""}`;
case "task_skipped": {
const errHint = event.error ? `\n ↳ ⚠️ 异常跳过原因: ${event.error}` : "";
return ` ⚠️ [${event.taskType}] ${event.name || event.executionId}${event.label ? ` (${event.label})` : ""} SKIPPED (${event.durationMs ?? 0}ms)${event.loopPath ? ` [循环: ${event.loopPath}]` : ""}${errHint}`;
}
case "task_failed":
return ` ❌ [${event.taskType}] ${event.name || event.executionId}${event.label ? ` (${event.label})` : ""} FAILED (${event.durationMs ?? 0}ms)${event.loopPath ? ` [循环: ${event.loopPath}]` : ""}\n ↳ 错误详情: ${event.error || "未捕获明确错误信息"}`;
case "job_completed": {
const summary = event.jobSummary;
const isOk = summary?.status === "COMPLETED" && !summary.hasError && summary.errorCount === 0;
const icon = isOk ? "🏁" : "❌";
const statusText = summary?.status || "COMPLETED";
return `${tag} ${icon} Job [${event.jobId}] 执行结束: 状态 [${statusText}] | 耗时: ${summary?.durationMs ?? "-"}ms | 成功: ${summary?.successCount ?? 0} | 失败: ${summary?.errorCount ?? 0} | 存在错误: ${summary?.hasError ? "是" : "否"}`;
}
case "timeout":
return `${tag} ⏱️ 监听超时,未在指定时间内捕获到新的任务状态变更。`;
case "cancelled":
return `${tag} 监听已取消,未宣称 Job 执行完成。`;
case "idle":
return `${tag} 💤 暂无新运行实例,继续等待中...`;
default:
return `${tag} [${event.type}]`;
}
}