From dc827496ad71a0f0f3412187b8ee5dbb6cb12a8a Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Fri, 7 Feb 2020 15:56:13 +0700 Subject: [PATCH] Add "duration" to task executions printed by CLI commands * Added "started_at" and "finished_at" to task executions' printout. * Added "duration" to task executions that's calculated as a delta between "finished_at" and "started_at". * Removed "updated_at" from task executions printout since it doesn't carry any relevant info for a user, "finished_at" makes more sense. Change-Id: Ie0bd01135548bc9fb5137368a1933d7b8b3c11e5 --- mistralclient/commands/v2/base.py | 5 +++- mistralclient/commands/v2/tasks.py | 26 ++++++++++++------- mistralclient/tests/unit/v2/test_cli_tasks.py | 11 +++++--- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mistralclient/commands/v2/base.py b/mistralclient/commands/v2/base.py index a27fd26a..710b421d 100644 --- a/mistralclient/commands/v2/base.py +++ b/mistralclient/commands/v2/base.py @@ -199,11 +199,14 @@ def get_filters(parsed_args): def get_duration_str(start_dt_str, end_dt_str): """Builds a human friendly duration string. - :param start_dt_str: Start date time as an ISO string. Must not be empty. + :param start_dt_str: Start date time as an ISO string. :param end_dt_str: End date time as an ISO string. If empty, duration is calculated from the current time. :return: Duration(delta) string. """ + if not start_dt_str: + return '' + start_dt = dt.datetime.strptime(start_dt_str, '%Y-%m-%d %H:%M:%S') if end_dt_str: diff --git a/mistralclient/commands/v2/tasks.py b/mistralclient/commands/v2/tasks.py index 655f2716..7a85148f 100644 --- a/mistralclient/commands/v2/tasks.py +++ b/mistralclient/commands/v2/tasks.py @@ -31,15 +31,17 @@ LOG = logging.getLogger(__name__) class TaskFormatter(base.MistralFormatter): COLUMNS = [ - ('id', 'ID'), - ('name', 'Name'), - ('workflow_name', 'Workflow name'), - ('workflow_namespace', 'Workflow namespace'), - ('workflow_execution_id', 'Workflow Execution ID'), - ('state', 'State'), - ('state_info', 'State info'), - ('created_at', 'Created at'), - ('updated_at', 'Updated at'), + ('id', 'ID'), + ('name', 'Name'), + ('workflow_name', 'Workflow name'), + ('workflow_namespace', 'Workflow namespace'), + ('workflow_execution_id', 'Workflow Execution ID'), + ('state', 'State'), + ('state_info', 'State info'), + ('created_at', 'Created at'), + ('started_at', 'Started at'), + ('finished_at', 'Finished at'), + ('duration', 'Duration', True), ] @staticmethod @@ -48,6 +50,8 @@ class TaskFormatter(base.MistralFormatter): state_info = (task.state_info if not lister else base.cut(task.state_info)) + duration = base.get_duration_str(task.started_at, task.finished_at) + data = ( task.id, task.name, @@ -57,7 +61,9 @@ class TaskFormatter(base.MistralFormatter): task.state, state_info, task.created_at, - task.updated_at or '' + task.started_at or '', + task.finished_at or '', + duration ) else: data = (tuple('' for _ in range(len(TaskFormatter.COLUMNS))),) diff --git a/mistralclient/tests/unit/v2/test_cli_tasks.py b/mistralclient/tests/unit/v2/test_cli_tasks.py index 6b5aaef4..94597aee 100644 --- a/mistralclient/tests/unit/v2/test_cli_tasks.py +++ b/mistralclient/tests/unit/v2/test_cli_tasks.py @@ -30,10 +30,11 @@ TASK_DICT = { 'workflow_name': 'thing', 'workflow_namespace': '', 'workflow_execution_id': '321', - 'state': 'RUNNING', + 'state': 'SUCCESS', 'state_info': None, - 'created_at': '1', - 'updated_at': '1', + 'created_at': '2020-02-07 08:10:32', + 'started_at': '2020-02-07 08:10:32', + 'finished_at': '2020-02-07 08:10:41' } TASK_SUB_WF_EXEC = Execution( @@ -81,7 +82,9 @@ TASK_WITH_RESULT = tasks.Task(mock, TASK_WITH_RESULT_DICT) TASK_WITH_PUBLISHED = tasks.Task(mock, TASK_WITH_PUBLISHED_DICT) EXPECTED_TASK_RESULT = ( - '123', 'some', 'thing', '', '321', 'RUNNING', None, '1', '1' + '123', 'some', 'thing', '', '321', 'SUCCESS', None, + '2020-02-07 08:10:32', '2020-02-07 08:10:32', + '2020-02-07 08:10:41', '0:00:09' )