Skip to content

Commit 5dd2288

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 1fe5cb8 commit 5dd2288

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
@@ -100,6 +100,8 @@ This release replaces the `@actor` decorator with a new `Actor` class.
100100

101101
- `Actor`: This new class inherits from `BackgroundService` and it replaces the `@actor` decorator.
102102

103+
- `MovingWindow`: Provide access to `capacity` (maximum number of elements).
104+
103105
## Bug Fixes
104106

105107
- Fixes a bug in the ring buffer updating the end timestamp of gaps when they are outdated.

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
@@ -127,8 +127,14 @@ async def test_window_size() -> None:
127127
"""Test the size of the window."""
128128
window, sender = init_moving_window(timedelta(seconds=5))
129129
async with window:
130-
await push_logical_meter_data(sender, range(0, 20))
131-
assert len(window) == 5
130+
assert window.capacity == 5, "Wrong window capacity"
131+
assert len(window) == 0, "Window should be empty"
132+
await push_logical_meter_data(sender, range(0, 2))
133+
assert window.capacity == 5, "Wrong window capacity"
134+
assert len(window) == 2, "Window should be partially full"
135+
await push_logical_meter_data(sender, range(2, 20))
136+
assert window.capacity == 5, "Wrong window capacity"
137+
assert len(window) == 5, "Window should be full"
132138

133139

134140
# pylint: disable=redefined-outer-name
@@ -148,6 +154,8 @@ async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
148154
input_sampling_period=input_sampling,
149155
resampler_config=resampler_config,
150156
) as window:
157+
assert window.capacity == window_size / output_sampling, "Wrong window capacity"
158+
assert len(window) == 0, "Window should be empty at the beginning"
151159
stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
152160
for value in stream_values:
153161
timestamp = datetime.now(tz=timezone.utc)

0 commit comments

Comments
 (0)