Skip to content

Commit ab07b5a

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 ab07b5a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ 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+
assert window.capacity == 5, "Wrong window capacity"
125+
assert len(window) == 0, "Window should be empty"
124126
await push_logical_meter_data(sender, range(0, 20))
125-
assert len(window) == 5
127+
assert len(window) == 5, "Window should be full"
126128

127129

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

148+
assert window.capacity == window_size / output_sampling, "Wrong window capacity"
149+
assert len(window) == 0, "Window should be empty at the beginning"
150+
146151
stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
147152
for value in stream_values:
148153
timestamp = datetime.now(tz=timezone.utc)

0 commit comments

Comments
 (0)