|
5 | 5 |
|
6 | 6 | from collections.abc import Iterable, Iterator |
7 | 7 | from dataclasses import dataclass |
8 | | -from datetime import datetime, timezone |
| 8 | +from datetime import datetime |
9 | 9 | from typing import Any, NamedTuple |
10 | 10 |
|
11 | 11 | # pylint: disable=no-name-in-module |
|
21 | 21 |
|
22 | 22 | # pylint: enable=no-name-in-module |
23 | 23 | from frequenz.client.common.metrics import Metric |
| 24 | +from frequenz.client.common.proto import datetime_from_proto, enum_from_proto |
24 | 25 |
|
25 | 26 |
|
26 | 27 | class MetricSample(NamedTuple): |
@@ -92,8 +93,8 @@ def __iter__(self) -> Iterator[MetricSample]: |
92 | 93 | for item in items: |
93 | 94 | cid = getattr(item, self.id_attr) |
94 | 95 | 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 |
97 | 98 |
|
98 | 99 | # Handle simple_metric |
99 | 100 | if sample.value.HasField("simple_metric"): |
@@ -127,7 +128,7 @@ def __iter__(self) -> Iterator[MetricSample]: |
127 | 128 | ) |
128 | 129 |
|
129 | 130 | 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) |
131 | 132 | for category, category_items in { |
132 | 133 | "state": getattr(state, "states", []), |
133 | 134 | "warning": getattr(state, "warnings", []), |
@@ -180,9 +181,7 @@ class AggregatedMetric: |
180 | 181 | def sample(self) -> MetricSample: |
181 | 182 | """Return the aggregated metric sample.""" |
182 | 183 | 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), |
186 | 185 | microgrid_id=self._data_pb.aggregation_config.microgrid_id, |
187 | 186 | component_id=self._data_pb.aggregation_config.aggregation_formula, |
188 | 187 | metric=Metric(self._data_pb.aggregation_config.metric).name, |
|
0 commit comments