Move snapshot age conversion to client and round

Moves the age human conversion logic to the client so that this value
can be used by API consumers programatically.

Rounding ensures recent snapshots are not showing as "older" than they
actually are, which is important for accuracy especially with
automirror snapshots and ages above half the next rounding value.
This commit is contained in:
2024-11-16 13:31:56 -05:00
parent 08227ba0f4
commit d7f40ba1aa
4 changed files with 42 additions and 28 deletions

View File

@ -64,6 +64,37 @@ def format_metric(integer):
return human_integer
def format_age(age_secs):
human_age = f"{age_secs} seconds"
age_minutes = int(age_secs / 60)
age_minutes_rounded = int(round(age_secs / 60))
if age_minutes > 0:
if age_minutes_rounded > 1:
s = "s"
else:
s = ""
human_age = f"{age_minutes_rounded} minute{s}"
age_hours = int(age_secs / 3600)
age_hours_rounded = int(round(age_secs / 3600))
if age_hours > 0:
if age_hours_rounded > 1:
s = "s"
else:
s = ""
human_age = f"{age_hours_rounded} hour{s}"
age_days = int(age_secs / 86400)
age_days_rounded = int(round(age_secs / 86400))
if age_days > 0:
if age_days_rounded > 1:
s = "s"
else:
s = ""
human_age = f"{age_days_rounded} day{s}"
return human_age
class UploadProgressBar(object):
def __init__(self, filename, end_message="", end_nl=True):
file_size = os.path.getsize(filename)

View File

@ -23,7 +23,13 @@ import time
import re
import pvc.lib.ansiprint as ansiprint
from pvc.lib.common import call_api, format_bytes, format_metric, get_wait_retdata
from pvc.lib.common import (
call_api,
format_bytes,
format_metric,
format_age,
get_wait_retdata,
)
#
@ -2056,7 +2062,7 @@ def format_info(config, domain_information, long_output):
if _snapshots_name_length > snapshots_name_length:
snapshots_name_length = _snapshots_name_length
_snapshots_age_length = len(snapshot["age"]) + 1
_snapshots_age_length = len(format_age(snapshot["age"])) + 1
if _snapshots_age_length > snapshots_age_length:
snapshots_age_length = _snapshots_age_length
@ -2096,7 +2102,7 @@ def format_info(config, domain_information, long_output):
snapshots_age_length=snapshots_age_length,
snapshots_xml_changes_length=snapshots_xml_changes_length,
snapshots_name=snapshot["name"],
snapshots_age=snapshot["age"],
snapshots_age=format_age(snapshot["age"]),
snapshots_xml_changes=xml_diff_counts,
end=ansiprint.end(),
)