Skip to content

Commit b194674

Browse files
committed
Return MetricAggregator instances from BatteryPool metrics methods
This makes their interface to be more consistent with the power and current methods. soc_recv = battery_pool.soc.new_receiver() instead of: soc_recv = battery_pool.soc() Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 0cdd6b2 commit b194674

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

examples/battery_pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ async def main() -> None:
3333

3434
battery_pool = microgrid.battery_pool()
3535
receivers: Dict[str, Receiver[Any]] = {
36-
"soc": await battery_pool.soc(maxsize=1),
37-
"capacity": await battery_pool.capacity(maxsize=1),
38-
"power_bounds": await battery_pool.power_bounds(maxsize=1),
36+
"soc": battery_pool.soc.new_receiver(maxsize=1),
37+
"capacity": battery_pool.capacity.new_receiver(maxsize=1),
38+
"power_bounds": battery_pool.power_bounds.new_receiver(maxsize=1),
3939
}
4040

4141
merged_channel = MergeNamed[Any](**receivers)

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from frequenz.channels import Receiver, Sender
1515

1616
from ..._internal._asyncio import cancel_and_await
17-
from ..._internal._constants import RECEIVER_MAX_SIZE
1817
from ...actor import ChannelRegistry, ComponentMetricRequest
1918
from ...actor.power_distributing._battery_pool_status import BatteryStatus
2019
from ...microgrid import connection_manager
@@ -184,19 +183,19 @@ def consumption_power(self) -> FormulaEngine:
184183
assert isinstance(engine, FormulaEngine)
185184
return engine
186185

187-
async def soc(
188-
self, maxsize: int | None = RECEIVER_MAX_SIZE
189-
) -> Receiver[SoCMetrics | None]:
186+
@property
187+
def soc(self) -> MetricAggregator[SoCMetrics]:
190188
"""Get receiver to receive new soc metrics when they change.
191189
192-
Soc formulas are described in the receiver return type.
193-
None will be send if there is no component to calculate metric.
190+
Soc formulas are described in the receiver return type. None will be send if
191+
there is no component to calculate metric.
194192
195-
Args:
196-
maxsize: Maxsize of the receiver channel.
193+
A receiver from the MetricAggregator can be obtained by calling the
194+
`new_receiver` method.
197195
198196
Returns:
199-
Receiver for this metric.
197+
A MetricAggregator that will calculate and stream the aggregate soc of
198+
all batteries in the pool.
200199
"""
201200
method_name = SendOnUpdate.name() + "_" + SoCCalculator.name()
202201

@@ -208,22 +207,21 @@ async def soc(
208207
min_update_interval=self._min_update_interval,
209208
)
210209

211-
running_method = self._active_methods[method_name]
212-
return running_method.new_receiver(maxsize)
210+
return self._active_methods[method_name]
213211

214-
async def capacity(
215-
self, maxsize: int | None = RECEIVER_MAX_SIZE
216-
) -> Receiver[CapacityMetrics | None]:
212+
@property
213+
def capacity(self) -> MetricAggregator[CapacityMetrics]:
217214
"""Get receiver to receive new capacity metrics when they change.
218215
219-
Capacity formulas are described in the receiver return type.
220-
None will be send if there is no component to calculate metrics.
216+
Capacity formulas are described in the receiver return type. None will be send
217+
if there is no component to calculate metrics.
221218
222-
Args:
223-
maxsize: Maxsize of the receiver channel.
219+
A receiver from the MetricAggregator can be obtained by calling the
220+
`new_receiver` method.
224221
225222
Returns:
226-
Receiver for this metric.
223+
A MetricAggregator that will calculate and stream the capacity of all
224+
batteries in the pool.
227225
"""
228226
method_name = SendOnUpdate.name() + "_" + CapacityCalculator.name()
229227

@@ -235,22 +233,21 @@ async def capacity(
235233
min_update_interval=self._min_update_interval,
236234
)
237235

238-
running_method = self._active_methods[method_name]
239-
return running_method.new_receiver(maxsize)
236+
return self._active_methods[method_name]
240237

241-
async def power_bounds(
242-
self, maxsize: int | None = RECEIVER_MAX_SIZE
243-
) -> Receiver[PowerMetrics | None]:
238+
@property
239+
def power_bounds(self) -> MetricAggregator[PowerMetrics]:
244240
"""Get receiver to receive new power bounds when they change.
245241
246242
Power bounds formulas are described in the receiver return type.
247243
None will be send if there is no component to calculate metrics.
248244
249-
Args:
250-
maxsize: Maxsize of the receivers channel.
245+
A receiver from the MetricAggregator can be obtained by calling the
246+
`new_receiver` method.
251247
252248
Returns:
253-
Receiver for this metric.
249+
A MetricAggregator that will calculate and stream the power bounds
250+
of all batteries in the pool.
254251
"""
255252
method_name = SendOnUpdate.name() + "_" + PowerBoundsCalculator.name()
256253

@@ -262,8 +259,7 @@ async def power_bounds(
262259
min_update_interval=self._min_update_interval,
263260
)
264261

265-
running_method = self._active_methods[method_name]
266-
return running_method.new_receiver(maxsize)
262+
return self._active_methods[method_name]
267263

268264
async def stop(self) -> None:
269265
"""Stop all pending async tasks."""

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ async def run_capacity_test(setup_args: SetupArgs) -> None:
500500
sampling_rate=0.05,
501501
)
502502

503-
capacity_receiver = await battery_pool.capacity(maxsize=50)
503+
capacity_receiver = battery_pool.capacity.new_receiver(maxsize=50)
504504

505505
# First metrics delivers slower because of the startup delay in the pool.
506506
msg = await asyncio.wait_for(
@@ -633,7 +633,7 @@ async def run_soc_test(setup_args: SetupArgs) -> None:
633633
sampling_rate=0.05,
634634
)
635635

636-
receiver = await battery_pool.soc(maxsize=50)
636+
receiver = battery_pool.soc.new_receiver(maxsize=50)
637637

638638
# First metrics delivers slower because of the startup delay in the pool.
639639
msg = await asyncio.wait_for(
@@ -775,7 +775,7 @@ async def run_power_bounds_test( # pylint: disable=too-many-locals
775775
sampling_rate=0.1,
776776
)
777777

778-
receiver = await battery_pool.power_bounds(maxsize=50)
778+
receiver = battery_pool.power_bounds.new_receiver(maxsize=50)
779779

780780
# First metrics delivers slower because of the startup delay in the pool.
781781
msg = await asyncio.wait_for(

0 commit comments

Comments
 (0)