From 2cb79d2bc9f341fff1f03a61c334fd9212e2e4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Wiedemann?= Date: Tue, 2 Feb 2021 22:14:46 +0000 Subject: [PATCH] fix: `group_by` was extending values into the future --- .devcontainer/ui-lovelace.yaml | 17 +++++++++++++++++ src/graphEntry.ts | 23 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.devcontainer/ui-lovelace.yaml b/.devcontainer/ui-lovelace.yaml index ad4378e..6af9f62 100644 --- a/.devcontainer/ui-lovelace.yaml +++ b/.devcontainer/ui-lovelace.yaml @@ -366,3 +366,20 @@ views: series: - entity: light.kitchen_lights attribute: brightness + + - type: custom:apexcharts-card + header: + show: true + title: shouldn't extend to end with group_by + span: + start: day + series: + - entity: sensor.random0_100 + group_by: + duration: 1h + func: last + - entity: sensor.random0_100 + extend_to_end: false + group_by: + duration: 1h + func: last diff --git a/src/graphEntry.ts b/src/graphEntry.ts index 7da64fb..d6789f4 100644 --- a/src/graphEntry.ts +++ b/src/graphEntry.ts @@ -288,7 +288,7 @@ export default class GraphEntry { private _dataBucketer(): HistoryBuckets { const ranges = Array.from(this._timeRange.reverseBy('milliseconds', { step: this._groupByDurationMs })).reverse(); // const res: EntityCachePoints[] = [[]]; - const buckets: HistoryBuckets = []; + let buckets: HistoryBuckets = []; ranges.forEach((range, index) => { buckets[index] = { timestamp: range.valueOf(), data: [] }; }); @@ -316,11 +316,12 @@ export default class GraphEntry { }); }); let lastNonNullBucketValue: number | null = null; + const now = new Date().getTime(); buckets.forEach((bucket) => { if (bucket.data.length === 0) { - if (this._config.group_by.fill === 'last') { + if (this._config.group_by.fill === 'last' && bucket.timestamp <= now) { bucket.data[0] = [bucket.timestamp, lastNonNullBucketValue]; - } else if (this._config.group_by.fill === 'zero') { + } else if (this._config.group_by.fill === 'zero' && bucket.timestamp <= now) { bucket.data[0] = [bucket.timestamp, 0]; } else if (this._config.group_by.fill === 'null') { bucket.data[0] = [bucket.timestamp, null]; @@ -330,6 +331,22 @@ export default class GraphEntry { } }); buckets.pop(); + // Could probably do better than double reverse... + // This is to stip any value at the end which is empty or null + // to make extend_to_end work with buckets + if ( + (buckets.length > 0 && buckets[buckets.length - 1].data.length === 0) || + (buckets[buckets.length - 1].data.length > 0 && + buckets[buckets.length - 1].data[buckets[buckets.length - 1].data.length - 1][1] === null) + ) + buckets = buckets + .reverse() + .flatMap((bucket) => { + if (bucket.data[1] === null) return []; + if (bucket.data.length === 0) return []; + else return [bucket]; + }) + .reverse(); return buckets; }