Skip to content

Commit

Permalink
Add --topology option to linfo command (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed Nov 20, 2024
1 parent 461f61d commit 6692c95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
35 changes: 30 additions & 5 deletions src/Kathara/cli/command/LinfoCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from rich.live import Live

from ..ui.utils import create_panel, LabMetaHighlighter
from ..ui.utils import create_table
from ..ui.utils import create_lab_table
from ..ui.utils import create_panel, LabMetaHighlighter, create_topology_table
from ... import utils
from ...foundation.cli.command.Command import Command
from ...manager.Kathara import Kathara
Expand Down Expand Up @@ -54,13 +54,22 @@ def __init__(self) -> None:
help='Read static information from lab.conf.'
)

self.parser.add_argument(
topology_group = self.parser.add_mutually_exclusive_group(required=False)

topology_group.add_argument(
'-n', '--name',
metavar='DEVICE_NAME',
required=False,
help='Show only information about a specified device.'
)

topology_group.add_argument(
'-t', '--topology',
required=False,
action='store_true',
help='Get running topology info'
)

def run(self, current_path: str, argv: List[str]) -> None:
self.parse_args(argv)
args = self.get_args()
Expand All @@ -75,6 +84,8 @@ def run(self, current_path: str, argv: List[str]) -> None:
if args['watch']:
if args['name']:
self._get_machine_live_info(lab, args['name'])
elif args['topology']:
self._get_live_topology_info(lab)
else:
self._get_lab_live_info(lab)

Expand All @@ -94,9 +105,12 @@ def run(self, current_path: str, argv: List[str]) -> None:
style = None if machine_stats else "red bold"

self.console.print(create_panel(message, title=f"{args['name']} Information", style=style))
elif args['topology']:
Kathara.get_instance().update_lab_from_api(lab)
self.console.print(create_topology_table(lab))
else:
machines_stats = Kathara.get_instance().get_machines_stats(lab.hash)
self.console.print(create_table(machines_stats))
self.console.print(create_lab_table(machines_stats))

def _get_machine_live_info(self, lab: Lab, machine_name: str) -> None:
with Live(None, refresh_per_second=12.5, screen=True) as live:
Expand All @@ -109,14 +123,25 @@ def _get_machine_live_info(self, lab: Lab, machine_name: str) -> None:

live.update(create_panel(message, title=f"{machine_name} Information", style=style))

def _get_live_topology_info(self, lab: Lab) -> None:
with Live(None, refresh_per_second=1, screen=True) as live:
live.update(self.console.status(f"Loading...", spinner="dots"))
while True:
Kathara.get_instance().update_lab_from_api(lab)
table = create_topology_table(lab)
if not table:
break

live.update(table)

def _get_lab_live_info(self, lab: Lab) -> None:
machines_stats = Kathara.get_instance().get_machines_stats(lab.hash)

with Live(None, refresh_per_second=12.5, screen=True) as live:
live.update(self.console.status(f"Loading...", spinner="dots"))
live.refresh_per_second = 1
while True:
table = create_table(machines_stats)
table = create_lab_table(machines_stats)
if not table:
break

Expand Down
6 changes: 3 additions & 3 deletions src/Kathara/cli/command/ListCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from rich.live import Live

from ..ui.utils import create_table
from ..ui.utils import create_lab_table
from ... import utils
from ...exceptions import PrivilegeError
from ...foundation.cli.command.Command import Command
Expand Down Expand Up @@ -69,15 +69,15 @@ def run(self, current_path: str, argv: List[str]) -> None:
machines_stats = Kathara.get_instance().get_machines_stats(
machine_name=args['name'], all_users=all_users
)
self.console.print(create_table(machines_stats))
self.console.print(create_lab_table(machines_stats))

def _get_live_info(self, machine_name: Optional[str], all_users: bool) -> None:
machines_stats = Kathara.get_instance().get_machines_stats(machine_name=machine_name, all_users=all_users)
with Live(None, refresh_per_second=12.5, screen=True) as live:
live.update(self.console.status(f"Loading...", spinner="dots"))
live.refresh_per_second = 1
while True:
table = create_table(machines_stats)
table = create_lab_table(machines_stats)
if not table:
break

Expand Down
4 changes: 2 additions & 2 deletions src/Kathara/cli/command/LstartCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from ..ui.utils import create_panel, LabMetaHighlighter
from ..ui.utils import create_table
from ..ui.utils import create_lab_table
from ... import utils
from ...exceptions import PrivilegeError, EmptyLabError
from ...foundation.cli.command.Command import Command
Expand Down Expand Up @@ -234,6 +234,6 @@ def run(self, current_path: str, argv: List[str]) -> Lab:
spinner="dots"
) as _:
machines_stats = Kathara.get_instance().get_machines_stats(lab_hash=lab.hash)
self.console.print(create_table(machines_stats))
self.console.print(create_lab_table(machines_stats))

return lab
23 changes: 22 additions & 1 deletion src/Kathara/cli/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ... import utils
from ...foundation.manager.stats.IMachineStats import IMachineStats
from ...model.Lab import Lab
from ...setting.Setting import Setting
from ...utils import parse_cd_mac_address

Expand Down Expand Up @@ -58,7 +59,7 @@ def create_panel(message: Union[str, Text], **kwargs) -> Panel:
)


def create_table(streams: Generator[Dict[str, IMachineStats], None, None]) -> Optional[RenderableType]:
def create_lab_table(streams: Generator[Dict[str, IMachineStats], None, None]) -> Optional[RenderableType]:
try:
result = next(streams)
except StopIteration:
Expand Down Expand Up @@ -86,6 +87,26 @@ def create_table(streams: Generator[Dict[str, IMachineStats], None, None]) -> Op
return table


def create_topology_table(lab: Lab) -> Optional[RenderableType]:
ts_header = f"TIMESTAMP: {datetime.now()}"

table = Table(title=ts_header, show_lines=True, box=box.SQUARE_DOUBLE_HEAD)

for link in sorted(lab.links.values(), key=lambda x: x.name):
row_data = {
'LINK NAME': link.name,
'DEVICES': ", ".join(link.machines.keys())
}

if not table.columns:
for col in row_data.keys():
table.add_column(col, header_style="dark_orange3")

table.add_row(*map(lambda x: str(x), row_data.values()))

return table


def open_machine_terminal(machine) -> None:
"""Connect to the device with the terminal specified in the settings.
Expand Down

0 comments on commit 6692c95

Please sign in to comment.