Skip to content

Commit 6a8336f

Browse files
committed
Fix aggregated values for None samples
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. Signed-off-by: cwasicki <[email protected]>
1 parent 4c9997e commit 6a8336f

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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)