coverage_reservations
coverage_reservations
#
Per-user location reservations — spread a crowd across a survey area.
The spatial sibling of
Reservations: where
that hands out times released by the clock, this hands out places
released by proximity. A void-and-cluster pass computes a batch of well-spread
target locations once (it is expensive); CoverageReservations hands them out
one per user — the one closest to each requester when their location is
known — stably, and releases a user's location once a capture lands within
threshold of it, so the next user is sent somewhere still uncovered.
A handout is a lease: the location is held for the requesting user for at
most lease (30 minutes by default), and that user is handed the same place
on every request meanwhile. Every location is in exactly one of three states::
reserve() capture within threshold
in batch ─────────────▶ held ─────────────────────────────▶ retired
▲ │ (the spot is covered)
│ lease lapses │
└─────────────────────┘ (nobody captured it)
Because a lapsed location flows back into the batch even after the batch was refilled, the number of live locations can briefly exceed a batch.
Locations persist in a Scope
as JSON: the batch as cg.Location codecs, each held lease as its location
plus a tz-faithful ISO-8601 expiry. The batch is held between requests because
recomputing it every call is wasteful.
Example
CoverageReservations
#
Hands each user a distinct location on a lease, recycling it when it lapses.
Construction claims scope: it is stamped with this class's persisted
format version, and any state written in another shape — including by an
unrelated consumer — is cleared and rebuilt rather than misread
(ensure_format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Scope
|
Where the batch and reservations persist, isolated to this
scheduler's use (e.g. |
required |
distance
|
Callable[[Location, Location], float]
|
A distance between two |
required |
threshold
|
float
|
A reservation is released once a capture lies within this
distance of it, in the units |
1.0
|
lease
|
timedelta
|
How long a handout is held before, absent a covering capture, the location returns to the batch for reallocation. Defaults to 30 minutes. |
timedelta(minutes=30)
|
Source code in capturegraph-lib/capturegraph/scheduling/organize/coverage_reservations.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
exhausted
property
#
Whether the batch has no locations left to hand out.
__init__(scope, *, distance, threshold=1.0, lease=timedelta(minutes=30))
#
Claim scope, resetting it if its stamped format has changed.
Source code in capturegraph-lib/capturegraph/scheduling/organize/coverage_reservations.py
refill(locations)
#
Replace the batch with locations, kept in the order given.
Hand-out order is decided per request by proximity to the asker (see [reserve][]), so the batch itself needs no ordering.
Source code in capturegraph-lib/capturegraph/scheduling/organize/coverage_reservations.py
release(user_id)
#
Release a user's reservation without returning its location to the batch.
reserve(user_id, *, at=None, fulfilled_by=(), now=None)
#
The location this user holds, leasing one from the batch if none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
The user asking for a location. |
required |
at
|
Location | None
|
The requester's current location, if known. The batch location closest to it is leased; without it, the batch is leased in insertion order (FIFO). |
None
|
fulfilled_by
|
Sequence[Location]
|
Captured locations; a reservation within |
()
|
now
|
datetime | None
|
The reference time; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Location | None
|
The user's reserved location, or |
Location | None
|
and the user holds nothing. |