Skip to content

Commit 8b41bbf

Browse files
committed
Add capacity property to moving window
The capacity is the maximum number of values that the moving window can hold. Signed-off-by: cwasicki <[email protected]>
1 parent cac5473 commit 8b41bbf

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
- `MovingWindow`: Provide access to `capacity` (maximum number of elements).
1414

1515
## Bug Fixes
1616

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ def sampling_period(self) -> timedelta:
208208
"""
209209
return self._sampling_period
210210

211+
@property
212+
def capacity(self) -> int:
213+
"""
214+
Return the capacity of the MovingWindow.
215+
216+
Capacity is the maximum number of samples that can be stored in the
217+
MovingWindow.
218+
219+
Returns:
220+
The capacity of the MovingWindow.
221+
"""
222+
return self._buffer.maxlen
223+
211224
async def _run_impl(self) -> None:
212225
"""Awaits samples from the receiver and updates the underlying ring buffer.
213226

tests/timeseries/test_moving_window.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ async def test_access_empty_window() -> None:
121121
async def test_window_size() -> None:
122122
"""Test the size of the window."""
123123
window, sender = init_moving_window(timedelta(seconds=5))
124-
await push_logical_meter_data(sender, range(0, 20))
125-
assert len(window) == 5
124+
assert window.capacity == 5, "Wrong window capacity"
125+
assert len(window) == 0, "Window should be empty"
126+
await push_logical_meter_data(sender, range(0, 2))
127+
assert window.capacity == 5, "Wrong window capacity"
128+
assert len(window) == 2, "Window should be partially full"
129+
await push_logical_meter_data(sender, range(2, 20))
130+
assert window.capacity == 5, "Wrong window capacity"
131+
assert len(window) == 5, "Window should be full"
126132

127133

128134
# pylint: disable=redefined-outer-name
@@ -143,6 +149,9 @@ async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
143149
resampler_config=resampler_config,
144150
)
145151

152+
assert window.capacity == window_size / output_sampling, "Wrong window capacity"
153+
assert len(window) == 0, "Window should be empty at the beginning"
154+
146155
stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
147156
for value in stream_values:
148157
timestamp = datetime.now(tz=timezone.utc)

0 commit comments

Comments
 (0)