Skip to content

Commit 2600fca

Browse files
committed
Add newest timestamp to moving window
Method to retrieve the timestamp of the newest sample in the moving window. Signed-off-by: cwasicki <[email protected]>
1 parent db7f40f commit 2600fca

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +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).
22+
- `MovingWindow`: Provide access to `capacity` (maximum number of elements)
23+
and `newest_timestamp` of the window.
2324

2425
## Bug Fixes
2526

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ def capacity(self) -> int:
222222
"""
223223
return self._buffer.maxlen
224224

225+
@property
226+
def newest_timestamp(self) -> datetime | None:
227+
"""
228+
Return the newest timestamp in the MovingWindow.
229+
230+
Returns:
231+
The newest timestamp in the MovingWindow.
232+
If the MovingWindow is empty, None is returned.
233+
"""
234+
if len(self._buffer) == 0:
235+
return None
236+
return self._buffer.time_bound_newest
237+
225238
async def _run_impl(self) -> None:
226239
"""Awaits samples from the receiver and updates the underlying ring buffer.
227240

tests/timeseries/test_moving_window.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ async def test_window_size() -> None:
135135
assert len(window) == 5, "Window should be full"
136136

137137

138+
async def test_timestamps() -> None:
139+
"""Test the newest_timestamp method of the window."""
140+
window, sender = init_moving_window(timedelta(seconds=5))
141+
async with window:
142+
assert (
143+
window.newest_timestamp is None
144+
), "For an empty window, newest timestamp should be None"
145+
await push_logical_meter_data(sender, range(0, 20))
146+
assert window.newest_timestamp == UNIX_EPOCH + timedelta(
147+
seconds=19
148+
), "Wrong newest timestamp"
149+
150+
138151
# pylint: disable=redefined-outer-name
139152
async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
140153
"""Test resampling in MovingWindow."""

0 commit comments

Comments
 (0)