mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-03 06:27:47 +00:00
Address review comments
This commit is contained in:
parent
131957fbb3
commit
0f534423a4
@ -30,6 +30,15 @@ import {
|
|||||||
import type { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
import "./ha-chart-base";
|
import "./ha-chart-base";
|
||||||
|
|
||||||
|
export type ExtendedStatisticType = StatisticType | "state" | "sum_rel";
|
||||||
|
|
||||||
|
export const statTypeMap: Record<ExtendedStatisticType, StatisticType> = {
|
||||||
|
mean: "mean",
|
||||||
|
min: "min",
|
||||||
|
max: "max",
|
||||||
|
sum: "sum",
|
||||||
|
sum_rel: "sum",
|
||||||
|
};
|
||||||
@customElement("statistics-chart")
|
@customElement("statistics-chart")
|
||||||
class StatisticsChart extends LitElement {
|
class StatisticsChart extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
@ -42,7 +51,7 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
@property({ attribute: false }) public endTime?: Date;
|
@property({ attribute: false }) public endTime?: Date;
|
||||||
|
|
||||||
@property({ type: Array }) public statTypes: Array<StatisticType> = [
|
@property({ type: Array }) public statTypes: Array<ExtendedStatisticType> = [
|
||||||
"sum",
|
"sum",
|
||||||
"min",
|
"min",
|
||||||
"mean",
|
"mean",
|
||||||
@ -307,7 +316,7 @@ class StatisticsChart extends LitElement {
|
|||||||
: this.statTypes;
|
: this.statTypes;
|
||||||
|
|
||||||
sortedTypes.forEach((type) => {
|
sortedTypes.forEach((type) => {
|
||||||
if (statisticsHaveType(stats, type)) {
|
if (statisticsHaveType(stats, statTypeMap[type])) {
|
||||||
const band = drawBands && (type === "min" || type === "max");
|
const band = drawBands && (type === "min" || type === "max");
|
||||||
statTypes.push(type);
|
statTypes.push(type);
|
||||||
statDataSets.push({
|
statDataSets.push({
|
||||||
@ -335,8 +344,14 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
let prevDate: Date | null = null;
|
let prevDate: Date | null = null;
|
||||||
// Process chart data.
|
// Process chart data.
|
||||||
let initVal: number | null = null;
|
const initVal: Record<"sum" | "sum_rel", number | null> = {
|
||||||
let prevSum: number | null = null;
|
sum: null,
|
||||||
|
sum_rel: null,
|
||||||
|
};
|
||||||
|
const prevSum: Record<"sum" | "sum_rel", number | null> = {
|
||||||
|
sum: null,
|
||||||
|
sum_rel: null,
|
||||||
|
};
|
||||||
stats.forEach((stat) => {
|
stats.forEach((stat) => {
|
||||||
const date = new Date(stat.start);
|
const date = new Date(stat.start);
|
||||||
if (prevDate === date) {
|
if (prevDate === date) {
|
||||||
@ -347,11 +362,11 @@ class StatisticsChart extends LitElement {
|
|||||||
statTypes.forEach((type) => {
|
statTypes.forEach((type) => {
|
||||||
let val: number | null;
|
let val: number | null;
|
||||||
if (type === "sum" || type === "sum_rel") {
|
if (type === "sum" || type === "sum_rel") {
|
||||||
if (initVal === null) {
|
if (initVal[type] === null) {
|
||||||
initVal = val = type === "sum_rel" ? 0 : stat.sum || 0;
|
initVal[type] = val = type === "sum_rel" ? 0 : stat.sum || 0;
|
||||||
prevSum = stat.sum;
|
prevSum[type] = stat.sum;
|
||||||
} else {
|
} else {
|
||||||
val = initVal + ((stat.sum || 0) - prevSum!);
|
val = initVal[type]! + ((stat.sum || 0) - prevSum[type]!);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val = stat[type];
|
val = stat[type];
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { computeStateName } from "../common/entity/compute_state_name";
|
import { computeStateName } from "../common/entity/compute_state_name";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
export type StatisticType = "sum" | "sum_rel" | "min" | "max" | "mean";
|
export type StatisticType = "sum" | "min" | "max" | "mean";
|
||||||
|
|
||||||
export interface Statistics {
|
export interface Statistics {
|
||||||
[statisticId: string]: StatisticValue[];
|
[statisticId: string]: StatisticValue[];
|
||||||
@ -201,13 +201,10 @@ export const calculateStatisticsSumGrowth = (
|
|||||||
export const statisticsHaveType = (
|
export const statisticsHaveType = (
|
||||||
stats: StatisticValue[],
|
stats: StatisticValue[],
|
||||||
type: StatisticType
|
type: StatisticType
|
||||||
) => {
|
) => stats.some((stat) => stat[type] !== null);
|
||||||
type = type === "sum_rel" ? "sum" : type;
|
|
||||||
return stats.some((stat) => stat[type] !== null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const mean_stat_types: readonly StatisticType[] = ["mean", "min", "max"];
|
const mean_stat_types: readonly StatisticType[] = ["mean", "min", "max"];
|
||||||
const sum_stat_types: readonly StatisticType[] = ["sum", "sum_rel"];
|
const sum_stat_types: readonly StatisticType[] = ["sum"];
|
||||||
|
|
||||||
export const statisticsMetaHasType = (
|
export const statisticsMetaHasType = (
|
||||||
metadata: StatisticsMetaData,
|
metadata: StatisticsMetaData,
|
||||||
|
@ -36,6 +36,7 @@ import {
|
|||||||
statisticsMetaHasType,
|
statisticsMetaHasType,
|
||||||
} from "../../../../data/recorder";
|
} from "../../../../data/recorder";
|
||||||
import { deepEqual } from "../../../../common/util/deep-equal";
|
import { deepEqual } from "../../../../common/util/deep-equal";
|
||||||
|
import { statTypeMap } from "../../../../components/chart/statistics-chart";
|
||||||
|
|
||||||
const statTypeStruct = union([
|
const statTypeStruct = union([
|
||||||
literal("sum"),
|
literal("sum"),
|
||||||
@ -157,7 +158,7 @@ export class HuiStatisticsGraphCardEditor
|
|||||||
disabled:
|
disabled:
|
||||||
!metaDatas ||
|
!metaDatas ||
|
||||||
!metaDatas?.every((metaData) =>
|
!metaDatas?.every((metaData) =>
|
||||||
statisticsMetaHasType(metaData, stat_type)
|
statisticsMetaHasType(metaData, statTypeMap[stat_type])
|
||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user