Skip to content

Commit 95064f5

Browse files
committed
Handle & Imitate start_time rounding behavior of server
Round start_time seconds to have the same behavior as the gRPC server frequenz-io/frequenz-service-dispatch#77 Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 031c2fe commit 95064f5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/frequenz/client/dispatch/_internal_types.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def from_protobuf(
8181
return DispatchCreateRequest(
8282
microgrid_id=pb_object.microgrid_id,
8383
type=pb_object.type,
84-
start_time=to_datetime(pb_object.start_time),
84+
start_time=rounded_start_time(to_datetime(pb_object.start_time)),
8585
duration=timedelta(seconds=pb_object.duration),
8686
selector=component_selector_from_protobuf(pb_object.selector),
8787
active=pb_object.is_active,
@@ -109,3 +109,20 @@ def to_protobuf(self) -> PBDispatchCreateRequest:
109109
pb_request.recurrence.CopyFrom(self.recurrence.to_protobuf())
110110

111111
return pb_request
112+
113+
114+
def rounded_start_time(start_time: datetime) -> datetime:
115+
"""Round the start time to the nearest second.
116+
117+
Args:
118+
start_time: The start time to round.
119+
120+
Returns:
121+
The rounded start time.
122+
"""
123+
# Round start_time seconds to have the same behavior as the gRPC server
124+
# https://github.com/frequenz-io/frequenz-service-dispatch/issues/77
125+
new_seconds = start_time.second + start_time.microsecond / 1_000_000
126+
start_time = start_time.replace(microsecond=0, second=0)
127+
start_time += timedelta(seconds=round(new_seconds))
128+
return start_time

src/frequenz/client/dispatch/test/generator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from frequenz.client.common.microgrid.components import ComponentCategory
1010

11+
from .._internal_types import rounded_start_time
1112
from ..types import Dispatch, EndCriteria, Frequency, RecurrenceRule, Weekday
1213

1314

@@ -84,8 +85,10 @@ def generate_dispatch(self, microgrid_id: int | None = None) -> Dispatch:
8485
update_time=create_time + timedelta(seconds=self._rng.randint(0, 1000000)),
8586
microgrid_id=microgrid_id or self._rng.randint(0, 100),
8687
type=str(self._rng.randint(0, 100_000)),
87-
start_time=datetime.now().astimezone(timezone.utc)
88-
+ timedelta(seconds=self._rng.randint(0, 1000000)),
88+
start_time=rounded_start_time(
89+
datetime.now(tz=timezone.utc)
90+
+ timedelta(seconds=self._rng.randint(0, 1000000))
91+
),
8992
duration=timedelta(seconds=self._rng.randint(0, 1000000)),
9093
selector=self._rng.choice( # type: ignore
9194
[

0 commit comments

Comments
 (0)