codex_services.booking.slot_master.chain_finder
chain_finder
codex_services.booking.slot_master.chain_finder
Main algorithm for finding slot chains for N services.
Framework-agnostic (does not depend on Django/ORM) -- works only with DTOs and datetime. Acts as an orchestrator for SlotCalculator and BookingValidator.
Performance principle
Inside recursive search (backtracking) we use lightweight _SlotCandidate with slots -- without Pydantic validation. Pydantic objects (SingleServiceSolution, BookingChainSolution) are created ONLY once for each found solution. This is critical with a large number of services and resources.
Imports
from codex_services.booking.slot_master import ChainFinder
Classes
ChainFinder
Finds combinations of time slots for N services (booking chains).
Core algorithm: recursive backtracking. Iterates over possible resources and their free windows for each service. Checks for the absence of conflicts with already assigned services in the chain.
Examples:
1 service, any free resource:
finder = ChainFinder(step_minutes=30)
request = BookingEngineRequest(
service_requests=[
ServiceRequest(service_id="5", duration_minutes=60,
possible_resource_ids=["1", "2"])
],
booking_date=date(2024, 5, 10),
mode=BookingMode.SINGLE_DAY,
)
result = finder.find(request, resources_availability)
# result.solutions -- list of BookingChainSolution
# result.get_unique_start_times() -> ["09:00", "09:30", "10:00", ...]
2 services on the same day:
request = BookingEngineRequest(
service_requests=[svc_task_1, svc_task_2],
booking_date=date(2024, 5, 10),
mode=BookingMode.SINGLE_DAY,
)
result = finder.find(request, availability)
Booking a specific resource (RESOURCE_LOCKED):
# Just pass possible_resource_ids=[locked_resource_id]
# and mode=BookingMode.RESOURCE_LOCKED
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_minutes
|
int
|
Grid slot step (30 min by default). |
30
|
min_start
|
datetime | None
|
Minimum acceptable start time of the first service. None = no restriction (e.g., in tests). |
None
|
Source code in src/codex_services/booking/slot_master/chain_finder.py
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
Functions
find(request, resources_availability, max_solutions=50, max_unique_starts=None)
Unified engine entry point. Delegates to the appropriate mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BookingEngineRequest
|
Input request with services, date, and mode. |
required |
resources_availability
|
dict[str, MasterAvailability]
|
Dictionary {resource_id: MasterAvailability}. Usually prepared by an AvailabilityAdapter. Keys -- strings (resource identifiers). |
required |
max_solutions
|
int
|
Maximum number of options the engine will return. Does not affect correctness -- only completeness. |
50
|
max_unique_starts
|
int | None
|
Stop after finding N unique start times (based on items[0].start_time). None = no limit. Example: max_unique_starts=8 -> only the closest 8 slots, even if there are 16 in a day. Halves engine iterations. |
None
|
Returns:
| Type | Description |
|---|---|
EngineResult
|
EngineResult with found solutions. |
EngineResult
|
solutions sorted by the start time of the first service. |
Source code in src/codex_services/booking/slot_master/chain_finder.py
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 | |
find_nearest(request, get_availability_for_date, search_from, search_days=60, max_solutions_per_day=1)
Searches for the first day with available slots in the search_days range.
Used for
- Rebooking: resource is sick -> find a new date for N appointments
- Waitlist: closest free slot to notify the client
- MULTI_DAY planning: find the first day the chain fits
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BookingEngineRequest
|
Request (booking_date will be replaced for each checked day). |
required |
get_availability_for_date
|
Callable[[date], dict[str, MasterAvailability]]
|
callable(date) -> dict[str, MasterAvailability]. Called for each checked day. Wraps DjangoAvailabilityAdapter in the Django layer. |
required |
search_from
|
date
|
Date to start the search from (inclusive). |
required |
search_days
|
int
|
Maximum days to check. Defaults to 60. |
60
|
max_solutions_per_day
|
int
|
How many solutions to look for per day. 1 = fast mode (stop at the first one). |
1
|
Returns:
| Type | Description |
|---|---|
EngineResult
|
EngineResult of the first day with solutions. |
EngineResult
|
If nothing found in search_days — EngineResult(solutions=[]). |
Example (Django layer): adapter = DjangoAvailabilityAdapter() resource_ids = [...]
def get_avail(d):
return adapter.build_resources_availability(resource_ids, d)
result = finder.find_nearest(request, get_avail, search_from=date.today())
if result.has_solutions:
print(result.best.starts_at) # date and time of the new slot
Source code in src/codex_services/booking/slot_master/chain_finder.py
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 | |