Skip to content

Commit 6efd3d8

Browse files
authored
Add Dispatch.end_time and print it in dispatch-cli (#142)
2 parents ec17a15 + d80afd6 commit 6efd3d8

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Frequenz Dispatch Client Library Release Notes
22

3+
## Features
4+
5+
* `Dispatch.end_time` has been added to the `Dispatch` class, which is the time when the dispatch ended as calculated by the server. `dispatch-cli` will also print this time.
6+
37
## Bug Fixes
48

5-
* Fix that duration=0 was sent & received as None.
69
* Fix that `dispatch-cli stream` would try to print an event as dispatch, causing an exception.

src/frequenz/client/dispatch/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def format_line(key: str, value: str, color: str = "cyan") -> str:
148148
lines.append(format_line("Recurrence", "None"))
149149
lines.append(format_line("Create Time", format_datetime(dispatch.create_time)))
150150
lines.append(format_line("Update Time", format_datetime(dispatch.update_time)))
151+
lines.append(format_line("End Time", format_datetime(dispatch.end_time)))
151152

152153
# Combine all lines
153154
dispatch_info: str = "\n".join(lines)

src/frequenz/client/dispatch/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class Dispatch: # pylint: disable=too-many-instance-attributes
158158
update_time: datetime
159159
"""The last update time of the dispatch in UTC. Set when a dispatch is modified."""
160160

161+
end_time: datetime | None = None
162+
"""The end time of the dispatch in UTC.
163+
164+
Calculated and sent by the backend service.
165+
"""
166+
161167
@property
162168
def started(self) -> bool:
163169
"""Check if the dispatch has started.
@@ -290,6 +296,7 @@ def from_protobuf(cls, pb_object: PBDispatch) -> "Dispatch":
290296
type=pb_object.data.type,
291297
create_time=to_datetime(pb_object.metadata.create_time),
292298
update_time=to_datetime(pb_object.metadata.modification_time),
299+
end_time=to_datetime(pb_object.metadata.end_time),
293300
start_time=to_datetime(pb_object.data.start_time),
294301
duration=(
295302
timedelta(seconds=pb_object.data.duration)
@@ -317,6 +324,7 @@ def to_protobuf(self) -> PBDispatch:
317324
dispatch_id=self.id,
318325
create_time=to_timestamp(self.create_time),
319326
modification_time=to_timestamp(self.update_time),
327+
end_time=to_timestamp(self.end_time),
320328
),
321329
data=DispatchData(
322330
type=self.type,

tests/test_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import random
88
from dataclasses import replace
99
from datetime import timedelta
10+
from functools import partial
1011

1112
import grpc
1213
from pytest import raises
@@ -38,6 +39,7 @@ def _update_metadata(dispatch: Dispatch, created: Dispatch) -> Dispatch:
3839
id=created.id,
3940
create_time=created.create_time,
4041
update_time=created.update_time,
42+
end_time=created.end_time,
4143
)
4244

4345

@@ -97,7 +99,16 @@ async def test_list_dispatches(
9799
dispatches = client.list(microgrid_id=1)
98100
async for page in dispatches:
99101
for dispatch in page:
100-
assert dispatch in client.dispatches(microgrid_id=1)
102+
# First find matching id in client.dispatches, then compare
103+
service_side_dispatch = next(
104+
filter(
105+
partial(lambda d, md: d.id == md.id, md=dispatch),
106+
client.dispatches(microgrid_id=1),
107+
),
108+
None,
109+
)
110+
111+
assert dispatch == service_side_dispatch
101112

102113

103114
async def test_list_dispatches_no_duration(

tests/test_proto.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def test_dispatch() -> None:
9797
create_time=datetime(2023, 1, 1, tzinfo=timezone.utc),
9898
update_time=datetime(2023, 1, 1, tzinfo=timezone.utc),
9999
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
100+
end_time=datetime(2024, 10, 20, tzinfo=timezone.utc),
100101
duration=timedelta(days=10),
101102
target=[1, 2, 3],
102103
active=True,
@@ -117,6 +118,7 @@ def test_dispatch() -> None:
117118
create_time=datetime(2024, 3, 10, tzinfo=timezone.utc),
118119
update_time=datetime(2024, 3, 11, tzinfo=timezone.utc),
119120
start_time=datetime(2024, 11, 10, tzinfo=timezone.utc),
121+
end_time=datetime(2024, 11, 20, tzinfo=timezone.utc),
120122
duration=timedelta(seconds=20),
121123
target=[ComponentCategory.BATTERY],
122124
active=False,

0 commit comments

Comments
 (0)