Skip to content

Commit

Permalink
fix: group_by was extending values into the future
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Feb 2, 2021
1 parent 1762049 commit 2cb79d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .devcontainer/ui-lovelace.yaml
Expand Up @@ -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
23 changes: 20 additions & 3 deletions src/graphEntry.ts
Expand Up @@ -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: [] };
});
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}

Expand Down

0 comments on commit 2cb79d2

Please sign in to comment.