|
3 | 3 |
|
4 | 4 | """Types for the Reporting API client.""" |
5 | 5 |
|
6 | | -from collections import namedtuple |
| 6 | +import math |
7 | 7 | from collections.abc import Iterable, Iterator |
8 | 8 | from dataclasses import dataclass |
9 | | -from datetime import timezone |
10 | | -from typing import Any |
| 9 | +from datetime import datetime, timezone |
| 10 | +from typing import Any, NamedTuple |
11 | 11 |
|
12 | 12 | # pylint: disable=no-name-in-module |
13 | 13 | from frequenz.api.reporting.v1.reporting_pb2 import ( |
|
23 | 23 | # pylint: enable=no-name-in-module |
24 | 24 | from frequenz.client.common.metric import Metric |
25 | 25 |
|
26 | | -MetricSample = namedtuple( |
27 | | - "MetricSample", ["timestamp", "microgrid_id", "component_id", "metric", "value"] |
28 | | -) |
29 | | -"""Type for a sample of a time series incl. metric type, microgrid and component ID |
30 | 26 |
|
31 | | -A named tuple was chosen to allow safe access to the fields while keeping the |
32 | | -simplicity of a tuple. This data type can be easily used to create a numpy array |
33 | | -or a pandas DataFrame. |
34 | | -""" |
| 27 | +class MetricSample(NamedTuple): |
| 28 | + """Type for a sample of a time series incl. metric type, microgrid and component ID. |
| 29 | +
|
| 30 | + A named tuple was chosen to allow safe access to the fields while keeping the |
| 31 | + simplicity of a tuple. This data type can be easily used to create a numpy array |
| 32 | + or a pandas DataFrame. |
| 33 | + """ |
| 34 | + |
| 35 | + timestamp: datetime |
| 36 | + microgrid_id: int |
| 37 | + component_id: str |
| 38 | + metric: str |
| 39 | + value: float |
35 | 40 |
|
36 | 41 |
|
37 | 42 | @dataclass(frozen=True) |
@@ -91,7 +96,7 @@ def __iter__(self) -> Iterator[MetricSample]: |
91 | 96 | value = ( |
92 | 97 | sample.value.simple_metric.value |
93 | 98 | if sample.value.HasField("simple_metric") |
94 | | - else None |
| 99 | + else math.nan |
95 | 100 | ) |
96 | 101 | yield MetricSample(ts, mid, cid, met, value) |
97 | 102 |
|
@@ -162,6 +167,6 @@ def sample(self) -> MetricSample: |
162 | 167 | ), |
163 | 168 | microgrid_id=self._data_pb.aggregation_config.microgrid_id, |
164 | 169 | component_id=self._data_pb.aggregation_config.aggregation_formula, |
165 | | - metric=self._data_pb.aggregation_config.metric, |
| 170 | + metric=Metric(self._data_pb.aggregation_config.metric).name, |
166 | 171 | value=self._data_pb.sample.sample.value, |
167 | 172 | ) |
0 commit comments