Skip to content

Commit

Permalink
fix: Added support for sort and page options to sessions' list command (
Browse files Browse the repository at this point in the history
#33)

# Motivation

Session's list command didn't have support for the sort and page
options.

# Description

Added support for `--sort-*` and `--page*` options to the sessions' list
command.

# Testing

- Tested sorting locally.
- Unit tests pass.

# Impact

Would impact the documentation if it existed.

# Additional Information

None. 

# Checklist

- [x] My code adheres to the coding and style guidelines of the project.
- [x] I have performed a self-review of my code.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have made corresponding changes to the documentation.
- [x] I have thoroughly tested my modifications and added tests when
necessary.
- [x] Tests pass locally and in the CI.
- [x] I have assessed the performance impact of my modifications.
  • Loading branch information
qdelamea-aneo authored Jan 16, 2025
2 parents bd442b0 + 39de881 commit 691a8ec
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/armonik_cli/commands/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from typing import List, Tuple, Union

from armonik.client.sessions import ArmoniKSessions
from armonik.common import SessionStatus, Session, TaskOptions
from armonik.common.filter import SessionFilter
from armonik.common import SessionStatus, Session, TaskOptions, Direction
from armonik.common.filter import SessionFilter, Filter

from armonik_cli.core import console, base_command, KeyValuePairParam, TimeDeltaParam, FilterParam
from armonik_cli.core.params import FieldParam


SESSION_TABLE_COLS = [("ID", "SessionId"), ("Status", "Status"), ("CreatedAt", "CreatedAt")]
Expand All @@ -25,23 +26,63 @@ def sessions() -> None:
@click.option(
"-f",
"--filter",
"filter_with",
type=FilterParam("Session"),
required=False,
help="An expression to filter the sessions to be listed.",
metavar="FILTER EXPR",
)
@click.option(
"--sort-by",
type=FieldParam("Session"),
required=False,
help="Attribute of session to sort with.",
)
@click.option(
"--sort-direction",
type=click.Choice(["asc", "desc"], case_sensitive=False),
default="asc",
required=False,
help="Whether to sort by ascending or by descending order.",
)
@click.option(
"--page", default=-1, help="Get a specific page, it defaults to -1 which gets all pages."
)
@click.option("--page-size", default=100, help="Number of elements in each page")
@base_command
def session_list(
endpoint: str, output: str, filter: Union[SessionFilter, None], debug: bool
def list(
endpoint: str,
output: str,
filter_with: Union[SessionFilter, None],
sort_by: Filter,
sort_direction: str,
page: int,
page_size: int,
debug: bool,
) -> None:
"""List the sessions of an ArmoniK cluster."""
with grpc.insecure_channel(endpoint) as channel:
sessions_client = ArmoniKSessions(channel)
total, sessions = sessions_client.list_sessions(session_filter=filter)
curr_page = page if page > 0 else 0
session_list = []
while True:
total, sessions = sessions_client.list_sessions(
session_filter=filter_with,
sort_field=Session.session_id if sort_by is None else sort_by,
sort_direction=Direction.ASC
if sort_direction.capitalize() == "ASC"
else Direction.DESC,
page=curr_page,
page_size=page_size,
)
session_list += sessions
if page > 0 or len(session_list) >= total:
break
curr_page += 1

if total > 0:
sessions = [_clean_up_status(s) for s in sessions]
console.formatted_print(sessions, format=output, table_cols=SESSION_TABLE_COLS)
session_list = [_clean_up_status(s) for s in session_list]
console.formatted_print(session_list, format=output, table_cols=SESSION_TABLE_COLS)

# TODO: Use logger to display this information
# console.print(f"\n{total} sessions found.")
Expand Down

0 comments on commit 691a8ec

Please sign in to comment.