From cc633dc311172d2e801c90de8b6f786dc05ea467 Mon Sep 17 00:00:00 2001 From: Wendelin Date: Wed, 30 Oct 2024 18:32:58 +0100 Subject: [PATCH 1/3] Add fallback for missing logs boot result. --- src/data/hassio/supervisor.ts | 14 ++++++++++++++ src/panels/config/logs/error-log-card.ts | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/data/hassio/supervisor.ts b/src/data/hassio/supervisor.ts index 2f34c77f84ce..abdeb8a6a0dc 100644 --- a/src/data/hassio/supervisor.ts +++ b/src/data/hassio/supervisor.ts @@ -203,6 +203,20 @@ export const fetchHassioLogs = async ( ); export const fetchHassioLogsFollow = async ( + hass: HomeAssistant, + provider: string, + signal: AbortSignal, + lines = 100 +) => + hass.callApiRaw( + "GET", + `hassio/${provider.includes("_") ? `addons/${provider}` : provider}/logs/follow?lines=${lines}`, + undefined, + undefined, + signal + ); + +export const fetchHassioLogsBootFollow = async ( hass: HomeAssistant, provider: string, signal: AbortSignal, diff --git a/src/panels/config/logs/error-log-card.ts b/src/panels/config/logs/error-log-card.ts index b487f07c9549..6c63f18eca7a 100644 --- a/src/panels/config/logs/error-log-card.ts +++ b/src/panels/config/logs/error-log-card.ts @@ -39,6 +39,7 @@ import { extractApiErrorMessage } from "../../../data/hassio/common"; import { fetchHassioBoots, fetchHassioLogs, + fetchHassioLogsBootFollow, fetchHassioLogsFollow, getHassioLogDownloadUrl, } from "../../../data/hassio/supervisor"; @@ -378,7 +379,7 @@ class ErrorLogCard extends LitElement { isComponentLoaded(this.hass, "hassio") && this.provider ) { - const response = await fetchHassioLogsFollow( + const response = await this._fetchLogs()( this.hass, this.provider, this._logStreamAborter.signal, @@ -468,6 +469,13 @@ class ErrorLogCard extends LitElement { } } + private _fetchLogs = () => { + if (this._boot === 0) { + return fetchHassioLogsFollow; + } + return fetchHassioLogsBootFollow; + }; + private _debounceSearch = debounce(() => { this._noSearchResults = !this._ansiToHtmlElement?.filterLines(this.filter); @@ -570,9 +578,13 @@ class ErrorLogCard extends LitElement { if (this._streamSupported && isComponentLoaded(this.hass, "hassio")) { try { const { data } = await fetchHassioBoots(this.hass); - this._boots = Object.keys(data.boots) + const boots = Object.keys(data.boots) .map(Number) .sort((a, b) => b - a); + + if (boots.length) { + this._boots = boots; + } } catch (err: any) { // eslint-disable-next-line no-console console.error(err); From 50bed7270895e29d3a9ca44aeb263b8e047fdaae Mon Sep 17 00:00:00 2001 From: Wendelin Date: Wed, 30 Oct 2024 18:38:17 +0100 Subject: [PATCH 2/3] Add fallback for logs full download when boots are missing --- src/data/hassio/supervisor.ts | 7 ++++++- src/panels/config/logs/dialog-download-logs.ts | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/data/hassio/supervisor.ts b/src/data/hassio/supervisor.ts index abdeb8a6a0dc..cc74a7fd70f8 100644 --- a/src/data/hassio/supervisor.ts +++ b/src/data/hassio/supervisor.ts @@ -236,7 +236,12 @@ export const getHassioLogDownloadUrl = (provider: string) => provider.includes("_") ? `addons/${provider}` : provider }/logs`; -export const getHassioLogDownloadLinesUrl = ( +export const getHassioLogDownloadLinesUrl = (provider: string, lines: number) => + `/api/hassio/${ + provider.includes("_") ? `addons/${provider}` : provider + }/logs?lines=${lines}`; + +export const getHassioLogBootDownloadLinesUrl = ( provider: string, lines: number, boot = 0 diff --git a/src/panels/config/logs/dialog-download-logs.ts b/src/panels/config/logs/dialog-download-logs.ts index fdd67e5a315b..72ae401846ca 100644 --- a/src/panels/config/logs/dialog-download-logs.ts +++ b/src/panels/config/logs/dialog-download-logs.ts @@ -14,7 +14,10 @@ import type { DownloadLogsDialogParams } from "./show-dialog-download-logs"; import "../../../components/ha-select"; import "../../../components/ha-list-item"; import { stopPropagation } from "../../../common/dom/stop_propagation"; -import { getHassioLogDownloadLinesUrl } from "../../../data/hassio/supervisor"; +import { + getHassioLogDownloadLinesUrl, + getHassioLogBootDownloadLinesUrl, +} from "../../../data/hassio/supervisor"; import { getSignedPath } from "../../../data/auth"; import { fileDownload } from "../../../util/file_download"; @@ -112,11 +115,7 @@ class DownloadLogsDialog extends LitElement { const boot = this._dialogParams!.boot; const timeString = new Date().toISOString().replace(/:/g, "-"); - const downloadUrl = getHassioLogDownloadLinesUrl( - provider, - this._lineCount, - boot - ); + const downloadUrl = this._getDownloadUrl()(provider, this._lineCount, boot); const logFileName = provider !== "core" ? `${provider}_${timeString}.log` @@ -126,6 +125,13 @@ class DownloadLogsDialog extends LitElement { this.closeDialog(); } + private _getDownloadUrl() { + if (this._dialogParams!.boot === 0) { + return getHassioLogDownloadLinesUrl; + } + return getHassioLogBootDownloadLinesUrl; + } + private _setNumberOfLogs(ev) { this._lineCount = Number(ev.target.value); } From dd012982e3677ffe2726c25ef1d8bddeaf64472e Mon Sep 17 00:00:00 2001 From: Wendelin Date: Thu, 31 Oct 2024 08:16:41 +0100 Subject: [PATCH 3/3] Fix function naming and single boot select in error-log-card --- src/panels/config/logs/dialog-download-logs.ts | 8 ++++++-- src/panels/config/logs/error-log-card.ts | 7 ++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/panels/config/logs/dialog-download-logs.ts b/src/panels/config/logs/dialog-download-logs.ts index 72ae401846ca..e4726e0528a6 100644 --- a/src/panels/config/logs/dialog-download-logs.ts +++ b/src/panels/config/logs/dialog-download-logs.ts @@ -115,7 +115,11 @@ class DownloadLogsDialog extends LitElement { const boot = this._dialogParams!.boot; const timeString = new Date().toISOString().replace(/:/g, "-"); - const downloadUrl = this._getDownloadUrl()(provider, this._lineCount, boot); + const downloadUrl = this._getDownloadUrlFunction()( + provider, + this._lineCount, + boot + ); const logFileName = provider !== "core" ? `${provider}_${timeString}.log` @@ -125,7 +129,7 @@ class DownloadLogsDialog extends LitElement { this.closeDialog(); } - private _getDownloadUrl() { + private _getDownloadUrlFunction() { if (this._dialogParams!.boot === 0) { return getHassioLogDownloadLinesUrl; } diff --git a/src/panels/config/logs/error-log-card.ts b/src/panels/config/logs/error-log-card.ts index 6c63f18eca7a..12994fe1e23f 100644 --- a/src/panels/config/logs/error-log-card.ts +++ b/src/panels/config/logs/error-log-card.ts @@ -379,7 +379,7 @@ class ErrorLogCard extends LitElement { isComponentLoaded(this.hass, "hassio") && this.provider ) { - const response = await this._fetchLogs()( + const response = await this._fetchLogsFunction()( this.hass, this.provider, this._logStreamAborter.signal, @@ -469,7 +469,7 @@ class ErrorLogCard extends LitElement { } } - private _fetchLogs = () => { + private _fetchLogsFunction = () => { if (this._boot === 0) { return fetchHassioLogsFollow; } @@ -582,7 +582,8 @@ class ErrorLogCard extends LitElement { .map(Number) .sort((a, b) => b - a); - if (boots.length) { + // only show boots select when there are more than one boot + if (boots.length > 1) { this._boots = boots; } } catch (err: any) {