diff --git a/scripts/find_http_nodes.py b/scripts/find_http_nodes.py new file mode 100644 index 0000000..47c742b --- /dev/null +++ b/scripts/find_http_nodes.py @@ -0,0 +1,123 @@ +"""临时脚本:扫描所有自建应用下含 HTTP 节点的工作流并输出汇总表格。 + +用法: + .venv/bin/python scripts/find_http_nodes.py + +说明: +- 复用单个 EasoClient 扫所有平台,减少认证请求次数 +- 工作流列表接口已包含完整 tasks,无需逐一调详情接口 +- 自动处理分页(page_size=100) +""" + +from rich.console import Console +from rich.table import Table +from rich import print as rprint + +from ekb_cli.api.platform.easo_client import get_easo_client +from ekb_cli.api.platform.platform_api import list_platforms +from ekb_cli.api.platform.workflow_api import list_workflows +from ekb_cli.api import get_client + + +console = Console() + + +def collect_http_tasks(tasks: list) -> list: + """递归收集所有层级中 type='http' 的节点(含控制节点内的嵌套节点)""" + result = [] + for t in tasks: + if t.get("type") == "http": + result.append(t) + # 控制节点(every / condition 等)内部可嵌套子节点 + nested = t.get("tasks") + if nested and isinstance(nested, list): + result.extend(collect_http_tasks(nested)) + return result + + +def main(): + console.print("[bold]扫描所有自建应用下含 HTTP 节点的工作流...[/bold]\n") + + # 获取所有自建应用 + with get_client() as ekb_client: + platforms = list_platforms(ekb_client) + + console.print(f"共 {len(platforms)} 个自建应用,开始扫描工作流...\n") + + rows = [] # (platform_name, platform_id, wf_name, wf_id, http_count, http_urls) + + with get_easo_client() as easo_client: + for p in platforms: + console.print(f" 扫描 [cyan]{p.name}[/cyan] ({p.id}) ...", end=" ") + + # 一次拉完(应用内工作流数量有限,page_size=100 足够覆盖) + result = list_workflows(easo_client, p.id, page=1, page_size=100) + all_workflows = result.pipelineList + + console.print(f"{len(all_workflows)} 个工作流") + + for wf in all_workflows: + if wf.state != 1: # 只看启用的工作流 + continue + http_tasks = collect_http_tasks(wf.tasks) + if not http_tasks: + continue + # 提取 HTTP 节点的 URL(如有) + urls = [t.get("url", "") for t in http_tasks] + rows.append((p.name, p.id, wf.pipelineName, wf.id, len(http_tasks), urls)) + + console.print() + + if not rows: + rprint("[yellow]未找到含 HTTP 节点的工作流[/yellow]") + return + + # 汇总表格 + table = Table( + title=f"含 HTTP 节点的工作流(共 {len(rows)} 个)", + show_lines=True, + ) + table.add_column("自建应用", style="cyan") + table.add_column("工作流名称") + table.add_column("工作流 ID", style="dim") + table.add_column("HTTP 节点数", justify="center", style="bold yellow") + table.add_column("HTTP URL(节点顺序)") + + for platform_name, _, wf_name, wf_id, http_count, urls in rows: + table.add_row( + platform_name, + wf_name, + wf_id, + str(http_count), + "\n".join(u if u else "(未配置 URL)" for u in urls), + ) + + console.print(table) + + # 按应用汇总 + console.print() + summary = {} + for platform_name, _, _, _, http_count, _ in rows: + summary[platform_name] = summary.get(platform_name, 0) + http_count + + summary_table = Table(title="按自建应用汇总(HTTP 节点总数)") + summary_table.add_column("自建应用", style="cyan") + summary_table.add_column("涉及工作流数", justify="center") + summary_table.add_column("HTTP 节点总数", justify="center", style="bold") + + wf_count_by_platform = {} + for platform_name, _, _, _, _, _ in rows: + wf_count_by_platform[platform_name] = wf_count_by_platform.get(platform_name, 0) + 1 + + for platform_name, total_http in sorted(summary.items(), key=lambda x: -x[1]): + summary_table.add_row( + platform_name, + str(wf_count_by_platform[platform_name]), + str(total_http), + ) + + console.print(summary_table) + + +if __name__ == "__main__": + main() diff --git a/src/ekb_cli/commands/platform/__init__.py b/src/ekb_cli/commands/platform/__init__.py index 5e50a45..662f49a 100644 --- a/src/ekb_cli/commands/platform/__init__.py +++ b/src/ekb_cli/commands/platform/__init__.py @@ -7,6 +7,7 @@ from ekb_cli.commands.platform.workflow_list import cmd_workflow_list from ekb_cli.commands.platform.workflow_get import cmd_workflow_get from ekb_cli.commands.platform.workflow_stats import cmd_workflow_stats from ekb_cli.commands.platform.workflow_charge import cmd_workflow_charge +from ekb_cli.commands.platform.workflow_search import cmd_workflow_search app = typer.Typer(help="自建应用(DataLink Platform)管理") @@ -15,3 +16,4 @@ app.command("workflow-list")(cmd_workflow_list) app.command("workflow-get")(cmd_workflow_get) app.command("workflow-stats")(cmd_workflow_stats) app.command("workflow-charge")(cmd_workflow_charge) +app.command("workflow-search")(cmd_workflow_search) diff --git a/src/ekb_cli/commands/platform/workflow_search.py b/src/ekb_cli/commands/platform/workflow_search.py new file mode 100644 index 0000000..60ce4f5 --- /dev/null +++ b/src/ekb_cli/commands/platform/workflow_search.py @@ -0,0 +1,42 @@ +"""ekb platform workflow-search:跨所有自建应用查找含指定节点类型的工作流""" + +import typer +from rich.console import Console +from rich.table import Table +from rich import print as rprint + +from ekb_cli.services.platform import find_workflows_by_task_type_cmd + +console = Console() + + +def cmd_workflow_search( + task_type: str = typer.Argument(..., help="节点类型,如 http / ekuaibao-ebot-trigger / stop"), + all_states: bool = typer.Option(False, "--all", "-a", help="包含已停用的工作流(默认只查启用)"), +): + """跨所有自建应用,查找含指定节点类型的工作流""" + enabled_only = not all_states + matches = find_workflows_by_task_type_cmd(task_type, enabled_only=enabled_only) + + if not matches: + rprint(f"[yellow]未找到含 [bold]{task_type}[/bold] 节点的工作流[/yellow]") + return + + table = Table( + title=f"含 {task_type} 节点的工作流(共 {len(matches)} 个)", + show_lines=True, + ) + table.add_column("自建应用", style="cyan") + table.add_column("工作流名称") + table.add_column("工作流 ID", style="dim") + table.add_column("节点数", justify="center", style="bold yellow") + + for m in matches: + table.add_row( + m.platform_name, + m.workflow_name, + m.workflow_id, + str(len(m.matched_tasks)), + ) + + console.print(table) diff --git a/src/ekb_cli/server/platform_router.py b/src/ekb_cli/server/platform_router.py index ccc01e2..8e06b41 100644 --- a/src/ekb_cli/server/platform_router.py +++ b/src/ekb_cli/server/platform_router.py @@ -23,6 +23,8 @@ from ekb_cli.services.platform import ( get_workflow, get_workflow_stats, get_workflow_charge, + list_all_workflows, + find_workflows_by_task_type, ) router = APIRouter() @@ -41,6 +43,45 @@ def route_workflow_charge(client: EasoClientDep): return get_workflow_charge(client).model_dump() +@router.get("/workflows", summary="跨所有应用汇总工作流列表") +def route_list_all_workflows( + ekb_client: ClientDep, + easo_client: EasoClientDep, + enabled_only: bool = Query(True, description="只返回启用状态的工作流"), + task_type: str = Query(None, description="按节点类型过滤,如 http / stop"), +): + """聚合所有自建应用下的工作流;可选按节点类型过滤""" + if task_type: + matches = find_workflows_by_task_type(easo_client, ekb_client, task_type, enabled_only=enabled_only) + return { + "total": len(matches), + "task_type": task_type, + "items": [ + { + "platform_id": m.platform_id, + "platform_name": m.platform_name, + "workflow_id": m.workflow_id, + "workflow_name": m.workflow_name, + "matched_task_count": len(m.matched_tasks), + "matched_tasks": m.matched_tasks, + } + for m in matches + ], + } + items = list_all_workflows(easo_client, ekb_client, enabled_only=enabled_only) + return { + "total": len(items), + "items": [ + { + "platform_id": i.platform_id, + "platform_name": i.platform_name, + **i.workflow.model_dump(), + } + for i in items + ], + } + + @router.get("/{platform_id}/workflows/stats", summary="工作流执行统计") def route_workflow_stats(platform_id: str, client: EasoClientDep): """获取指定自建应用的工作流执行统计(总数/成功/失败等)""" diff --git a/src/ekb_cli/services/platform/__init__.py b/src/ekb_cli/services/platform/__init__.py index b0228d8..d814f76 100644 --- a/src/ekb_cli/services/platform/__init__.py +++ b/src/ekb_cli/services/platform/__init__.py @@ -35,9 +35,15 @@ from ekb_cli.services.platform.workflow_service import ( # noqa: F401, E402 get_workflow, get_workflow_stats, get_workflow_charge, + list_all_workflows, + find_workflows_by_task_type, + WorkflowWithPlatform, + WorkflowTaskMatch, list_workflows_cmd, get_workflow_cmd, get_workflow_stats_cmd, get_workflow_charge_cmd, + list_all_workflows_cmd, + find_workflows_by_task_type_cmd, ) diff --git a/src/ekb_cli/services/platform/workflow_service.py b/src/ekb_cli/services/platform/workflow_service.py index 2dcf722..cdab83b 100644 --- a/src/ekb_cli/services/platform/workflow_service.py +++ b/src/ekb_cli/services/platform/workflow_service.py @@ -9,12 +9,25 @@ - 获取单个工作流完整配置(节点列表) - 查询工作流执行统计 - 查询企业工作流配额 +- 跨应用聚合查询(按节点类型过滤) - 统一抛出 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.schemas import WorkflowPage, WorkflowStats, WorkflowCharge, WorkflowDetail +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, @@ -50,6 +63,92 @@ 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 依赖方向。 @@ -81,49 +180,16 @@ def get_workflow_charge_cmd() -> WorkflowCharge: return get_workflow_charge(client) - -# ── 查询 ───────────────────────────────────────────────────── - -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 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 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) - - -# ── 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_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 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)