Skip to content

Commit

Permalink
Add capacity property to moving window
Browse files Browse the repository at this point in the history
The capacity is the maximum number of values that the moving window can
hold.

Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com>
  • Loading branch information
cwasicki committed Aug 23, 2023
1 parent cac5473 commit ab07b5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/frequenz/sdk/timeseries/_moving_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ def sampling_period(self) -> timedelta:
"""
return self._sampling_period

@property
def capacity(self) -> int:
"""
Return the capacity of the MovingWindow.
Capacity is the maximum number of samples that can be stored in the
MovingWindow.
Returns:
The capacity of the MovingWindow.
"""
return self._buffer.maxlen

async def _run_impl(self) -> None:
"""Awaits samples from the receiver and updates the underlying ring buffer.
Expand Down
7 changes: 6 additions & 1 deletion tests/timeseries/test_moving_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ async def test_access_empty_window() -> None:
async def test_window_size() -> None:
"""Test the size of the window."""
window, sender = init_moving_window(timedelta(seconds=5))
assert window.capacity == 5, "Wrong window capacity"
assert len(window) == 0, "Window should be empty"
await push_logical_meter_data(sender, range(0, 20))
assert len(window) == 5
assert len(window) == 5, "Window should be full"


# pylint: disable=redefined-outer-name
Expand All @@ -143,6 +145,9 @@ async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
resampler_config=resampler_config,
)

assert window.capacity == window_size / output_sampling, "Wrong window capacity"
assert len(window) == 0, "Window should be empty at the beginning"

stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
for value in stream_values:
timestamp = datetime.now(tz=timezone.utc)
Expand Down

0 comments on commit ab07b5a

Please sign in to comment.