Skip to content

Commit b0d9142

Browse files
committed
Improve LogicalMeter documentation and add a usage example
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent cb63987 commit b0d9142

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

src/frequenz/sdk/timeseries/logical_meter/_logical_meter.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,61 @@
3131
class LogicalMeter:
3232
"""A logical meter for calculating high level metrics in a microgrid.
3333
34-
LogicalMeter can be used to run formulas on resampled component metric streams.
34+
LogicalMeter provides methods for fetching power values from different points in the
35+
microgrid. These methods return `FormulaReceiver` objects, which can be used like
36+
normal `Receiver`s, but can also be composed to form higher-order formula streams.
37+
38+
Example:
39+
``` python
40+
channel_registry = ChannelRegistry(name="data-registry")
41+
42+
# Create a channels for sending/receiving subscription requests
43+
data_source_request_channel = Broadcast[ComponentMetricRequest]("data-source")
44+
data_source_request_sender = data_source_request_channel.new_sender()
45+
data_source_request_receiver = data_source_request_channel.new_receiver()
46+
47+
resampling_request_channel = Broadcast[ComponentMetricRequest]("resample")
48+
resampling_request_sender = resampling_request_channel.new_sender()
49+
resampling_request_receiver = resampling_request_channel.new_receiver()
50+
51+
# Instantiate a data sourcing actor
52+
_data_sourcing_actor = DataSourcingActor(
53+
request_receiver=data_source_request_receiver, registry=channel_registry
54+
)
55+
56+
# Instantiate a resampling actor
57+
_resampling_actor = ComponentMetricsResamplingActor(
58+
channel_registry=channel_registry,
59+
data_sourcing_request_sender=data_source_request_sender,
60+
resampling_request_receiver=resampling_request_receiver,
61+
config=ResamplerConfig(resampling_period_s=1),
62+
)
3563
36-
Formulas can have Component IDs that are preceeded by a pound symbol("#"), and these
37-
operators: +, -, *, /, (, ).
64+
# Create a logical meter instance
65+
logical_meter = LogicalMeter(
66+
channel_registry,
67+
resampling_request_sender,
68+
microgrid.get().component_graph,
69+
)
3870
39-
For example, the input string: "#20 + #5" is a formula for adding metrics from two
40-
components with ids 20 and 5.
71+
# Get a receiver for a builtin formula
72+
grid_power_recv = logical_meter.grid_power()
73+
for grid_power_sample in grid_power_recv:
74+
print(grid_power_sample)
75+
76+
# or compose formula receivers to create a new formula
77+
net_power_recv = (
78+
(
79+
logical_meter.grid_power()
80+
- logical_meter.battery_power()
81+
- logical_meter.pv_power()
82+
)
83+
.build("net_power")
84+
.new_receiver()
85+
)
86+
for net_power_sample in net_power_recv:
87+
print(net_power_sample)
88+
```
4189
"""
4290

4391
def __init__(
@@ -83,7 +131,13 @@ async def start_formula(
83131
component_metric_id: ComponentMetricId,
84132
nones_are_zeros: bool = False,
85133
) -> FormulaReceiver:
86-
"""Start execution of the given formula name.
134+
"""Start execution of the given formula.
135+
136+
Formulas can have Component IDs that are preceeded by a pound symbol("#"), and
137+
these operators: +, -, *, /, (, ).
138+
139+
For example, the input string: "#20 + #5" is a formula for adding metrics from
140+
two components with ids 20 and 5.
87141
88142
Args:
89143
formula: formula to execute.

0 commit comments

Comments
 (0)