Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display consistent format for job runtime #10504

Merged
merged 13 commits into from
Oct 23, 2020
22 changes: 19 additions & 3 deletions lib/galaxy/job_metrics/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@ def seconds_to_str(value):
"""Convert seconds to a simple simple string describing the amount of time."""
mins, secs = divmod(value, 60)
nsoranzo marked this conversation as resolved.
Show resolved Hide resolved
hours, mins = divmod(mins, 60)
if value < 60:

if value == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! As a suggestion, you could simplify this, if you like. It's just 3 cases: value < 60; if not then value < 3600; and the rest - i.e., if/elif/else. As for the s suffix - you could pre-calculate it: it's '' or 's' for secs/mins/hours depending on whether they == 1.

return "0 seconds"
elif value == 1:
return "1 second"
elif value < 60:
return f"{secs} seconds"
elif value < 3600:
elif value >= 60 and value <= 119:
return "1 minute"
elif value >= 120 and value < 3600:
return f"{mins} minutes"
elif value >= 3600 and value < 7200:
if mins == 1:
return "1 hour and 1 minute"
else:
return f"1 hour and {mins} minutes"
kxk302 marked this conversation as resolved.
Show resolved Hide resolved
else:
return f"{hours} hours and {mins} minutes"
if mins == 1:
return f"{hours} hours and 1 minute"
else:
return f"{hours} hours and {mins} minutes"
kxk302 marked this conversation as resolved.
Show resolved Hide resolved

17 changes: 10 additions & 7 deletions test/unit/jobs/test_job_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ def test_job_metrics_load():

def test_job_metric_formatting():
nsoranzo marked this conversation as resolved.
Show resolved Hide resolved
assert formatting.seconds_to_str(0) == "0 seconds"
assert formatting.seconds_to_str(1) == "1 second"
assert formatting.seconds_to_str(59) == "59 seconds"
assert formatting.seconds_to_str(60) == "1 minutes"
assert formatting.seconds_to_str(61) == "1 minutes"
assert formatting.seconds_to_str(119) == "1 minutes"
assert formatting.seconds_to_str(60) == "1 minute"
assert formatting.seconds_to_str(61) == "1 minute"
assert formatting.seconds_to_str(119) == "1 minute"
assert formatting.seconds_to_str(120) == "2 minutes"
assert formatting.seconds_to_str(121) == "2 minutes"
assert formatting.seconds_to_str(600) == "10 minutes"
assert formatting.seconds_to_str(601) == "10 minutes"
assert formatting.seconds_to_str(660) == "11 minutes"
assert formatting.seconds_to_str(661) == "11 minutes"
assert formatting.seconds_to_str(3600) == "1 hours and 0 minutes"
assert formatting.seconds_to_str(3601) == "1 hours and 0 minutes"
assert formatting.seconds_to_str(3660) == "1 hours and 1 minutes"
assert formatting.seconds_to_str(3661) == "1 hours and 1 minutes"
assert formatting.seconds_to_str(3600) == "1 hour and 0 minutes"
assert formatting.seconds_to_str(3601) == "1 hour and 0 minutes"
assert formatting.seconds_to_str(3660) == "1 hour and 1 minute"
assert formatting.seconds_to_str(3661) == "1 hour and 1 minute"
assert formatting.seconds_to_str(7260) == "2 hours and 1 minute"
assert formatting.seconds_to_str(7320) == "2 hours and 2 minutes"
assert formatting.seconds_to_str(36181) == "10 hours and 3 minutes"