From fbb3bc20da77c3b7fbed07f5b08ed1a9264e8935 Mon Sep 17 00:00:00 2001 From: Dalena Date: Tue, 14 Jan 2025 12:23:36 -0500 Subject: [PATCH] fix: show bots on multiple networks --- silverback/_cli.py | 27 +++++++++++---------------- silverback/cluster/client.py | 8 ++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index 27b4d452..c52e3fd3 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -1060,27 +1060,22 @@ def new_bot( def list_bots(cluster: "ClusterClient"): """List all bots in a CLUSTER (Regardless of status)""" - if bot_names := cluster.bots: + if bot_names := cluster.bot_list(): grouped_bots: dict[str, dict[str, list[Bot]]] = {} - for bot in bot_names.values(): - ecosystem, network, provider = bot.network.split("-") - if ecosystem not in grouped_bots: - grouped_bots[ecosystem] = {} - network_key = f"{network}-{provider}" - if network_key not in grouped_bots[ecosystem]: - grouped_bots[ecosystem][network_key] = [] - grouped_bots[ecosystem][network_key].append(bot) - - sorted_ecosystem = sorted(grouped_bots.keys()) - for ecosystem in sorted_ecosystem: - sorted_networks = sorted(grouped_bots[ecosystem].keys()) + 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(grouped_bots[ecosystem][network], key=lambda b: b.name) - for network in sorted_networks + network: sorted(bots, key=lambda b: b.name) + for network, bots in sorted(grouped_bots[ecosystem].items()) } output = "" - for ecosystem in sorted_ecosystem: + for ecosystem in grouped_bots: output += f"{ecosystem}:\n" for network in grouped_bots[ecosystem]: output += f" {network}:\n" diff --git a/silverback/cluster/client.py b/silverback/cluster/client.py index 0adb0399..b8d96e12 100644 --- a/silverback/cluster/client.py +++ b/silverback/cluster/client.py @@ -296,6 +296,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 bot_list(self) -> dict[str, list[Bot]]: + response = self.get("/bots") + handle_error_with_response(response) + bots_dict: dict[str, list[Bot]] = {} + for bot in map(Bot.model_validate, response.json()): + bots_dict.setdefault(bot.name, []).append(bot) + return bots_dict + def new_bot( self, name: str,