Add timeline entry when a long period of time passes (#8638)

This commit is contained in:
Paulus Schoutsen 2021-03-14 16:45:49 -07:00 committed by GitHub
parent a841e287e5
commit 03f694922d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 10 deletions

View File

@ -43,7 +43,8 @@ export class DemoAutomationTrace extends LitElement {
protected firstUpdated(changedProps) { protected firstUpdated(changedProps) {
super.firstUpdated(changedProps); super.firstUpdated(changedProps);
provideHass(this); const hass = provideHass(this);
hass.updateTranslations(null, "en");
} }
static get styles() { static get styles() {

View File

@ -19,16 +19,21 @@ import {
mdiCheckCircleOutline, mdiCheckCircleOutline,
mdiCircle, mdiCircle,
mdiCircleOutline, mdiCircleOutline,
mdiClockOutline,
mdiPauseCircleOutline, mdiPauseCircleOutline,
mdiRecordCircleOutline, mdiRecordCircleOutline,
mdiStopCircleOutline, mdiStopCircleOutline,
} from "@mdi/js"; } from "@mdi/js";
import { LogbookEntry } from "../../data/logbook"; import { LogbookEntry } from "../../data/logbook";
import { Action, describeAction } from "../../data/script"; import { Action, describeAction } from "../../data/script";
import relativeTime from "../../common/datetime/relative_time";
const LOGBOOK_ENTRIES_BEFORE_FOLD = 2;
const pathToName = (path: string) => path.split("/").join(" "); const pathToName = (path: string) => path.split("/").join(" ");
const LOGBOOK_ENTRIES_BEFORE_FOLD = 2; // Report time entry when more than this time has passed
const SIGNIFICANT_TIME_CHANGE = 5000; // 5 seconds
@customElement("hat-trace") @customElement("hat-trace")
export class HaAutomationTracer extends LitElement { export class HaAutomationTracer extends LitElement {
@ -77,6 +82,27 @@ export class HaAutomationTracer extends LitElement {
let logbookIndex = 0; let logbookIndex = 0;
let actionTraceIndex = 0; let actionTraceIndex = 0;
let lastReportedTime = new Date(this.trace.timestamp.start);
const maybeRenderTime = (nextItemTimestamp: Date) => {
if (
nextItemTimestamp.getTime() - lastReportedTime.getTime() <
SIGNIFICANT_TIME_CHANGE
) {
return;
}
entries.push(html`
<ha-timeline .icon=${mdiClockOutline}>
${relativeTime(lastReportedTime, this.hass.localize, {
compareTime: nextItemTimestamp,
includeTense: false,
})}
later
</ha-timeline>
`);
lastReportedTime = nextItemTimestamp;
};
let groupedLogbookItems: LogbookEntry[] = []; let groupedLogbookItems: LogbookEntry[] = [];
@ -98,15 +124,16 @@ export class HaAutomationTracer extends LitElement {
// Find next item time-wise. // Find next item time-wise.
const logbookItem = this.logbookEntries[logbookIndex]; const logbookItem = this.logbookEntries[logbookIndex];
const actionTrace = actionTraces[actionTraceIndex]; const actionTrace = actionTraces[actionTraceIndex];
const actionTimestamp = new Date(actionTrace[1][0].timestamp);
if ( if (new Date(logbookItem.when) > actionTimestamp) {
new Date(logbookItem.when) > new Date(actionTrace[1][0].timestamp)
) {
actionTraceIndex++; actionTraceIndex++;
if (groupedLogbookItems.length > 0) { if (groupedLogbookItems.length > 0) {
maybeRenderTime(new Date(groupedLogbookItems[0].when));
entries.push(this._renderLogbookEntries(groupedLogbookItems)); entries.push(this._renderLogbookEntries(groupedLogbookItems));
groupedLogbookItems = []; groupedLogbookItems = [];
} }
maybeRenderTime(actionTimestamp);
entries.push(this._renderActionTrace(...actionTrace)); entries.push(this._renderActionTrace(...actionTrace));
} else { } else {
logbookIndex++; logbookIndex++;
@ -120,13 +147,14 @@ export class HaAutomationTracer extends LitElement {
} }
if (groupedLogbookItems.length > 0) { if (groupedLogbookItems.length > 0) {
maybeRenderTime(new Date(groupedLogbookItems[0].when));
entries.push(this._renderLogbookEntries(groupedLogbookItems)); entries.push(this._renderLogbookEntries(groupedLogbookItems));
} }
while (actionTraceIndex < actionTraces.length) { while (actionTraceIndex < actionTraces.length) {
entries.push( const trace = actionTraces[actionTraceIndex];
this._renderActionTrace(...actionTraces[actionTraceIndex]) maybeRenderTime(new Date(trace[1][0].timestamp));
); entries.push(this._renderActionTrace(...trace));
actionTraceIndex++; actionTraceIndex++;
} }
} }
@ -148,8 +176,8 @@ export class HaAutomationTracer extends LitElement {
)} )}
(runtime: (runtime:
${( ${(
(Number(new Date(this.trace.timestamp.finish!)) - (new Date(this.trace.timestamp.finish!).getTime() -
Number(new Date(this.trace.timestamp.start))) / new Date(this.trace.timestamp.start).getTime()) /
1000 1000
).toFixed(2)} ).toFixed(2)}
seconds)` seconds)`