Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback for missing logs boot result. #22600

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/data/hassio/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -222,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
Expand Down
18 changes: 12 additions & 6 deletions src/panels/config/logs/dialog-download-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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`
Expand All @@ -126,6 +125,13 @@ class DownloadLogsDialog extends LitElement {
this.closeDialog();
}

private _getDownloadUrl() {
if (this._dialogParams!.boot === 0) {
return getHassioLogDownloadLinesUrl;
}
return getHassioLogBootDownloadLinesUrl;
}

wendevlin marked this conversation as resolved.
Show resolved Hide resolved
private _setNumberOfLogs(ev) {
this._lineCount = Number(ev.target.value);
}
Expand Down
16 changes: 14 additions & 2 deletions src/panels/config/logs/error-log-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { extractApiErrorMessage } from "../../../data/hassio/common";
import {
fetchHassioBoots,
fetchHassioLogs,
fetchHassioLogsBootFollow,
fetchHassioLogsFollow,
getHassioLogDownloadUrl,
} from "../../../data/hassio/supervisor";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
Loading