Skip to content

Commit 6ab8228

Browse files
Add fine-grained Result types for the PowerDistributingActor (#104)
Improve power distributing results.
2 parents f70fd05 + 8bb2533 commit 6ab8228

File tree

9 files changed

+411
-205
lines changed

9 files changed

+411
-205
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1010

11+
- Add fine-grained Result types for the PowerDistributingActor.
12+
Previously Result was one class with many fields. Now each result has its own class
13+
that derives from Result parent class.
14+
1115
## New Features
1216

1317
<!-- Here goes the main new features and examples or instructions on how to use them -->

benchmarks/power_distribution/power_distributor.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
from frequenz.channels import Bidirectional
1515

1616
from frequenz.sdk.actor.power_distributing import (
17+
Error,
18+
Ignored,
19+
OutOfBound,
20+
PartialFailure,
1721
PowerDistributingActor,
1822
Request,
1923
Result,
24+
Success,
2025
)
2126
from frequenz.sdk.microgrid import ComponentGraph
2227
from frequenz.sdk.microgrid._graph import _MicrogridComponentGraph
@@ -75,21 +80,23 @@ def parse_result(result: List[List[Result]]) -> Dict[str, float]:
7580
Number of each result.
7681
"""
7782
result_counts = {
78-
Result.Status.ERROR: 0,
79-
Result.Status.IGNORED: 0,
80-
Result.Status.SUCCESS: 0,
81-
Result.Status.FAILED: 0,
83+
Error: 0,
84+
Ignored: 0,
85+
Success: 0,
86+
PartialFailure: 0,
87+
OutOfBound: 0,
8288
}
8389

8490
for result_list in result:
8591
for item in result_list:
86-
result_counts[item.status] += 1
92+
result_counts[type(item)] += 1
8793

8894
return {
89-
"success_num": result_counts[Result.Status.SUCCESS],
90-
"failed_num": result_counts[Result.Status.FAILED],
91-
"ignore_num": result_counts[Result.Status.IGNORED],
92-
"error_num": result_counts[Result.Status.ERROR],
95+
"success_num": result_counts[Success],
96+
"failed_num": result_counts[PartialFailure],
97+
"ignore_num": result_counts[Ignored],
98+
"error_num": result_counts[Error],
99+
"out_of_bound": result_counts[OutOfBound],
93100
}
94101

95102

examples/power_distribution.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
PowerDistributingActor,
2828
Request,
2929
Result,
30+
Success,
3031
)
3132
from frequenz.sdk.microgrid.component import Component, ComponentCategory
3233

@@ -97,14 +98,12 @@ async def run(self) -> None:
9798
continue
9899
if result is None:
99100
raise RuntimeError("PowerDistributingActor channel has been closed.")
100-
if result.status != Result.Status.SUCCESS:
101+
if not isinstance(result, Success):
101102
_logger.error(
102-
"Could not set %d power. Result: %s", power_to_set, str(result)
103+
"Could not set %d power. Result: %s", power_to_set, type(result)
103104
)
104105
else:
105-
_logger.info(
106-
"Set power with %d succeed, result: %s", power_to_set, str(result)
107-
)
106+
_logger.info("Set power with %d succeed.", power_to_set)
108107

109108

110109
@actor

src/frequenz/sdk/actor/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""A base class for creating simple composable actors."""
55

6-
from . import power_distributing
76
from ._channel_registry import ChannelRegistry
87
from ._config_managing import ConfigManagingActor
98
from ._data_sourcing import ComponentMetricRequest, DataSourcingActor
@@ -17,5 +16,4 @@
1716
"ConfigManagingActor",
1817
"DataSourcingActor",
1918
"actor",
20-
"power_distributing",
2119
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# License: MIT
2+
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
3+
4+
"""This module provides feature to set power between many batteries.
5+
6+
Distributing power is very important to keep the microgrid ready
7+
for the power requirements.
8+
This module provides PowerDistributingActor that knows how to distribute power.
9+
It also provides all the secondary features that should be used to communicate with
10+
PowerDistributingActor and send requests for charging or discharging power.
11+
"""
12+
13+
from .power_distributing import PowerDistributingActor
14+
from .request import Request
15+
from .result import Error, Ignored, OutOfBound, PartialFailure, Result, Success
16+
17+
__all__ = [
18+
"PowerDistributingActor",
19+
"Request",
20+
"Result",
21+
"Error",
22+
"Success",
23+
"Ignored",
24+
"OutOfBound",
25+
"PartialFailure",
26+
]

0 commit comments

Comments
 (0)