Skip to content

Commit

Permalink
feat: list bots by network (#187)
Browse files Browse the repository at this point in the history
* feat: list bots by network

* chore: lint

* fix: show bots on multiple networks

* fix: bots
  • Loading branch information
dtdang authored Jan 14, 2025
1 parent d99cc72 commit f67ea6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
27 changes: 24 additions & 3 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions silverback/cluster/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from datetime import datetime
from functools import cache
from typing import ClassVar, Literal
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f67ea6c

Please sign in to comment.