Skip to content

Commit 0707542

Browse files
committed
Improve component graph's filtering
While we are changing the interface of component retrieval in the component graph, we take the opportunity to make its interface more generic. We now accept any Iterable for `matching_ids` and `matching_types`, as well as one individual items. Most uses of it filter by a single item anyway so it is annoying to have to wrap it in a Iterable. Most uses of those methods were updated to use the single-item overload. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 9c51730 commit 0707542

File tree

18 files changed

+47
-33
lines changed

18 files changed

+47
-33
lines changed

benchmarks/power_distribution/power_distributor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def run() -> None:
143143
)
144144

145145
all_batteries = connection_manager.get().component_graph.components(
146-
matching_types={Battery},
146+
matching_types=Battery
147147
)
148148
batteries_ids = {c.id for c in all_batteries}
149149
# Take some time to get data from components

src/frequenz/sdk/microgrid/_old_component_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _check_category(cls, component_id: ComponentId) -> None:
160160
from .. import microgrid
161161

162162
components = microgrid.connection_manager.get().component_graph.components(
163-
matching_ids={component_id}
163+
matching_ids=component_id
164164
)
165165
if not components:
166166
raise ValueError(f"Unable to find component with {component_id}")

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_battery_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __init__(
146146
self._results_sender = results_sender
147147
self._api_power_request_timeout = api_power_request_timeout
148148
self._batteries = connection_manager.get().component_graph.components(
149-
matching_types={Battery}
149+
matching_types=Battery
150150
)
151151
self._battery_ids = {battery.id for battery in self._batteries}
152152

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_ev_charger_manager/_ev_charger_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _get_ev_charger_ids(self) -> collections.abc.Set[ComponentId]:
112112
return {
113113
evc.id
114114
for evc in connection_manager.get().component_graph.components(
115-
matching_types={EvCharger}
115+
matching_types=EvCharger
116116
)
117117
}
118118

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_pv_inverter_manager/_pv_inverter_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,6 @@ def _get_pv_inverter_ids(self) -> collections.abc.Set[ComponentId]:
255255
return {
256256
inv.id
257257
for inv in connection_manager.get().component_graph.components(
258-
matching_types={SolarInverter}
258+
matching_types=SolarInverter
259259
)
260260
}

src/frequenz/sdk/microgrid/_power_wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _start_power_managing_actor(self) -> None:
8787
# Currently the power managing actor only supports batteries. The below
8888
# constraint needs to be relaxed if the actor is extended to support other
8989
# components.
90-
if not component_graph.components(matching_types={self._component_class}):
90+
if not component_graph.components(matching_types=self._component_class):
9191
_logger.warning(
9292
"No %s found in the component graph. "
9393
"The power managing actor will not be started.",
@@ -119,7 +119,7 @@ def _start_power_distributing_actor(self) -> None:
119119
return
120120

121121
component_graph = connection_manager.get().component_graph
122-
if not component_graph.components(matching_types={self._component_class}):
122+
if not component_graph.components(matching_types=self._component_class):
123123
_logger.warning(
124124
"No %s found in the component graph. "
125125
"The power distributing actor will not be started.",

src/frequenz/sdk/microgrid/component_graph.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import asyncio
2525
import logging
2626
from abc import ABC, abstractmethod
27-
from collections.abc import Callable, Iterable
27+
from collections.abc import Callable, Iterable, Set
2828

2929
import networkx as nx
3030
from frequenz.client.common.microgrid.components import ComponentId
@@ -66,8 +66,8 @@ class ComponentGraph(ABC):
6666
@abstractmethod
6767
def components(
6868
self,
69-
matching_ids: set[ComponentId] | None = None,
70-
matching_types: set[type[Component]] | None = None,
69+
matching_ids: Iterable[ComponentId] | ComponentId | None = None,
70+
matching_types: Iterable[type[Component]] | type[Component] | None = None,
7171
) -> set[Component]:
7272
"""Fetch the components of the microgrid.
7373
@@ -386,8 +386,8 @@ def __init__(
386386
@override
387387
def components(
388388
self,
389-
matching_ids: set[ComponentId] | None = None,
390-
matching_types: set[type[Component]] | None = None,
389+
matching_ids: Iterable[ComponentId] | ComponentId | None = None,
390+
matching_types: Iterable[type[Component]] | type[Component] | None = None,
391391
) -> set[Component]:
392392
"""Fetch the components of the microgrid.
393393
@@ -399,6 +399,22 @@ def components(
399399
The set of components currently connected to the microgrid, filtered by
400400
the provided `matching_ids` and `matching_types` values.
401401
"""
402+
match matching_ids:
403+
case ComponentId():
404+
matching_ids = {matching_ids}
405+
case Set():
406+
pass
407+
case Iterable():
408+
matching_ids = set(matching_ids)
409+
410+
match matching_types:
411+
case type():
412+
matching_types = {matching_types}
413+
case Set():
414+
pass
415+
case Iterable():
416+
matching_types = set(matching_types)
417+
402418
selection: Iterable[Component]
403419
selection_ids = (
404420
self._graph.nodes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _get_all_batteries(self) -> frozenset[ComponentId]:
142142
"""
143143
graph = connection_manager.get().component_graph
144144
return frozenset(
145-
battery.id for battery in graph.components(matching_types={Battery})
145+
battery.id for battery in graph.components(matching_types=Battery)
146146
)
147147

148148
async def _update_battery_status(

src/frequenz/sdk/timeseries/ev_charger_pool/_ev_charger_pool_reference_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__( # pylint: disable=too-many-arguments
7676
else:
7777
graph = connection_manager.get().component_graph
7878
self.component_ids = frozenset(
79-
{evc.id for evc in graph.components(matching_types={EvCharger})}
79+
{evc.id for evc in graph.components(matching_types=EvCharger)}
8080
)
8181

8282
self.power_bounds_subs: dict[str, asyncio.Task[None]] = {}

src/frequenz/sdk/timeseries/formula_engine/_formula_generators/_chp_power_formula.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _get_chp_meters(self) -> abc.Set[ComponentId]:
7171
FormulaGenerationError: If there's no dedicated meter attached to every CHP.
7272
"""
7373
component_graph = connection_manager.get().component_graph
74-
chps = component_graph.components(matching_types={Chp})
74+
chps = component_graph.components(matching_types=Chp)
7575

7676
chp_meters: set[ComponentId] = set()
7777
for chp in chps:

0 commit comments

Comments
 (0)