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.""" if not isinstance(at, int) or isinstance(at, bool): raise ValueError("at must be a whole second, received %r" % (at,)) best: Optional[MaintenanceWindow] = None for 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 is None or w.start < best.start): best = w return None if best is None else MaintenanceWindow(name=best.name, start=best.start, end=best.end)