Refactor backup UI actions and update cache file

This commit refactors the backup UI actions in the `actions.py` file. It removes unnecessary code and improves the logic for setting the `date_last_backup` value. Additionally, the cache file in the `utils.py` file is updated to include the current date and time.
This commit is contained in:
Théophile Diot 2024-10-28 12:00:57 +01:00
parent 5f8bc4ec82
commit 892ca65552
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
2 changed files with 3 additions and 8 deletions

View file

@ -1,4 +1,3 @@
from datetime import datetime
from json import loads
from logging import getLogger
from traceback import format_exc
@ -29,13 +28,8 @@ def pre_render(**kwargs):
ret["list_backup_files"]["data"]["file name"] = []
ret["list_backup_files"]["data"]["file name"].append(backup_file)
if ret["list_backup_files"]["data"]:
ret["date_last_backup"]["value"] = datetime.strptime(
"-".join(ret["list_backup_files"]["data"]["file name"][len(ret["list_backup_files"]["data"]["file name"]) - 1].split("-")[2:]).replace(
".zip", ""
),
"%Y-%m-%d_%H-%M-%S",
).isoformat()
if data.get("date"):
ret["date_last_backup"]["value"] = data["date"]
except BaseException as e:
logger.debug(format_exc())
logger.error(f"Failed to get backup metrics: {e}")

View file

@ -42,6 +42,7 @@ def update_cache_file(db: Database, backup_dir: Path) -> str:
"""Update the cache file in the database."""
backup_data = loads(db.get_job_cache_file("backup-data", "backup.json") or "{}")
backup_data["files"] = sorted([file.name for file in backup_dir.glob("backup-*.zip")])
backup_data["date"] = datetime.now().astimezone().isoformat()
content = dumps(backup_data, indent=2).encode()
checksum = bytes_hash(content)
err = db.upsert_job_cache(None, "backup.json", content, job_name="backup-data", checksum=checksum)