Skip to content

Commit c36e839

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

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-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: 11 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

@@ -56,7 +56,12 @@ def main() -> None:
5656
help="End datetime in YYYY-MM-DDTHH:MM:SS format",
5757
required=True,
5858
)
59-
parser.add_argument("--resolution", type=int, help="Resolution", default=None)
59+
parser.add_argument(
60+
"--resampling_period",
61+
type=int,
62+
help="Resampling period in seconds (integer, rounded to avoid subsecond precision issues).",
63+
default=None,
64+
)
6065
parser.add_argument("--psize", type=int, help="Page size", default=1000)
6166
parser.add_argument(
6267
"--format", choices=["iter", "csv", "dict"], help="Output format", default="csv"
@@ -75,7 +80,7 @@ def main() -> None:
7580
metric_names=args.metrics,
7681
start_dt=args.start,
7782
end_dt=args.end,
78-
resolution=args.resolution,
83+
resampling_period=args.resampling_period,
7984
states=args.states,
8085
bounds=args.bounds,
8186
service_address=args.url,
@@ -93,7 +98,7 @@ async def run(
9398
metric_names: list[str],
9499
start_dt: datetime,
95100
end_dt: datetime,
96-
resolution: int,
101+
resampling_period: timedelta,
97102
states: bool,
98103
bounds: bool,
99104
service_address: str,
@@ -108,7 +113,7 @@ async def run(
108113
metric_names: list of metric names
109114
start_dt: start datetime
110115
end_dt: end datetime
111-
resolution: resampling resolution in sec
116+
resampling_period: The period for resampling the data.
112117
states: include states in the output
113118
bounds: include bounds in the output
114119
service_address: service address
@@ -136,7 +141,7 @@ def data_iter() -> AsyncIterator[MetricSample]:
136141
metrics=metrics,
137142
start_dt=start_dt,
138143
end_dt=end_dt,
139-
resolution=resolution,
144+
resampling_period=resampling_period,
140145
include_states=states,
141146
include_bounds=bounds,
142147
)

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, specified as `timedelta` object.
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, specified as `timedelta` object.
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, specified as `timedelta` object.
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)