Adjust logbook stream consumer to handle new metadata (#12755)

This commit is contained in:
J. Nick Koston 2022-05-24 00:37:45 -05:00 committed by GitHub
parent 49cfde1fe7
commit 3acab5a39c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 17 deletions

View File

@ -13,6 +13,13 @@ import { UNAVAILABLE_STATES } from "./entity";
const LOGBOOK_LOCALIZE_PATH = "ui.components.logbook.messages";
export const CONTINUOUS_DOMAINS = ["proximity", "sensor"];
export interface LogbookStreamMessage {
events: LogbookEntry[];
start_time?: number; // Start time of this historical chunk
end_time?: number; // End time of this historical chunk
partial?: boolean; // Indiciates more historical chunks are coming
}
export interface LogbookEntry {
// Base data
when: number; // Python timestamp. Do *1000 to get JS timestamp.
@ -163,7 +170,7 @@ const getLogbookDataFromServer = (
export const subscribeLogbook = (
hass: HomeAssistant,
callbackFunction: (message: LogbookEntry[]) => void,
callbackFunction: (message: LogbookStreamMessage) => void,
startDate: string,
endDate: string,
entityIds?: string[],
@ -188,8 +195,8 @@ export const subscribeLogbook = (
if (deviceIds?.length) {
params.device_ids = deviceIds;
}
return hass.connection.subscribeMessage<LogbookEntry[]>(
(message?) => callbackFunction(message),
return hass.connection.subscribeMessage<LogbookStreamMessage>(
(message) => callbackFunction(message),
params
);
};

View File

@ -9,6 +9,7 @@ import "../../components/ha-circular-progress";
import {
clearLogbookCache,
LogbookEntry,
LogbookStreamMessage,
subscribeLogbook,
} from "../../data/logbook";
import { loadTraceContexts, TraceContexts } from "../../data/trace";
@ -247,17 +248,16 @@ export class HaLogbook extends LitElement {
}
this._subscribed = subscribeLogbook(
this.hass,
(newEntries?) => {
if ("recent" in this.time) {
// start time is a sliding window purge old ones
this._processNewEntries(
newEntries,
findStartOfRecentTime(new Date(), this.time.recent)
);
} else if ("range" in this.time) {
// start time is fixed, we can just append
this._processNewEntries(newEntries, undefined);
}
(streamMessage) => {
// "recent" means start time is a sliding window
// so we need to calculate an expireTime to
// purge old events
this._processStreamMessage(
streamMessage,
"recent" in this.time
? findStartOfRecentTime(new Date(), this.time.recent)
: undefined
);
},
logbookPeriod.startTime.toISOString(),
logbookPeriod.endTime.toISOString(),
@ -303,17 +303,22 @@ export class HaLogbook extends LitElement {
)
: this._logbookEntries;
private _processNewEntries = (
newEntries: LogbookEntry[],
private _processStreamMessage = (
streamMessage: LogbookStreamMessage,
purgeBeforePythonTime: number | undefined
) => {
// Put newest ones on top. Reverse works in-place so
// make a copy first.
newEntries = [...newEntries].reverse();
const newEntries = [...streamMessage.events].reverse();
if (!this._logbookEntries) {
this._logbookEntries = newEntries;
return;
}
if (!newEntries.length) {
// Empty messages are still sent to
// indicate no more historical events
return;
}
const nonExpiredRecords = this._nonExpiredRecords(purgeBeforePythonTime);
this._logbookEntries =
newEntries[0].when >= this._logbookEntries[0].when