Skip to content

Commit 4596dbb

Browse files
committed
Make ComponentMetricsData a dataclass
This is mainly to get the nice `__str__` and `__repr__` methods for better debugging. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 18c1b56 commit 4596dbb

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

src/frequenz/sdk/timeseries/battery_pool/_component_metrics.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,24 @@
55

66

77
from collections.abc import Mapping
8+
from dataclasses import dataclass
89
from datetime import datetime
910

1011
from frequenz.client.microgrid import ComponentMetricId
1112

1213

14+
@dataclass(frozen=True, eq=False)
1315
class ComponentMetricsData:
1416
"""Store values of the component metrics."""
1517

16-
def __init__(
17-
self,
18-
component_id: int,
19-
timestamp: datetime,
20-
metrics: Mapping[ComponentMetricId, float],
21-
) -> None:
22-
"""Create class instance.
18+
component_id: int
19+
"""The component ID the data is for."""
2320

24-
Args:
25-
component_id: component id
26-
timestamp: timestamp the same for all metrics
27-
metrics: map between metrics and its values.
28-
"""
29-
self._component_id = component_id
30-
self._timestamp = timestamp
31-
self._metrics: Mapping[ComponentMetricId, float] = metrics
32-
33-
@property
34-
def component_id(self) -> int:
35-
"""Get component id of the given metrics.
21+
timestamp: datetime
22+
"""The timestamp for all the metrics."""
3623

37-
Returns:
38-
Component id
39-
"""
40-
return self._component_id
41-
42-
@property
43-
def timestamp(self) -> datetime:
44-
"""Get timestamp of the given metrics.
45-
46-
Returns:
47-
Timestamp (one for all metrics).
48-
"""
49-
return self._timestamp
24+
metrics: Mapping[ComponentMetricId, float]
25+
"""The values for each metric."""
5026

5127
def get(self, metric: ComponentMetricId) -> float | None:
5228
"""Get metric value.
@@ -57,7 +33,7 @@ def get(self, metric: ComponentMetricId) -> float | None:
5733
Returns:
5834
Value of the metric.
5935
"""
60-
return self._metrics.get(metric, None)
36+
return self.metrics.get(metric, None)
6137

6238
def __eq__(self, other: object) -> bool:
6339
"""Compare two objects of this class.
@@ -74,6 +50,4 @@ def __eq__(self, other: object) -> bool:
7450
if not isinstance(other, ComponentMetricsData):
7551
return False
7652

77-
return (
78-
self.component_id == other.component_id and self._metrics == other._metrics
79-
)
53+
return self.component_id == other.component_id and self.metrics == other.metrics

0 commit comments

Comments
 (0)