Skip to content

Commit

Permalink
db: fix display of "-a" (last pass/fail) output
Browse files Browse the repository at this point in the history
It was using float division instead of integer, and so was always
showing zeroes for time deltas less than 1 hour.
  • Loading branch information
wwade committed Apr 3, 2024
1 parent 8a2e245 commit 9a7b20e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
8 changes: 2 additions & 6 deletions jobrunner/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
dateTimeFromJson,
dateTimeToJson,
doMsg,
humanTimeDeltaSecs,
pidDebug,
safeSleep,
sprint,
Expand Down Expand Up @@ -771,12 +772,7 @@ def _byAge(refA, refB):
for res in ["pass", "fail"]:
if wkspace in perWs and res in perWs[wkspace]:
j = perWs[wkspace][res]
sec = int((unow - j.stopTime).total_seconds())
tmHour = sec / (60 * 60)
sec -= tmHour * 60 * 60
tmMin = sec / 60
sec -= tmMin * 60
diffTime = "%d:%02d:%02d" % (tmHour, tmMin, sec)
diffTime = humanTimeDeltaSecs(unow, j.stopTime)
sprint(
" last %s, \033[97m%s\033[0m ago" %
(res, diffTime))
Expand Down
30 changes: 29 additions & 1 deletion jobrunner/test/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

from __future__ import absolute_import, division, print_function

from dataclasses import dataclass
from datetime import datetime, timedelta

import pytest

from jobrunner.utils import autoDecode
from jobrunner.utils import autoDecode, humanTimeDeltaSecs


@pytest.mark.parametrize(("value", "encoding"), [
Expand All @@ -16,3 +19,28 @@
])
def testAutoDecode(value, encoding):
assert value.decode(encoding) == autoDecode(value)


@dataclass(frozen=True)
class HTDCase:
delta: timedelta
expected: str


@pytest.mark.parametrize("tc", [
HTDCase(timedelta(), "0:00:00"),
HTDCase(timedelta(seconds=1), "0:00:01"),
HTDCase(timedelta(seconds=59), "0:00:59"),
HTDCase(timedelta(minutes=1), "0:01:00"),
HTDCase(timedelta(minutes=59), "0:59:00"),
HTDCase(timedelta(hours=1), "1:00:00"),
HTDCase(timedelta(hours=23), "23:00:00"),
HTDCase(timedelta(days=6), "6 days, 0:00:00"),
HTDCase(timedelta(days=4, hours=3, minutes=2, seconds=1), "4 days, 3:02:01"),
HTDCase(timedelta(milliseconds=900), "0:00:01"),
])
def testHumanTimeDeltaSecs(tc: HTDCase) -> None:
b = datetime.now()
a = b + tc.delta
actual = humanTimeDeltaSecs(a, b)
assert tc.expected == actual
8 changes: 8 additions & 0 deletions jobrunner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,11 @@ def autoDecode(byteArray: bytes) -> str:
encoding = "utf-8"

return byteArray.decode(encoding)


def humanTimeDeltaSecs(a: datetime.datetime, b: datetime.datetime) -> str:
"""
Returns a human readable string for the time difference a - b.
"""
seconds = round((a - b).total_seconds(), 0)
return f"{datetime.timedelta(seconds=seconds)}"

0 comments on commit 9a7b20e

Please sign in to comment.