Skip to content

Commit 3cc23db

Browse files
committed
Expose count_covered in moving window
Signed-off-by: cwasicki <[email protected]>
1 parent 1a7a24d commit 3cc23db

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ def count_valid(self) -> int:
315315
"""
316316
return self._buffer.count_valid()
317317

318+
@property
319+
def count_covered(self) -> int:
320+
"""Count the number of samples that are covered by the oldest and newest valid samples.
321+
322+
Returns:
323+
The count of samples between the oldest and newest (inclusive) valid samples
324+
or 0 if there are is no time range covered.
325+
"""
326+
return self._buffer.count_covered
327+
318328
@overload
319329
def __getitem__(self, key: SupportsIndex) -> float:
320330
"""See the main __getitem__ method.

tests/timeseries/test_moving_window.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ async def test_window_size() -> None:
144144
async with window:
145145
assert window.capacity == 5, "Wrong window capacity"
146146
assert window.count_valid() == 0, "Window should be empty"
147+
assert window.count_covered == 0, "Window should be empty"
147148
await push_logical_meter_data(sender, range(0, 2))
148149
assert window.capacity == 5, "Wrong window capacity"
149150
assert window.count_valid() == 2, "Window should be partially full"
151+
assert window.count_covered == 2, "Window should be partially full"
150152
await push_logical_meter_data(sender, range(2, 20))
151153
assert window.capacity == 5, "Wrong window capacity"
152154
assert window.count_valid() == 5, "Window should be full"
155+
assert window.count_covered == 5, "Window should be full"
153156

154157

155158
# pylint: disable=redefined-outer-name

0 commit comments

Comments
 (0)