198 lines
7.3 KiB
TypeScript
198 lines
7.3 KiB
TypeScript
import {
|
||
diffWorkflowJobs,
|
||
diffWorkflowSnapshots,
|
||
getJob,
|
||
getJobExecutionTree,
|
||
getWorkflowSnapshot,
|
||
} from "../../../domains/platform/services.js";
|
||
import { stringOption } from "../../parser.js";
|
||
import { printJson, printTable } from "../../printer.js";
|
||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||
import { createJobPiperClient } from "./shared.js";
|
||
|
||
export async function handleWorkflowDiff(
|
||
parsed: CliParsedArgs,
|
||
runtime: CliRuntime
|
||
): Promise<CliResult> {
|
||
const snapIdA = parsed.args[0] || stringOption(parsed.options, ["from", "a", "snapshot1", "snapshotA"]);
|
||
const snapIdB = parsed.args[1] || stringOption(parsed.options, ["to", "b", "snapshot2", "snapshotB"]);
|
||
|
||
if (!snapIdA || !snapIdB) {
|
||
return {
|
||
stdout: "",
|
||
stderr: "用法: hose-use workflow diff <snapshotId1> <snapshotId2> [--pipelineId=<id>]\n",
|
||
exitCode: 1,
|
||
};
|
||
}
|
||
|
||
const client = await createJobPiperClient(parsed, runtime);
|
||
const pipelineId = stringOption(parsed.options, ["pipelineId", "pipeline", "id"]);
|
||
|
||
const [snapA, snapB] = await Promise.all([
|
||
getWorkflowSnapshot(client, snapIdA, pipelineId),
|
||
getWorkflowSnapshot(client, snapIdB, pipelineId),
|
||
]);
|
||
|
||
const diff = diffWorkflowSnapshots(snapA, snapB);
|
||
|
||
if (runtime.jsonMode) {
|
||
return {
|
||
stdout: printJson(diff, runtime.outputOptions),
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
const lines: string[] = [
|
||
`====== 工作流快照比对 [${diff.snapshotA.version || snapIdA} -> ${diff.snapshotB.version || snapIdB}] ======`,
|
||
` 快照 A : ${snapIdA} (版本: ${diff.snapshotA.version || "-"}) 更新人: ${diff.snapshotA.staffName || "-"} 时间: ${diff.snapshotA.updateTime || "-"}`,
|
||
` 快照 B : ${snapIdB} (版本: ${diff.snapshotB.version || "-"}) 更新人: ${diff.snapshotB.staffName || "-"} 时间: ${diff.snapshotB.updateTime || "-"}`,
|
||
` 变更统计: ${diff.summary.changedCount} 个节点有变动 (节点总数: A=${diff.summary.totalA}, B=${diff.summary.totalB})`,
|
||
"",
|
||
];
|
||
|
||
if (diff.addedTasks.length > 0) {
|
||
lines.push(`── ➕ 新增节点 (${diff.addedTasks.length} 个) ──`);
|
||
lines.push(` ${diff.addedTasks.join(", ")}`);
|
||
lines.push("");
|
||
}
|
||
|
||
if (diff.removedTasks.length > 0) {
|
||
lines.push(`── ➖ 删除节点 (${diff.removedTasks.length} 个) ──`);
|
||
lines.push(` ${diff.removedTasks.join(", ")}`);
|
||
lines.push("");
|
||
}
|
||
|
||
if (diff.modifiedTasks.length === 0) {
|
||
lines.push("✅ 两个快照间未检测到任何节点或代码变更。");
|
||
} else {
|
||
lines.push(`── 📝 节点变更明细 (${diff.modifiedTasks.length} 个) ──`);
|
||
for (const task of diff.modifiedTasks) {
|
||
lines.push(`\n• 节点 [${task.taskName}] (${task.displayName || task.type}) [${task.changeType}]:`);
|
||
if (task.codeDiff?.unifiedDiff) {
|
||
lines.push(` --- 脚本代码变动 ---`);
|
||
lines.push(task.codeDiff.unifiedDiff.split("\n").map((l) => ` ${l}`).join("\n"));
|
||
}
|
||
if (task.filterDiff) {
|
||
lines.push(` --- 过滤/触发条件变动 ---`);
|
||
lines.push(` 原条件: ${JSON.stringify(task.filterDiff.from) || "(空)"}`);
|
||
lines.push(` 现条件: ${JSON.stringify(task.filterDiff.to) || "(空)"}`);
|
||
}
|
||
if (task.configChanges.length > 0) {
|
||
lines.push(` --- 属性配置变动 ---`);
|
||
for (const c of task.configChanges) {
|
||
lines.push(` • ${c.field}: ${JSON.stringify(c.from)} -> ${JSON.stringify(c.to)}`);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return {
|
||
stdout: lines.join("\n") + "\n",
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
export async function handleWorkflowDiffJob(
|
||
parsed: CliParsedArgs,
|
||
runtime: CliRuntime
|
||
): Promise<CliResult> {
|
||
const jobIdA = parsed.args[0] || stringOption(parsed.options, ["from", "a", "job1", "jobA"]);
|
||
const jobIdB = parsed.args[1] || stringOption(parsed.options, ["to", "b", "job2", "jobB"]);
|
||
|
||
if (!jobIdA || !jobIdB) {
|
||
return {
|
||
stdout: "",
|
||
stderr: "用法: hose-use workflow diff-job <jobId1> <jobId2>\n",
|
||
exitCode: 1,
|
||
};
|
||
}
|
||
|
||
const client = await createJobPiperClient(parsed, runtime);
|
||
|
||
const [jobA, treeA, jobB, treeB] = await Promise.all([
|
||
getJob(client, jobIdA),
|
||
getJobExecutionTree(client, jobIdA),
|
||
getJob(client, jobIdB),
|
||
getJobExecutionTree(client, jobIdB),
|
||
]);
|
||
|
||
const diff = diffWorkflowJobs(jobA, treeA, jobB, treeB);
|
||
|
||
if (runtime.jsonMode) {
|
||
return {
|
||
stdout: printJson(diff, runtime.outputOptions),
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
const summaryRows = [
|
||
["Job ID", diff.jobA.id, diff.jobB.id],
|
||
["运行状态", String(diff.jobA.state ?? "-"), String(diff.jobB.state ?? "-")],
|
||
["开始时间", diff.jobA.startTime || "-", diff.jobB.startTime || "-"],
|
||
["执行耗时(ms)", String(diff.jobA.durationTime ?? "-"), String(diff.jobB.durationTime ?? "-")],
|
||
["执行节点数", String(diff.summary.totalNodesA), String(diff.summary.totalNodesB)],
|
||
["错误标记 (hasError)", diff.jobA.hasError ? "⚠️ 包含错误" : "正常", diff.jobB.hasError ? "⚠️ 包含错误" : "正常"],
|
||
];
|
||
|
||
const lines: string[] = [
|
||
`====== 工作流 Job 执行比对 [${jobIdA} vs ${jobIdB}] ======`,
|
||
printTable(["指标项", `Job A (${jobIdA})`, `Job B (${jobIdB})`], summaryRows),
|
||
"",
|
||
`── 🔀 分支走向与节点执行状态比对 (差异节点: ${diff.summary.divergedNodesCount} 个) ──`,
|
||
];
|
||
|
||
if (diff.summary.divergedNodesCount === 0) {
|
||
lines.push(" ✅ 两个 Job 执行链路完全一致,各节点状态与入参相同。");
|
||
} else {
|
||
const divergedNodes = diff.nodes.filter((n) => n.diverged);
|
||
const nodeRows = divergedNodes.map((n) => [
|
||
n.currentLoopPath ? `${n.taskName} [loop ${n.currentLoopPath}]` : n.taskName,
|
||
n.displayName || "-",
|
||
n.type || "-",
|
||
n.statusA || "(未执行)",
|
||
n.statusB || "(未执行)",
|
||
n.statusA !== n.statusB ? "⚠️ 状态不一致" : "一致",
|
||
n.errorB ? "❌ B报错" : n.errorA ? "⚠️ A报错" : "-",
|
||
]);
|
||
lines.push(
|
||
printTable(
|
||
["节点名称", "显示名称", "类型", "Job A 状态", "Job B 状态", "状态差异", "错误差异"],
|
||
nodeRows
|
||
)
|
||
);
|
||
|
||
lines.push("\n── 🔍 差异节点入参/出参与报错透视 ──");
|
||
for (const n of divergedNodes) {
|
||
lines.push(`\n• 节点 [${n.taskName}] (${n.displayName || n.type || "-"}):`);
|
||
if (n.statusA !== n.statusB) {
|
||
lines.push(` 状态对比: ${n.statusA || "(未执行)"} -> ${n.statusB || "(未执行)"}`);
|
||
}
|
||
if (n.errorB || n.errorA) {
|
||
if (n.errorA) lines.push(` Job A 报错: ${JSON.stringify(n.errorA)}`);
|
||
if (n.errorB) lines.push(` Job B 报错: ${JSON.stringify(n.errorB)}`);
|
||
}
|
||
if (n.inputsDiff && n.inputsDiff.length > 0) {
|
||
lines.push(` 入参差异 (inputs):`);
|
||
for (const diffItem of n.inputsDiff) {
|
||
lines.push(` ~ ${diffItem.field}: ${JSON.stringify(diffItem.from)} -> ${JSON.stringify(diffItem.to)}`);
|
||
}
|
||
}
|
||
if (n.outputsDiff && n.outputsDiff.length > 0) {
|
||
lines.push(` 出参差异 (outputs):`);
|
||
for (const diffItem of n.outputsDiff) {
|
||
lines.push(` ~ ${diffItem.field}: ${JSON.stringify(diffItem.from)} -> ${JSON.stringify(diffItem.to)}`);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return {
|
||
stdout: lines.join("\n") + "\n",
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|