Skip to content

Commit 8f0586e

Browse files
kvgbshifaw
and
bshifaw
authored
Added list command (#233)
* Added list command that colors output by status and updates the submission database if requested * Added missing 'tabulate' package to requirements, reformatted with blank and made minor changes to make linters happy * lint test fixes (#234) * Changed some variable names for clarity Co-authored-by: bshifaw <[email protected]>
1 parent bf15ba9 commit 8f0586e

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ termcolor>=1.1.0
33
click>=8.0.0
44
requests>=2.27.1
55
pygments>=2.12.0
6+
tabulate>=0.8.9

src/cromshell/__main__.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .abort import command as abort
1010
from .alias import command as alias
1111
from .counts import command as counts
12+
from .list import command as list
1213
from .logs import command as logs
1314
from .metadata import command as metadata
1415
from .slim_metadata import command as slim_metadata
@@ -145,6 +146,7 @@ def version():
145146
main_entry.add_command(metadata.main)
146147
main_entry.add_command(update_server.main)
147148
main_entry.add_command(timing.main)
149+
main_entry.add_command(list.main)
148150

149151

150152
if __name__ == "__main__":

src/cromshell/list/__init__.py

Whitespace-only changes.

src/cromshell/list/command.py

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)