Skip to content

Commit

Permalink
Fix history charts refreshing with cached history
Browse files Browse the repository at this point in the history
Fixes #12859
  • Loading branch information
bdraco committed Jun 4, 2022
1 parent 4922e57 commit d823457
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/components/chart/state-history-chart-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../../common/number/format_number";
import { LineChartEntity, LineChartState } from "../../data/history";
import { HomeAssistant } from "../../types";
import "./ha-chart-base";
import { MIN_TIME_BETWEEN_UPDATES } from "./ha-chart-base";

const safeParseFloat = (value) => {
const parsed = parseFloat(value);
Expand All @@ -34,6 +34,8 @@ class StateHistoryChartLine extends LitElement {

@state() private _chartOptions?: ChartOptions;

private _chartTime: Date = new Date();

protected render() {
return html`
<ha-chart-base
Expand Down Expand Up @@ -121,7 +123,13 @@ class StateHistoryChartLine extends LitElement {
locale: numberFormatToLocale(this.hass.locale),
};
}
if (changedProps.has("data")) {
if (
changedProps.has("data") ||
this._chartTime <
new Date(this.endTime.getTime() - MIN_TIME_BETWEEN_UPDATES)
) {
// If the line is more than 5 minutes old, re-gen it
// so the X axis grows even if there is no new data
this._generateData();
}
}
Expand All @@ -135,6 +143,7 @@ class StateHistoryChartLine extends LitElement {
return;
}

this._chartTime = new Date();
const endTime = this.endTime;
const names = this.names || {};
entityStates.forEach((states) => {
Expand Down
14 changes: 13 additions & 1 deletion src/components/chart/state-history-chart-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { numberFormatToLocale } from "../../common/number/format_number";
import { computeRTL } from "../../common/util/compute_rtl";
import { TimelineEntity } from "../../data/history";
import { HomeAssistant } from "../../types";
import "./ha-chart-base";
import { MIN_TIME_BETWEEN_UPDATES } from "./ha-chart-base";
import type { TimeLineData } from "./timeline-chart/const";

/** Binary sensor device classes for which the static colors for on/off are NOT inverted.
Expand Down Expand Up @@ -103,6 +103,8 @@ export class StateHistoryChartTimeline extends LitElement {

@state() private _chartOptions?: ChartOptions<"timeline">;

private _chartTime: Date = new Date();

protected render() {
return html`
<ha-chart-base
Expand Down Expand Up @@ -213,6 +215,15 @@ export class StateHistoryChartTimeline extends LitElement {
}
if (changedProps.has("data")) {
this._generateData();
return;
}
if (
this._chartTime <
new Date(this.endTime.getTime() - MIN_TIME_BETWEEN_UPDATES)
) {
// If the line is more than 5 minutes old, re-gen it
// so the X axis grows even if there is no new data
this._generateData();
}
}

Expand All @@ -224,6 +235,7 @@ export class StateHistoryChartTimeline extends LitElement {
stateHistory = [];
}

this._chartTime = new Date();
const startTime = this.startTime;
const endTime = this.endTime;
const labels: string[] = [];
Expand Down
12 changes: 10 additions & 2 deletions src/data/cached-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ export const getRecentWithCache = (
}
const stateHistory = computeHistory(hass, fetchedHistory, localize);
if (appendingToCache) {
mergeLine(stateHistory.line, cache.data.line);
mergeTimeline(stateHistory.timeline, cache.data.timeline);
if (stateHistory.line.length) {
mergeLine(stateHistory.line, cache.data.line);
}
if (stateHistory.timeline.length) {
mergeTimeline(stateHistory.timeline, cache.data.timeline);
// Replace the timeline array to force an update
cache.data.timeline = [...cache.data.timeline];
}
pruneStartTime(startTime, cache.data);
} else {
cache.data = stateHistory;
Expand Down Expand Up @@ -191,6 +197,8 @@ const mergeLine = (
oldLine.data.push(entity);
}
});
// Replace the cached line data to force an update
oldLine.data = [...oldLine.data];
} else {
cacheLines.push(line);
}
Expand Down

0 comments on commit d823457

Please sign in to comment.