|
| 1 | +import csv |
| 2 | +import json |
| 3 | +import logging |
| 4 | + |
| 5 | +import click |
| 6 | +import requests |
| 7 | +from tabulate import tabulate |
| 8 | + |
| 9 | +import cromshell.utilities.submissions_file_utils |
| 10 | +from cromshell.utilities import command_setup_utils, cromshellconfig, http_utils |
| 11 | + |
| 12 | +LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +@click.command(name="list") |
| 16 | +@click.option( |
| 17 | + "-c", |
| 18 | + "--color", |
| 19 | + is_flag=True, |
| 20 | + default=False, |
| 21 | + help="Color the output by completion status.", |
| 22 | +) |
| 23 | +@click.option( |
| 24 | + "-u", |
| 25 | + "--update", |
| 26 | + is_flag=True, |
| 27 | + default=False, |
| 28 | + help="Check completion status of all unfinished jobs.", |
| 29 | +) |
| 30 | +@click.pass_obj |
| 31 | +def main(config, color, update): |
| 32 | + """List the status of workflows.""" |
| 33 | + |
| 34 | + LOGGER.info("list") |
| 35 | + |
| 36 | + # Update the submission database if so requested |
| 37 | + if update: |
| 38 | + update_submission_db(config) |
| 39 | + |
| 40 | + # Iterate over the submissions text database and print to screen in a pretty way |
| 41 | + with open(cromshellconfig.submission_file_path, "r") as sub_f: |
| 42 | + reader = csv.reader(sub_f, delimiter="\t", lineterminator="\n") |
| 43 | + all_table_rows = [] |
| 44 | + for single_table_row in reader: |
| 45 | + all_table_rows.append( |
| 46 | + format_status(single_table_row) if color else single_table_row |
| 47 | + ) |
| 48 | + |
| 49 | + print(tabulate(all_table_rows, headers="firstrow", numalign="left")) |
| 50 | + |
| 51 | + return 0 |
| 52 | + |
| 53 | + |
| 54 | +def update_submission_db(config): |
| 55 | + # Iterate over the submissions text database and update their status |
| 56 | + workflow_ids = [] |
| 57 | + with open(cromshellconfig.submission_file_path, "r") as sub_f: |
| 58 | + reader = csv.reader(sub_f, delimiter="\t", lineterminator="\n") |
| 59 | + for table_row in reader: |
| 60 | + if table_row[2] != "RUN_ID" and table_row[4] in [ |
| 61 | + "Submitted", |
| 62 | + "Running", |
| 63 | + "DOOMED", |
| 64 | + ]: |
| 65 | + workflow_ids.append(table_row[2]) |
| 66 | + |
| 67 | + for workflow_id in workflow_ids: |
| 68 | + command_setup_utils.resolve_workflow_id_and_server( |
| 69 | + workflow_id=workflow_id, cromshell_config=config |
| 70 | + ) |
| 71 | + |
| 72 | + # Request workflow status |
| 73 | + request_out = requests.get( |
| 74 | + f"{config.cromwell_api_workflow_id}/status", |
| 75 | + timeout=config.requests_connect_timeout, |
| 76 | + verify=config.requests_verify_certs, |
| 77 | + headers=http_utils.generate_headers(config), |
| 78 | + ) |
| 79 | + |
| 80 | + workflow_status_description = json.loads(request_out.content) |
| 81 | + |
| 82 | + # Hold our status string here |
| 83 | + workflow_status = workflow_status_description["status"] |
| 84 | + |
| 85 | + # Update config.submission_file: |
| 86 | + cromshell.utilities.submissions_file_utils.update_row_values_in_submission_db( |
| 87 | + workflow_database_path=config.submission_file_path, |
| 88 | + workflow_id=workflow_id, |
| 89 | + column_to_update="STATUS", |
| 90 | + update_value=workflow_status, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def format_status(table_row): |
| 95 | + colorful_status = { |
| 96 | + "Failed": "\033[1;37;41mFailed\033[0m", |
| 97 | + "DOOMED": "\033[1;31;47mDOOMED\033[0m", |
| 98 | + "Succeeded": "\033[1;37;42mSucceeded\033[0m", |
| 99 | + "Running": "\033[0;30;46mRunning\033[0m", |
| 100 | + "Aborted": "\033[0;30;43mAborted\033[0m", |
| 101 | + } |
| 102 | + |
| 103 | + status_column = -1 if table_row[-1] in colorful_status else -2 |
| 104 | + |
| 105 | + if table_row[status_column] in colorful_status: |
| 106 | + table_row[status_column] = colorful_status[table_row[status_column]] |
| 107 | + |
| 108 | + return table_row |
0 commit comments