Skip to content

Commit f8d6502

Browse files
committed
Fix gap in ring buffer when updating a missing value
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. Signed-off-by: cwasicki <[email protected]>
1 parent 841fea8 commit f8d6502

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Frequenz Python SDK Release Notes
2+
3+
## Bug Fixes
4+
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)