Use new logbook streaming websocket api for cases where we need end_time (#12753)

This commit is contained in:
J. Nick Koston 2022-05-23 17:40:05 -05:00 committed by GitHub
parent a02b817d7f
commit 067c2fdfa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 45 deletions

View File

@ -165,6 +165,7 @@ export const subscribeLogbook = (
hass: HomeAssistant, hass: HomeAssistant,
callbackFunction: (message: LogbookEntry[]) => void, callbackFunction: (message: LogbookEntry[]) => void,
startDate: string, startDate: string,
endDate: string,
entityIds?: string[], entityIds?: string[],
deviceIds?: string[] deviceIds?: string[]
): Promise<UnsubscribeFunc> => { ): Promise<UnsubscribeFunc> => {
@ -179,6 +180,7 @@ export const subscribeLogbook = (
const params: any = { const params: any = {
type: "logbook/event_stream", type: "logbook/event_stream",
start_time: startDate, start_time: startDate,
end_time: endDate,
}; };
if (entityIds?.length) { if (entityIds?.length) {
params.entity_ids = entityIds; params.entity_ids = entityIds;

View File

@ -8,7 +8,6 @@ import { throttle } from "../../common/util/throttle";
import "../../components/ha-circular-progress"; import "../../components/ha-circular-progress";
import { import {
clearLogbookCache, clearLogbookCache,
getLogbookData,
LogbookEntry, LogbookEntry,
subscribeLogbook, subscribeLogbook,
} from "../../data/logbook"; } from "../../data/logbook";
@ -79,9 +78,7 @@ export class HaLogbook extends LitElement {
@state() private _error?: string; @state() private _error?: string;
private _renderId = 1; private _subscribed?: Promise<UnsubscribeFunc | void>;
private _subscribed?: Promise<UnsubscribeFunc>;
private _throttleGetLogbookEntries = throttle( private _throttleGetLogbookEntries = throttle(
() => this._getLogBookData(), () => this._getLogBookData(),
@ -196,7 +193,7 @@ export class HaLogbook extends LitElement {
private _unsubscribe(): void { private _unsubscribe(): void {
if (this._subscribed) { if (this._subscribed) {
this._subscribed.then((unsub) => unsub()); this._subscribed.then((unsub) => (unsub ? unsub() : undefined));
this._subscribed = undefined; this._subscribed = undefined;
} }
} }
@ -236,7 +233,8 @@ export class HaLogbook extends LitElement {
return <LogbookTimePeriod>{ return <LogbookTimePeriod>{
now: now, now: now,
startTime: new Date(purgeBeforePythonTime * 1000), startTime: new Date(purgeBeforePythonTime * 1000),
endTime: now, // end streaming one year from now
endTime: new Date(now.getTime() + 86400 * 365 * 1000),
purgeBeforePythonTime: findStartOfRecentTime(now, this.time.recent), purgeBeforePythonTime: findStartOfRecentTime(now, this.time.recent),
}; };
} }
@ -244,9 +242,6 @@ export class HaLogbook extends LitElement {
} }
private _subscribeLogbookPeriod(logbookPeriod: LogbookTimePeriod) { private _subscribeLogbookPeriod(logbookPeriod: LogbookTimePeriod) {
if (logbookPeriod.endTime < logbookPeriod.now) {
return false;
}
if (this._subscribed) { if (this._subscribed) {
return true; return true;
} }
@ -265,15 +260,17 @@ export class HaLogbook extends LitElement {
} }
}, },
logbookPeriod.startTime.toISOString(), logbookPeriod.startTime.toISOString(),
logbookPeriod.endTime.toISOString(),
ensureArray(this.entityIds), ensureArray(this.entityIds),
ensureArray(this.deviceIds) ensureArray(this.deviceIds)
); ).catch((err) => {
this._error = err.message;
this._subscribed = undefined;
});
return true; return true;
} }
private async _getLogBookData() { private async _getLogBookData() {
this._renderId += 1;
const renderId = this._renderId;
this._error = undefined; this._error = undefined;
if (this._filterAlwaysEmptyResults) { if (this._filterAlwaysEmptyResults) {
@ -294,39 +291,7 @@ export class HaLogbook extends LitElement {
this._updateTraceContexts(); this._updateTraceContexts();
} }
if (this._subscribeLogbookPeriod(logbookPeriod)) { this._subscribeLogbookPeriod(logbookPeriod);
// We can go live
return;
}
// We are only fetching in the past
// with a time window that does not
// extend into the future
this._unsubscribe();
let newEntries: LogbookEntry[];
try {
newEntries = await getLogbookData(
this.hass,
logbookPeriod.startTime.toISOString(),
logbookPeriod.endTime.toISOString(),
ensureArray(this.entityIds),
ensureArray(this.deviceIds)
);
} catch (err: any) {
if (renderId === this._renderId) {
this._error = err.message;
}
return;
}
// New render happening.
if (renderId !== this._renderId) {
return;
}
this._logbookEntries = [...newEntries].reverse();
} }
private _nonExpiredRecords = (purgeBeforePythonTime: number | undefined) => private _nonExpiredRecords = (purgeBeforePythonTime: number | undefined) =>