Skip to content

Commit 5dd9e8f

Browse files
authored
Fix aggregated values for missing data (#253)
If the samples in the protobuf of the aggregated response has the value not set (e.g. when the service sends None values), the generated python code falls back to zeroes when the field is accessed. This case needs to be handled explicitly by the client, which now sets these values to NaN.
2 parents 927f6f1 + 4d9fde6 commit 5dd9e8f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
## Summary
44

5-
> **Warning:** This client is compatible *only* with Reporting API `v1alpha10` or later.
6-
> Using with services that use an older API version **will cause failures**.
5+
<!-- Here goes a general summary of what this release is about -->
76

87
## Upgrading
98

10-
* Breaking change to reporting API v1alpha10.
11-
* Switch to new `metrics` package from client-common.
9+
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1210

1311
## New Features
1412

1513
<!-- Here goes the main new features and examples or instructions on how to use them -->
1614

1715
## Bug Fixes
1816

19-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
17+
* Fix default value of formula-aggregated metrics when no data was sent.

src/frequenz/client/reporting/_types.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Types for the Reporting API client."""
55

6+
import math
67
from collections.abc import Iterable, Iterator
78
from dataclasses import dataclass
89
from datetime import datetime
@@ -180,10 +181,25 @@ class AggregatedMetric:
180181

181182
def sample(self) -> MetricSample:
182183
"""Return the aggregated metric sample."""
183-
return MetricSample(
184-
timestamp=datetime_from_proto(self._data_pb.sample.sample_time),
185-
microgrid_id=self._data_pb.aggregation_config.microgrid_id,
186-
component_id=self._data_pb.aggregation_config.aggregation_formula,
187-
metric=Metric(self._data_pb.aggregation_config.metric).name,
188-
value=self._data_pb.sample.sample.value,
184+
config = self._data_pb.aggregation_config
185+
sample = self._data_pb.sample
186+
187+
timestamp = datetime_from_proto(sample.sample_time)
188+
microgrid_id = config.microgrid_id
189+
component_id = config.aggregation_formula
190+
metric = Metric(config.metric).name
191+
# Ignoring this verification results in
192+
# values of zero if the field is not set.
193+
if sample.HasField("sample") and sample.sample.HasField("value"):
194+
value = sample.sample.value
195+
else:
196+
value = math.nan
197+
198+
ret = MetricSample(
199+
timestamp=timestamp,
200+
microgrid_id=microgrid_id,
201+
component_id=component_id,
202+
metric=metric,
203+
value=value,
189204
)
205+
return ret

0 commit comments

Comments
 (0)