Skip to content

Commit c826753

Browse files
committed
Add support for bounds in client
1 parent 7f5cca8 commit c826753

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/frequenz/client/reporting/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def main() -> None:
3939
action="store_true",
4040
help="Include states in the output",
4141
)
42+
parser.add_argument(
43+
"--bounds",
44+
action="store_true",
45+
help="Include bounds in the output",
46+
)
4247
parser.add_argument(
4348
"--start",
4449
type=datetime.fromisoformat,
@@ -72,6 +77,7 @@ def main() -> None:
7277
args.end,
7378
args.resolution,
7479
states=args.states,
80+
bounds=args.bounds,
7581
service_address=args.url,
7682
key=args.key,
7783
fmt=args.format,
@@ -88,6 +94,7 @@ async def run(
8894
end_dt: datetime,
8995
resolution: int,
9096
states: bool,
97+
bounds: bool,
9198
service_address: str,
9299
key: str,
93100
fmt: str,
@@ -102,6 +109,7 @@ async def run(
102109
end_dt: end datetime
103110
resolution: resampling resolution in sec
104111
states: include states in the output
112+
bounds: include bounds in the output
105113
service_address: service address
106114
key: API key
107115
fmt: output format
@@ -129,6 +137,7 @@ def data_iter() -> AsyncIterator[MetricSample]:
129137
end_dt=end_dt,
130138
resolution=resolution,
131139
include_states=states,
140+
include_bounds=bounds,
132141
)
133142

134143
if fmt == "iter":

src/frequenz/client/reporting/_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ def __iter__(self) -> Iterator[MetricSample]:
9898
metric=met,
9999
value=value,
100100
)
101+
for i, bound in enumerate(msample.bounds):
102+
if bound.lower:
103+
yield MetricSample(
104+
timestamp=ts,
105+
microgrid_id=mid,
106+
component_id=cid,
107+
metric=f"{met}_bound_{i}_lower",
108+
value=bound.lower,
109+
)
110+
if bound.upper:
111+
yield MetricSample(
112+
timestamp=ts,
113+
microgrid_id=mid,
114+
component_id=cid,
115+
metric=f"{met}_bound_{i}_upper",
116+
value=bound.upper,
117+
)
101118
for state in cdata.states:
102119
ts = state.sampled_at.ToDatetime()
103120
for name, category in {
@@ -140,6 +157,7 @@ async def list_single_component_data(
140157
end_dt: datetime,
141158
resolution: int | None,
142159
include_states: bool = False,
160+
include_bounds: bool = False,
143161
) -> AsyncIterator[MetricSample]:
144162
"""Iterate over the data for a single metric.
145163
@@ -151,6 +169,7 @@ async def list_single_component_data(
151169
end_dt: The end date and time.
152170
resolution: The resampling resolution for the data, represented in seconds.
153171
include_states: Whether to include the state data.
172+
include_bounds: Whether to include the bound data.
154173
155174
Yields:
156175
A named tuple with the following fields:
@@ -164,6 +183,7 @@ async def list_single_component_data(
164183
end_dt=end_dt,
165184
resolution=resolution,
166185
include_states=include_states,
186+
include_bounds=include_bounds,
167187
):
168188
for entry in batch:
169189
yield entry
@@ -178,6 +198,7 @@ async def list_microgrid_components_data(
178198
end_dt: datetime,
179199
resolution: int | None,
180200
include_states: bool = False,
201+
include_bounds: bool = False,
181202
) -> AsyncIterator[MetricSample]:
182203
"""Iterate over the data for multiple microgrids and components.
183204
@@ -189,6 +210,7 @@ async def list_microgrid_components_data(
189210
end_dt: The end date and time.
190211
resolution: The resampling resolution for the data, represented in seconds.
191212
include_states: Whether to include the state data.
213+
include_bounds: Whether to include the bound data.
192214
193215
Yields:
194216
A named tuple with the following fields:
@@ -205,6 +227,7 @@ async def list_microgrid_components_data(
205227
end_dt=end_dt,
206228
resolution=resolution,
207229
include_states=include_states,
230+
include_bounds=include_bounds,
208231
):
209232
for entry in batch:
210233
yield entry
@@ -220,6 +243,7 @@ async def _list_microgrid_components_data_batch(
220243
end_dt: datetime,
221244
resolution: int | None,
222245
include_states: bool = False,
246+
include_bounds: bool = False,
223247
) -> AsyncIterator[ComponentsDataBatch]:
224248
"""Iterate over the component data batches in the stream.
225249
@@ -233,6 +257,7 @@ async def _list_microgrid_components_data_batch(
233257
end_dt: The end date and time.
234258
resolution: The resampling resolution for the data, represented in seconds.
235259
include_states: Whether to include the state data.
260+
include_bounds: Whether to include the bound data.
236261
237262
Yields:
238263
A ComponentsDataBatch object of microgrid components data.
@@ -257,7 +282,13 @@ def dt2ts(dt: datetime) -> PBTimestamp:
257282
if include_states
258283
else PBIncludeOptions.FilterOption.FILTER_OPTION_EXCLUDE
259284
)
285+
incl_bounds = (
286+
PBIncludeOptions.FilterOption.FILTER_OPTION_INCLUDE
287+
if include_bounds
288+
else PBIncludeOptions.FilterOption.FILTER_OPTION_EXCLUDE
289+
)
260290
include_options = PBIncludeOptions(
291+
bounds=incl_bounds,
261292
states=incl_states,
262293
)
263294

0 commit comments

Comments
 (0)