|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sedate |
| 4 | + |
| 5 | +from datetime import datetime, timedelta |
| 6 | + |
| 7 | +from sqlalchemy import types |
| 8 | +from sqlalchemy.orm import object_session |
| 9 | +from sqlalchemy.schema import Column |
| 10 | +from sqlalchemy.schema import Index |
| 11 | + |
| 12 | +from libres.db.models.base import ORMBase |
| 13 | +from libres.db.models.types import UUID, UTCDateTime |
| 14 | +from libres.db.models.other import OtherModels |
| 15 | +from libres.db.models.timespan import Timespan |
| 16 | +from libres.db.models.timestamp import TimestampMixin |
| 17 | + |
| 18 | + |
| 19 | +from typing import Literal |
| 20 | +from typing import TYPE_CHECKING |
| 21 | +if TYPE_CHECKING: |
| 22 | + import uuid |
| 23 | + from sedate.types import TzInfoOrName |
| 24 | + from sqlalchemy.orm import Query |
| 25 | + |
| 26 | + from libres.db.models import Allocation |
| 27 | + |
| 28 | + |
| 29 | +class ReservationBlocker(TimestampMixin, ORMBase, OtherModels): |
| 30 | + """Describes a reservation blocker. |
| 31 | +
|
| 32 | + Blockers can be used to signify that an allocation will be blocked for |
| 33 | + the specified time span, in order to e.g. perform cleaning duties on |
| 34 | + the relevant resource. |
| 35 | +
|
| 36 | + """ |
| 37 | + |
| 38 | + __tablename__ = 'reservation_blockers' |
| 39 | + |
| 40 | + id: Column[int] = Column( |
| 41 | + types.Integer(), |
| 42 | + primary_key=True, |
| 43 | + autoincrement=True |
| 44 | + ) |
| 45 | + |
| 46 | + token: Column[uuid.UUID] = Column( |
| 47 | + UUID(), |
| 48 | + nullable=False, |
| 49 | + ) |
| 50 | + |
| 51 | + target: Column[uuid.UUID] = Column( |
| 52 | + UUID(), |
| 53 | + nullable=False, |
| 54 | + ) |
| 55 | + |
| 56 | + target_type: Column[Literal['group', 'allocation']] = Column( |
| 57 | + types.Enum( # type:ignore[arg-type] |
| 58 | + 'group', 'allocation', |
| 59 | + name='reservation_blocker_target_type' |
| 60 | + ), |
| 61 | + nullable=False |
| 62 | + ) |
| 63 | + |
| 64 | + resource: Column[uuid.UUID] = Column( |
| 65 | + UUID(), |
| 66 | + nullable=False |
| 67 | + ) |
| 68 | + |
| 69 | + start: Column[datetime | None] = Column( |
| 70 | + UTCDateTime(timezone=False), |
| 71 | + nullable=True |
| 72 | + ) |
| 73 | + |
| 74 | + end: Column[datetime | None] = Column( |
| 75 | + UTCDateTime(timezone=False), |
| 76 | + nullable=True |
| 77 | + ) |
| 78 | + |
| 79 | + timezone: Column[str | None] = Column( |
| 80 | + types.String(), |
| 81 | + nullable=True |
| 82 | + ) |
| 83 | + |
| 84 | + reason: Column[str | None] = Column( |
| 85 | + types.String(), |
| 86 | + nullable=True |
| 87 | + ) |
| 88 | + |
| 89 | + __table_args__ = ( |
| 90 | + Index('blocker_target_ix', 'target', 'id'), |
| 91 | + ) |
| 92 | + |
| 93 | + def target_allocations( |
| 94 | + self, |
| 95 | + masters_only: bool = True |
| 96 | + ) -> Query[Allocation]: |
| 97 | + """ Returns the allocations this blocker is targeting. |
| 98 | +
|
| 99 | + """ |
| 100 | + Allocation = self.models.Allocation # noqa: N806 |
| 101 | + query = object_session(self).query(Allocation) |
| 102 | + query = query.filter(Allocation.group == self.target) |
| 103 | + |
| 104 | + if masters_only: |
| 105 | + query = query.filter(Allocation.resource == Allocation.mirror_of) |
| 106 | + |
| 107 | + # order by date |
| 108 | + query = query.order_by(Allocation._start) |
| 109 | + |
| 110 | + return query # type: ignore[no-any-return] |
| 111 | + |
| 112 | + def display_start( |
| 113 | + self, |
| 114 | + timezone: TzInfoOrName | None = None |
| 115 | + ) -> datetime: |
| 116 | + """Does nothing but to form a nice pair to display_end.""" |
| 117 | + assert self.start is not None |
| 118 | + if timezone is None: |
| 119 | + assert self.timezone is not None |
| 120 | + timezone = self.timezone |
| 121 | + return sedate.to_timezone(self.start, timezone) |
| 122 | + |
| 123 | + def display_end( |
| 124 | + self, |
| 125 | + timezone: TzInfoOrName | None = None |
| 126 | + ) -> datetime: |
| 127 | + """Returns the end plus one microsecond (nicer display).""" |
| 128 | + assert self.end is not None |
| 129 | + if timezone is None: |
| 130 | + assert self.timezone is not None |
| 131 | + timezone = self.timezone |
| 132 | + |
| 133 | + end = self.end + timedelta(microseconds=1) |
| 134 | + return sedate.to_timezone(end, timezone) |
| 135 | + |
| 136 | + def timespans(self) -> list[Timespan]: |
| 137 | + """ Returns the timespans targeted by this blocker. |
| 138 | +
|
| 139 | + The result is a list of :class:`~libres.db.models.timespan.Timespan` |
| 140 | + timespans. The start and end are the start and end dates of the |
| 141 | + referenced allocations. |
| 142 | +
|
| 143 | + The timespans are ordered by start. |
| 144 | +
|
| 145 | + """ |
| 146 | + |
| 147 | + if self.target_type == 'allocation': |
| 148 | + # we don't need to hit the database in this case |
| 149 | + assert self.start is not None |
| 150 | + assert self.end is not None |
| 151 | + return [ |
| 152 | + Timespan(self.start, self.end + timedelta(microseconds=1)) |
| 153 | + ] |
| 154 | + elif self.target_type == 'group': |
| 155 | + return [ |
| 156 | + Timespan(allocation.start, allocation.end) |
| 157 | + for allocation in self.target_allocations() |
| 158 | + ] |
| 159 | + else: |
| 160 | + raise NotImplementedError |
| 161 | + |
| 162 | + @property |
| 163 | + def title(self) -> str | None: |
| 164 | + return self.reason |
0 commit comments