Skip to content

Commit aa259ec

Browse files
authored
Fix gap in ring buffer when updating missing values (#1034)
If there are no gaps in the ring buffer and the new value is missing and creates a gap in time, we need to start the new gap after the latest value from before. Fixes #646.
2 parents c9115e0 + f8d6502 commit aa259ec

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
## Bug Fixes
44

5-
- Fix PV power distribution excluding inverters that haven't sent any data since the application started.
5+
- Fixes a bug in the ring buffer in case the updated value is missing and creates a gap in time.

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,11 @@ def _update_gaps(
491491
# New missing entry that is not already in a gap?
492492
if record_as_missing:
493493
if not found_in_gaps:
494+
# If there are no gaps and the new value is not subsequent to the
495+
# newest value, we need to start the new gap after the newest value
496+
start_gap = min(newest + self._sampling_period, timestamp)
494497
self._gaps.append(
495-
Gap(start=timestamp, end=timestamp + self._sampling_period)
498+
Gap(start=start_gap, end=timestamp + self._sampling_period)
496499
)
497500
elif len(self._gaps) > 0:
498501
if found_in_gaps:

tests/timeseries/test_ringbuffer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ def test_gaps() -> None: # pylint: disable=too-many-statements
277277
assert buffer.count_covered() == 5
278278
assert len(buffer.gaps) == 0
279279

280-
# whole range gap suffers from sdk#646
280+
# whole range gap
281281
buffer.update(Sample(dt(99), None))
282-
assert buffer.oldest_timestamp == dt(95) # bug: should be None
283-
assert buffer.newest_timestamp == dt(99) # bug: should be None
284-
assert buffer.count_valid() == 4 # bug: should be 0 (whole range gap)
285-
assert buffer.count_covered() == 5 # bug: should be 0
282+
assert buffer.oldest_timestamp is None
283+
assert buffer.newest_timestamp is None
284+
assert buffer.count_valid() == 0
285+
assert buffer.count_covered() == 0
286286
assert len(buffer.gaps) == 1
287287

288288

0 commit comments

Comments
 (0)