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

Allow to fix statistic issue from repairs #22055

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 33 additions & 1 deletion src/panels/config/repairs/ha-config-repairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
fetchRepairsIssueData,
type RepairsIssue,
} from "../../../data/repairs";
import { showConfigFlowDialog } from "../../../dialogs/config-flow/show-dialog-config-flow";
import type { HomeAssistant } from "../../../types";
import { brandsUrl } from "../../../util/brands-url";
import { fixStatisticsIssue } from "../../developer-tools/statistics/fix-statistics";
import { showRepairsFlowDialog } from "./show-dialog-repair-flow";
import { showRepairsIssueDialog } from "./show-repair-issue-dialog";
import { showConfigFlowDialog } from "../../../dialogs/config-flow/show-dialog-config-flow";
import { StatisticsValidationResult } from "../../../data/recorder";

@customElement("ha-config-repairs")
class HaConfigRepairs extends LitElement {
Expand Down Expand Up @@ -130,6 +132,36 @@ class HaConfigRepairs extends LitElement {
continueFlowId: data.issue_data.flow_id as string,
});
}
} else if (
issue.domain === "sensor" &&
issue.translation_key &&
[
"entity_not_recorded",
"entity_no_longer_recorded",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core doesn't create issues for these two

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't harm having them here, rather have all of it 🙃

"unsupported_state_class",
"units_changed",
].includes(issue.translation_key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor translation keys into a constant

For better maintainability and readability, consider defining the array of translation keys as a constant outside the method.

Apply this refactor:

  1. Define the constant at the top of the file or outside the class:
const STATISTICS_ISSUE_TRANSLATION_KEYS = [
  "entity_not_recorded",
  "entity_no_longer_recorded",
  "unsupported_state_class",
  "units_changed",
];
  1. Update the condition in the _openShowMoreDialog method:
         issue.translation_key &&
-        [
-          "entity_not_recorded",
-          "entity_no_longer_recorded",
-          "unsupported_state_class",
-          "units_changed",
-        ].includes(issue.translation_key)
+        STATISTICS_ISSUE_TRANSLATION_KEYS.includes(issue.translation_key)

) {
const localize =
await this.hass.loadFragmentTranslation("developer-tools");
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
const data = await fetchRepairsIssueData(
this.hass.connection,
issue.domain,
issue.issue_id
);
bramkragten marked this conversation as resolved.
Show resolved Hide resolved
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" });
}
} else {
showRepairsIssueDialog(this, {
issue,
Expand Down
176 changes: 19 additions & 157 deletions src/panels/developer-tools/statistics/developer-tools-statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ import { CSSResultGroup, html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../common/dom/fire_event";
import { LocalizeFunc } from "../../../common/translations/localize";
import { computeStateName } from "../../../common/entity/compute_state_name";
import { LocalizeFunc } from "../../../common/translations/localize";
import "../../../components/data-table/ha-data-table";
import type { DataTableColumnContainer } from "../../../components/data-table/ha-data-table";
import { subscribeEntityRegistry } from "../../../data/entity_registry";
import {
clearStatistics,
getStatisticIds,
StatisticsMetaData,
StatisticsValidationResult,
validateStatistics,
} from "../../../data/recorder";
import {
showAlertDialog,
showConfirmationDialog,
} from "../../../dialogs/generic/show-dialog-box";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import { fixStatisticsIssue } from "./fix-statistics";
import { showStatisticsAdjustSumDialog } from "./show-dialog-statistics-adjust-sum";
import { showFixStatisticsUnitsChangedDialog } from "./show-dialog-statistics-fix-units-changed";
import { documentationUrl } from "../../../util/documentation-url";

const FIX_ISSUES_ORDER = {
no_state: 0,
Expand Down Expand Up @@ -264,162 +258,30 @@ class HaPanelDevStatistics extends SubscribeMixin(LitElement) {
});
}

private _fixIssue = (ev) => {
private _fixIssue = async (ev) => {
const issues = (ev.currentTarget.data as StatisticsValidationResult[]).sort(
(itemA, itemB) =>
(FIX_ISSUES_ORDER[itemA.type] ?? 99) -
(FIX_ISSUES_ORDER[itemB.type] ?? 99)
);
const issue = issues[0];
switch (issue.type) {
case "no_state":
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.title"
),
text: html`${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.info_text_1"
)}<br /><br />${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_state.info_text_2",
{ statistic_id: issue.data.statistic_id }
)}`,
confirmText: this.hass.localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(this.hass, [issue.data.statistic_id]);
this._deletedStatistics.add(issue.data.statistic_id);
this._validateStatistics();
},
});
break;
case "entity_not_recorded":
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.title"
),
text: html`${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_1"
)}<br /><br />${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_not_recorded.info_text_2"
)}<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
>`,
});
break;
case "entity_no_longer_recorded":
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.title"
),
text: html`${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_1"
)}
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.entity_no_longer_recorded.info_text_2"
)}
<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"
)}`,
confirmText: this.hass.localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(this.hass, [issue.data.statistic_id]);
this._deletedStatistics.add(issue.data.statistic_id);
this._validateStatistics();
},
});
break;
case "unsupported_state_class":
showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.title"
),
text: html`${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.info_text_1",
{ state_class: issue.data.state_class }
)}<br /><br />
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.info_text_2"
)}
<ul>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.info_text_3"
)}
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.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.unsupported_state_class.info_text_4_link"
)}</a
>
</li>
<li>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.info_text_5"
)}
</li>
</ul>
${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.unsupported_state_class.info_text_6",
{ statistic_id: issue.data.statistic_id }
)}`,
confirmText: this.hass.localize("ui.common.delete"),
destructive: true,
confirm: async () => {
await clearStatistics(this.hass, [issue.data.statistic_id]);
this._deletedStatistics.add(issue.data.statistic_id);
this._validateStatistics();
},
});
break;
case "units_changed":
showFixStatisticsUnitsChangedDialog(this, {
issue,
fixedCallback: () => {
this._validateStatistics();
},
});
break;
default:
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_support.title"
),
text: this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.no_support.info_text_1"
),
});
const result = await fixStatisticsIssue(
this,
this.hass,
this.hass.localize,
issue
);
if (
result &&
[
"no_state",
"entity_no_longer_recorded",
"unsupported_state_class",
].includes(issue.type)
) {
this._deletedStatistics.add(issue.data.statistic_id);
}
this._validateStatistics();
};

static get styles(): CSSResultGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "../../../components/ha-formfield";
import "../../../components/ha-radio";
import {
clearStatistics,
getStatisticLabel,
updateStatisticsMetadata,
} from "../../../data/recorder";
import { haStyle, haStyleDialog } from "../../../resources/styles";
Expand All @@ -27,6 +28,10 @@ export class DialogStatisticsFixUnitsChanged extends LitElement {
}

public closeDialog(): void {
this._cancel();
}

private _closeDialog(): void {
this._params = undefined;
this._action = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
Expand All @@ -40,9 +45,18 @@ export class DialogStatisticsFixUnitsChanged extends LitElement {
return html`
<ha-dialog
open
@closed=${this.closeDialog}
scrimClickAction
escapeKeyAction
@closed=${this._closeDialog}
.heading=${this.hass.localize(
"ui.panel.developer-tools.tabs.statistics.fix_issue.units_changed.title"
"ui.panel.developer-tools.tabs.statistics.fix_issue.units_changed.title",
{
name: getStatisticLabel(
this.hass,
this._params.issue.data.statistic_id,
undefined
),
}
)}
>
<p>
Expand Down Expand Up @@ -98,7 +112,7 @@ export class DialogStatisticsFixUnitsChanged extends LitElement {
"ui.panel.developer-tools.tabs.statistics.fix_issue.fix"
)}
</mwc-button>
<mwc-button slot="secondaryAction" @click=${this.closeDialog}>
<mwc-button slot="secondaryAction" @click=${this._cancel}>
${this.hass.localize("ui.common.close")}
</mwc-button>
</ha-dialog>
Expand All @@ -109,6 +123,11 @@ export class DialogStatisticsFixUnitsChanged extends LitElement {
this._action = ev.target.value;
}

private _cancel(): void {
this._params?.cancelCallback!();
this._closeDialog();
}

private async _fixIssue(): Promise<void> {
if (this._action === "clear") {
await clearStatistics(this.hass, [this._params!.issue.data.statistic_id]);
Expand All @@ -119,8 +138,8 @@ export class DialogStatisticsFixUnitsChanged extends LitElement {
this._params!.issue.data.state_unit
);
}
this._params?.fixedCallback();
this.closeDialog();
this._params?.fixedCallback!();
this._closeDialog();
}

static get styles(): CSSResultGroup {
Expand Down
Loading
Loading