Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list bots by network #187

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
dtdang marked this conversation as resolved.
Show resolved Hide resolved

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)
dtdang marked this conversation as resolved.
Show resolved Hide resolved

def new_bot(
self,
name: str,
Expand Down
Loading