264 lines
8.4 KiB
TypeScript
264 lines
8.4 KiB
TypeScript
import type { WorkflowTaskExecution } from "./types.js";
|
|
|
|
export interface ConditionRuleExplanation {
|
|
leftLabel: string;
|
|
upstreamNode: string;
|
|
upstreamProp: string;
|
|
operator: string;
|
|
operatorLabel: string;
|
|
expectedValue: any;
|
|
actualValue: any;
|
|
isMissing: boolean;
|
|
matched: boolean;
|
|
detail: string;
|
|
}
|
|
|
|
export interface ConditionBranchExplanation {
|
|
conditionNodeId: string;
|
|
conditionNodeName: string;
|
|
conditionNodeLabel: string;
|
|
hasSkippedTasks: boolean;
|
|
skippedTaskNames: string[];
|
|
rules: ConditionRuleExplanation[];
|
|
summary: string;
|
|
}
|
|
|
|
const OP_MAP: Record<string, string> = {
|
|
eq: "等于",
|
|
neq: "不等于",
|
|
include: "包含",
|
|
not_include: "不包含",
|
|
in: "在列表中",
|
|
not_in: "不在列表中",
|
|
gt: "大于",
|
|
gte: "大于等于",
|
|
lt: "小于",
|
|
lte: "小于等于",
|
|
empty: "为空",
|
|
not_empty: "不为空",
|
|
};
|
|
|
|
/**
|
|
* 尝试从 @{choose('...')} 标签中提取上游节点名、属性名与可读标签
|
|
*/
|
|
function parseChooseTag(tag: string): {
|
|
upstreamNode: string;
|
|
upstreamProp: string;
|
|
label: string;
|
|
} | null {
|
|
if (typeof tag !== "string") return null;
|
|
const match = tag.match(/@\{choose\('(.*)'\)\}/);
|
|
if (!match) return null;
|
|
try {
|
|
const parsed = JSON.parse(match[1]);
|
|
const upstreamNode = parsed.key || "";
|
|
const upstreamProp = parsed.properties?.[0]?.key || "";
|
|
const label = parsed.label || `${upstreamNode}.${upstreamProp}`;
|
|
return { upstreamNode, upstreamProp, label };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 尝试从 @{value('...')} 中提取人类可读值
|
|
*/
|
|
function parseValueTag(val: any): any {
|
|
if (Array.isArray(val)) {
|
|
return val.map((v) => parseValueTag(v));
|
|
}
|
|
if (typeof val === "string") {
|
|
const match = val.match(/@\{value\('(.*)'\)\}/);
|
|
if (match) {
|
|
try {
|
|
const parsed = JSON.parse(match[1]);
|
|
return parsed.label ? `${parsed.label} (${parsed.key})` : parsed.key || parsed.value;
|
|
} catch {
|
|
return val;
|
|
}
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* 比较两个值是否匹配
|
|
*/
|
|
function evaluateRule(actual: any, expected: any, compare: string): boolean {
|
|
if (actual === undefined || actual === null) {
|
|
if (compare === "empty") return true;
|
|
if (compare === "not_empty") return false;
|
|
return false;
|
|
}
|
|
const actualStr = String(actual);
|
|
const expectedStr = typeof expected === "string" ? expected : JSON.stringify(expected);
|
|
|
|
switch (compare) {
|
|
case "eq":
|
|
if (Array.isArray(actual)) {
|
|
if (Array.isArray(expected)) {
|
|
return JSON.stringify(actual) === JSON.stringify(expected);
|
|
}
|
|
return actual.length === 1 && String(actual[0]) === expectedStr;
|
|
}
|
|
return actualStr === expectedStr;
|
|
case "neq":
|
|
return actualStr !== expectedStr;
|
|
case "include":
|
|
if (Array.isArray(actual)) {
|
|
return actual.some((item) => String(item).includes(expectedStr) || expectedStr.includes(String(item)));
|
|
}
|
|
return actualStr.includes(expectedStr);
|
|
case "not_include":
|
|
if (Array.isArray(actual)) {
|
|
return !actual.some((item) => String(item).includes(expectedStr));
|
|
}
|
|
return !actualStr.includes(expectedStr);
|
|
case "empty":
|
|
return Array.isArray(actual) ? actual.length === 0 : !actual;
|
|
case "not_empty":
|
|
return Array.isArray(actual) ? actual.length > 0 : Boolean(actual);
|
|
default:
|
|
return actualStr === expectedStr;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 分析 Job 执行树中的条件节点,诊断分支是否跳过及跳过的具体字段原因
|
|
*/
|
|
export function explainConditionBranches(
|
|
tree: WorkflowTaskExecution[]
|
|
): ConditionBranchExplanation[] {
|
|
const nodeMap = new Map<string, WorkflowTaskExecution>();
|
|
const executedNames = new Set<string>();
|
|
|
|
for (const node of tree) {
|
|
if (node.name) {
|
|
nodeMap.set(node.name, node);
|
|
executedNames.add(node.name);
|
|
}
|
|
if (node.id) {
|
|
nodeMap.set(node.id, node);
|
|
}
|
|
}
|
|
|
|
const explanations: ConditionBranchExplanation[] = [];
|
|
|
|
for (const node of tree) {
|
|
const rawNode = node as Record<string, any>;
|
|
if (rawNode.type !== "condition") continue;
|
|
|
|
const childTasks = Array.isArray(rawNode.tasks) ? rawNode.tasks : [];
|
|
if (childTasks.length === 0) continue;
|
|
|
|
// 检查子任务是否执行过
|
|
const childNames = childTasks.map((t: any) => t.name || t.id).filter(Boolean);
|
|
const anyExecuted = childNames.some((name: string) => executedNames.has(name));
|
|
|
|
if (anyExecuted) {
|
|
// 子分支正常执行,无需诊断跳过
|
|
continue;
|
|
}
|
|
|
|
const skippedTaskNames = childTasks.map((t: any) => `${t.name || t.id} (${t.label || t.displayName || t.type})`);
|
|
const rules: ConditionRuleExplanation[] = [];
|
|
|
|
// 解析 combineFilters
|
|
const rawFilters = rawNode.combineFilters?.filters;
|
|
if (Array.isArray(rawFilters)) {
|
|
for (const group of rawFilters) {
|
|
const subFilters = Array.isArray(group.filters) ? group.filters : [group];
|
|
for (const f of subFilters) {
|
|
const chooseInfo = parseChooseTag(f.key);
|
|
const rawExpected = f.value;
|
|
const parsedExpected = parseValueTag(rawExpected);
|
|
const op = f.compare || "eq";
|
|
const opLabel = OP_MAP[op] || op;
|
|
|
|
let upstreamNode = "";
|
|
let upstreamProp = "";
|
|
let leftLabel = String(f.key);
|
|
|
|
if (chooseInfo) {
|
|
upstreamNode = chooseInfo.upstreamNode;
|
|
upstreamProp = chooseInfo.upstreamProp;
|
|
leftLabel = chooseInfo.label;
|
|
} else if (typeof f.key === "string") {
|
|
upstreamProp = f.key;
|
|
}
|
|
|
|
// 从上游节点提取真实 outputs
|
|
const upstreamExecution = upstreamNode ? nodeMap.get(upstreamNode) : undefined;
|
|
const upstreamOutputs = upstreamExecution?.output ?? (upstreamExecution as any)?.outputs;
|
|
|
|
let actualVal: any = undefined;
|
|
let isMissing = false;
|
|
|
|
if (upstreamOutputs && typeof upstreamOutputs === "object") {
|
|
if (upstreamProp in upstreamOutputs) {
|
|
actualVal = upstreamOutputs[upstreamProp];
|
|
} else {
|
|
isMissing = true;
|
|
}
|
|
} else if (upstreamExecution) {
|
|
isMissing = true;
|
|
}
|
|
|
|
// 提取纯净 ID 比对
|
|
let cleanExpected = rawExpected;
|
|
if (typeof rawExpected === "string" && rawExpected.includes("@{value(")) {
|
|
const m = rawExpected.match(/"key":"([^"]+)"/);
|
|
if (m) cleanExpected = m[1];
|
|
} else if (Array.isArray(rawExpected) && rawExpected.length > 0 && typeof rawExpected[0] === "string") {
|
|
const m = rawExpected[0].match(/"key":"([^"]+)"/);
|
|
if (m) cleanExpected = m[1];
|
|
}
|
|
|
|
const matched = evaluateRule(actualVal, cleanExpected, op);
|
|
|
|
let detail = "";
|
|
if (isMissing) {
|
|
const availableKeys = upstreamOutputs ? Object.keys(upstreamOutputs).join(", ") : "无出参";
|
|
detail = `上游节点 [${upstreamNode}] 未返回该属性 [${upstreamProp}](字段名可能脱节,上游可用字段: ${availableKeys})➔ 解析值为 null`;
|
|
} else {
|
|
const actStr = typeof actualVal === "object" ? JSON.stringify(actualVal) : String(actualVal ?? "null");
|
|
const expStr = typeof parsedExpected === "object" ? JSON.stringify(parsedExpected) : String(parsedExpected);
|
|
detail = `实际值: ${actStr} 与期望值 [${expStr}] 比对结果为 ${matched ? "True" : "False"}`;
|
|
}
|
|
|
|
rules.push({
|
|
leftLabel,
|
|
upstreamNode,
|
|
upstreamProp,
|
|
operator: op,
|
|
operatorLabel: opLabel,
|
|
expectedValue: parsedExpected,
|
|
actualValue: actualVal,
|
|
isMissing,
|
|
matched,
|
|
detail,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
let summary = `条件判定未通过 (False),子任务 [${skippedTaskNames.join(", ")}] 被跳过未执行`;
|
|
const missingRule = rules.find((r) => r.isMissing);
|
|
if (missingRule) {
|
|
summary = `条件字段脱节: 上游节点 [${missingRule.upstreamNode}] 缺少属性 [${missingRule.upstreamProp}],判定为 False,子任务被跳过`;
|
|
}
|
|
|
|
explanations.push({
|
|
conditionNodeId: node.id,
|
|
conditionNodeName: node.name || "-",
|
|
conditionNodeLabel: node.label || (node as any).displayName || "条件节点",
|
|
hasSkippedTasks: true,
|
|
skippedTaskNames,
|
|
rules,
|
|
summary,
|
|
});
|
|
}
|
|
|
|
return explanations;
|
|
}
|