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"; const LOGBOOK_LOCALIZE_PATH = "ui.components.logbook.messages";
export const CONTINUOUS_DOMAINS = ["proximity", "sensor"]; 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 { export interface LogbookEntry {
// Base data // Base data
when: number; // Python timestamp. Do *1000 to get JS timestamp. when: number; // Python timestamp. Do *1000 to get JS timestamp.
@ -163,7 +170,7 @@ const getLogbookDataFromServer = (
export const subscribeLogbook = ( export const subscribeLogbook = (
hass: HomeAssistant, hass: HomeAssistant,
callbackFunction: (message: LogbookEntry[]) => void, callbackFunction: (message: LogbookStreamMessage) => void,
startDate: string, startDate: string,
endDate: string, endDate: string,
entityIds?: string[], entityIds?: string[],
@ -188,8 +195,8 @@ export const subscribeLogbook = (
if (deviceIds?.length) { if (deviceIds?.length) {
params.device_ids = deviceIds; params.device_ids = deviceIds;
} }
return hass.connection.subscribeMessage<LogbookEntry[]>( return hass.connection.subscribeMessage<LogbookStreamMessage>(
(message?) => callbackFunction(message), (message) => callbackFunction(message),
params params
); );
}; };

View File

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