mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-26 14:27:20 +00:00
Adjust logbook stream consumer to handle new metadata (#12755)
This commit is contained in:
parent
49cfde1fe7
commit
3acab5a39c
@ -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
|
||||
);
|
||||
};
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user