Await removal of statistics when fixing (#22167)

* Await removal of statistics when fixing

* refactor

* translations
This commit is contained in:
Bram Kragten 2024-10-02 09:24:46 +02:00 committed by GitHub
parent c721de109f
commit f9c336890d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 260 additions and 204 deletions

View File

@ -332,3 +332,6 @@ export const getDisplayUnit = (
export const isExternalStatistic = (statisticsId: string): boolean =>
statisticsId.includes(":");
export const updateStatisticsIssues = (hass: HomeAssistant) =>
hass.callWS({ type: "recorder/update_statistics_issues" });

View File

@ -18,6 +18,7 @@ import { showRepairsIssueDialog } from "./show-repair-issue-dialog";
import {
STATISTIC_TYPES,
StatisticsValidationResult,
updateStatisticsIssues,
} from "../../../data/recorder";
@customElement("ha-config-repairs")
@ -144,25 +145,19 @@ class HaConfigRepairs extends LitElement {
issue.translation_key &&
STATISTIC_TYPES.includes(issue.translation_key as any)
) {
const localize =
await this.hass.loadFragmentTranslation("developer-tools");
this.hass.loadFragmentTranslation("developer-tools");
const data = await fetchRepairsIssueData(
this.hass.connection,
issue.domain,
issue.issue_id
);
if ("issue_type" in data.issue_data) {
await fixStatisticsIssue(
this,
this.hass,
localize || this.hass.localize,
{
type: data.issue_data
.issue_type as StatisticsValidationResult["type"],
data: data.issue_data as any,
}
);
this.hass.callWS({ type: "recorder/update_statistics_issues" });
await fixStatisticsIssue(this, {
type: data.issue_data
.issue_type as StatisticsValidationResult["type"],
data: data.issue_data as any,
});
updateStatisticsIssues(this.hass);
}
} else {
showRepairsIssueDialog(this, {

View File

@ -50,8 +50,6 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
private _disabledEntities = new Set<string>();
private _deletedStatistics = new Set<string>();
protected firstUpdated() {
this._validateStatistics();
}
@ -225,9 +223,7 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
this._data = statisticIds
.filter(
(statistic) =>
!this._disabledEntities.has(statistic.statistic_id) &&
!this._deletedStatistics.has(statistic.statistic_id)
(statistic) => !this._disabledEntities.has(statistic.statistic_id)
)
.map((statistic) => {
statsIds.add(statistic.statistic_id);
@ -241,8 +237,7 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
Object.keys(issues).forEach((statisticId) => {
if (
!statsIds.has(statisticId) &&
!this._disabledEntities.has(statisticId) &&
!this._deletedStatistics.has(statisticId)
!this._disabledEntities.has(statisticId)
) {
this._data.push({
statistic_id: statisticId,
@ -265,20 +260,7 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
(FIX_ISSUES_ORDER[itemB.type] ?? 99)
);
const issue = issues[0];
const result = await fixStatisticsIssue(
this,
this.hass,
this.hass.localize,
issue
);
if (
result &&
["no_state", "entity_no_longer_recorded", "state_class_removed"].includes(
issue.type
)
) {
this._deletedStatistics.add(issue.data.statistic_id);
}
await fixStatisticsIssue(this, issue);
this._validateStatistics();
};

View File

@ -0,0 +1,201 @@
import "@material/mwc-button/mwc-button";
import { CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import "../../../components/ha-circular-progress";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/ha-dialog";
import { clearStatistics, getStatisticLabel } from "../../../data/recorder";
import { haStyle, haStyleDialog } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import { documentationUrl } from "../../../util/documentation-url";
import type { DialogStatisticsFixParams } from "./show-dialog-statistics-fix";
import { showAlertDialog } from "../../lovelace/custom-card-helpers";
@customElement("dialog-statistics-fix")
export class DialogStatisticsFix extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _params?: DialogStatisticsFixParams;
@state() private _clearing = false;
public showDialog(params: DialogStatisticsFixParams): void {
this._params = params;
}
public closeDialog(): void {
this._cancel();
}
private _closeDialog(): void {
this._params = undefined;
this._clearing = false;
fireEvent(this, "dialog-closed", { dialog: this.localName });
}
protected render() {
if (!this._params) {
return nothing;
}
const issue = this._params.issue;
return html`
<ha-dialog
open
scrimClickAction
escapeKeyAction
@closed=${this._closeDialog}
.heading=${this.hass.localize(
`ui.panel.developer-tools.tabs.statistics.fix_issue.${issue.type}.title`
)}
>
<p>
${this.hass.localize(
`ui.panel.developer-tools.tabs.statistics.fix_issue.${issue.type}.info_text_1`,
{
name: getStatisticLabel(
this.hass,
this._params.issue.data.statistic_id,
undefined
),
statistic_id: this._params.issue.data.statistic_id,
}
)}<br /><br />
${this.hass.localize(
`ui.panel.developer-tools.tabs.statistics.fix_issue.${issue.type}.info_text_2`,
{ statistic_id: issue.data.statistic_id }
)}
${issue.type === "entity_not_recorded"
? html`<br /><br />
<a
href=${documentationUrl(
this.hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_3_link"
)}</a
>`
: issue.type === "entity_no_longer_recorded"
? html`<a
href=${documentationUrl(
this.hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_3_link"
)}</a
><br /><br />
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_4"
)}`
: issue.type === "state_class_removed"
? html`<ul>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_3"
)}
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4"
)}
<a
href="https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics"
target="_blank"
rel="noreferrer noopener"
>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4_link"
)}</a
>
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_5"
)}
</li>
</ul>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_6",
{ statistic_id: issue.data.statistic_id }
)}`
: nothing}
</p>
${issue.type !== "entity_not_recorded"
? html`<mwc-button
slot="primaryAction"
@click=${this._clearStatistics}
class="warning"
.disabled=${this._clearing}
>
${this._clearing
? html`<ha-circular-progress
indeterminate
size="small"
aria-label="Saving"
></ha-circular-progress>`
: nothing}
${this.hass.localize("ui.common.delete")}
</mwc-button>
<mwc-button slot="secondaryAction" @click=${this._cancel}>
${this.hass.localize("ui.common.close")}
</mwc-button>`
: html`<mwc-button slot="primaryAction" @click=${this._cancel}>
${this.hass.localize("ui.common.ok")}
</mwc-button>`}
</ha-dialog>
`;
}
private _cancel(): void {
this._params?.cancelCallback!();
this._closeDialog();
}
private async _clearStatistics(): Promise<void> {
this._clearing = true;
try {
await clearStatistics(this.hass, [this._params!.issue.data.statistic_id]);
} catch (err: any) {
await showAlertDialog(this, {
title:
err.code === "timeout"
? this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.clearing_timeout_title"
)
: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.clearing_failed"
),
text:
err.code === "timeout"
? this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.clearing_timeout_text"
)
: err.message,
});
} finally {
this._clearing = false;
this._params?.fixedCallback!();
this._closeDialog();
}
}
static get styles(): CSSResultGroup {
return [haStyle, haStyleDialog];
}
}
declare global {
interface HTMLElementTagNameMap {
"dialog-statistics-fix": DialogStatisticsFix;
}
}

View File

@ -1,171 +1,17 @@
import { html } from "lit";
import {
clearStatistics,
getStatisticLabel,
StatisticsValidationResult,
} from "../../../data/recorder";
import { documentationUrl } from "../../../util/documentation-url";
import {
showConfirmationDialog,
showAlertDialog,
} from "../../lovelace/custom-card-helpers";
import { StatisticsValidationResult } from "../../../data/recorder";
import { showFixStatisticsDialog } from "./show-dialog-statistics-fix";
import { showFixStatisticsUnitsChangedDialog } from "./show-dialog-statistics-fix-units-changed";
import { LocalizeFunc } from "../../../common/translations/localize";
import { HomeAssistant } from "../../../types";
export const fixStatisticsIssue = async (
element: HTMLElement,
hass: HomeAssistant,
localize: LocalizeFunc,
issue: StatisticsValidationResult
) => {
switch (issue.type) {
case "no_state":
return showConfirmationDialog(element, {
title: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.title"
),
text: html`${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.info_text_1",
{
name: getStatisticLabel(hass, issue.data.statistic_id, undefined),
statistic_id: issue.data.statistic_id,
}
)}<br /><br />${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.info_text_2",
{ statistic_id: issue.data.statistic_id }
)}`,
confirmText: localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(hass, [issue.data.statistic_id]);
},
});
case "entity_not_recorded":
return showAlertDialog(element, {
title: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.title"
),
text: html`${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_1",
{
name: getStatisticLabel(hass, issue.data.statistic_id, undefined),
}
)}<br /><br />${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_2"
)}<br /><br />
<a
href=${documentationUrl(
hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_3_link"
)}</a
>`,
});
case "entity_no_longer_recorded":
return showConfirmationDialog(element, {
title: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.title"
),
text: html`${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_1",
{
name: getStatisticLabel(hass, issue.data.statistic_id, undefined),
statistic_id: issue.data.statistic_id,
}
)}
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_2"
)}
<a
href=${documentationUrl(
hass,
"/integrations/recorder/#configure-filter"
)}
target="_blank"
rel="noreferrer noopener"
>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_3_link"
)}</a
><br /><br />
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_4"
)}`,
confirmText: localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(hass, [issue.data.statistic_id]);
},
});
case "state_class_removed":
return showConfirmationDialog(element, {
title: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.title"
),
text: html`${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_1",
{
name: getStatisticLabel(hass, issue.data.statistic_id, undefined),
statistic_id: issue.data.statistic_id,
}
)}<br /><br />
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_2"
)}
<ul>
<li>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_3"
)}
</li>
<li>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4"
)}
<a
href="https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics"
target="_blank"
rel="noreferrer noopener"
>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_4_link"
)}</a
>
</li>
<li>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_5"
)}
</li>
</ul>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.state_class_removed.info_text_6",
{ statistic_id: issue.data.statistic_id }
)}`,
confirmText: localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(hass, [issue.data.statistic_id]);
},
});
case "units_changed":
return showFixStatisticsUnitsChangedDialog(element, {
issue,
});
default:
return showAlertDialog(element, {
title: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_support.title"
),
text: localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_support.info_text_1"
),
});
if (issue.type === "units_changed") {
return showFixStatisticsUnitsChangedDialog(element, {
issue,
});
}
return showFixStatisticsDialog(element, {
issue,
});
};

View File

@ -0,0 +1,33 @@
import { fireEvent } from "../../../common/dom/fire_event";
import { StatisticsValidationResult } from "../../../data/recorder";
export const loadFixDialog = () => import("./dialog-statistics-fix");
export interface DialogStatisticsFixParams {
issue: StatisticsValidationResult;
fixedCallback?: () => void;
cancelCallback?: () => void;
}
export const showFixStatisticsDialog = (
element: HTMLElement,
detailParams: DialogStatisticsFixParams
) =>
new Promise((resolve) => {
const origCallback = detailParams.fixedCallback;
fireEvent(element, "show-dialog", {
dialogTag: "dialog-statistics-fix",
dialogImport: loadFixDialog,
dialogParams: {
...detailParams,
cancelCallback: () => {
resolve(false);
},
fixedCallback: () => {
resolve(true);
origCallback?.();
},
},
});
});

View File

@ -15,7 +15,6 @@ import { fireEvent } from "../../../../common/dom/fire_event";
import { stopPropagation } from "../../../../common/dom/stop_propagation";
import "../../../../components/ha-button";
import "../../../../components/ha-button-menu";
import "../../../../components/ha-circular-progress";
import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-header";
import "../../../../components/ha-icon-button";

View File

@ -507,12 +507,6 @@ export class HuiDialogEditView extends LitElement {
margin-inline-end: auto;
margin-inline-start: initial;
}
ha-circular-progress {
display: none;
}
ha-circular-progress[indeterminate] {
display: block;
}
.selected_menu_item {
color: var(--primary-color);
}

View File

@ -6968,6 +6968,9 @@
},
"fix_issue": {
"fix": "Fix issue",
"clearing_failed": "Clearing the statistics failed",
"clearing_timeout_title": "Clearing not done yet",
"clearing_timeout_text": "The clearing of the statistics took longer than expected, it might take longer for the issue to disappear.",
"no_support": {
"title": "Fix issue",
"info_text_1": "Fixing this issue is not supported yet."