codex_services.booking._shared.calculator
calculator
codex_services.booking._shared.calculator
Basic slot operations inside time windows.
This is "math without ORM" — works with datetime objects, does not know about Django. Used inside ChainFinder, and can also be applied independently.
Imports
from codex_services.booking import SlotCalculator
Classes
SlotCalculator
Slot generator inside a free time window (sliding window).
Does not depend on ORM. Works with datetime objects.
Examples:
calc = SlotCalculator(step_minutes=30)
# Get all possible slots in a window from 9:00 to 12:00 for a 60 min service:
slots = calc.find_slots_in_window(
window_start=datetime(2024, 5, 10, 9, 0),
window_end=datetime(2024, 5, 10, 12, 0),
duration_minutes=60,
)
# -> [datetime(2024,5,10,9,0), datetime(2024,5,10,9,30), ...]
# Get free windows from a working day:
windows = calc.merge_free_windows(
work_start=datetime(2024,5,10,9,0),
work_end=datetime(2024,5,10,18,0),
busy_intervals=[(datetime(2024,5,10,10,0), datetime(2024,5,10,11,0))],
)
# -> [(9:00, 10:00), (11:10, 18:00)]
Source code in src/codex_services/booking/_shared/calculator.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 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 | |
Functions
__init__(step_minutes=30)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_minutes
|
int
|
Grid slot step in minutes. Defaults to 30. Determines the interval at which slots are offered. |
30
|
Source code in src/codex_services/booking/_shared/calculator.py
46 47 48 49 50 51 52 53 54 55 | |
find_slots_in_window(window_start, window_end, duration_minutes, min_start=None, grid_anchor=None)
Returns a list of possible start times within the window.
Algorithm: sliding window with a step of self.step_minutes. Each candidate is checked: does the service [slot, slot+duration] fit within the end of the window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_start
|
datetime
|
Start of the free time window. |
required |
window_end
|
datetime
|
End of the free time window. |
required |
duration_minutes
|
int
|
Duration of the service in minutes. |
required |
min_start
|
datetime | None
|
Minimum acceptable start time (e.g., "no earlier than 15 minutes from the current time"). None = no limit. |
None
|
grid_anchor
|
datetime | None
|
Anchor for grid alignment (e.g., shifts start). If provided, slots will be aligned to (grid_anchor + N * step). |
None
|
Returns:
| Type | Description |
|---|---|
list[datetime]
|
List of datetime — possible service start moments. |
list[datetime]
|
Empty list if the service does not fit in the window. |
Example
Window 9:00-11:00, service 60 min, step 30 min:
-> [9:00, 9:30, 10:00] (10:30 + 60 min = 11:30, does not fit)
Source code in src/codex_services/booking/_shared/calculator.py
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 | |
merge_free_windows(work_start, work_end, busy_intervals, break_interval=None, buffer_minutes=0, min_duration_minutes=0)
Calculates a list of free windows from a working day.
Subtracts from the interval [work_start, work_end]: - busy_intervals (booked appointments) - break_interval (resource's break) - buffer_minutes (buffer after each booked interval)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work_start
|
datetime
|
Start of the resource's working day. |
required |
work_end
|
datetime
|
End of the resource's working day. |
required |
busy_intervals
|
list[tuple[datetime, datetime]]
|
List of busy segments [(start, end), ...]. |
required |
break_interval
|
tuple[datetime, datetime] | None
|
Resource's break (lunch), tuple (start, end) or None. |
None
|
buffer_minutes
|
int
|
Buffer in minutes added after each booked appointment. |
0
|
min_duration_minutes
|
int
|
Minimum window length. Windows shorter than this value are discarded as "junk". |
0
|
Returns:
| Type | Description |
|---|---|
list[tuple[datetime, datetime]]
|
List of free windows [(start, end), ...] sorted by time. |
Example
# Work day 9:00-18:00, booked 10:00-11:00, lunch 13:00-14:00
windows = calc.merge_free_windows(
work_start=dt(9,0), work_end=dt(18,0),
busy_intervals=[(dt(10,0), dt(11,0))],
break_interval=(dt(13,0), dt(14,0)),
buffer_minutes=10,
)
# -> [(9:00, 10:00), (11:10, 13:00), (14:00, 18:00)]
Source code in src/codex_services/booking/_shared/calculator.py
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 | |
find_gaps(free_windows, min_gap_minutes)
Finds all free windows of minimum length in the resource's schedule.
Used for
- Load analysis: how much free time the resource has
- Notifications: resource is free for N minutes -> offer to user
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
free_windows
|
list[tuple[datetime, datetime]]
|
Free windows from ResourceAvailability.free_windows |
required |
min_gap_minutes
|
int
|
Minimum window length in minutes. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[datetime, datetime, int]]
|
List (window_start, window_end, duration_minutes) — windows |
list[tuple[datetime, datetime, int]]
|
longer than min_gap_minutes, sorted by start time. |
Examples:
# Search for windows >= 60 minutes:
gaps = calc.find_gaps(free_windows, min_gap_minutes=60)
# -> [(9:00, 10:00, 60), (11:30, 14:00, 150), (16:00, 18:00, 120)]
Source code in src/codex_services/booking/_shared/calculator.py
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 | |
split_window_by_service(window_start, window_end, service_start, service_end)
Splits a free window into parts around a booked service segment.
Used for dynamic calculation: "if we put a service here — what windows will remain free for the next ones?"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_start
|
datetime
|
Start of the free window. |
required |
window_end
|
datetime
|
End of the free window. |
required |
service_start
|
datetime
|
Start of the booked segment (must be inside the window). |
required |
service_end
|
datetime
|
End of the booked segment (must be inside the window). |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[datetime, datetime]]
|
List of remaining free windows (0, 1, or 2 elements). |
Example
Window 9:00-18:00, service 11:00-12:00:
split_window_by_service(9:00, 18:00, 11:00, 12:00)
-> [(9:00, 11:00), (12:00, 18:00)]
Service at the start of the window:
split_window_by_service(9:00, 18:00, 9:00, 11:00)
-> [(11:00, 18:00)]
Source code in src/codex_services/booking/_shared/calculator.py
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 | |