From 3a4a22179512e37f93be8c9b27a5fa6f7b4545fd Mon Sep 17 00:00:00 2001 From: Stephen Beechen Date: Fri, 17 Nov 2023 20:23:56 -0700 Subject: [PATCH] Provide supervisor log context for pending backups --- .../backup/ha/hasource.py | 15 +++++ .../backup/static/js/scripts.js | 7 +- .../partials/modals/backup/details.jinja2 | 21 ++++++ .../modals/pending_backup_info.jinja2 | 65 +++++++++++++++++++ .../static/layouts/partials/navbar.jinja2 | 2 + .../backup/static/working.jinja2 | 1 + .../backup/ui/uiserver.py | 5 +- .../dev/simulated_supervisor.py | 19 +++++- 8 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 hassio-google-drive-backup/backup/static/layouts/partials/modals/pending_backup_info.jinja2 diff --git a/hassio-google-drive-backup/backup/ha/hasource.py b/hassio-google-drive-backup/backup/ha/hasource.py index 175a447b..9d16f6e0 100644 --- a/hassio-google-drive-backup/backup/ha/hasource.py +++ b/hassio-google-drive-backup/backup/ha/hasource.py @@ -51,6 +51,7 @@ def __init__(self, backupType, protected, options: CreateOptions, request_info, self._time = time self._pending_subverted = False self._start_time = time.now() + self._logs = None def considerForPurge(self) -> bool: return False @@ -111,6 +112,15 @@ def isStale(self): Setting.FAILED_BACKUP_TIMEOUT_SECONDS)) staleTime = self.getFailureTime() + delta return self._time.now() >= staleTime + + def attach_logs(self, logs): + self._logs = logs + + def error_logs(self): + if self._logs: + return self._logs + else: + return None def madeByTheAddon(self): return True @@ -512,6 +522,11 @@ async def _requestAsync(self, pending: PendingBackup, start=[]) -> None: logger.error("Backup failed:") logger.printException(e) pending.failed(e, self.time.now()) + try: + pending.attach_logs(await self.harequests.getSuperLogs()) + except Exception as e: + logger.error("Failed to get sueprvisor logs after failed backup request") + logger.printException(e) finally: await self.stopper.startAddons() self.trigger() diff --git a/hassio-google-drive-backup/backup/static/js/scripts.js b/hassio-google-drive-backup/backup/static/js/scripts.js index 0abc2eb7..b1af89c3 100644 --- a/hassio-google-drive-backup/backup/static/js/scripts.js +++ b/hassio-google-drive-backup/backup/static/js/scripts.js @@ -612,7 +612,12 @@ function populateBackupDiv(backup_div, backups, icon) { if (backup.isPending) { $("#loading" + backup.slug).show(); - $("#backup_card" + backup.slug).css("cursor", "auto"); + if (backup.super_logs) { + $("#backup_card" + backup.slug).css("cursor", "pointer"); + $("#has-logs", template).show(); + } else { + $("#backup_card" + backup.slug).css("cursor", "auto"); + } } else { $("#loading" + backup.slug).hide(); $("#backup_card" + backup.slug).css("cursor", "pointer"); diff --git a/hassio-google-drive-backup/backup/static/layouts/partials/modals/backup/details.jinja2 b/hassio-google-drive-backup/backup/static/layouts/partials/modals/backup/details.jinja2 index f66d3491..eafc5916 100644 --- a/hassio-google-drive-backup/backup/static/layouts/partials/modals/backup/details.jinja2 +++ b/hassio-google-drive-backup/backup/static/layouts/partials/modals/backup/details.jinja2 @@ -362,9 +362,30 @@ `; } + function colorizeLog(log) { + if (log.includes("WARN")) return `${log}`; + if (log.includes("INFO")) return `${log}`; + if (log.includes("ERROR")) return `${log}`; + if (log.includes("DEBUG")) return `${log}`; + return `${log}`; + } + function showDetails(target) { var backup = $(target).data('backup'); if (backup.isPending) { + if (backup.super_logs) { + let modal = $('#pending_backup_modal'); + modal.data('slug', backup.slug); + modal.data('backup', backup); + const logs = backup.super_logs.split('\n'); + const container = document.getElementById('super_logs_pending_request'); + logs.forEach(log => { + const colorizedLog = colorizeLog(log); + container.innerHTML += `${colorizedLog}\n`; + }); + M.Modal.getInstance(document.getElementById('pending_backup_modal')).open(); + container.scrollTop = container.scrollHeight; + } return; } diff --git a/hassio-google-drive-backup/backup/static/layouts/partials/modals/pending_backup_info.jinja2 b/hassio-google-drive-backup/backup/static/layouts/partials/modals/pending_backup_info.jinja2 new file mode 100644 index 00000000..7c9441de --- /dev/null +++ b/hassio-google-drive-backup/backup/static/layouts/partials/modals/pending_backup_info.jinja2 @@ -0,0 +1,65 @@ + diff --git a/hassio-google-drive-backup/backup/static/layouts/partials/navbar.jinja2 b/hassio-google-drive-backup/backup/static/layouts/partials/navbar.jinja2 index b138cc15..7939d273 100644 --- a/hassio-google-drive-backup/backup/static/layouts/partials/navbar.jinja2 +++ b/hassio-google-drive-backup/backup/static/layouts/partials/navbar.jinja2 @@ -13,6 +13,7 @@ {% include 'layouts/partials/modals/about.jinja2' %} {% include 'layouts/partials/modals/about.jinja2' %} {% include 'layouts/partials/modals/crypto_donate.jinja2' %} +{% include 'layouts/partials/modals/pending_backup_info.jinja2' %} {% if coordEnabled %}