Skip to content

Commit 7494d27

Browse files
Update delivery time checks for gridpool streams and support delivery time filter in CLI (#231)
This pull request refactors and improves the handling of delivery and execution time filters in the electricity trading CLI and client code. It standardizes the naming of time-related parameters, enhances validation for delivery time filters, and updates the CLI and output formatting to be more consistent and informative. **Parameter naming and CLI improvements:** * Standardized CLI arguments for time filters, replacing ambiguous options like `--start`/`--end` with clearer `--execution-from`, `--execution-to`, `--delivery-from`, and `--delivery-to` across relevant commands in `cli/__main__.py` and `cli/etrading.py`. This clarifies the distinction between execution and delivery periods and improves usability. * Updated function signatures and internal variable names to match the new CLI arguments, ensuring consistency throughout the codebase. **Validation and robustness:** * Added an `is_valid` property to the `DeliveryTimeFilter` class to check that the delivery time interval is logical (start < end), and updated the streaming methods in `_client.py` to raise a `ValueError` if an invalid filter is provided. This prevents invalid queries and improves error messaging. **Output formatting and clarity:** * Enhanced output of order and trade listings by adding the `gridpool_id` to CSV headers and output, and updated the corresponding print functions to include this information. This makes CSV exports more informative and easier to analyze. **Code simplification and cleanup:** * Simplified the logic for listing and streaming gridpool trades and orders by removing redundant checks and using the new delivery time filter structure, leading to cleaner and more maintainable code. **Other minor changes:** * Removed unnecessary imports (such as `timezone`) to clean up the code.
2 parents 1ee23a2 + 1192df5 commit 7494d27

5 files changed

Lines changed: 121 additions & 74 deletions

File tree

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
* Update delivery period time filter validation to remove inappropriate start time check.
1010

1111
## New Features
1212

1313
* Add check to validate order details.
14+
* CLI: Support delivery time filter in gridpool streams.
1415

1516
## Bug Fixes
1617

src/frequenz/client/electricity_trading/_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ def gridpool_orders_stream(
300300
301301
Raises:
302302
grpc.RpcError: If an error occurs while streaming the orders.
303+
ValueError: If an invalid delivery_time_filter is provided.
303304
"""
304-
self.validate_params(delivery_time_filter=delivery_time_filter)
305+
if delivery_time_filter and not delivery_time_filter.is_valid:
306+
raise ValueError("Invalid delivery_time_filter provided.")
305307

306308
gridpool_order_filter = GridpoolOrderFilter(
307309
order_states=order_states,
@@ -366,8 +368,10 @@ def gridpool_trades_stream(
366368
367369
Raises:
368370
grpc.RpcError: If an error occurs while streaming gridpool trades.
371+
ValueError: If an invalid delivery_time_filter is provided.
369372
"""
370-
self.validate_params(delivery_time_filter=delivery_time_filter)
373+
if delivery_time_filter and not delivery_time_filter.is_valid:
374+
raise ValueError("Invalid delivery_time_filter provided.")
371375

372376
gridpool_trade_filter = GridpoolTradeFilter(
373377
trade_states=trade_states,

src/frequenz/client/electricity_trading/_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,25 @@ def to_pb(self) -> electricity_trading_pb2.DeliveryTimeFilter:
15771577
),
15781578
)
15791579

1580+
@property
1581+
def is_valid(self) -> bool:
1582+
"""Check if the DeliveryTimeFilter is valid.
1583+
1584+
Verifies that the start and end dates in the time interval are logical, if both are set.
1585+
1586+
Returns:
1587+
True if the filter is valid, False otherwise.
1588+
"""
1589+
if self.time_interval is None:
1590+
return True
1591+
1592+
start = self.time_interval.start_time
1593+
end = self.time_interval.end_time
1594+
if start is None or end is None:
1595+
return True
1596+
1597+
return start < end
1598+
15801599

15811600
@dataclass(frozen=True)
15821601
class GridpoolOrderFilter:

src/frequenz/client/electricity_trading/cli/__main__.py

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def cli() -> None:
5151
@click.option("--url", required=True, type=str)
5252
@click.option("--auth_key", required=True, type=str)
5353
@click.option("--delivery-start", default=None, type=iso)
54-
@click.option("--start", default=None, type=iso)
55-
@click.option("--end", default=None, type=iso)
54+
@click.option("--execution-from", default=None, type=iso)
55+
@click.option("--execution-to", default=None, type=iso)
5656
@click.option("--sign_secret", default=None, type=str)
5757
def receive_public_trades( # pylint: disable=too-many-arguments
5858
url: str,
5959
auth_key: str,
6060
*,
61-
start: datetime,
62-
end: datetime,
61+
execution_from: datetime,
62+
execution_to: datetime,
6363
delivery_start: datetime,
6464
sign_secret: str | None = None,
6565
) -> None:
@@ -69,8 +69,8 @@ def receive_public_trades( # pylint: disable=too-many-arguments
6969
url=url,
7070
auth_key=auth_key,
7171
delivery_start=delivery_start,
72-
start=start,
73-
end=end,
72+
start=execution_from,
73+
end=execution_to,
7474
sign_secret=sign_secret,
7575
)
7676
)
@@ -80,15 +80,15 @@ def receive_public_trades( # pylint: disable=too-many-arguments
8080
@click.option("--url", required=True, type=str)
8181
@click.option("--auth_key", required=True, type=str)
8282
@click.option("--delivery-start", default=None, type=iso)
83-
@click.option("--start", default=None, type=iso)
84-
@click.option("--end", default=None, type=iso)
83+
@click.option("--execution-from", default=None, type=iso)
84+
@click.option("--execution-to", default=None, type=iso)
8585
@click.option("--sign_secret", default=None, type=str)
8686
def receive_public_orders( # pylint: disable=too-many-arguments
8787
url: str,
8888
auth_key: str,
8989
*,
90-
start: datetime,
91-
end: datetime,
90+
execution_from: datetime,
91+
execution_to: datetime,
9292
delivery_start: datetime,
9393
sign_secret: str | None = None,
9494
) -> None:
@@ -98,25 +98,38 @@ def receive_public_orders( # pylint: disable=too-many-arguments
9898
url=url,
9999
auth_key=auth_key,
100100
delivery_start=delivery_start,
101-
start=start,
102-
end=end,
101+
start=execution_from,
102+
end=execution_to,
103103
sign_secret=sign_secret,
104104
)
105105
)
106106

107107

108+
# pylint: disable=too-many-arguments
108109
@cli.command()
109110
@click.option("--url", required=True, type=str)
110111
@click.option("--auth_key", required=True, type=str)
111112
@click.option("--gid", required=True, type=int)
112-
@click.option("--start", default=None, type=iso)
113+
@click.option(
114+
"--delivery-from",
115+
default=None,
116+
type=iso,
117+
help="Start timestamp (inclusive) to filter delivery start times.",
118+
)
119+
@click.option(
120+
"--delivery-to",
121+
default=None,
122+
type=iso,
123+
help="End timestamp (exclusive) to filter delivery start times.",
124+
)
113125
@click.option("--sign_secret", default=None, type=str)
114126
def receive_gridpool_trades(
115127
url: str,
116128
auth_key: str,
117129
gid: int,
118130
*,
119-
start: datetime,
131+
delivery_from: datetime | None,
132+
delivery_to: datetime | None,
120133
sign_secret: str | None = None,
121134
) -> None:
122135
"""List and/or stream gridpool trades."""
@@ -125,23 +138,37 @@ def receive_gridpool_trades(
125138
url=url,
126139
auth_key=auth_key,
127140
gid=gid,
128-
delivery_start=start,
141+
delivery_from=delivery_from,
142+
delivery_to=delivery_to,
129143
sign_secret=sign_secret,
130144
)
131145
)
132146

133147

148+
# pylint: disable=too-many-arguments
134149
@cli.command()
135150
@click.option("--url", required=True, type=str)
136151
@click.option("--auth_key", required=True, type=str)
137-
@click.option("--start", default=None, type=iso)
152+
@click.option(
153+
"--delivery-from",
154+
default=None,
155+
type=iso,
156+
help="Start timestamp (inclusive) to filter delivery start times.",
157+
)
158+
@click.option(
159+
"--delivery-to",
160+
default=None,
161+
type=iso,
162+
help="End timestamp (exclusive) to filter delivery start times.",
163+
)
138164
@click.option("--gid", required=True, type=int)
139165
@click.option("--sign_secret", default=None, type=str)
140166
def receive_gridpool_orders(
141167
url: str,
142168
auth_key: str,
143169
*,
144-
start: datetime,
170+
delivery_from: datetime | None,
171+
delivery_to: datetime | None,
145172
gid: int,
146173
sign_secret: str | None = None,
147174
) -> None:
@@ -150,7 +177,8 @@ def receive_gridpool_orders(
150177
run_list_gridpool_orders(
151178
url=url,
152179
auth_key=auth_key,
153-
delivery_start=start,
180+
delivery_from=delivery_from,
181+
delivery_to=delivery_to,
154182
gid=gid,
155183
sign_secret=sign_secret,
156184
)

0 commit comments

Comments
 (0)