Skip to content

Commit

Permalink
Add oldest and newest timestamps to moving window
Browse files Browse the repository at this point in the history
Methods to retrieve the timestamps of the oldest and newest sample in
the moving window.

Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com>
  • Loading branch information
cwasicki committed Aug 24, 2023
1 parent 22baf1d commit 39bfa84
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
## New Features

- DFS for compentent graph
- `MovingWindow`: Provide access to `capacity` (maximum number of elements).
- `MovingWindow`: Provide access to `capacity` (maximum number of elements),
`oldest_timestamp` and `newest_timestamp` of the window. The names in the
`OrderedRingBuffer` have been changed to match these names.

## Bug Fixes

Expand Down
26 changes: 26 additions & 0 deletions src/frequenz/sdk/timeseries/_moving_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ def capacity(self) -> int:
"""
return self._buffer.maxlen

@property
def oldest_timestamp(self) -> datetime | None:
"""
Return the oldest timestamp in the MovingWindow.
Returns:
The oldest timestamp in the MovingWindow.
If the MovingWindow is empty, None is returned.
"""
if len(self._buffer) == 0:
return None
return self._buffer.oldest_timestamp

@property
def newest_timestamp(self) -> datetime | None:
"""
Return the newest timestamp in the MovingWindow.
Returns:
The newest timestamp in the MovingWindow.
If the MovingWindow is empty, None is returned.
"""
if len(self._buffer) == 0:
return None
return self._buffer.newest_timestamp

async def _run_impl(self) -> None:
"""Awaits samples from the receiver and updates the underlying ring buffer.
Expand Down
18 changes: 18 additions & 0 deletions tests/timeseries/test_moving_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ async def test_window_size() -> None:
assert len(window) == 5, "Window should be full"


async def test_timestamps() -> None:
"""Test the timestamp methods of the window."""
window, sender = init_moving_window(timedelta(seconds=5))
assert (
window.oldest_timestamp is None
), "For an empty window, oldest timestamp should be None"
assert (
window.newest_timestamp is None
), "For an empty window, newest timestamp should be None"
await push_logical_meter_data(sender, range(0, 20))
assert window.oldest_timestamp == UNIX_EPOCH + timedelta(
seconds=15
), "Wrong oldest timestamp"
assert window.newest_timestamp == UNIX_EPOCH + timedelta(
seconds=19
), "Wrong newest timestamp"


# pylint: disable=redefined-outer-name
async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
"""Test resampling in MovingWindow."""
Expand Down

0 comments on commit 39bfa84

Please sign in to comment.