Skip to content

Commit

Permalink
Log when no relevant samples are found for resampling
Browse files Browse the repository at this point in the history
These additional logs will help in understanding
why certain microgrid components, such as meters,
fail to provide relevant samples for resampling.

Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
  • Loading branch information
daniel-zullo-frequenz committed Apr 10, 2024
1 parent 6637d7f commit 5a29f4d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion benchmarks/timeseries/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _do_work() -> None:
for _n_sample in range(samples):
now = now + timedelta(seconds=1 / samples)
helper.add_sample(Sample(now, Quantity(0.0)))
helper.resample(now)
helper.resample(now, helper._name) # pylint: disable=protected-access

print(timeit(_do_work, number=5))

Expand Down
12 changes: 10 additions & 2 deletions src/frequenz/sdk/timeseries/_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,13 @@ def _update_buffer_len(self) -> bool:

return True

def resample(self, timestamp: datetime) -> Sample[Quantity]:
def resample(self, timestamp: datetime, component_name: str) -> Sample[Quantity]:
"""Generate a new sample based on all the current *relevant* samples.
Args:
timestamp: The timestamp to be used to calculate the new sample.
component_name: The name of the component that is being resampled
(for debugging purposes).
Returns:
A new sample generated by calling the resampling function with all
Expand Down Expand Up @@ -745,6 +747,11 @@ def resample(self, timestamp: datetime) -> Sample[Quantity]:
# So if we need more performance beyond this point, we probably need to
# resort to some C (or similar) implementation.
relevant_samples = list(itertools.islice(self._buffer, min_index, max_index))
if not relevant_samples:
_logger.warning(
"No relevant samples found for component: %s",
component_name,
)
value = (
conf.resampling_function(relevant_samples, conf, props)
if relevant_samples
Expand Down Expand Up @@ -826,4 +833,5 @@ async def resample(self, timestamp: datetime) -> None:
raise recv_exception
raise SourceStoppedError(self._source)

await self._sink(self._helper.resample(timestamp))
# pylint: disable=protected-access
await self._sink(self._helper.resample(timestamp, self._helper._name))
4 changes: 3 additions & 1 deletion tests/timeseries/test_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ async def test_helper_buffer_too_big(
helper.add_sample(sample)
await _advance_time(fake_time, 1)

_ = helper.resample(datetime.now(timezone.utc))
_ = helper.resample(
datetime.now(timezone.utc), helper._name # pylint: disable=protected-access
)
# Ignore errors produced by wrongly finalized gRPC server in unrelated tests
assert (
"frequenz.sdk.timeseries._resampling",
Expand Down

0 comments on commit 5a29f4d

Please sign in to comment.