Skip to content

Commit 2dbd795

Browse files
committed
Add supporting for configuring formula generators
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent b3a10cb commit 2dbd795

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/frequenz/sdk/timeseries/_formula_engine/_formula_engine_pool.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
from ...actor import ChannelRegistry, ComponentMetricRequest
1313
from ...microgrid.component import ComponentMetricId
14+
from ._formula_generators._formula_generator import (
15+
FormulaGenerator,
16+
FormulaGeneratorConfig,
17+
)
1418
from ._resampled_formula_builder import ResampledFormulaBuilder
1519

1620
if TYPE_CHECKING:
@@ -21,7 +25,6 @@
2125
_GenericEngine,
2226
_GenericFormulaReceiver,
2327
)
24-
from ._formula_generators import FormulaGenerator
2528

2629

2730
class FormulaEnginePool:
@@ -88,12 +91,14 @@ async def from_generator(
8891
self,
8992
channel_key: str,
9093
generator: "Type[FormulaGenerator[_GenericEngine]]",
94+
config: FormulaGeneratorConfig = FormulaGeneratorConfig(),
9195
) -> "_GenericFormulaReceiver":
9296
"""Get a receiver for a formula from a generator.
9397
9498
Args:
9599
channel_key: A string to uniquely identify the formula.
96100
generator: A formula generator.
101+
config: config to initialize the formula generator with.
97102
98103
Returns:
99104
A FormulaReceiver or a FormulaReceiver3Phase instance based on what the
@@ -103,7 +108,10 @@ async def from_generator(
103108
return self._engines[channel_key].new_receiver()
104109

105110
engine = await generator(
106-
self._namespace, self._channel_registry, self._resampler_subscription_sender
111+
self._namespace,
112+
self._channel_registry,
113+
self._resampler_subscription_sender,
114+
config,
107115
).generate()
108116
self._engines[channel_key] = engine
109117
return engine.new_receiver()

src/frequenz/sdk/timeseries/_formula_engine/_formula_generators/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ComponentNotFound,
1212
FormulaGenerationError,
1313
FormulaGenerator,
14+
FormulaGeneratorConfig,
1415
)
1516
from ._grid_current_formula import GridCurrentFormula
1617
from ._grid_power_formula import GridPowerFormula
@@ -21,6 +22,7 @@
2122
# Base class
2223
#
2324
"FormulaGenerator",
25+
"FormulaGeneratorConfig",
2426
#
2527
# Power Formula generators
2628
#

src/frequenz/sdk/timeseries/_formula_engine/_formula_generators/_formula_generator.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
"""Base class for formula generators that use the component graphs."""
55

6+
from __future__ import annotations
7+
68
import sys
79
from abc import ABC, abstractmethod
10+
from dataclasses import dataclass
811
from typing import Generic
912

1013
from frequenz.channels import Sender
@@ -26,6 +29,13 @@ class ComponentNotFound(FormulaGenerationError):
2629
NON_EXISTING_COMPONENT_ID = sys.maxsize
2730

2831

32+
@dataclass(frozen=True)
33+
class FormulaGeneratorConfig:
34+
"""Config for formula generators."""
35+
36+
component_ids: set[int] | None = None
37+
38+
2939
class FormulaGenerator(ABC, Generic[_GenericEngine]):
3040
"""A class for generating formulas from the component graph."""
3141

@@ -34,6 +44,7 @@ def __init__(
3444
namespace: str,
3545
channel_registry: ChannelRegistry,
3646
resampler_subscription_sender: Sender[ComponentMetricRequest],
47+
config: FormulaGeneratorConfig,
3748
) -> None:
3849
"""Create a `FormulaGenerator` instance.
3950
@@ -43,10 +54,12 @@ def __init__(
4354
actor.
4455
resampler_subscription_sender: A sender for sending metric requests to the
4556
resampling actor.
57+
config: configs for the formula generator.
4658
"""
4759
self._channel_registry = channel_registry
4860
self._resampler_subscription_sender = resampler_subscription_sender
4961
self._namespace = namespace
62+
self._config = config
5063

5164
def _get_builder(
5265
self, name: str, component_metric_id: ComponentMetricId

0 commit comments

Comments
 (0)