Skip to content

Commit

Permalink
fix: show bots on multiple networks
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdang committed Jan 14, 2025
1 parent ee9600a commit fbb3bc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
27 changes: 11 additions & 16 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions silverback/cluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fbb3bc2

Please sign in to comment.