Skip to content

Commit abe0641

Browse files
Change resolution to resample_period (frequenz-floss#105)
Addressing Issue frequenz-floss#79.
2 parents f67f0f2 + b80007d commit abe0641

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
@@ -168,7 +168,7 @@ async def list_single_component_data(
168168
metrics: Metric | list[Metric],
169169
start_dt: datetime,
170170
end_dt: datetime,
171-
resolution: int | None,
171+
resampling_period: timedelta | None,
172172
include_states: bool = False,
173173
include_bounds: bool = False,
174174
) -> AsyncIterator[MetricSample]:
@@ -180,7 +180,7 @@ async def list_single_component_data(
180180
metrics: The metric name or list of metric names.
181181
start_dt: The start date and time.
182182
end_dt: The end date and time.
183-
resolution: The resampling resolution for the data, represented in seconds.
183+
resampling_period: The period for resampling the data.
184184
include_states: Whether to include the state data.
185185
include_bounds: Whether to include the bound data.
186186
@@ -194,7 +194,7 @@ async def list_single_component_data(
194194
metrics=[metrics] if isinstance(metrics, Metric) else metrics,
195195
start_dt=start_dt,
196196
end_dt=end_dt,
197-
resolution=resolution,
197+
resampling_period=resampling_period,
198198
include_states=include_states,
199199
include_bounds=include_bounds,
200200
):
@@ -209,7 +209,7 @@ async def list_microgrid_components_data(
209209
metrics: Metric | list[Metric],
210210
start_dt: datetime,
211211
end_dt: datetime,
212-
resolution: int | None,
212+
resampling_period: timedelta | None,
213213
include_states: bool = False,
214214
include_bounds: bool = False,
215215
) -> AsyncIterator[MetricSample]:
@@ -221,7 +221,7 @@ async def list_microgrid_components_data(
221221
metrics: The metric name or list of metric names.
222222
start_dt: The start date and time.
223223
end_dt: The end date and time.
224-
resolution: The resampling resolution for the data, represented in seconds.
224+
resampling_period: The period for resampling the data.
225225
include_states: Whether to include the state data.
226226
include_bounds: Whether to include the bound data.
227227
@@ -238,7 +238,7 @@ async def list_microgrid_components_data(
238238
metrics=[metrics] if isinstance(metrics, Metric) else metrics,
239239
start_dt=start_dt,
240240
end_dt=end_dt,
241-
resolution=resolution,
241+
resampling_period=resampling_period,
242242
include_states=include_states,
243243
include_bounds=include_bounds,
244244
):
@@ -254,7 +254,7 @@ async def _list_microgrid_components_data_batch(
254254
metrics: list[Metric],
255255
start_dt: datetime,
256256
end_dt: datetime,
257-
resolution: int | None,
257+
resampling_period: timedelta | None,
258258
include_states: bool = False,
259259
include_bounds: bool = False,
260260
) -> AsyncIterator[ComponentsDataBatch]:
@@ -268,7 +268,7 @@ async def _list_microgrid_components_data_batch(
268268
metrics: A list of metrics.
269269
start_dt: The start date and time.
270270
end_dt: The end date and time.
271-
resolution: The resampling resolution for the data, represented in seconds.
271+
resampling_period: The period for resampling the data.
272272
include_states: Whether to include the state data.
273273
include_bounds: Whether to include the bound data.
274274
@@ -307,7 +307,13 @@ def dt2ts(dt: datetime) -> PBTimestamp:
307307

308308
stream_filter = PBReceiveMicrogridComponentsDataStreamRequest.StreamFilter(
309309
time_filter=time_filter,
310-
resampling_options=PBResamplingOptions(resolution=resolution),
310+
resampling_options=PBResamplingOptions(
311+
resolution=(
312+
round(resampling_period.total_seconds())
313+
if resampling_period is not None
314+
else None
315+
)
316+
),
311317
include_options=include_options,
312318
)
313319

0 commit comments

Comments
 (0)