Skip to content

Commit 29c99be

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 a8f3d62 commit 29c99be

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
- A new class `Fuse` has been added to represent fuses. This class has a member variable `max_current` which represents the maximum current that can course through the fuse. If the current flowing through a fuse is greater than this limit, then the fuse will break the circuit.
2020

2121

22+
- `MovingWindow`: Provide access to `capacity` (maximum number of elements).
23+
2224
## Bug Fixes
2325

2426
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

src/frequenz/sdk/timeseries/_moving_window.py

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

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

tests/timeseries/test_moving_window.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,14 @@ async def test_window_size() -> None:
125125
"""Test the size of the window."""
126126
window, sender = init_moving_window(timedelta(seconds=5))
127127
async with window:
128-
await push_logical_meter_data(sender, range(0, 20))
129-
assert len(window) == 5
128+
assert window.capacity == 5, "Wrong window capacity"
129+
assert len(window) == 0, "Window should be empty"
130+
await push_logical_meter_data(sender, range(0, 2))
131+
assert window.capacity == 5, "Wrong window capacity"
132+
assert len(window) == 2, "Window should be partially full"
133+
await push_logical_meter_data(sender, range(2, 20))
134+
assert window.capacity == 5, "Wrong window capacity"
135+
assert len(window) == 5, "Window should be full"
130136

131137

132138
# pylint: disable=redefined-outer-name
@@ -146,6 +152,8 @@ async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
146152
input_sampling_period=input_sampling,
147153
resampler_config=resampler_config,
148154
) as window:
155+
assert window.capacity == window_size / output_sampling, "Wrong window capacity"
156+
assert len(window) == 0, "Window should be empty at the beginning"
149157
stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
150158
for value in stream_values:
151159
timestamp = datetime.now(tz=timezone.utc)

0 commit comments

Comments
 (0)