162 lines
7.0 KiB
TypeScript
162 lines
7.0 KiB
TypeScript
import { getWorkflowDetail, listWorkflowLogs, listWorkflowSnapshots } from "../../../domains/platform/services.js";
|
|
import { stringOption, numberOption, booleanOption } from "../../parser.js";
|
|
import { printJson, printTable, printKeyValue } from "../../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
|
import { createWorkflowDetailClient } from "./shared.js";
|
|
|
|
export async function handleHistory(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
|
if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow history <pipelineId> [--page=1] [--page-size=10]\n", exitCode: 1 };
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
|
const page = numberOption(parsed.options, ["page"]) || 1;
|
|
const pageSize = numberOption(parsed.options, ["page-size", "pageSize", "size"]) || 10;
|
|
const result = await listWorkflowSnapshots(client, pipelineId, page, pageSize);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
const rows = result.items.map((snapshot, index) => [
|
|
(page - 1) * pageSize + index + 1,
|
|
snapshot.version,
|
|
snapshot.id,
|
|
snapshot.updateTime,
|
|
snapshot.staffName || snapshot.staffId || "-",
|
|
]);
|
|
return {
|
|
stdout: `====== 工作流版本历史 [${pipelineId}](共 ${result.totalItems} 个) ======\n${printTable(["#", "版本", "快照 ID", "更新时间", "操作者"], rows)}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
export async function handleLogs(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
|
if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow logs <pipelineId> [--state=FAILED] [--has-error] [--start-time=YYYY-MM-DD] [--end-time=YYYY-MM-DD]\n", exitCode: 1 };
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
|
const rawResult = await listWorkflowLogs(client, pipelineId, {
|
|
state: stringOption(parsed.options, ["state"]),
|
|
page: numberOption(parsed.options, ["page"]),
|
|
pageSize: numberOption(parsed.options, ["page-size", "pageSize", "size"]),
|
|
startTime: stringOption(parsed.options, ["start-time", "startTime"]),
|
|
endTime: stringOption(parsed.options, ["end-time", "endTime"]),
|
|
});
|
|
const stateOption = stringOption(parsed.options, ["state"]);
|
|
const hasErrorFilter = booleanOption(parsed.options, ["has-error", "hasError", "errors"]);
|
|
const isJobError = (job: any): boolean => {
|
|
const s = (job.status || "").toUpperCase();
|
|
if (s === "STARTED" || s === "RUNNING") return false;
|
|
if (job.hasError === true || job.hasError === "存在" || job.hasError === "true") return true;
|
|
if (typeof job.errorTaskCount === "number" && job.errorTaskCount > 0) return true;
|
|
if (job.state === "FAILED" || job.status === "FAILED") return true;
|
|
return false;
|
|
};
|
|
|
|
let filteredJobList = rawResult.jobList;
|
|
if (hasErrorFilter) {
|
|
filteredJobList = filteredJobList.filter(isJobError);
|
|
} else if (stateOption) {
|
|
const norm = stateOption.trim().toUpperCase();
|
|
if (norm === "FAILED" || norm === "FAIL" || norm === "ERROR") {
|
|
filteredJobList = filteredJobList.filter(isJobError);
|
|
} else if (norm === "COMPLETED" || norm === "SUCCESS") {
|
|
filteredJobList = filteredJobList.filter((j: any) => (j.status || "").toUpperCase() === "COMPLETED" && !isJobError(j));
|
|
} else if (norm === "RUNNING" || norm === "STARTED" || norm === "IN_PROGRESS") {
|
|
filteredJobList = filteredJobList.filter((j: any) => {
|
|
const s = (j.status || "").toUpperCase();
|
|
return s === "STARTED" || s === "RUNNING";
|
|
});
|
|
}
|
|
}
|
|
|
|
const inProgressCount = Math.max(
|
|
0,
|
|
(rawResult.executeCount ?? 0) - (rawResult.successCount ?? 0) - (rawResult.errorCount ?? 0)
|
|
);
|
|
|
|
const result = {
|
|
...rawResult,
|
|
jobList: filteredJobList,
|
|
inProgressCount,
|
|
};
|
|
|
|
let workflowName = rawResult.jobList[0]?.jobName;
|
|
let workflowState: string | undefined;
|
|
try {
|
|
const detail = await getWorkflowDetail(client, pipelineId);
|
|
if (detail) {
|
|
workflowName = detail.pipelineName || detail.label || workflowName;
|
|
workflowState = detail.state === 1 ? "🟢 启用中" : "⚪ 已停用";
|
|
}
|
|
} catch {}
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({ ...result, workflowName, workflowState }, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const formatRunType = (runType?: string): string => {
|
|
if (!runType) return "-";
|
|
const upper = runType.toUpperCase();
|
|
if (upper === "DEBUG") return "🧪 调试";
|
|
if (upper === "RUN") return "🏃 运行";
|
|
return runType;
|
|
};
|
|
|
|
const formatJobStatus = (status?: string, state?: unknown): string => {
|
|
const s = (status || "").toUpperCase();
|
|
if (s === "STARTED" || s === "RUNNING") return "⏳ 运行中";
|
|
if (s === "COMPLETED" || s === "SUCCESS") return "✅ 已完成";
|
|
if (s === "FAILED" || s === "ERROR") return "❌ 失败";
|
|
if (s === "STOP" || s === "STOPPED") return "🛑 终止";
|
|
if (s === "CANCELLED" || s === "CANCELED") return "🚫 取消";
|
|
if (!s && (state === 0 || state === "0")) return "COMPLETED";
|
|
return s || String(state ?? "-");
|
|
};
|
|
|
|
const formatErrorColumn = (job: any): string => {
|
|
const s = (job.status || "").toUpperCase();
|
|
if (s === "STARTED" || s === "RUNNING") return "⏳ 执行中";
|
|
if (isJobError(job)) return "⚠️ 包含错误";
|
|
return "正常";
|
|
};
|
|
|
|
const rows = result.jobList.map((job: any, idx) => [
|
|
idx + 1,
|
|
job.jobId || job.id || "-",
|
|
formatRunType(job.runType),
|
|
formatJobStatus(job.status, job.state),
|
|
job.successTaskCount ?? 0,
|
|
job.errorTaskCount ?? 0,
|
|
formatErrorColumn(job),
|
|
job.durationTime ?? "-",
|
|
job.executeTime || job.startTime || "-",
|
|
]);
|
|
const tableStr = rows.length
|
|
? printTable(
|
|
["#", "Job ID", "类型 (runType)", "状态 (state)", "成功节点", "失败节点", "有错误", "耗时(ms)", "开始时间"],
|
|
rows
|
|
)
|
|
: "暂无匹配的运行记录";
|
|
|
|
return {
|
|
stdout: [
|
|
`====== 工作流运行日志 [${pipelineId}]${workflowName ? ` · ${workflowName}` : ""}${workflowState ? ` (${workflowState})` : ""} ======`,
|
|
printKeyValue({
|
|
...(workflowName ? { "工作流名称": workflowName } : {}),
|
|
...(workflowState ? { "当前状态": workflowState } : {}),
|
|
"执行次数": result.executeCount,
|
|
"成功次数": result.successCount,
|
|
"错误次数": result.errorCount,
|
|
...(result.inProgressCount > 0 ? { "运行中/延时": `${result.inProgressCount} (正在执行或延时等待中)` } : {}),
|
|
"日志条数": result.totalItems,
|
|
"当前返回记录": result.jobList.length,
|
|
}),
|
|
"",
|
|
tableStr,
|
|
"\n提示: 运行 hose-use workflow job <jobId> [--failed] 下钻查看单次 Job 节点级报错与入参明细\n",
|
|
].join("\n"),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|