1,191 bytes · the Python implementation · view raw
from typing import Optional, Sequence
from .monitor_maintenance_window_types import MaintenanceWindow
def active_maintenance(windows: Sequence[MaintenanceWindow], at: int) -> Optional[MaintenanceWindow]:
"""The window with start <= at < end. Half-open, so a window ending at 02:00 and the next starting at 02:00 never both apply. When several overlap, the one that started first wins (ties: the first in the list), so the answer does not depend on how the caller sorted them. Every window is checked, not only the active ones: a backwards window is a config error that would otherwise silently never apply."""ifnot isinstance(at, int) or isinstance(at, bool):
raise ValueError("at must be a whole second, received %r" % (at,))
best: Optional[MaintenanceWindow] = Nonefor w in windows:
if w.end <= w.start:
raise ValueError('maintenance window "%s" must end after it starts: start %d, end %d' % (w.name, w.start, w.end))
if w.start <= at < w.end and (best isNoneor w.start < best.start):
best = w
returnNoneif best isNoneelse MaintenanceWindow(name=best.name, start=best.start, end=best.end)