mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-26 02:36:37 +00:00
Improve StatisticsChart
to only fetch needed metadata (#13617)
This commit is contained in:
parent
81b21f874b
commit
544c8fe3bb
@ -13,6 +13,7 @@ import {
|
|||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { getGraphColorByIndex } from "../../common/color/colors";
|
import { getGraphColorByIndex } from "../../common/color/colors";
|
||||||
import { isComponentLoaded } from "../../common/config/is_component_loaded";
|
import { isComponentLoaded } from "../../common/config/is_component_loaded";
|
||||||
import {
|
import {
|
||||||
@ -20,11 +21,10 @@ import {
|
|||||||
numberFormatToLocale,
|
numberFormatToLocale,
|
||||||
} from "../../common/number/format_number";
|
} from "../../common/number/format_number";
|
||||||
import {
|
import {
|
||||||
getStatisticIds,
|
|
||||||
getStatisticLabel,
|
getStatisticLabel,
|
||||||
|
getStatisticMetadata,
|
||||||
Statistics,
|
Statistics,
|
||||||
statisticsHaveType,
|
statisticsHaveType,
|
||||||
StatisticsMetaData,
|
|
||||||
StatisticType,
|
StatisticType,
|
||||||
} from "../../data/recorder";
|
} from "../../data/recorder";
|
||||||
import type { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
@ -36,8 +36,6 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
@property({ attribute: false }) public statisticsData!: Statistics;
|
@property({ attribute: false }) public statisticsData!: Statistics;
|
||||||
|
|
||||||
@property({ type: Array }) public statisticIds?: StatisticsMetaData[];
|
|
||||||
|
|
||||||
@property() public names: boolean | Record<string, string> = false;
|
@property() public names: boolean | Record<string, string> = false;
|
||||||
|
|
||||||
@property() public unit?: string;
|
@property() public unit?: string;
|
||||||
@ -191,18 +189,28 @@ class StatisticsChart extends LitElement {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _getStatisticIds() {
|
private _getStatisticsMetaData = memoizeOne(
|
||||||
this.statisticIds = await getStatisticIds(this.hass);
|
async (statisticIds: string[] | undefined) => {
|
||||||
}
|
const statsMetadataArray = await getStatisticMetadata(
|
||||||
|
this.hass,
|
||||||
|
statisticIds
|
||||||
|
);
|
||||||
|
const statisticsMetaData = {};
|
||||||
|
statsMetadataArray.forEach((x) => {
|
||||||
|
statisticsMetaData[x.statistic_id] = x;
|
||||||
|
});
|
||||||
|
return statisticsMetaData;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
private async _generateData() {
|
private async _generateData() {
|
||||||
if (!this.statisticsData) {
|
if (!this.statisticsData) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.statisticIds) {
|
const statisticsMetaData = await this._getStatisticsMetaData(
|
||||||
await this._getStatisticIds();
|
Object.keys(this.statisticsData)
|
||||||
}
|
);
|
||||||
|
|
||||||
let colorIndex = 0;
|
let colorIndex = 0;
|
||||||
const statisticsData = Object.values(this.statisticsData);
|
const statisticsData = Object.values(this.statisticsData);
|
||||||
@ -233,9 +241,7 @@ class StatisticsChart extends LitElement {
|
|||||||
const names = this.names || {};
|
const names = this.names || {};
|
||||||
statisticsData.forEach((stats) => {
|
statisticsData.forEach((stats) => {
|
||||||
const firstStat = stats[0];
|
const firstStat = stats[0];
|
||||||
const meta = this.statisticIds!.find(
|
const meta = statisticsMetaData?.[firstStat.statistic_id];
|
||||||
(stat) => stat.statistic_id === firstStat.statistic_id
|
|
||||||
);
|
|
||||||
let name = names[firstStat.statistic_id];
|
let name = names[firstStat.statistic_id];
|
||||||
if (!name) {
|
if (!name) {
|
||||||
name = getStatisticLabel(this.hass, firstStat.statistic_id, meta);
|
name = getStatisticLabel(this.hass, firstStat.statistic_id, meta);
|
||||||
|
@ -19,7 +19,6 @@ import {
|
|||||||
} from "../../../../common/color/convert-color";
|
} from "../../../../common/color/convert-color";
|
||||||
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
import { labBrighten, labDarken } from "../../../../common/color/lab";
|
||||||
import { formatNumber } from "../../../../common/number/format_number";
|
import { formatNumber } from "../../../../common/number/format_number";
|
||||||
import "../../../../components/chart/statistics-chart";
|
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import {
|
import {
|
||||||
EnergyData,
|
EnergyData,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user