91 lines
4.3 KiB
TypeScript
91 lines
4.3 KiB
TypeScript
import { 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 hasErrorFilter = booleanOption(parsed.options, ["has-error", "hasError", "errors"]);
|
|
const isJobError = (job: any): boolean => {
|
|
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;
|
|
};
|
|
const filteredJobList = hasErrorFilter
|
|
? rawResult.jobList.filter(isJobError)
|
|
: rawResult.jobList;
|
|
const result = hasErrorFilter
|
|
? { ...rawResult, jobList: filteredJobList }
|
|
: rawResult;
|
|
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
|
|
const rows = result.jobList.map((job: any, idx) => [
|
|
idx + 1,
|
|
job.jobId || job.id || "-",
|
|
job.status || (job.state === 0 ? "COMPLETED" : String(job.state ?? "-")),
|
|
job.successTaskCount ?? 0,
|
|
job.errorTaskCount ?? 0,
|
|
isJobError(job) ? "⚠️ 包含错误" : "正常",
|
|
job.durationTime ?? "-",
|
|
job.executeTime || job.startTime || "-",
|
|
]);
|
|
const tableStr = rows.length
|
|
? printTable(
|
|
["#", "Job ID", "状态 (state)", "成功节点 (successTaskCount)", "失败节点 (errorTaskCount)", "有错误 (hasError)", "耗时(ms)", "开始时间"],
|
|
rows
|
|
)
|
|
: "暂无匹配的运行记录";
|
|
|
|
return {
|
|
stdout: [
|
|
`====== 工作流运行日志 [${pipelineId}] ======`,
|
|
printKeyValue({
|
|
"执行次数": result.executeCount,
|
|
"成功次数": result.successCount,
|
|
"错误次数": result.errorCount,
|
|
"日志条数": result.totalItems,
|
|
"当前返回记录": result.jobList.length,
|
|
}),
|
|
"",
|
|
tableStr,
|
|
"\n提示: 运行 hose-use workflow job <jobId> [--failed] 下钻查看单次 Job 节点级报错与入参明细\n",
|
|
].join("\n"),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|