From d7f40ba1aa99a616b1963faa62fb57b0c23648bd Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sat, 16 Nov 2024 13:31:56 -0500 Subject: [PATCH] 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. --- api-daemon/pvcapid/flaskapi.py | 2 +- client-cli/pvc/lib/common.py | 31 +++++++++++++++++++++++++++++++ client-cli/pvc/lib/vm.py | 12 +++++++++--- daemon-common/common.py | 25 +------------------------ 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/api-daemon/pvcapid/flaskapi.py b/api-daemon/pvcapid/flaskapi.py index 2c8dac62..c5b08990 100755 --- a/api-daemon/pvcapid/flaskapi.py +++ b/api-daemon/pvcapid/flaskapi.py @@ -1783,7 +1783,7 @@ class API_VM_Root(Resource): description: Unix timestamp of the snapshot age: type: string - description: Human-readable age of the snapshot in the largest viable unit (seconds, minutes, hours, days) + description: Age of the snapshot in seconds rbd_snapshots: type: array items: diff --git a/client-cli/pvc/lib/common.py b/client-cli/pvc/lib/common.py index 5cc9aaef..000dd28c 100644 --- a/client-cli/pvc/lib/common.py +++ b/client-cli/pvc/lib/common.py @@ -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) diff --git a/client-cli/pvc/lib/vm.py b/client-cli/pvc/lib/vm.py index 8430a27f..d85c7ead 100644 --- a/client-cli/pvc/lib/vm.py +++ b/client-cli/pvc/lib/vm.py @@ -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(), ) diff --git a/daemon-common/common.py b/daemon-common/common.py index c8e48569..0888e989 100644 --- a/daemon-common/common.py +++ b/daemon-common/common.py @@ -486,34 +486,11 @@ def getDomainSnapshots(zkhandler, dom_uuid): _snap_timestamp = float(snap_timestamp) snap_age_secs = int(current_timestamp) - int(_snap_timestamp) - snap_age = f"{snap_age_secs} seconds" - snap_age_minutes = int(snap_age_secs / 60) - if snap_age_minutes > 0: - if snap_age_minutes > 1: - s = "s" - else: - s = "" - snap_age = f"{snap_age_minutes} minute{s}" - snap_age_hours = int(snap_age_secs / 3600) - if snap_age_hours > 0: - if snap_age_hours > 1: - s = "s" - else: - s = "" - snap_age = f"{snap_age_hours} hour{s}" - snap_age_days = int(snap_age_secs / 86400) - if snap_age_days > 0: - if snap_age_days > 1: - s = "s" - else: - s = "" - snap_age = f"{snap_age_days} day{s}" - snapshots.append( { "name": snap_name, "timestamp": snap_timestamp, - "age": snap_age, + "age": snap_age_secs, "xml_diff_lines": snap_dom_xml_diff, "rbd_snapshots": snap_rbd_snapshots, }