Skip to content

Commit 1f59c20

Browse files
committed
Switch to a strongly-typed MetricSample class
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 0dec700 commit 1f59c20

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/frequenz/client/reporting/_types.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

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

6-
from collections import namedtuple
6+
import math
77
from collections.abc import Iterable, Iterator
88
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
1111

1212
# pylint: disable=no-name-in-module
1313
from frequenz.api.reporting.v1.reporting_pb2 import (
@@ -23,15 +23,20 @@
2323
# pylint: enable=no-name-in-module
2424
from frequenz.client.common.metric import Metric
2525

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
3026

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
3540

3641

3742
@dataclass(frozen=True)
@@ -91,7 +96,7 @@ def __iter__(self) -> Iterator[MetricSample]:
9196
value = (
9297
sample.value.simple_metric.value
9398
if sample.value.HasField("simple_metric")
94-
else None
99+
else math.nan
95100
)
96101
yield MetricSample(ts, mid, cid, met, value)
97102

@@ -162,6 +167,6 @@ def sample(self) -> MetricSample:
162167
),
163168
microgrid_id=self._data_pb.aggregation_config.microgrid_id,
164169
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,
166171
value=self._data_pb.sample.sample.value,
167172
)

0 commit comments

Comments
 (0)