219 lines
6.4 KiB
TypeScript
219 lines
6.4 KiB
TypeScript
import { listBasicPlatforms } from "../applications/services.js";
|
|
import { KyInstance } from "ky";
|
|
import { CorpConfig } from "../../../core/config/types.js";
|
|
import * as api from "./api.js";
|
|
import { WorkflowSummary, WorkflowListAllError, WorkflowTask, WorkflowWithMatches, TaskMatch } from "./types.js";
|
|
|
|
|
|
export interface WorkflowClients {
|
|
corp: CorpConfig;
|
|
ekbClient: KyInstance;
|
|
easoClient: KyInstance;
|
|
}
|
|
|
|
|
|
export function listWorkflows(...args: Parameters<typeof api.listWorkflows>) {
|
|
return api.listWorkflows(...args);
|
|
}
|
|
|
|
|
|
export function getWorkflowStats(...args: Parameters<typeof api.getWorkflowStats>) {
|
|
return api.getWorkflowStats(...args);
|
|
}
|
|
|
|
|
|
export function getWorkflowCharge(...args: Parameters<typeof api.getWorkflowCharge>) {
|
|
return api.getWorkflowCharge(...args);
|
|
}
|
|
|
|
|
|
export function getWorkflowDetail(...args: Parameters<typeof api.getWorkflowDetail>) {
|
|
return api.getWorkflowDetail(...args);
|
|
}
|
|
|
|
|
|
export function listWorkflowSnapshots(...args: Parameters<typeof api.listWorkflowSnapshots>) {
|
|
return api.listWorkflowSnapshots(...args);
|
|
}
|
|
|
|
|
|
export function getWorkflowSnapshot(...args: Parameters<typeof api.getWorkflowSnapshot>) {
|
|
return api.getWorkflowSnapshot(...args);
|
|
}
|
|
|
|
|
|
export function listWorkflowLogs(...args: Parameters<typeof api.listWorkflowLogs>) {
|
|
return api.listWorkflowLogs(...args);
|
|
}
|
|
|
|
|
|
export function listRunningJobs(...args: Parameters<typeof api.listRunningJobs>) {
|
|
return api.listRunningJobs(...args);
|
|
}
|
|
|
|
|
|
export function getJob(...args: Parameters<typeof api.getJob>) {
|
|
return api.getJob(...args);
|
|
}
|
|
|
|
|
|
export function getJobExecutionTree(...args: Parameters<typeof api.getJobExecutionTree>) {
|
|
return api.getJobExecutionTree(...args);
|
|
}
|
|
|
|
|
|
export function getJobFailedExecutions(...args: Parameters<typeof api.getJobFailedExecutions>) {
|
|
return api.getJobFailedExecutions(...args);
|
|
}
|
|
|
|
|
|
export function getJobExecutionLevel(...args: Parameters<typeof api.getJobExecutionLevel>) {
|
|
return api.getJobExecutionLevel(...args);
|
|
}
|
|
|
|
|
|
export function getExecutionLogs(...args: Parameters<typeof api.getExecutionLogs>) {
|
|
return api.getExecutionLogs(...args);
|
|
}
|
|
|
|
|
|
// ── 服务层:跨应用聚合 ───────────────────────────────────────────────────────
|
|
|
|
export interface ListAllWorkflowsOptions {
|
|
page?: number;
|
|
pageSize?: number;
|
|
}
|
|
|
|
|
|
export interface ListAllWorkflowsResult {
|
|
workflows: WorkflowSummary[];
|
|
errors: WorkflowListAllError[];
|
|
pages?: Array<{ platformId: string; page: number; pageSize: number; totalPages: number; totalItems: number; complete: boolean }>;
|
|
complete?: boolean;
|
|
}
|
|
|
|
|
|
export async function listAllWorkflows(
|
|
clients: WorkflowClients,
|
|
options: ListAllWorkflowsOptions = {}
|
|
): Promise<ListAllWorkflowsResult> {
|
|
const { ekbClient, easoClient, corp } = clients;
|
|
const page = options.page ?? 1;
|
|
const pageSize = options.pageSize ?? 20;
|
|
|
|
const platforms = await listBasicPlatforms(ekbClient, corp.credentials.corpId);
|
|
const enabledPlatforms = platforms.filter((p) => p.active !== false);
|
|
|
|
const workflows: WorkflowSummary[] = [];
|
|
const errors: WorkflowListAllError[] = [];
|
|
const pages: NonNullable<ListAllWorkflowsResult["pages"]> = [];
|
|
|
|
await Promise.all(
|
|
enabledPlatforms.map(async (platform) => {
|
|
try {
|
|
const pageResult = await api.listWorkflows(
|
|
easoClient,
|
|
platform.id,
|
|
page,
|
|
pageSize
|
|
);
|
|
// 服务端有时不提供分页总数;满页时无法证明没有下一页。
|
|
const complete = page === 1 && (pageResult.totalPages > 0
|
|
? pageResult.totalPages === 1
|
|
: pageResult.totalItems > 0
|
|
? pageResult.totalItems <= pageResult.pipelineList.length
|
|
: pageResult.pipelineList.length < pageSize);
|
|
pages.push({ platformId: platform.id, page, pageSize, totalPages: pageResult.totalPages, totalItems: pageResult.totalItems, complete });
|
|
for (const workflow of pageResult.pipelineList) {
|
|
workflows.push({
|
|
...workflow,
|
|
platformId: platform.id,
|
|
platformName: platform.name,
|
|
});
|
|
}
|
|
} catch (err) {
|
|
errors.push({
|
|
platformId: platform.id,
|
|
platformName: platform.name,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
}
|
|
})
|
|
);
|
|
|
|
return { workflows, errors, pages, complete: errors.length === 0 && pages.every((item) => item.complete) };
|
|
}
|
|
|
|
|
|
function collectTasksByType(
|
|
tasks: WorkflowTask[],
|
|
taskType: string,
|
|
matches: TaskMatch[] = []
|
|
): TaskMatch[] {
|
|
for (const task of tasks) {
|
|
if (task.type === taskType) {
|
|
matches.push({
|
|
taskName: task.name,
|
|
taskType: task.type,
|
|
levelIndex: task.levelIndex,
|
|
});
|
|
}
|
|
if ("tasks" in task && Array.isArray(task.tasks)) {
|
|
collectTasksByType(task.tasks as WorkflowTask[], taskType, matches);
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
|
|
|
|
export interface FindWorkflowsByTaskTypeOptions extends ListAllWorkflowsOptions {
|
|
includeDetail?: boolean;
|
|
}
|
|
|
|
|
|
export interface FindWorkflowsByTaskTypeResult {
|
|
workflows: WorkflowWithMatches[];
|
|
errors: WorkflowListAllError[];
|
|
pages?: ListAllWorkflowsResult["pages"];
|
|
complete?: boolean;
|
|
}
|
|
|
|
|
|
export async function findWorkflowsByTaskType(
|
|
clients: WorkflowClients,
|
|
taskType: string,
|
|
options: FindWorkflowsByTaskTypeOptions = {}
|
|
): Promise<FindWorkflowsByTaskTypeResult> {
|
|
const { easoClient } = clients;
|
|
const { includeDetail, ...listOptions } = options;
|
|
|
|
const all = await listAllWorkflows(clients, listOptions);
|
|
const matched: WorkflowWithMatches[] = [];
|
|
const detailErrors: WorkflowListAllError[] = [];
|
|
|
|
for (const workflow of all.workflows) {
|
|
let tasks = workflow.tasks;
|
|
|
|
if (includeDetail) {
|
|
try {
|
|
const detail = await api.getWorkflowDetail(easoClient, workflow.id);
|
|
tasks = detail.tasks;
|
|
} catch (err) {
|
|
detailErrors.push({
|
|
platformId: workflow.platformId,
|
|
platformName: workflow.platformName,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
continue;
|
|
}
|
|
}
|
|
|
|
const matchedTasks = collectTasksByType(tasks, taskType);
|
|
if (matchedTasks.length > 0) {
|
|
matched.push({ ...workflow, matchedTasks });
|
|
}
|
|
}
|
|
|
|
return { workflows: matched, errors: [...all.errors, ...detailErrors], pages: all.pages, complete: all.complete === true && detailErrors.length === 0 };
|
|
}
|