mirror of
https://github.com/ANewProfile/breadcrumbs.git
synced 2026-09-23 07:19:56 +02:00
95 lines
3.7 KiB
Python
95 lines
3.7 KiB
Python
# backend/services/scheduler.py
|
|
from datetime import datetime, timedelta, timezone
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
def _earliest_slot(now: datetime) -> datetime:
|
|
candidate = (now + timedelta(minutes=10)).replace(second=0, microsecond=0)
|
|
remainder = candidate.minute % 5
|
|
if remainder:
|
|
candidate += timedelta(minutes=5 - remainder)
|
|
return candidate
|
|
|
|
|
|
def compute_free_blocks(events: list, day_start: str = "08:00", day_end: str = "22:00", tz_str: str = "America/New_York", lookahead_days: int = 7, no_go_zones: list | None = None) -> list:
|
|
tz = ZoneInfo(tz_str)
|
|
no_go_zones = no_go_zones or []
|
|
|
|
busy = []
|
|
for e in events:
|
|
start_raw = e.get("start", {})
|
|
end_raw = e.get("end", {})
|
|
if "dateTime" not in start_raw:
|
|
continue
|
|
start = datetime.fromisoformat(start_raw["dateTime"]).astimezone(tz)
|
|
end = datetime.fromisoformat(end_raw["dateTime"]).astimezone(tz)
|
|
busy.append((start, end))
|
|
|
|
now = datetime.now(tz)
|
|
earliest = _earliest_slot(now)
|
|
|
|
date_min = now.date()
|
|
date_max = now.date() + timedelta(days=lookahead_days)
|
|
if busy:
|
|
event_dates = [s.date() for s, _ in busy] + [e.date() for _, e in busy]
|
|
date_max = max(date_max, max(event_dates))
|
|
|
|
free_blocks = []
|
|
current_date = date_min
|
|
|
|
while current_date <= date_max:
|
|
window_start = max(
|
|
datetime(current_date.year, current_date.month, current_date.day,
|
|
int(day_start.split(":")[0]), int(day_start.split(":")[1]),
|
|
tzinfo=tz),
|
|
earliest,
|
|
)
|
|
window_end = datetime(current_date.year, current_date.month, current_date.day,
|
|
int(day_end.split(":")[0]), int(day_end.split(":")[1]),
|
|
tzinfo=tz)
|
|
|
|
day_busy = []
|
|
for s, e in busy:
|
|
if s < window_end and e > window_start:
|
|
clipped_start = max(s, window_start)
|
|
clipped_end = min(e, window_end)
|
|
day_busy.append((clipped_start, clipped_end))
|
|
|
|
for z in no_go_zones:
|
|
if z.get("date"):
|
|
applies = z["date"] == current_date.isoformat()
|
|
else:
|
|
days_of_week = z.get("days_of_week")
|
|
applies = not days_of_week or current_date.weekday() in days_of_week
|
|
if not applies:
|
|
continue
|
|
z_start_h, z_start_m = map(int, z["start_time"].split(":"))
|
|
z_end_h, z_end_m = map(int, z["end_time"].split(":"))
|
|
z_start = datetime(current_date.year, current_date.month, current_date.day, z_start_h, z_start_m, tzinfo=tz)
|
|
z_end = datetime(current_date.year, current_date.month, current_date.day, z_end_h, z_end_m, tzinfo=tz)
|
|
if z_start < window_end and z_end > window_start:
|
|
day_busy.append((max(z_start, window_start), min(z_end, window_end)))
|
|
|
|
day_busy.sort(key=lambda x: x[0])
|
|
merged = []
|
|
for s, e in day_busy:
|
|
if merged and s <= merged[-1][1]:
|
|
merged[-1] = (merged[-1][0], max(merged[-1][1], e))
|
|
else:
|
|
merged.append((s, e))
|
|
|
|
cursor = window_start
|
|
for s, e in merged:
|
|
if cursor < s:
|
|
duration = int((s - cursor).total_seconds() / 60)
|
|
free_blocks.append({"start": cursor, "end": s, "duration_min": duration})
|
|
cursor = max(cursor, e)
|
|
|
|
if cursor < window_end:
|
|
duration = int((window_end - cursor).total_seconds() / 60)
|
|
free_blocks.append({"start": cursor, "end": window_end, "duration_min": duration})
|
|
|
|
current_date += timedelta(days=1)
|
|
|
|
return free_blocks
|