Skip to content

Commit

Permalink
Merge pull request #16 from stfc/fix-hypervisor-uptime-less-than-day
Browse files Browse the repository at this point in the history
Handle uptime days is 1
  • Loading branch information
lizsalmon authored Feb 13, 2025
2 parents 5d65e72 + 3687ef5 commit fc34ccf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 5 additions & 4 deletions openstackquery/time_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ def extract_uptime(uptime_string: str) -> Optional[float]:
if match:
uptime_string = match.group(1)
days = 0
if "days" in uptime_string:
days_part, time_part = uptime_string.split(" days, ")
time_part = uptime_string
if "day" in uptime_string:
days_part, time_part = uptime_string.split(
" day" + ("s" if "days" in uptime_string else "") + ", "
)
days += int(days_part)
else:
time_part = uptime_string

hours, minutes = map(int, time_part.split(":"))
days += hours / 24 + minutes / 1440
Expand Down
10 changes: 10 additions & 0 deletions tests/test_time_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ def test_extract_uptime_less_than_day():
res = TimeUtils.extract_uptime(mock_string)

assert res == 0.25


def test_extract_uptime_is_one_day():
"""
Test extraction when less than a day uptime
"""
mock_string = "17:13:49 up 1 day, 7:03, 0 users, load average: 0.00, 0.01, 0.00"
res = TimeUtils.extract_uptime(mock_string)

assert res == 1.29

0 comments on commit fc34ccf

Please sign in to comment.