Skip to content

Commit

Permalink
update format
Browse files Browse the repository at this point in the history
  • Loading branch information
panh99 committed Nov 14, 2024
1 parent cd9fe34 commit 28e6a5a
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions src/py/flwr/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Flower command line interface `ls` command."""


from datetime import datetime, timedelta
from logging import DEBUG
from pathlib import Path
from typing import Annotated, Any, Optional
Expand All @@ -32,6 +33,7 @@
validate_project_config,
)
from flwr.common.constant import FAB_CONFIG_FILE, SubStatus
from flwr.common.date import format_timedelta, isoformat8601_utc
from flwr.common.grpc import GRPC_MAX_MESSAGE_LENGTH, create_channel
from flwr.common.logger import log
from flwr.common.serde import run_from_proto, scalar_to_proto
Expand Down Expand Up @@ -134,21 +136,28 @@ def _init_channel(app: Path, federation_config: dict[str, Any]) -> grpc.Channel:
return channel


def _format_run_table(run_dict: dict[int, Run]) -> Table:
def _format_run_table(run_dict: dict[int, Run], now_isoformat: str) -> Table:
"""Format run status as a rich Table."""
table = Table(header_style="bold cyan")
table = Table(header_style="bold cyan", show_lines=True)

def _format_datetime(dt: Optional[datetime]) -> str:
return isoformat8601_utc(dt).replace("T", " ") if dt else "N/A"

# Add columns
table.add_column(Text("Run ID", justify="center"), style="bright_white")
table.add_column(Text("FAB", justify="center"))
table.add_column(
Text("Run ID", justify="center"), style="bright_white", overflow="fold"
)
table.add_column(Text("FAB", justify="center"), style="dim white")
table.add_column(Text("Status", justify="center"))
table.add_column(Text("Created At", justify="center"))
table.add_column(Text("Running At", justify="center"))
table.add_column(Text("Finished At", justify="center"))
table.add_column(Text("Elapsed Time", justify="center"))
table.add_column(Text("Elapsed", justify="center"), style="blue")
table.add_column(Text("Created At", justify="center"), style="dim white")
table.add_column(Text("Running At", justify="center"), style="dim white")
table.add_column(Text("Finished At", justify="center"), style="dim white")

# Add rows
for run_id, run in run_dict.items():
for run in sorted(
run_dict.values(), key=lambda x: datetime.fromisoformat(x.pending_at)
):
# Combine status and sub-status into a single string
if run.status.sub_status == "":
status_text = run.status.status
Expand All @@ -164,11 +173,30 @@ def _format_run_table(run_dict: dict[int, Run]) -> Table:
else:
status_style = "yellow"

# Convert isoformat to datetime
pending_at = datetime.fromisoformat(run.pending_at) if run.pending_at else None
running_at = datetime.fromisoformat(run.running_at) if run.running_at else None
finished_at = (
datetime.fromisoformat(run.finished_at) if run.finished_at else None
)

# Calculate elapsed time
elapsed_time = timedelta()
if running_at:
if finished_at:
end_time = finished_at
else:
end_time = datetime.fromisoformat(now_isoformat)
elapsed_time = end_time - running_at

table.add_row(
f"[bold]{run_id}[/bold]",
f"[bold]{run.run_id}[/bold]",
f"{run.fab_id} (v{run.fab_version})",
f"[{status_style}]{status_text}[/{status_style}]",

format_timedelta(elapsed_time),
_format_datetime(pending_at),
_format_datetime(running_at),
_format_datetime(finished_at),
)
return table

Expand All @@ -178,12 +206,9 @@ def _list_runs(
) -> None:
"""List all runs."""
res: ListResponse = stub.List(ListRequest(option="--runs"))
run_dict = {
run_id: run_from_proto(proto)
for run_id, proto in res.run_dict.items()
}
run_dict = {run_id: run_from_proto(proto) for run_id, proto in res.run_dict.items()}

Console().print(_format_run_table(run_dict))
Console().print(_format_run_table(run_dict, res.now))


def _display_one_run(
Expand All @@ -197,9 +222,6 @@ def _display_one_run(
if not res.run_dict:
raise ValueError(f"Run ID {run_id} not found")

run_dict = {
run_id: run_from_proto(proto)
for run_id, proto in res.run_dict.items()
}
run_dict = {run_id: run_from_proto(proto) for run_id, proto in res.run_dict.items()}

Console().print(_format_run_table(run_dict))
Console().print(_format_run_table(run_dict, res.now))

0 comments on commit 28e6a5a

Please sign in to comment.