feat: auto-fix statistics

This commit is contained in:
Muka Schultze 2024-04-03 18:33:33 -03:00
parent 4326519a3f
commit 9115073017
2 changed files with 59 additions and 1 deletions

View File

@ -132,7 +132,11 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
localize("ui.panel.developer-tools.tabs.statistics.no_issue")}`,
},
fix: {
title: "",
title: html`<mwc-button @click=${this._fixAllAutofixableIssues}>
${localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.fix_all"
)}
</mwc-button>`,
label: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.fix"
),
@ -262,6 +266,52 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
});
}
private _fixAllAutofixableIssues = async () => {
const validationResults = await validateStatistics(this.hass);
const autoFixable = new Set(["no_state", "unsupported_state_class"]);
const autoFixableIds = Object.entries(validationResults)
.filter(([, issues]) =>
issues.some((issue) => autoFixable.has(issue.type))
)
.map(([statistic_id]) => statistic_id);
if (autoFixableIds.length <= 0) {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.title"
),
text: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.info_text_not_available"
),
});
return;
}
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.title"
),
text: html`${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.info_text_1"
)}<br /><br />${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.info_text_2",
{ statistic_count: autoFixableIds.length }
)}<br /><br />${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.auto_fix.info_text_3"
)}<br /><br />
${autoFixableIds.map((i) => i).join(", ")}`,
confirmText: this.hass.localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(this.hass, autoFixableIds);
autoFixableIds.forEach((statistic_id) =>
this._deletedStatistics.add(statistic_id)
);
this._validateStatistics();
},
});
};
private _fixIssue = (ev) => {
const issues = (ev.currentTarget.data as StatisticsValidationResult[]).sort(
(itemA, itemB) =>

View File

@ -6607,6 +6607,14 @@
},
"fix_issue": {
"fix": "Fix issue",
"fix_all": "Fix all",
"auto_fix": {
"title": "Fix all auto-fixable issues",
"info_text_not_available": "There are no auto-fixable issues, no action will be taken.",
"info_text_1": "This will delete long term statistics of all entities that either have an unsupported state class or have no state.",
"info_text_2": "Do you want to permanently delete {statistic_count} long term statistics from your database?",
"info_text_3": "The following statistics will be deleted:"
},
"no_support": {
"title": "Fix issue",
"info_text_1": "Fixing this issue is not supported yet."