Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Added --format and --plain options to the 'backend.ai ps' command #80

Merged
merged 8 commits into from
Jan 8, 2020
41 changes: 40 additions & 1 deletion docs/cli/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ other users by adding ``--access-key`` option here.

backend.ai admin sessions

Both commands offers options to set the status filter as follows.
Both commands offer options to set the status filter as follows.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

For other options, please consult the output of ``--help``.

.. list-table::
Expand All @@ -48,6 +48,45 @@ For other options, please consult the output of ``--help``.
* - ``--dead``
- ``CANCELLED`` and ``TERMINATED``.

Both commands offer options to specify which fields of sessions should be printed as follows.

.. list-table::
:widths: 20 80
:header-rows: 1

* - Option
- Included Session Fields

* - (no option)
- ``Session ID``, ``Owner``, ``Image``, ``Type``,

``Status``, ``Status Info``, ``Last updated``, and ``Result``.

* - ``--id-only``
- ``Session ID``.

* - ``--detail``
- ``Session ID``, ``Owner``, ``Image``, ``Type``,

``Status``, ``Status Info``, ``Last updated``, ``Result``,

``Tag``, ``Created At``, ``Occupied Resource``, ``Used Memory (MiB)``,

``Max Used Memory (MiB)``, and ``CPU Using (%)``.

* - ``-f``, ``--format``
- ``Session ID``, ``Owner``, and specified fields by user.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to remove these two default-included fields for full customizability.
Such "templated" options are already there: --detail, --id-only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just show an error sayng "at least one field is required for custom format" if there is no fields given at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that part. If user does not pass any argument, it shows error message saying that Error: --format option requires an argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I print failure message "At least one field is required for custom format", it still prints the error message above. I think it is related to the click package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then it's okay to assume that there will be at least one item in the format argument.


.. note::
Fields for ``-f/--format`` option can be displayed by specifying comma-separated parameters.

Available parameters for this option are: ``id``, ``status``, ``status_info``, ``created_at``, ``last_updated``, ``result``, ``image``, ``type``, ``task_id``, ``tag``, ``occupied_slots``, ``used_memory``, ``max_used_memory``, ``cpu_using``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


For example:

.. code-block:: shell

backend.ai admin session --format id,status,cpu_using

.. _simple-execution:

Expand Down
44 changes: 39 additions & 5 deletions src/ai/backend/client/cli/admin/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@
from . import admin
from ...helper import is_admin
from ...session import Session, is_legacy_server
from ..pretty import print_error
from ..pretty import print_error, print_fail


# Lets say formattable options are:
format_options = {
'id': ('Session ID', 'sess_id'),
'status': ('Status', 'status'),
'status_info': ('Status Info', 'status_info'),
'created_at': ('Created At', 'created_at'),
'last_updated': ('Last updated', 'status_changed'),
'result': ('Result', 'result'),
'image': ('Image', 'image'),
'type': ('Type', 'sess_type'),
'task_id': ('Task/Kernel ID', 'id'),
'tag': ('Tag', 'tag'),
'occupied_slots': ('Occupied Resource', 'occupied_slots'),
'used_memory': ('Used Memory (MiB)', 'mem_cur_bytes'),
'max_used_memory': ('Max Used Memory (MiB)', 'mem_max_bytes'),
'cpu_using': ('CPU Using (%)', 'cpu_using'),
}


@admin.command()
Expand All @@ -32,7 +51,9 @@
@click.option('-a', '--all', is_flag=True,
help='Display all sessions matching the condition using pagination.')
@click.option('--detail', is_flag=True, help='Show more details using more columns.')
def sessions(status, access_key, id_only, show_tid, dead, running, all, detail):
@click.option('-f', '--format', default=None, help='Display only specified fields.')
@click.option('--plain', is_flag=True, help='Display process status in plain format.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"process status" looks somewhat ambiguous.
Let's say "Display the session list without decorative line drawings and the header."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also don't forget to update the same option in ps.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

def sessions(status, access_key, id_only, show_tid, dead, running, all, detail, plain, format):
'''
List and manage compute sessions.
'''
Expand All @@ -46,7 +67,18 @@ def sessions(status, access_key, id_only, show_tid, dead, running, all, detail):
except Exception as e:
print_error(e)
sys.exit(1)
if not id_only:
if id_only:
pass
elif format is not None:
options = format.split(',')
for opt in options:
if opt not in format_options:
print_fail(f'There is no such format option: {opt}')
sys.exit(1)
fields.extend([
format_options[opt] for opt in options
])
else:
fields.extend([
('Image', 'image'),
('Type', 'sess_type'),
Expand Down Expand Up @@ -140,7 +172,8 @@ def _generate_paginated_results(interval):
else:
table = tabulate(
[item.values() for item in items],
headers=(item[0] for item in fields)
headers=[] if plain else (item[0] for item in fields),
tablefmt="plain" if plain else None
)
if not is_first:
table_rows = table.split('\n')
Expand Down Expand Up @@ -171,7 +204,8 @@ def _generate_paginated_results(interval):
print(item['sess_id'])
else:
print(tabulate([item.values() for item in items],
headers=(item[0] for item in fields)))
headers=[] if plain else (item[0] for item in fields),
tablefmt="plain" if plain else None))
if total_count > paginating_interval:
print("More sessions can be displayed by using -a/--all option.")
except Exception as e:
Expand Down
5 changes: 3 additions & 2 deletions src/ai/backend/client/cli/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from . import main
from .admin.sessions import sessions


@main.command()
@click.option('-s', '--status', default=None,
type=click.Choice([
Expand All @@ -23,8 +22,10 @@
@click.option('-a', '--all', is_flag=True,
help='Display all sessions matching the condition using pagination.')
@click.option('--detail', is_flag=True, help='Show more details using more columns.')
@click.option('-f', '--format', default=None, help='Display only specified fields.')
@click.option('--plain', is_flag=True, help='Display process status in plain format.')
@click.pass_context
def ps(ctx, status, id_only, show_tid, dead, running, all, detail):
def ps(ctx, status, id_only, show_tid, dead, running, all, detail, plain, format):
'''
Lists the current running compute sessions for the current keypair.
This is an alias of the "admin sessions --status=RUNNING" command.
Expand Down