Skip to content

Commit 678c78a

Browse files
committed
Replace FormulaEngine with the new Formula
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent edd8617 commit 678c78a

File tree

18 files changed

+222
-224
lines changed

18 files changed

+222
-224
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717

1818
from ... import timeseries
1919
from ..._internal._channels import MappingReceiverFetcher, ReceiverFetcher
20-
from ...microgrid import _power_distributing, _power_managing
20+
from ...microgrid import _power_distributing, _power_managing, connection_manager
2121
from ...timeseries import Sample
2222
from .._base_types import SystemBounds
23-
from ..formula_engine import FormulaEngine
24-
from ..formula_engine._formula_generators import (
25-
BatteryPowerFormula,
26-
FormulaGeneratorConfig,
27-
)
23+
from ..formulas._formula import Formula
2824
from ._battery_pool_reference_store import BatteryPoolReferenceStore
2925
from ._methods import SendOnUpdate
3026
from ._metric_calculator import (
@@ -195,7 +191,7 @@ def component_ids(self) -> abc.Set[ComponentId]:
195191
return self._pool_ref_store._batteries
196192

197193
@property
198-
def power(self) -> FormulaEngine[Power]:
194+
def power(self) -> Formula[Power]:
199195
"""Fetch the total power of the batteries in the pool.
200196
201197
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -210,15 +206,12 @@ def power(self) -> FormulaEngine[Power]:
210206
A FormulaEngine that will calculate and stream the total power of all
211207
batteries in the pool.
212208
"""
213-
engine = self._pool_ref_store._formula_pool.from_power_formula_generator(
209+
engine = self._pool_ref_store._formula_pool.from_power_formula(
214210
"battery_pool_power",
215-
BatteryPowerFormula,
216-
FormulaGeneratorConfig(
217-
component_ids=self._pool_ref_store._batteries,
218-
allow_fallback=True,
211+
connection_manager.get().component_graph.battery_formula(
212+
self._pool_ref_store._batteries
219213
),
220214
)
221-
assert isinstance(engine, FormulaEngine)
222215
return engine
223216

224217
@property

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ...microgrid._power_distributing import Result
2222
from ...microgrid._power_distributing._component_status import ComponentPoolStatus
2323
from ...microgrid._power_managing._base_classes import Proposal, ReportRequest
24-
from ..formula_engine._formula_engine_pool import FormulaEnginePool
24+
from ..formulas._formula_pool import FormulaPool
2525
from ._methods import MetricAggregator
2626

2727

@@ -117,7 +117,7 @@ def __init__( # pylint: disable=too-many-arguments
117117
self._power_dist_results_fetcher: ReceiverFetcher[Result] = (
118118
power_distribution_results_fetcher
119119
)
120-
self._formula_pool: FormulaEnginePool = FormulaEnginePool(
120+
self._formula_pool: FormulaPool = FormulaPool(
121121
self._namespace,
122122
self._channel_registry,
123123
resampler_subscription_sender,

src/frequenz/sdk/timeseries/consumer.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from frequenz.quantities import Power
1010

1111
from .._internal._channels import ChannelRegistry
12+
from ..microgrid import connection_manager
1213
from ..microgrid._data_sourcing import ComponentMetricRequest
13-
from .formula_engine import FormulaEngine
14-
from .formula_engine._formula_engine_pool import FormulaEnginePool
15-
from .formula_engine._formula_generators import ConsumerPowerFormula
14+
from .formulas._formula import Formula
15+
from .formulas._formula_pool import FormulaPool
1616

1717

1818
class Consumer:
@@ -52,7 +52,7 @@ class Consumer:
5252
```
5353
"""
5454

55-
_formula_pool: FormulaEnginePool
55+
_formula_pool: FormulaPool
5656
"""The formula engine pool to generate consumer metrics."""
5757

5858
def __init__(
@@ -67,14 +67,14 @@ def __init__(
6767
resampler_subscription_sender: The sender to use for resampler subscriptions.
6868
"""
6969
namespace = f"consumer-{uuid.uuid4()}"
70-
self._formula_pool = FormulaEnginePool(
70+
self._formula_pool = FormulaPool(
7171
namespace,
7272
channel_registry,
7373
resampler_subscription_sender,
7474
)
7575

7676
@property
77-
def power(self) -> FormulaEngine[Power]:
77+
def power(self) -> Formula[Power]:
7878
"""Fetch the consumer power for the microgrid.
7979
8080
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -88,12 +88,10 @@ def power(self) -> FormulaEngine[Power]:
8888
Returns:
8989
A FormulaEngine that will calculate and stream consumer power.
9090
"""
91-
engine = self._formula_pool.from_power_formula_generator(
91+
return self._formula_pool.from_power_formula(
9292
"consumer_power",
93-
ConsumerPowerFormula,
93+
connection_manager.get().component_graph.consumer_formula(),
9494
)
95-
assert isinstance(engine, FormulaEngine)
96-
return engine
9795

9896
async def stop(self) -> None:
9997
"""Stop all formula engines."""

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
from frequenz.quantities import Current, Power
1313

1414
from ..._internal._channels import MappingReceiverFetcher, ReceiverFetcher
15-
from ...microgrid import _power_distributing, _power_managing
15+
from ...microgrid import _power_distributing, _power_managing, connection_manager
1616
from ...timeseries import Bounds
1717
from .._base_types import SystemBounds
18-
from ..formula_engine import FormulaEngine, FormulaEngine3Phase
19-
from ..formula_engine._formula_generators import (
20-
EVChargerCurrentFormula,
21-
EVChargerPowerFormula,
22-
FormulaGeneratorConfig,
23-
)
18+
from ..formulas._formula import Formula
19+
from ..formulas._formula_3_phase import Formula3Phase
2420
from ._ev_charger_pool_reference_store import EVChargerPoolReferenceStore
2521
from ._result_types import EVChargerPoolReport
2622

@@ -118,7 +114,7 @@ def component_ids(self) -> abc.Set[ComponentId]:
118114
return self._pool_ref_store.component_ids
119115

120116
@property
121-
def current_per_phase(self) -> FormulaEngine3Phase[Current]:
117+
def current_per_phase(self) -> Formula3Phase[Current]:
122118
"""Fetch the total current for the EV Chargers in the pool.
123119
124120
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -133,20 +129,15 @@ def current_per_phase(self) -> FormulaEngine3Phase[Current]:
133129
A FormulaEngine that will calculate and stream the total current of all EV
134130
Chargers.
135131
"""
136-
engine = (
137-
self._pool_ref_store.formula_pool.from_3_phase_current_formula_generator(
138-
"ev_charger_total_current",
139-
EVChargerCurrentFormula,
140-
FormulaGeneratorConfig(
141-
component_ids=self._pool_ref_store.component_ids
142-
),
143-
)
132+
return self._pool_ref_store.formula_pool.from_current_3_phase_formula(
133+
"ev_charger_total_current",
134+
connection_manager.get().component_graph.ev_charger_formula(
135+
self._pool_ref_store.component_ids
136+
),
144137
)
145-
assert isinstance(engine, FormulaEngine3Phase)
146-
return engine
147138

148139
@property
149-
def power(self) -> FormulaEngine[Power]:
140+
def power(self) -> Formula[Power]:
150141
"""Fetch the total power for the EV Chargers in the pool.
151142
152143
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -161,15 +152,12 @@ def power(self) -> FormulaEngine[Power]:
161152
A FormulaEngine that will calculate and stream the total power of all EV
162153
Chargers.
163154
"""
164-
engine = self._pool_ref_store.formula_pool.from_power_formula_generator(
155+
return self._pool_ref_store.formula_pool.from_power_formula(
165156
"ev_charger_power",
166-
EVChargerPowerFormula,
167-
FormulaGeneratorConfig(
168-
component_ids=self._pool_ref_store.component_ids,
157+
connection_manager.get().component_graph.ev_charger_formula(
158+
self._pool_ref_store.component_ids
169159
),
170160
)
171-
assert isinstance(engine, FormulaEngine)
172-
return engine
173161

174162
@property
175163
def power_status(self) -> ReceiverFetcher[EVChargerPoolReport]:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ...microgrid._power_distributing import ComponentPoolStatus, Result
1818
from ...microgrid._power_managing._base_classes import Proposal, ReportRequest
1919
from .._base_types import SystemBounds
20-
from ..formula_engine._formula_engine_pool import FormulaEnginePool
20+
from ..formulas._formula_pool import FormulaPool
2121
from ._system_bounds_tracker import EVCSystemBoundsTracker
2222

2323

@@ -82,7 +82,7 @@ def __init__( # pylint: disable=too-many-arguments
8282
self.power_bounds_subs: dict[str, asyncio.Task[None]] = {}
8383

8484
self.namespace: str = f"ev-charger-pool-{uuid.uuid4()}"
85-
self.formula_pool = FormulaEnginePool(
85+
self.formula_pool = FormulaPool(
8686
self.namespace,
8787
self.channel_registry,
8888
self.resampler_subscription_sender,

src/frequenz/sdk/timeseries/grid.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@
1414

1515
from frequenz.channels import Sender
1616
from frequenz.client.microgrid.component import GridConnectionPoint
17-
from frequenz.client.microgrid.metrics import Metric
1817
from frequenz.quantities import Current, Power, ReactivePower
1918

2019
from .._internal._channels import ChannelRegistry
2120
from ..microgrid import connection_manager
2221
from ..microgrid._data_sourcing import ComponentMetricRequest
2322
from ._fuse import Fuse
24-
from .formula_engine import FormulaEngine, FormulaEngine3Phase
25-
from .formula_engine._formula_engine_pool import FormulaEnginePool
26-
from .formula_engine._formula_generators import (
27-
GridCurrentFormula,
28-
GridPower3PhaseFormula,
29-
GridPowerFormula,
30-
GridReactivePowerFormula,
31-
)
23+
from .formulas._formula import Formula
24+
from .formulas._formula_3_phase import Formula3Phase
25+
from .formulas._formula_pool import FormulaPool
3226

3327
_logger = logging.getLogger(__name__)
3428

@@ -72,11 +66,11 @@ class Grid:
7266
lacks information about the fuse.
7367
"""
7468

75-
_formula_pool: FormulaEnginePool
69+
_formula_pool: FormulaPool
7670
"""The formula engine pool to generate grid metrics."""
7771

7872
@property
79-
def power(self) -> FormulaEngine[Power]:
73+
def power(self) -> Formula[Power]:
8074
"""Fetch the grid power for the microgrid.
8175
8276
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -90,15 +84,13 @@ def power(self) -> FormulaEngine[Power]:
9084
Returns:
9185
A FormulaEngine that will calculate and stream grid power.
9286
"""
93-
engine = self._formula_pool.from_power_formula_generator(
87+
return self._formula_pool.from_power_formula(
9488
"grid_power",
95-
GridPowerFormula,
89+
connection_manager.get().component_graph.grid_formula(),
9690
)
97-
assert isinstance(engine, FormulaEngine)
98-
return engine
9991

10092
@property
101-
def reactive_power(self) -> FormulaEngine[ReactivePower]:
93+
def reactive_power(self) -> Formula[ReactivePower]:
10294
"""Fetch the grid reactive power for the microgrid.
10395
10496
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -112,15 +104,13 @@ def reactive_power(self) -> FormulaEngine[ReactivePower]:
112104
Returns:
113105
A FormulaEngine that will calculate and stream grid reactive power.
114106
"""
115-
engine = self._formula_pool.from_reactive_power_formula_generator(
116-
f"grid-{Metric.AC_REACTIVE_POWER.value}",
117-
GridReactivePowerFormula,
107+
return self._formula_pool.from_reactive_power_formula(
108+
"grid_reactive_power",
109+
connection_manager.get().component_graph.grid_formula(),
118110
)
119-
assert isinstance(engine, FormulaEngine)
120-
return engine
121111

122112
@property
123-
def _power_per_phase(self) -> FormulaEngine3Phase[Power]:
113+
def _power_per_phase(self) -> Formula3Phase[Power]:
124114
"""Fetch the per-phase grid power for the microgrid.
125115
126116
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -131,14 +121,13 @@ def _power_per_phase(self) -> FormulaEngine3Phase[Power]:
131121
Returns:
132122
A FormulaEngine that will calculate and stream grid 3-phase power.
133123
"""
134-
engine = self._formula_pool.from_power_3_phase_formula_generator(
135-
"grid_power_3_phase", GridPower3PhaseFormula
124+
return self._formula_pool.from_power_3_phase_formula(
125+
"grid_power_3_phase",
126+
connection_manager.get().component_graph.grid_formula(),
136127
)
137-
assert isinstance(engine, FormulaEngine3Phase)
138-
return engine
139128

140129
@property
141-
def current_per_phase(self) -> FormulaEngine3Phase[Current]:
130+
def current_per_phase(self) -> Formula3Phase[Current]:
142131
"""Fetch the per-phase grid current for the microgrid.
143132
144133
This formula produces values that are in the Passive Sign Convention (PSC).
@@ -152,12 +141,10 @@ def current_per_phase(self) -> FormulaEngine3Phase[Current]:
152141
Returns:
153142
A FormulaEngine that will calculate and stream grid current.
154143
"""
155-
engine = self._formula_pool.from_3_phase_current_formula_generator(
144+
return self._formula_pool.from_current_3_phase_formula(
156145
"grid_current",
157-
GridCurrentFormula,
146+
connection_manager.get().component_graph.grid_formula(),
158147
)
159-
assert isinstance(engine, FormulaEngine3Phase)
160-
return engine
161148

162149
async def stop(self) -> None:
163150
"""Stop all formula engines."""
@@ -215,7 +202,7 @@ def initialize(
215202
)
216203

217204
namespace = f"grid-{uuid.uuid4()}"
218-
formula_pool = FormulaEnginePool(
205+
formula_pool = FormulaPool(
219206
namespace,
220207
channel_registry,
221208
resampler_subscription_sender,

0 commit comments

Comments
 (0)