Skip to content

Commit

Permalink
Feature: Allow filter on aleph node cmd (#184)
Browse files Browse the repository at this point in the history
* Fix: Error on text UI

* Feature: Allow user to filter on aleph node cmd

* Feature: Allow filter on active & address for aleph node cmd

* Update src/aleph_client/commands/node.py

Co-authored-by: Mike Hukiewitz <[email protected]>

* Update src/aleph_client/commands/node.py

Co-authored-by: Mike Hukiewitz <[email protected]>

* Update src/aleph_client/commands/node.py

Co-authored-by: Mike Hukiewitz <[email protected]>

* Update src/aleph_client/commands/node.py

Co-authored-by: Mike Hukiewitz <[email protected]>

* Update src/aleph_client/commands/node.py

Co-authored-by: Mike Hukiewitz <[email protected]>

* Fix: no await before async func

* Refactor: moove _filter_node with other private func

---------

Co-authored-by: Mike Hukiewitz <[email protected]>
  • Loading branch information
1yam and MHHukiewitz authored Dec 5, 2023
1 parent 6f70582 commit 79a377a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/aleph_client/commands/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ def _show_files(files_data: dict) -> None:
f"[bold]Page:[/bold] {pagination_page}",
)
console.print(
f"[bold]Total Pages:[/bold] {pagination_total}",
f"[bold]Total Item:[/bold] {pagination_total}",
)
console.print(f"[bold]Items Per Page:[/bold] {pagination_per_page}")
console.print(f"[bold]Items Max Per Page:[/bold] {pagination_per_page}")

console.print(table)

Expand Down
33 changes: 31 additions & 2 deletions src/aleph_client/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
import unicodedata
from typing import Dict, List, Optional

import aiohttp
import typer
Expand Down Expand Up @@ -101,6 +102,18 @@ def _show_compute(node_info):
console.print(table)


def _filter_node(active: bool, address: Optional[str], core_info):
result = []
for node in core_info:
if active and node["status"] == "active" and node["score"] > 0:
result.append(node)
elif address and node["owner"] == address:
result.append(node)
elif not active and not address:
result.append(node)
return result


def _show_core(node_info):
table = Table(title="Core Channel Node Information")
table.add_column("Score", style="green", no_wrap=True, justify="right")
Expand All @@ -110,7 +123,7 @@ def _show_core(node_info):
table.add_column("Creation Time", style="#029AFF", justify="center")
table.add_column("Status", style="green", justify="right")

for node in node_info.core_node:
for node in node_info:
# Prevent escaping with name
node_name = node["name"]
node_name = _escape_and_normalize(node_name)
Expand Down Expand Up @@ -141,13 +154,21 @@ async def compute(
json: bool = typer.Option(
default=False, help="Print as json instead of rich table"
),
active: bool = typer.Option(default=False, help="Only show active nodes"),
address: Optional[str] = typer.Option(
default=None, help="Owner address to filter by"
),
debug: bool = False,
):
"""Get all compute node on aleph network"""

setup_logging(debug)

compute_info: NodeInfo = await _fetch_nodes()
compute_info.nodes = _filter_node(
core_info=compute_info.nodes, active=active, address=address
)

if not json:
_show_compute(compute_info)
else:
Expand All @@ -159,13 +180,21 @@ async def core(
json: bool = typer.Option(
default=False, help="Print as json instead of rich table"
),
active: bool = typer.Option(default=False, help="Only show active nodes"),
address: Optional[str] = typer.Option(
default=None, help="Owner address to filter by"
),
debug: bool = False,
):
"""Get all core node on aleph"""
setup_logging(debug)

core_info: NodeInfo = await _fetch_nodes()
core_info.core_node = _filter_node(
core_info=core_info.core_node, active=active, address=address
)

if not json:
_show_core(core_info)
_show_core(node_info=core_info.core_node)
else:
typer.echo(json_lib.dumps(core_info.core_node, indent=4))

0 comments on commit 79a377a

Please sign in to comment.