Skip to content

Commit 3ae0941

Browse files
committed
Replace TemperatureMetrics with Sample[Temperature]
This would just stream average of average temperatures of all batteries in a pool. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 25cd074 commit 3ae0941

File tree

4 files changed

+13
-39
lines changed

4 files changed

+13
-39
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Manage a pool of batteries."""
55

6-
from ._result_types import Bound, PowerMetrics, TemperatureMetrics
6+
from ._result_types import Bound, PowerMetrics
77
from .battery_pool import BatteryPool
88

99
__all__ = [
1010
"BatteryPool",
1111
"PowerMetrics",
12-
"TemperatureMetrics",
1312
"Bound",
1413
]

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ...microgrid.component import ComponentCategory, ComponentMetricId, InverterType
1515
from ...timeseries import Energy, Percentage, Sample, Temperature
1616
from ._component_metrics import ComponentMetricsData
17-
from ._result_types import Bound, PowerMetrics, TemperatureMetrics
17+
from ._result_types import Bound, PowerMetrics
1818

1919
_logger = logging.getLogger(__name__)
2020
_MIN_TIMESTAMP = datetime.min.replace(tzinfo=timezone.utc)
@@ -59,7 +59,7 @@ def battery_inverter_mapping(batteries: Iterable[int]) -> dict[int, int]:
5959

6060
# Formula output types class have no common interface
6161
# Print all possible types here.
62-
T = TypeVar("T", Sample[Percentage], Sample[Energy], PowerMetrics, TemperatureMetrics)
62+
T = TypeVar("T", Sample[Percentage], Sample[Energy], PowerMetrics, Sample[Temperature])
6363

6464

6565
class MetricCalculator(ABC, Generic[T]):
@@ -234,7 +234,7 @@ def calculate(
234234
)
235235

236236

237-
class TemperatureCalculator(MetricCalculator[TemperatureMetrics]):
237+
class TemperatureCalculator(MetricCalculator[Sample[Temperature]]):
238238
"""Define how to calculate temperature metrics."""
239239

240240
def __init__(self, batteries: Set[int]) -> None:
@@ -280,7 +280,7 @@ def calculate(
280280
self,
281281
metrics_data: dict[int, ComponentMetricsData],
282282
working_batteries: set[int],
283-
) -> TemperatureMetrics | None:
283+
) -> Sample[Temperature] | None:
284284
"""Aggregate the metrics_data and calculate high level metric for temperature.
285285
286286
Missing components will be ignored. Formula will be calculated for all
@@ -298,9 +298,7 @@ def calculate(
298298
Return None if there are no component metrics.
299299
"""
300300
timestamp = _MIN_TIMESTAMP
301-
temperature_sum: float = 0
302-
temperature_min: float = float("inf")
303-
temperature_max: float = float("-inf")
301+
temperature_sum: float = 0.0
304302
temperature_count: int = 0
305303
for battery_id in working_batteries:
306304
if battery_id not in metrics_data:
@@ -311,19 +309,15 @@ def calculate(
311309
continue
312310
timestamp = max(timestamp, metrics.timestamp)
313311
temperature_sum += temperature
314-
temperature_min = min(temperature_min, temperature)
315-
temperature_max = max(temperature_max, temperature)
316312
temperature_count += 1
317313
if timestamp == _MIN_TIMESTAMP:
318314
return None
319315

320316
temperature_avg = temperature_sum / temperature_count
321317

322-
return TemperatureMetrics(
318+
return Sample[Temperature](
323319
timestamp=timestamp,
324-
min=Temperature.from_celsius(value=temperature_min),
325-
avg=Temperature.from_celsius(value=temperature_avg),
326-
max=Temperature.from_celsius(value=temperature_max),
320+
value=Temperature.from_celsius(value=temperature_avg),
327321
)
328322

329323

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from dataclasses import dataclass, field
77
from datetime import datetime
88

9-
from ...timeseries import Temperature
10-
119

1210
@dataclass
1311
class Bound:
@@ -63,20 +61,3 @@ class PowerMetrics:
6361
)
6462
```
6563
"""
66-
67-
68-
@dataclass
69-
class TemperatureMetrics:
70-
"""Container for temperature metrics."""
71-
72-
timestamp: datetime
73-
"""Timestamp of the metrics."""
74-
75-
max: Temperature
76-
"""Maximum temperature of the collection of temperatures."""
77-
78-
min: Temperature
79-
"""Minimum temperature of the collection of temperatures."""
80-
81-
avg: Temperature
82-
"""Average temperature of the collection of temperatures."""

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
FormulaGeneratorConfig,
2828
FormulaType,
2929
)
30-
from .._quantities import Energy, Percentage, Power
30+
from .._quantities import Energy, Percentage, Power, Temperature
3131
from ._methods import MetricAggregator, SendOnUpdate
3232
from ._metric_calculator import (
3333
CapacityCalculator,
3434
PowerBoundsCalculator,
3535
SoCCalculator,
3636
TemperatureCalculator,
3737
)
38-
from ._result_types import PowerMetrics, TemperatureMetrics
38+
from ._result_types import PowerMetrics
3939

4040

4141
class BatteryPool:
@@ -388,11 +388,11 @@ def soc(self) -> MetricAggregator[Sample[Percentage]]:
388388
return self._active_methods[method_name]
389389

390390
@property
391-
def temperature(self) -> MetricAggregator[TemperatureMetrics]:
392-
"""Fetch the maximum temperature of the batteries in the pool.
391+
def temperature(self) -> MetricAggregator[Sample[Temperature]]:
392+
"""Fetch the average temperature of the batteries in the pool.
393393
394394
Returns:
395-
A MetricAggregator that will calculate and stream the maximum temperature
395+
A MetricAggregator that will calculate and stream the average temperature
396396
of all batteries in the pool.
397397
"""
398398
method_name = SendOnUpdate.name() + "_" + TemperatureCalculator.name()

0 commit comments

Comments
 (0)