-
Notifications
You must be signed in to change notification settings - Fork 13
Added --format and --plain options to the 'backend.ai ps' command #80
Changes from 4 commits
126bcaa
5a48acf
ab74e7e
70c220c
92d0b53
88c71d4
60c9060
6fccf18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
For other options, please consult the output of ``--help``. | ||
|
||
.. list-table:: | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "process status" looks somewhat ambiguous. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also don't forget to update the same option in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
''' | ||
|
@@ -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'), | ||
|
@@ -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') | ||
|
@@ -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: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍