capturegraph.scheduling.organize.batched_distance_store
#
Batched Distance Store - Distribution-Aware Assignment Management#
Manages session assignments using a distance function to detect when assignments have been fulfilled by new captures. As new sessions are captured, assignments that are "close enough" to a new capture are automatically removed (fulfilled).
Example
import capturegraph.scheduling as cgsh
store = cgsh.organize.BatchedDistanceStore(
persistence=persistence,
sessions=target.captures,
distance_fn=cgsh.distance.location(sigma_m=50),
threshold=1.0,
)
# Add candidate slots to assign from
store.set_batch(candidate_slots)
# Assign to a user
slot = store.request_slot(user_id)
# Later, create new store with updated sessions to detect fulfillment
store = cgsh.organize.BatchedDistanceStore(
persistence=persistence,
sessions=target.captures, # New captures fulfill nearby assignments
distance_fn=cgsh.distance.location(sigma_m=50),
threshold=1.0,
)
BatchedDistanceStore
#
Manages slot assignments with distance-aware fulfillment detection.
When new sessions (captures) are added, the store computes distances from
the new captures to all currently assigned slots. Any assigned slot within
threshold distance of a new capture is considered "fulfilled" and removed.
State is persisted in the provided persistence dict:
- persistence.batched_assignments: user_id -> assigned slot
- persistence.batched_batch: list of unassigned candidate slots
- persistence.batched_prior_count: session count baseline
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
persistence
|
Dict
|
Server persistence dict (must support attribute access). |
required |
sessions
|
List[Dict[Any]]
|
Current list of captured sessions. Used to establish baseline count and detect new captures. Sessions beyond the prior count are checked for fulfillment of existing assignments. |
required |
distance_fn
|
Callable[[Any, Any], float]
|
A distance function |
required |
threshold
|
float
|
Distance threshold for fulfillment. Assignments with distance < threshold to any new capture are removed. Default is 1.0. |
1.0
|
Example
import capturegraph.scheduling as cgsh
store = cgsh.organize.BatchedDistanceStore(
persistence=persistence,
sessions=target.captures,
distance_fn=cgsh.distance.location(sigma_m=50),
threshold=1.0,
)
store.set_batch(candidate_slots)
slot = store.request_slot("user_123") # Returns first available
slot = store.request_slot("user_123") # Returns same slot (sticky)
# When new captures arrive, create new store to detect fulfillment
store = cgsh.organize.BatchedDistanceStore(
persistence=persistence,
sessions=target.captures, # Fulfilled assignments removed
distance_fn=cgsh.distance.location(sigma_m=50),
threshold=1.0,
)
Source code in capturegraph-lib/capturegraph/scheduling/organize/batched_distance_store.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
is_empty()
#
request_slot(user_id)
#
Request a slot for a user.
If the user already has an assignment, returns it (sticky assignment). Otherwise, pops the next slot from the batch and assigns it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
The user's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
Dict[Any] | None
|
The assigned slot, or None if the batch is empty. |
Example
Source code in capturegraph-lib/capturegraph/scheduling/organize/batched_distance_store.py
set_batch(slots)
#
Replace the current batch with new candidate slots.
Slots are reordered using a greedy nearest-neighbor TSP approximation so that consecutive slots are close together, making it easier for users to visit them in sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slots
|
list[Dict[Any]]
|
New candidate slots to assign from. |
required |