- 新增 list_all_workflows / find_workflows_by_task_type 及其 _cmd 变体 - CLI 新增 workflow-search <task_type> 命令(--all 含停用) - HTTP 新增 GET /platforms/workflows?task_type=http&enabled_only=true - 修复 workflow_service.py 中重复定义的函数
196 lines
7.0 KiB
Python
196 lines
7.0 KiB
Python
"""Service 层:工作流(EASO Pipeline)业务逻辑
|
||
|
||
域:
|
||
- unity.ekuaibao.com(EasoClient):列表 / 统计 / 配额
|
||
- piper.ekuaibao.com(PiperClient):工作流配置详情
|
||
|
||
职责:
|
||
- 分页列出指定自建应用下的工作流
|
||
- 获取单个工作流完整配置(节点列表)
|
||
- 查询工作流执行统计
|
||
- 查询企业工作流配额
|
||
- 跨应用聚合查询(按节点类型过滤)
|
||
- 统一抛出 EkbError 子类,不关心上层如何渲染
|
||
"""
|
||
|
||
from dataclasses import dataclass, field
|
||
from typing import Any, Dict, List, Optional
|
||
|
||
from ekb_cli.api import get_client
|
||
from ekb_cli.api.platform.easo_client import EasoClient, get_easo_client
|
||
from ekb_cli.api.platform.piper_client import PiperClient, get_piper_client
|
||
from ekb_cli.api.platform.platform_api import list_platforms as api_list_platforms
|
||
from ekb_cli.api.platform.schemas import (
|
||
Platform,
|
||
Workflow,
|
||
WorkflowPage,
|
||
WorkflowStats,
|
||
WorkflowCharge,
|
||
WorkflowDetail,
|
||
)
|
||
from ekb_cli.api.platform.workflow_api import (
|
||
list_workflows as api_list_workflows,
|
||
get_workflow as api_get_workflow,
|
||
get_workflow_stats as api_get_workflow_stats,
|
||
get_workflow_charge as api_get_workflow_charge,
|
||
)
|
||
|
||
|
||
# ── 查询 ─────────────────────────────────────────────────────
|
||
|
||
def list_workflows(
|
||
client: EasoClient,
|
||
platform_id: str,
|
||
page: int = 1,
|
||
page_size: int = 20,
|
||
) -> WorkflowPage:
|
||
"""列出指定自建应用下的工作流(分页)"""
|
||
return api_list_workflows(client, platform_id, page, page_size)
|
||
|
||
|
||
def get_workflow(client: PiperClient, pipeline_id: str) -> WorkflowDetail:
|
||
"""获取单个工作流的完整配置(包含所有节点及参数)"""
|
||
return api_get_workflow(client, pipeline_id)
|
||
|
||
|
||
def get_workflow_stats(client: EasoClient, platform_id: str) -> WorkflowStats:
|
||
"""获取指定自建应用的工作流执行统计"""
|
||
return api_get_workflow_stats(client, platform_id)
|
||
|
||
|
||
def get_workflow_charge(client: EasoClient) -> WorkflowCharge:
|
||
"""获取企业工作流资源包/配额"""
|
||
return api_get_workflow_charge(client)
|
||
|
||
|
||
# ── 跨应用聚合 ────────────────────────────────────────────────
|
||
|
||
@dataclass
|
||
class WorkflowWithPlatform:
|
||
"""携带所属自建应用信息的工作流摘要"""
|
||
platform_id: str
|
||
platform_name: str
|
||
workflow: Workflow
|
||
|
||
|
||
@dataclass
|
||
class WorkflowTaskMatch:
|
||
"""在工作流中匹配到的节点"""
|
||
platform_id: str
|
||
platform_name: str
|
||
workflow_id: str
|
||
workflow_name: str
|
||
matched_tasks: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
|
||
def _collect_tasks_by_type(tasks: list, task_type: Optional[str]) -> List[Dict[str, Any]]:
|
||
"""递归收集所有层级中匹配类型的节点(含控制节点内的嵌套节点)。
|
||
task_type 为 None 时收集全部节点。
|
||
"""
|
||
result = []
|
||
for t in tasks:
|
||
t_type = t.get("type") if isinstance(t, dict) else getattr(t, "type", "")
|
||
if task_type is None or t_type == task_type:
|
||
result.append(t if isinstance(t, dict) else t.model_dump())
|
||
nested = t.get("tasks") if isinstance(t, dict) else getattr(t, "tasks", None)
|
||
if nested and isinstance(nested, list):
|
||
result.extend(_collect_tasks_by_type(nested, task_type))
|
||
return result
|
||
|
||
|
||
def list_all_workflows(
|
||
easo_client: EasoClient,
|
||
ekb_client,
|
||
enabled_only: bool = True,
|
||
) -> List[WorkflowWithPlatform]:
|
||
"""聚合所有自建应用下的工作流,返回带平台信息的扁平列表。
|
||
|
||
enabled_only=True 时只返回 state=1 的工作流。
|
||
"""
|
||
platforms: List[Platform] = api_list_platforms(ekb_client)
|
||
result: List[WorkflowWithPlatform] = []
|
||
for p in platforms:
|
||
page = api_list_workflows(easo_client, p.id, page=1, page_size=100)
|
||
for wf in page.pipelineList:
|
||
if enabled_only and wf.state != 1:
|
||
continue
|
||
result.append(WorkflowWithPlatform(
|
||
platform_id=p.id,
|
||
platform_name=p.name,
|
||
workflow=wf,
|
||
))
|
||
return result
|
||
|
||
|
||
def find_workflows_by_task_type(
|
||
easo_client: EasoClient,
|
||
ekb_client,
|
||
task_type: str,
|
||
enabled_only: bool = True,
|
||
) -> List[WorkflowTaskMatch]:
|
||
"""跨所有自建应用,找出含指定节点类型的工作流。
|
||
|
||
递归检查控制节点(every / condition 等)内的嵌套节点。
|
||
返回含匹配节点列表的结果,每条记录对应一个工作流。
|
||
"""
|
||
all_wfs = list_all_workflows(easo_client, ekb_client, enabled_only=enabled_only)
|
||
matches: List[WorkflowTaskMatch] = []
|
||
for item in all_wfs:
|
||
wf = item.workflow
|
||
matched = _collect_tasks_by_type(wf.tasks, task_type)
|
||
if matched:
|
||
matches.append(WorkflowTaskMatch(
|
||
platform_id=item.platform_id,
|
||
platform_name=item.platform_name,
|
||
workflow_id=wf.id,
|
||
workflow_name=wf.pipelineName,
|
||
matched_tasks=matched,
|
||
))
|
||
return matches
|
||
|
||
|
||
# ── CLI 便捷函数(自管 client 生命周期)────────────────────────
|
||
# commands/* 层调用这批函数,保持 commands → services → api 依赖方向。
|
||
|
||
def list_workflows_cmd(
|
||
platform_id: str,
|
||
page: int = 1,
|
||
page_size: int = 20,
|
||
) -> WorkflowPage:
|
||
"""CLI 便捷函数:自动获取 EasoClient 并列出工作流"""
|
||
with get_easo_client() as client:
|
||
return list_workflows(client, platform_id, page, page_size)
|
||
|
||
|
||
def get_workflow_cmd(platform_id: str, pipeline_id: str) -> WorkflowDetail:
|
||
"""CLI 便捷函数:自动获取 PiperClient 并查询工作流完整配置"""
|
||
with get_piper_client(platform_id) as client:
|
||
return get_workflow(client, pipeline_id)
|
||
|
||
|
||
def get_workflow_stats_cmd(platform_id: str) -> WorkflowStats:
|
||
"""CLI 便捷函数:自动获取 EasoClient 并查询工作流统计"""
|
||
with get_easo_client() as client:
|
||
return get_workflow_stats(client, platform_id)
|
||
|
||
|
||
def get_workflow_charge_cmd() -> WorkflowCharge:
|
||
"""CLI 便捷函数:自动获取 EasoClient 并查询工作流配额"""
|
||
with get_easo_client() as client:
|
||
return get_workflow_charge(client)
|
||
|
||
|
||
def list_all_workflows_cmd(enabled_only: bool = True) -> List[WorkflowWithPlatform]:
|
||
"""CLI 便捷函数:跨所有应用汇总工作流"""
|
||
with get_client() as ekb_client, get_easo_client() as easo_client:
|
||
return list_all_workflows(easo_client, ekb_client, enabled_only=enabled_only)
|
||
|
||
|
||
def find_workflows_by_task_type_cmd(
|
||
task_type: str,
|
||
enabled_only: bool = True,
|
||
) -> List[WorkflowTaskMatch]:
|
||
"""CLI 便捷函数:跨所有应用查找含指定节点类型的工作流"""
|
||
with get_client() as ekb_client, get_easo_client() as easo_client:
|
||
return find_workflows_by_task_type(easo_client, ekb_client, task_type, enabled_only=enabled_only)
|