Optimize purge of old live history data in the time window (#15153)

from https://github.com/home-assistant/frontend/pull/15112#discussion_r1083382923
This commit is contained in:
J. Nick Koston 2023-01-21 18:16:52 -10:00 committed by GitHub
parent f34d9c3d75
commit 3e14d825e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -263,20 +263,27 @@ class HistoryStream {
}
// Remove old history
if (entityId in this.combinedHistory) {
const entityHistory = newHistory[entityId];
while (entityHistory[0].lu < purgeBeforePythonTime) {
if (entityHistory.length > 1) {
if (entityHistory[1].lu < purgeBeforePythonTime) {
newHistory[entityId].shift();
continue;
}
}
// Update the first entry to the start time state
// as we need to preserve the start time state and
// only expire the rest of the history as it ages.
entityHistory[0].lu = purgeBeforePythonTime;
break;
const expiredStates = newHistory[entityId].filter(
(state) => state.lu < purgeBeforePythonTime
);
if (!expiredStates.length) {
continue;
}
newHistory[entityId] = newHistory[entityId].filter(
(state) => state.lu >= purgeBeforePythonTime
);
if (
newHistory[entityId].length &&
newHistory[entityId][0].lu === purgeBeforePythonTime
) {
continue;
}
// Update the first entry to the start time state
// as we need to preserve the start time state and
// only expire the rest of the history as it ages.
const lastExpiredState = expiredStates[expiredStates.length - 1];
lastExpiredState.lu = purgeBeforePythonTime;
newHistory[entityId].unshift(lastExpiredState);
}
}
this.combinedHistory = newHistory;