Skip to content

Commit f492944

Browse files
committed
Use from_proto functions from client common
Common conversion helpers might help with future API version updates. Signed-off-by: cwasicki <[email protected]>
1 parent 9687614 commit f492944

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/frequenz/client/reporting/_types.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from collections.abc import Iterable, Iterator
77
from dataclasses import dataclass
8-
from datetime import datetime, timezone
8+
from datetime import datetime
99
from typing import Any, NamedTuple
1010

1111
# pylint: disable=no-name-in-module
@@ -21,6 +21,7 @@
2121

2222
# pylint: enable=no-name-in-module
2323
from frequenz.client.common.metrics import Metric
24+
from frequenz.client.common.proto import datetime_from_proto, enum_from_proto
2425

2526

2627
class MetricSample(NamedTuple):
@@ -92,8 +93,8 @@ def __iter__(self) -> Iterator[MetricSample]:
9293
for item in items:
9394
cid = getattr(item, self.id_attr)
9495
for sample in getattr(item, "metric_samples", []):
95-
ts = sample.sample_time.ToDatetime().replace(tzinfo=timezone.utc)
96-
met = Metric(sample.metric).name
96+
ts = datetime_from_proto(sample.sample_time)
97+
met = enum_from_proto(sample.metric, Metric, allow_invalid=False).name
9798

9899
# Handle simple_metric
99100
if sample.value.HasField("simple_metric"):
@@ -127,7 +128,7 @@ def __iter__(self) -> Iterator[MetricSample]:
127128
)
128129

129130
for state in getattr(item, "state_snapshots", []):
130-
ts = state.origin_time.ToDatetime().replace(tzinfo=timezone.utc)
131+
ts = datetime_from_proto(state.origin_time)
131132
for category, category_items in {
132133
"state": getattr(state, "states", []),
133134
"warning": getattr(state, "warnings", []),
@@ -180,9 +181,7 @@ class AggregatedMetric:
180181
def sample(self) -> MetricSample:
181182
"""Return the aggregated metric sample."""
182183
return MetricSample(
183-
timestamp=self._data_pb.sample.sample_time.ToDatetime().replace(
184-
tzinfo=timezone.utc
185-
),
184+
timestamp=datetime_from_proto(self._data_pb.sample.sample_time),
186185
microgrid_id=self._data_pb.aggregation_config.microgrid_id,
187186
component_id=self._data_pb.aggregation_config.aggregation_formula,
188187
metric=Metric(self._data_pb.aggregation_config.metric).name,

0 commit comments

Comments
 (0)