|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Class that stores values of the component metrics.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from collections.abc import Mapping |
| 9 | +from datetime import datetime |
| 10 | + |
| 11 | +from ...microgrid.component import ComponentMetricId |
| 12 | + |
| 13 | + |
| 14 | +class ComponentMetricsData: |
| 15 | + """Store values of the component metrics.""" |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + component_id: int, |
| 20 | + timestamp: datetime, |
| 21 | + metrics: Mapping[ComponentMetricId, float], |
| 22 | + ) -> None: |
| 23 | + """Create class instance. |
| 24 | +
|
| 25 | + Args: |
| 26 | + component_id: component id |
| 27 | + timestamp: timestamp the same for all metrics |
| 28 | + metrics: map between metrics and its values. |
| 29 | + """ |
| 30 | + self._component_id = component_id |
| 31 | + self._timestamp = timestamp |
| 32 | + self._metrics: Mapping[ComponentMetricId, float] = metrics |
| 33 | + |
| 34 | + @property |
| 35 | + def component_id(self) -> int: |
| 36 | + """Get component id of the given metrics. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Component id |
| 40 | + """ |
| 41 | + return self._component_id |
| 42 | + |
| 43 | + @property |
| 44 | + def timestamp(self) -> datetime: |
| 45 | + """Get timestamp of the given metrics. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + Timestamp (one for all metrics). |
| 49 | + """ |
| 50 | + return self._timestamp |
| 51 | + |
| 52 | + def get(self, metric: ComponentMetricId) -> float | None: |
| 53 | + """Get metric value. |
| 54 | +
|
| 55 | + Args: |
| 56 | + metric: metric id |
| 57 | +
|
| 58 | + Raises: |
| 59 | + KeyError: If given metric is not stored in the object. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + Value of the metric. |
| 63 | + """ |
| 64 | + return self._metrics.get(metric, None) |
| 65 | + |
| 66 | + def __eq__(self, other: object) -> bool: |
| 67 | + """Compare two objects of this class. |
| 68 | +
|
| 69 | + Object are considered as equal if all stored values except for timestamp |
| 70 | + are equal. |
| 71 | +
|
| 72 | + Args: |
| 73 | + other: object to compare. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + True if two objects are equal, false otherwise. |
| 77 | + """ |
| 78 | + if not isinstance(other, ComponentMetricsData): |
| 79 | + return False |
| 80 | + |
| 81 | + return ( |
| 82 | + self.component_id == other.component_id and self._metrics == other._metrics |
| 83 | + ) |
0 commit comments