From f67ea6cdc6566360d3cbd3eaa4dfea099cde1c3b Mon Sep 17 00:00:00 2001 From: Dalena Date: Tue, 14 Jan 2025 12:43:09 -0600 Subject: [PATCH] feat: list bots by network (#187) * feat: list bots by network * chore: lint * fix: show bots on multiple networks * fix: bots --- silverback/_cli.py | 27 ++++++++++++++++++++++++--- silverback/cluster/client.py | 9 +++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index b3b65677..c3381bec 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -37,7 +37,7 @@ from ape.contracts import ContractInstance from fief_client.integrations.cli import FiefAuth - from silverback.cluster.client import ClusterClient, PlatformClient + from silverback.cluster.client import Bot, ClusterClient, PlatformClient from silverback.cluster.types import VariableGroupInfo LOCAL_DATETIME = "%Y-%m-%d %H:%M:%S %Z" @@ -1060,8 +1060,29 @@ def new_bot( def list_bots(cluster: "ClusterClient"): """List all bots in a CLUSTER (Regardless of status)""" - if bot_names := list(cluster.bots): - click.echo(yaml.safe_dump(bot_names)) + if bot_names := cluster.bots_list(): + grouped_bots: dict[str, dict[str, list[Bot]]] = {} + for bot_list in bot_names.values(): + for bot in bot_list: + ecosystem, network, provider = bot.network.split("-") + network_key = f"{network}-{provider}" + grouped_bots.setdefault(ecosystem, {}).setdefault(network_key, []).append(bot) + + for ecosystem in sorted(grouped_bots.keys()): + grouped_bots[ecosystem] = { + network: sorted(bots, key=lambda b: b.name) + for network, bots in sorted(grouped_bots[ecosystem].items()) + } + + output = "" + for ecosystem in grouped_bots: + output += f"{ecosystem}:\n" + for network in grouped_bots[ecosystem]: + output += f" {network}:\n" + for bot in grouped_bots[ecosystem][network]: + output += f" - {bot.name}\n" + + click.echo(output) else: click.secho("No bots in this cluster", bold=True, fg="red") diff --git a/silverback/cluster/client.py b/silverback/cluster/client.py index 0adb0399..2392662b 100644 --- a/silverback/cluster/client.py +++ b/silverback/cluster/client.py @@ -1,3 +1,4 @@ +from collections import defaultdict from datetime import datetime from functools import cache from typing import ClassVar, Literal @@ -296,6 +297,14 @@ def bots(self) -> dict[str, Bot]: handle_error_with_response(response) return {bot.name: bot for bot in map(Bot.model_validate, response.json())} + def bots_list(self) -> dict[str, list[Bot]]: + response = self.get("/bots") + handle_error_with_response(response) + bots_dict = defaultdict(list) + for bot in map(Bot.model_validate, response.json()): + bots_dict[bot.name].append(bot) + return dict(bots_dict) + def new_bot( self, name: str,