Merge branch 'streaming_history' into streaming_history_hui-graph-header-footer

This commit is contained in:
J. Nick Koston 2023-01-21 17:11:25 -10:00
commit 72605051e4
2 changed files with 16 additions and 12 deletions

View File

@ -231,37 +231,38 @@ class HistoryStream {
const purgeBeforePythonTime = const purgeBeforePythonTime =
(new Date().getTime() - 60 * 60 * this.hoursToShow * 1000) / 1000; (new Date().getTime() - 60 * 60 * this.hoursToShow * 1000) / 1000;
const newHistory: HistoryStates = {}; const newHistory: HistoryStates = {};
Object.keys(this.combinedHistory).forEach((entityId) => { for (const entityId of Object.keys(this.combinedHistory)) {
newHistory[entityId] = []; newHistory[entityId] = [];
}); }
Object.keys(streamMessage.states).forEach((entityId) => { for (const entityId of Object.keys(streamMessage.states)) {
newHistory[entityId] = []; newHistory[entityId] = [];
}); }
Object.keys(newHistory).forEach((entityId) => { for (const entityId of Object.keys(newHistory)) {
let purgeOld = false;
if ( if (
entityId in this.combinedHistory && entityId in this.combinedHistory &&
entityId in streamMessage.states entityId in streamMessage.states
) { ) {
newHistory[entityId] = this.combinedHistory[entityId].concat( const entityCombinedHistory = this.combinedHistory[entityId];
const lastEntityCombinedHistory =
entityCombinedHistory[entityCombinedHistory.length - 1];
newHistory[entityId] = entityCombinedHistory.concat(
streamMessage.states[entityId] streamMessage.states[entityId]
); );
if ( if (
streamMessage.states[entityId][0] > this.combinedHistory[entityId][-1] streamMessage.states[entityId][0].lu < lastEntityCombinedHistory.lu
) { ) {
// If the history is out of order we have to sort it. // If the history is out of order we have to sort it.
newHistory[entityId] = newHistory[entityId].sort( newHistory[entityId] = newHistory[entityId].sort(
(a, b) => a.lu - b.lu (a, b) => a.lu - b.lu
); );
} }
purgeOld = true;
} else if (entityId in this.combinedHistory) { } else if (entityId in this.combinedHistory) {
newHistory[entityId] = this.combinedHistory[entityId]; newHistory[entityId] = this.combinedHistory[entityId];
purgeOld = true;
} else { } else {
newHistory[entityId] = streamMessage.states[entityId]; newHistory[entityId] = streamMessage.states[entityId];
} }
if (purgeOld) { // Remove old history
if (entityId in this.combinedHistory) {
const entityHistory = newHistory[entityId]; const entityHistory = newHistory[entityId];
while (entityHistory[0].lu < purgeBeforePythonTime) { while (entityHistory[0].lu < purgeBeforePythonTime) {
if (entityHistory.length > 1) { if (entityHistory.length > 1) {
@ -277,7 +278,7 @@ class HistoryStream {
break; break;
} }
} }
}); }
this.combinedHistory = newHistory; this.combinedHistory = newHistory;
return this.combinedHistory; return this.combinedHistory;
} }

View File

@ -164,6 +164,9 @@ export class MoreInfoHistory extends LitElement {
if (!isComponentLoaded(this.hass, "history") || this._subscribed) { if (!isComponentLoaded(this.hass, "history") || this._subscribed) {
return; return;
} }
if (this._subscribed) {
this._unsubscribeHistoryTimeWindow();
}
this._subscribed = subscribeHistoryStatesTimeWindow( this._subscribed = subscribeHistoryStatesTimeWindow(
this.hass!, this.hass!,
(combinedHistory) => { (combinedHistory) => {