Skip to content

Commit ff0c626

Browse files
Use SimplePowerFormula in ProducerPowerFormula
This should simplify code Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent 54a6877 commit ff0c626

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

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

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from ._fallback_formula_metric_fetcher import FallbackFormulaMetricFetcher
1515
from ._formula_generator import (
1616
NON_EXISTING_COMPONENT_ID,
17-
ComponentNotFound,
1817
FormulaGenerator,
1918
FormulaGeneratorConfig,
2019
)
20+
from ._simple_power_formula import SimplePowerFormula
2121

2222
_logger = logging.getLogger(__name__)
2323

@@ -50,39 +50,27 @@ def generate( # noqa: DOC502
5050
)
5151

5252
component_graph = connection_manager.get().component_graph
53-
if self._config.component_ids is None:
54-
# if in the future we support additional producers, we need to add them to the lambda
55-
producer_components = component_graph.dfs(
56-
self._get_grid_component(),
57-
set(),
58-
lambda component: component_graph.is_pv_chain(component)
59-
or component_graph.is_chp_chain(component),
60-
)
61-
62-
if not producer_components:
63-
_logger.warning(
64-
"Unable to find any producer components in the component graph. "
65-
"Subscribing to the resampling actor with a non-existing "
66-
"component id, so that `0` values are sent from the formula."
67-
)
68-
# If there are no producer components, we have to send 0 values at the same
69-
# frequency as the other streams. So we subscribe with a non-existing
70-
# component id, just to get a `None` message at the resampling interval.
71-
builder.push_component_metric(
72-
NON_EXISTING_COMPONENT_ID, nones_are_zeros=True
73-
)
74-
return builder.build()
53+
# if in the future we support additional producers, we need to add them to the lambda
54+
producer_components = component_graph.dfs(
55+
self._get_grid_component(),
56+
set(),
57+
lambda component: component_graph.is_pv_chain(component)
58+
or component_graph.is_chp_chain(component),
59+
)
7560

76-
else:
77-
producer_components = component_graph.components(
78-
component_ids=set(self._config.component_ids)
61+
if not producer_components:
62+
_logger.warning(
63+
"Unable to find any producer components in the component graph. "
64+
"Subscribing to the resampling actor with a non-existing "
65+
"component id, so that `0` values are sent from the formula."
7966
)
80-
if len(producer_components) != len(self._config.component_ids):
81-
raise ComponentNotFound(
82-
"Unable to find all requested producer components."
83-
f"Requested {self._config.component_ids}, "
84-
f" found {producer_components}."
85-
)
67+
# If there are no producer components, we have to send 0 values at the same
68+
# frequency as the other streams. So we subscribe with a non-existing
69+
# component id, just to get a `None` message at the resampling interval.
70+
builder.push_component_metric(
71+
NON_EXISTING_COMPONENT_ID, nones_are_zeros=True
72+
)
73+
return builder.build()
8674

8775
is_not_meter: Callable[[Component], bool] = (
8876
lambda component: component.category != ComponentCategory.METER
@@ -143,7 +131,7 @@ def _get_fallback_formulas(
143131
continue
144132

145133
fallback_ids = [c.component_id for c in fallback_components]
146-
generator = ProducerPowerFormula(
134+
generator = SimplePowerFormula(
147135
f"{self._namespace}_fallback_{fallback_ids}",
148136
self._channel_registry,
149137
self._resampler_subscription_sender,

0 commit comments

Comments
 (0)