113 lines
4.3 KiB
TypeScript
113 lines
4.3 KiB
TypeScript
import { indexExecutionContexts } from "../execution/context.js";
|
|
import type { WorkflowTaskExecution, WorkflowJobDetail, WorkflowJobDiff, NodeExecutionDiff } from "../types.js";
|
|
|
|
/**
|
|
* 对比两个 Job 的执行分支、入参 Payload、下游输出及报错信息
|
|
*/
|
|
export function diffWorkflowJobs(
|
|
jobA: WorkflowJobDetail,
|
|
treeA: WorkflowTaskExecution[],
|
|
jobB: WorkflowJobDetail,
|
|
treeB: WorkflowTaskExecution[]
|
|
): WorkflowJobDiff {
|
|
const flatA = indexExecutionContexts(treeA);
|
|
const flatB = indexExecutionContexts(treeB);
|
|
const byKeyA = new Map(flatA.map(context => [context.key, context]));
|
|
const byKeyB = new Map(flatB.map(context => [context.key, context]));
|
|
const allKeys = new Set([...byKeyA.keys(), ...byKeyB.keys()]);
|
|
|
|
const nodes: NodeExecutionDiff[] = [];
|
|
let divergedNodesCount = 0;
|
|
let statusDiffCount = 0;
|
|
|
|
for (const key of allKeys) {
|
|
const contextA = byKeyA.get(key);
|
|
const contextB = byKeyB.get(key);
|
|
const nodeA = contextA?.node;
|
|
const nodeB = contextB?.node;
|
|
const name = nodeB?.name || nodeA?.name || nodeB?.type || nodeA?.type || "unnamed";
|
|
|
|
const statusA = nodeA?.status;
|
|
const statusB = nodeB?.status;
|
|
const isStatusDiff = statusA !== statusB;
|
|
if (isStatusDiff) statusDiffCount++;
|
|
|
|
// 比较 inputs
|
|
const inputsDiff: Array<{ field: string; from?: unknown; to?: unknown }> = [];
|
|
const payloadFields = (value: unknown): Record<string, unknown> => value === undefined ? {} : value !== null && typeof value === "object" ? value as Record<string, unknown> : { $value: value };
|
|
const inA = payloadFields(nodeA?.inputs);
|
|
const inB = payloadFields(nodeB?.inputs);
|
|
const inputKeys = Array.from(new Set([...Object.keys(inA), ...Object.keys(inB)]));
|
|
for (const k of inputKeys) {
|
|
if (JSON.stringify(inA[k]) !== JSON.stringify(inB[k])) {
|
|
inputsDiff.push({ field: k, from: inA[k], to: inB[k] });
|
|
}
|
|
}
|
|
|
|
// 比较 outputs
|
|
const outputsDiff: Array<{ field: string; from?: unknown; to?: unknown }> = [];
|
|
const outA = payloadFields(nodeA?.outputs ?? nodeA?.output);
|
|
const outB = payloadFields(nodeB?.outputs ?? nodeB?.output);
|
|
const outputKeys = Array.from(new Set([...Object.keys(outA), ...Object.keys(outB)]));
|
|
for (const k of outputKeys) {
|
|
if (JSON.stringify(outA[k]) !== JSON.stringify(outB[k])) {
|
|
outputsDiff.push({ field: k, from: outA[k], to: outB[k] });
|
|
}
|
|
}
|
|
|
|
const isDiverged =
|
|
!nodeA ||
|
|
!nodeB ||
|
|
isStatusDiff ||
|
|
JSON.stringify(nodeA.error) !== JSON.stringify(nodeB.error) ||
|
|
inputsDiff.length > 0 || outputsDiff.length > 0;
|
|
|
|
if (isDiverged) divergedNodesCount++;
|
|
|
|
nodes.push({
|
|
taskName: name,
|
|
instanceKey: key,
|
|
currentLoopPath: contextB?.loopPath || contextA?.loopPath || undefined,
|
|
displayName: (nodeB?.displayName || nodeA?.displayName || nodeB?.label || nodeA?.label) ?? undefined,
|
|
type: nodeB?.type || nodeA?.type,
|
|
statusA,
|
|
statusB,
|
|
durationMsA: nodeA?.executionTime,
|
|
durationMsB: nodeB?.executionTime,
|
|
errorA: nodeA?.error,
|
|
errorB: nodeB?.error,
|
|
inputsDiff,
|
|
outputsDiff,
|
|
diverged: isDiverged,
|
|
});
|
|
}
|
|
|
|
return {
|
|
jobA: {
|
|
id: jobA.id,
|
|
pipelineId: jobA.pipelineId,
|
|
state: typeof jobA.state === "string" || typeof jobA.state === "number" ? jobA.state : undefined,
|
|
startTime: jobA.startTime ? String(jobA.startTime) : undefined,
|
|
durationTime: typeof jobA.durationTime === "number" ? jobA.durationTime : undefined,
|
|
hasError: Boolean(jobA.hasError),
|
|
errorCount: typeof (jobA as any).errorTaskCount === "number" ? (jobA as any).errorTaskCount : undefined,
|
|
},
|
|
jobB: {
|
|
id: jobB.id,
|
|
pipelineId: jobB.pipelineId,
|
|
state: typeof jobB.state === "string" || typeof jobB.state === "number" ? jobB.state : undefined,
|
|
startTime: jobB.startTime ? String(jobB.startTime) : undefined,
|
|
durationTime: typeof jobB.durationTime === "number" ? jobB.durationTime : undefined,
|
|
hasError: Boolean(jobB.hasError),
|
|
errorCount: typeof (jobB as any).errorTaskCount === "number" ? (jobB as any).errorTaskCount : undefined,
|
|
},
|
|
nodes,
|
|
summary: {
|
|
totalNodesA: flatA.length,
|
|
totalNodesB: flatB.length,
|
|
divergedNodesCount,
|
|
statusDiffCount,
|
|
},
|
|
};
|
|
}
|