Skip to content

Commit

Permalink
cli: adds option to get logs of a specific step
Browse files Browse the repository at this point in the history
* Closes reanahub#369
  • Loading branch information
Rokas Maciulaitis committed Feb 10, 2020
1 parent e2866d1 commit 1fb4828
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion reana_client/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,13 @@ def upload_file(workflow_id, file_, file_name, access_token):
raise e


def get_workflow_logs(workflow_id, access_token):
def get_workflow_logs(workflow_id, access_token, steps=None):
"""Get logs from a workflow engine."""
try:
(response,
http_response) = current_rs_api_client.api.get_workflow_logs(
workflow_id_or_name=workflow_id,
steps=steps or None,
access_token=access_token).result()

if http_response.status_code == 200:
Expand Down
25 changes: 20 additions & 5 deletions reana_client/cli/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def workflow_workflows(ctx, sessions, _filter, output_format, access_token,
response = get_workflows(access_token, type, bool(verbose), block_size)
verbose_headers = ['id', 'user', 'size']
headers = {
'batch': ['name', 'run_number', 'created', 'status'],
'batch': ['name', 'run_number', 'created', 'started', 'completed',
'status'],
'interactive': ['name', 'run_number', 'created', 'session_type',
'session_uri']
}
Expand Down Expand Up @@ -547,6 +548,8 @@ def add_data_from_reponse(row, data, headers):
[name,
run_number,
row['created'],
row['progress']['run_started_at'],
row['progress']['run_finished_at'],
row['status'],
render_progress(finished_jobs, total_jobs)])))
return data
Expand Down Expand Up @@ -583,7 +586,8 @@ def add_verbose_data_from_response(response, verbose_headers,
parsed_filters = parse_parameters(_filter)
_filter = [item['column_name'] for item in parsed_filters]
response = get_workflow_status(workflow, access_token)
headers = ['name', 'run_number', 'created', 'status', 'progress']
headers = ['name', 'run_number', 'created', 'started', 'completed',
'status', 'progress']
verbose_headers = ['id', 'user', 'command']
data = []
if not isinstance(response, list):
Expand Down Expand Up @@ -627,9 +631,16 @@ def add_verbose_data_from_response(response, verbose_headers,
count=True,
help='Get output in JSON format.')
@add_access_token_options
@click.option(
'-s',
'--step',
'steps',
multiple=True,
help='Get logs of a specific step.')
@check_connection
@click.pass_context
def workflow_logs(ctx, workflow, access_token, json_format): # noqa: D301
def workflow_logs(ctx, workflow, access_token, json_format,
steps=None): # noqa: D301
"""Get workflow logs.
The `logs` command allows to retrieve logs of running workflow. Note that
Expand All @@ -638,15 +649,15 @@ def workflow_logs(ctx, workflow, access_token, json_format): # noqa: D301
Examples: \n
\t $ reana-client logs -w myanalysis.42
\t $ reana-client logs -w myanalysis.42 -s 1st_step
"""
from reana_client.api.client import get_workflow_logs
logging.debug('command: {}'.format(ctx.command_path.replace(" ", ".")))
for p in ctx.params:
logging.debug('{param}: {value}'.format(param=p, value=ctx.params[p]))

if workflow:
try:
response = get_workflow_logs(workflow, access_token)
response = get_workflow_logs(workflow, access_token, steps or None)
workflow_logs = json.loads(response['logs'])
if json_format:
click.echo(json.dumps(workflow_logs, indent=2))
Expand All @@ -657,6 +668,10 @@ def workflow_logs(ctx, workflow, access_token, json_format): # noqa: D301
click.echo(workflow_logs['workflow_logs'])

first = True
if len(workflow_logs['job_logs'].items()) == 0 and steps:
click.echo(
click.style('Workflow logs of step(s) {} were not found.'
.format(','.join(steps)), fg='red'))
for job_id, job_logs in workflow_logs['job_logs'].items():
if job_logs:
if first:
Expand Down

0 comments on commit 1fb4828

Please sign in to comment.