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 28, 2023
1 parent 6e113f4 commit b94c48b
Show file tree
Hide file tree
Showing 3 changed files with 48 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 @@ -100,7 +100,9 @@ This release replaces the `@actor` decorator with a new `Actor` class.

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

- `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 @@ -222,6 +222,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
19 changes: 19 additions & 0 deletions tests/timeseries/test_moving_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ 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))
async with window:
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 b94c48b

Please sign in to comment.