Skip to content

Commit

Permalink
Fix gap in ring buffer when updating missing values (#1034)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cwasicki authored Aug 9, 2024
2 parents c9115e0 + f8d6502 commit aa259ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## Bug Fixes

- Fix PV power distribution excluding inverters that haven't sent any data since the application started.
- Fixes a bug in the ring buffer in case the updated value is missing and creates a gap in time.
5 changes: 4 additions & 1 deletion src/frequenz/sdk/timeseries/_ringbuffer/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,11 @@ def _update_gaps(
# New missing entry that is not already in a gap?
if record_as_missing:
if not found_in_gaps:
# If there are no gaps and the new value is not subsequent to the
# newest value, we need to start the new gap after the newest value
start_gap = min(newest + self._sampling_period, timestamp)
self._gaps.append(
Gap(start=timestamp, end=timestamp + self._sampling_period)
Gap(start=start_gap, end=timestamp + self._sampling_period)
)
elif len(self._gaps) > 0:
if found_in_gaps:
Expand Down
10 changes: 5 additions & 5 deletions tests/timeseries/test_ringbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ def test_gaps() -> None: # pylint: disable=too-many-statements
assert buffer.count_covered() == 5
assert len(buffer.gaps) == 0

# whole range gap suffers from sdk#646
# whole range gap
buffer.update(Sample(dt(99), None))
assert buffer.oldest_timestamp == dt(95) # bug: should be None
assert buffer.newest_timestamp == dt(99) # bug: should be None
assert buffer.count_valid() == 4 # bug: should be 0 (whole range gap)
assert buffer.count_covered() == 5 # bug: should be 0
assert buffer.oldest_timestamp is None
assert buffer.newest_timestamp is None
assert buffer.count_valid() == 0
assert buffer.count_covered() == 0
assert len(buffer.gaps) == 1


Expand Down

0 comments on commit aa259ec

Please sign in to comment.