Skip to content

Commit 4678858

Browse files
committed
Remove RunningState and change method running to started property
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 7251a66 commit 4678858

File tree

3 files changed

+24
-62
lines changed

3 files changed

+24
-62
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## New Features
44

55
* Update BaseApiClient to get the http2 keepalive feature.
6-
* Some Methods from the high-level API have been moved to this repo: The dispatch class now offers: `until`, `running`, `next_run` and `next_run_after`.
6+
* Some Methods from the high-level API have been moved to this repo: The dispatch class now offers: `until`, `started`, `next_run` and `next_run_after`.
77

88
## Bug Fixes
99

src/frequenz/client/dispatch/types.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from dataclasses import dataclass
88
from datetime import datetime, timedelta, timezone
9-
from enum import Enum, IntEnum
9+
from enum import IntEnum
1010
from typing import Any, cast
1111

1212
# pylint: disable=no-name-in-module
@@ -115,19 +115,6 @@ class TimeIntervalFilter:
115115
"""Filter by end_time < end_to."""
116116

117117

118-
class RunningState(Enum):
119-
"""The running state of a dispatch."""
120-
121-
RUNNING = "RUNNING"
122-
"""The dispatch is running."""
123-
124-
STOPPED = "STOPPED"
125-
"""The dispatch is stopped."""
126-
127-
DIFFERENT_TYPE = "DIFFERENT_TYPE"
128-
"""The dispatch is for a different type."""
129-
130-
131118
@dataclass(kw_only=True, frozen=True)
132119
class Dispatch: # pylint: disable=too-many-instance-attributes
133120
"""Represents a dispatch operation within a microgrid system."""
@@ -173,36 +160,33 @@ class Dispatch: # pylint: disable=too-many-instance-attributes
173160
update_time: datetime
174161
"""The last update time of the dispatch in UTC. Set when a dispatch is modified."""
175162

176-
def running(self, type_: str) -> RunningState:
177-
"""Check if the dispatch is currently supposed to be running.
163+
@property
164+
def started(self) -> bool:
165+
"""Check if the dispatch has started.
178166
179-
Args:
180-
type_: The type of the dispatch that should be running.
167+
A dispatch is considered started if the current time is after the start
168+
time but before the end time.
181169
182-
Returns:
183-
RUNNING if the dispatch is running,
184-
STOPPED if it is stopped,
185-
DIFFERENT_TYPE if it is for a different type.
170+
Recurring dispatches are considered started if the current time is after
171+
the start time of the last occurrence but before the end time of the
172+
last occurrence.
186173
"""
187-
if self.type != type_:
188-
return RunningState.DIFFERENT_TYPE
189-
190174
if not self.active:
191-
return RunningState.STOPPED
175+
return False
192176

193177
now = datetime.now(tz=timezone.utc)
194178

195179
if now < self.start_time:
196-
return RunningState.STOPPED
180+
return False
197181

198182
# A dispatch without duration is always running, once it started
199183
if self.duration is None:
200-
return RunningState.RUNNING
184+
return True
201185

202186
if until := self._until(now):
203-
return RunningState.RUNNING if now < until else RunningState.STOPPED
187+
return now < until
204188

205-
return RunningState.STOPPED
189+
return False
206190

207191
@property
208192
def until(self) -> datetime | None:

tests/test_dispatch.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from frequenz.client.common.microgrid.components import ComponentCategory
1313
from frequenz.client.dispatch.recurrence import Frequency, RecurrenceRule, Weekday
14-
from frequenz.client.dispatch.types import Dispatch, RunningState
14+
from frequenz.client.dispatch.types import Dispatch
1515

1616
# Define a fixed current time for testing to avoid issues with datetime.now()
1717
CURRENT_TIME = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
@@ -37,84 +37,62 @@ def dispatch_base() -> Dispatch:
3737

3838
@time_machine.travel(CURRENT_TIME)
3939
@pytest.mark.parametrize(
40-
"dispatch_type, requested_type, active, start_time_offset, duration, expected_state",
40+
"active, start_time_offset, duration, expected_state",
4141
[
42-
# Dispatch type does not match the requested type
43-
(
44-
"TypeA",
45-
"TypeB",
46-
True,
47-
timedelta(minutes=-10),
48-
timedelta(minutes=20),
49-
RunningState.DIFFERENT_TYPE,
50-
),
5142
# Dispatch is inactive
5243
(
53-
"TypeA1",
54-
"TypeA1",
5544
False,
5645
timedelta(minutes=-10),
5746
timedelta(minutes=20),
58-
RunningState.STOPPED,
47+
False,
5948
),
6049
# Current time is before the start time
6150
(
62-
"TypeA2",
63-
"TypeA2",
6451
True,
6552
timedelta(minutes=10),
6653
timedelta(minutes=20),
67-
RunningState.STOPPED,
54+
False,
6855
),
6956
# Dispatch with infinite duration
7057
(
71-
"TypeA3",
72-
"TypeA3",
7358
True,
7459
timedelta(minutes=-10),
7560
None,
76-
RunningState.RUNNING,
61+
True,
7762
),
7863
# Dispatch is currently running
7964
(
80-
"TypeA4",
81-
"TypeA4",
8265
True,
8366
timedelta(minutes=-10),
8467
timedelta(minutes=20),
85-
RunningState.RUNNING,
68+
True,
8669
),
8770
# Dispatch duration has passed
8871
(
89-
"TypeA5",
90-
"TypeA5",
9172
True,
9273
timedelta(minutes=-30),
9374
timedelta(minutes=20),
94-
RunningState.STOPPED,
75+
False,
9576
),
9677
],
9778
)
9879
# pylint: disable=too-many-arguments,too-many-positional-arguments
9980
def test_dispatch_running(
10081
dispatch_base: Dispatch,
101-
dispatch_type: str,
102-
requested_type: str,
10382
active: bool,
10483
start_time_offset: timedelta,
10584
duration: timedelta | None,
106-
expected_state: RunningState,
85+
expected_state: bool,
10786
) -> None:
10887
"""Test the running method of the Dispatch class."""
10988
dispatch = replace(
11089
dispatch_base,
111-
type=dispatch_type,
11290
start_time=CURRENT_TIME + start_time_offset,
11391
duration=duration,
11492
active=active,
11593
)
11694

117-
assert dispatch.running(requested_type) == expected_state
95+
assert dispatch.started == expected_state
11896

11997

12098
@time_machine.travel(CURRENT_TIME)

0 commit comments

Comments
 (0)