Skip to content

Commit b80007d

Browse files
Change resolution to resample_period
Signed-off-by: Flora <[email protected]>
1 parent e6e9235 commit b80007d

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ client = ReportingApiClient(server_url=SERVER_URL, key=API_KEY)
5050
```
5151

5252
Besides the microgrid_id, component_ids, and metrics, start, and end time,
53-
you can also set the sampling period for resampling using the `resolution` parameter.
54-
For example, to resample data every 15 minutes, use a `resolution` of 900 seconds. The default is 1 second.
53+
you can also set the sampling period for resampling using the `resampling_period`
54+
parameter. For example, to resample data every 15 minutes, use a `resampling_period`
55+
of timedelta(minutes=15).
5556

5657
### Query metrics for a single microgrid and component:
5758

@@ -64,7 +65,7 @@ data = [
6465
metrics=[Metric.AC_ACTIVE_POWER, Metric.AC_REACTIVE_POWER],
6566
start_dt=datetime.fromisoformat("2024-05-01T00:00:00"),
6667
end_dt=datetime.fromisoformat("2024-05-02T00:00:00"),
67-
resolution=1,
68+
resampling_period=timedelta(seconds=1),
6869
)
6970
]
7071
```
@@ -91,7 +92,7 @@ data = [
9192
metrics=[Metric.AC_ACTIVE_POWER, Metric.AC_REACTIVE_POWER],
9293
start_dt=datetime.fromisoformat("2024-05-01T00:00:00"),
9394
end_dt=datetime.fromisoformat("2024-05-02T00:00:00"),
94-
resolution=1,
95+
resampling_period=timedelta(seconds=1),
9596
states=False, # Set to True to include state data
9697
bounds=False, # Set to True to include metric bounds data
9798
)

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## Upgrading
88

99
* Enforce keyword arguments in 'run' function of 'main' module
10+
* Change 'resolution' 'int' to 'resample_period' 'timedelta'
1011

1112
## New Features
1213

src/frequenz/client/reporting/__main__.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import argparse
77
import asyncio
8-
from datetime import datetime
8+
from datetime import datetime, timedelta
99
from pprint import pprint
1010
from typing import AsyncIterator
1111

@@ -57,7 +57,12 @@ def main() -> None:
5757
help="End datetime in YYYY-MM-DDTHH:MM:SS format",
5858
required=True,
5959
)
60-
parser.add_argument("--resolution", type=int, help="Resolution", default=None)
60+
parser.add_argument(
61+
"--resampling_period_s",
62+
type=int,
63+
help="Resampling period in seconds (integer, rounded to avoid subsecond precision issues).",
64+
default=None,
65+
)
6166
parser.add_argument("--psize", type=int, help="Page size", default=1000)
6267
parser.add_argument(
6368
"--format", choices=["iter", "csv", "dict"], help="Output format", default="csv"
@@ -76,7 +81,7 @@ def main() -> None:
7681
metric_names=args.metrics,
7782
start_dt=args.start,
7883
end_dt=args.end,
79-
resolution=args.resolution,
84+
resampling_period_s=args.resampling_period_s,
8085
states=args.states,
8186
bounds=args.bounds,
8287
service_address=args.url,
@@ -94,7 +99,7 @@ async def run(
9499
metric_names: list[str],
95100
start_dt: datetime,
96101
end_dt: datetime,
97-
resolution: int,
102+
resampling_period_s: int | None,
98103
states: bool,
99104
bounds: bool,
100105
service_address: str,
@@ -109,7 +114,7 @@ async def run(
109114
metric_names: list of metric names
110115
start_dt: start datetime
111116
end_dt: end datetime
112-
resolution: resampling resolution in sec
117+
resampling_period_s: The period for resampling the data.
113118
states: include states in the output
114119
bounds: include bounds in the output
115120
service_address: service address
@@ -131,13 +136,19 @@ def data_iter() -> AsyncIterator[MetricSample]:
131136
Returns:
132137
Iterator over single metric samples
133138
"""
139+
resampling_period = (
140+
timedelta(seconds=resampling_period_s)
141+
if resampling_period_s is not None
142+
else None
143+
)
144+
134145
return client.list_single_component_data(
135146
microgrid_id=microgrid_id,
136147
component_id=component_id,
137148
metrics=metrics,
138149
start_dt=start_dt,
139150
end_dt=end_dt,
140-
resolution=resolution,
151+
resampling_period=resampling_period,
141152
include_states=states,
142153
include_bounds=bounds,
143154
)

src/frequenz/client/reporting/_client.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections import namedtuple
77
from collections.abc import AsyncIterator, Iterable, Iterator
88
from dataclasses import dataclass
9-
from datetime import datetime
9+
from datetime import datetime, timedelta
1010
from typing import cast
1111

1212
import grpc.aio as grpcaio
@@ -160,7 +160,7 @@ async def list_single_component_data(
160160
metrics: Metric | list[Metric],
161161
start_dt: datetime,
162162
end_dt: datetime,
163-
resolution: int | None,
163+
resampling_period: timedelta | None,
164164
include_states: bool = False,
165165
include_bounds: bool = False,
166166
) -> AsyncIterator[MetricSample]:
@@ -172,7 +172,7 @@ async def list_single_component_data(
172172
metrics: The metric name or list of metric names.
173173
start_dt: The start date and time.
174174
end_dt: The end date and time.
175-
resolution: The resampling resolution for the data, represented in seconds.
175+
resampling_period: The period for resampling the data.
176176
include_states: Whether to include the state data.
177177
include_bounds: Whether to include the bound data.
178178
@@ -186,7 +186,7 @@ async def list_single_component_data(
186186
metrics=[metrics] if isinstance(metrics, Metric) else metrics,
187187
start_dt=start_dt,
188188
end_dt=end_dt,
189-
resolution=resolution,
189+
resampling_period=resampling_period,
190190
include_states=include_states,
191191
include_bounds=include_bounds,
192192
):
@@ -201,7 +201,7 @@ async def list_microgrid_components_data(
201201
metrics: Metric | list[Metric],
202202
start_dt: datetime,
203203
end_dt: datetime,
204-
resolution: int | None,
204+
resampling_period: timedelta | None,
205205
include_states: bool = False,
206206
include_bounds: bool = False,
207207
) -> AsyncIterator[MetricSample]:
@@ -213,7 +213,7 @@ async def list_microgrid_components_data(
213213
metrics: The metric name or list of metric names.
214214
start_dt: The start date and time.
215215
end_dt: The end date and time.
216-
resolution: The resampling resolution for the data, represented in seconds.
216+
resampling_period: The period for resampling the data.
217217
include_states: Whether to include the state data.
218218
include_bounds: Whether to include the bound data.
219219
@@ -230,7 +230,7 @@ async def list_microgrid_components_data(
230230
metrics=[metrics] if isinstance(metrics, Metric) else metrics,
231231
start_dt=start_dt,
232232
end_dt=end_dt,
233-
resolution=resolution,
233+
resampling_period=resampling_period,
234234
include_states=include_states,
235235
include_bounds=include_bounds,
236236
):
@@ -246,7 +246,7 @@ async def _list_microgrid_components_data_batch(
246246
metrics: list[Metric],
247247
start_dt: datetime,
248248
end_dt: datetime,
249-
resolution: int | None,
249+
resampling_period: timedelta | None,
250250
include_states: bool = False,
251251
include_bounds: bool = False,
252252
) -> AsyncIterator[ComponentsDataBatch]:
@@ -260,7 +260,7 @@ async def _list_microgrid_components_data_batch(
260260
metrics: A list of metrics.
261261
start_dt: The start date and time.
262262
end_dt: The end date and time.
263-
resolution: The resampling resolution for the data, represented in seconds.
263+
resampling_period: The period for resampling the data.
264264
include_states: Whether to include the state data.
265265
include_bounds: Whether to include the bound data.
266266
@@ -299,7 +299,13 @@ def dt2ts(dt: datetime) -> PBTimestamp:
299299

300300
stream_filter = PBReceiveMicrogridComponentsDataStreamRequest.StreamFilter(
301301
time_filter=time_filter,
302-
resampling_options=PBResamplingOptions(resolution=resolution),
302+
resampling_options=PBResamplingOptions(
303+
resolution=(
304+
round(resampling_period.total_seconds())
305+
if resampling_period is not None
306+
else None
307+
)
308+
),
303309
include_options=include_options,
304310
)
305311

0 commit comments

Comments
 (0)