fix scheduler orario start, immagine parcheggio, added more logs function
This commit is contained in:
@@ -228,7 +228,10 @@ def send_daily_parking_reminder(user: "User", date_obj: datetime, db: "Session")
|
||||
from database.models import DailyParkingAssignment, NotificationLog
|
||||
from services.parking import get_spot_display_name
|
||||
|
||||
config.logger.info(f"[SCHEDULER] Checking daily parking reminder for user {user.email}")
|
||||
|
||||
if not user.notify_daily_parking:
|
||||
config.logger.debug(f"[SCHEDULER] User {user.email} has disabled daily parking notifications")
|
||||
return False
|
||||
|
||||
date_str = date_obj.strftime("%Y-%m-%d")
|
||||
@@ -308,10 +311,10 @@ def run_scheduled_notifications(db: "Session"):
|
||||
users = db.query(User).all()
|
||||
|
||||
for user in users:
|
||||
# Thursday at 12: Presence reminder
|
||||
if current_weekday == 3 and current_hour == 12 and current_minute < 5:
|
||||
next_week = get_next_week_dates(today_date)
|
||||
send_presence_reminder(user, next_week, db)
|
||||
# Thursday Reminder: DISABLED as per user request
|
||||
# if current_weekday == 3 and current_hour == 12 and current_minute < 5:
|
||||
# next_week = get_next_week_dates(today_date)
|
||||
# send_presence_reminder(user, next_week, db)
|
||||
|
||||
# Daily parking reminder at user's preferred time
|
||||
user_hour = user.notify_daily_parking_hour or 8
|
||||
@@ -319,6 +322,7 @@ def run_scheduled_notifications(db: "Session"):
|
||||
|
||||
# Check if it's the right time for this user
|
||||
if current_hour == user_hour and abs(current_minute - user_minute) < 5:
|
||||
config.logger.info(f"[SCHEDULER] Triggering Daily Parking Reminder check for user {user.email} (Scheduled: {user_hour}:{user_minute})")
|
||||
# Check if Office is OPEN today
|
||||
is_office_open = True
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ from database.models import (
|
||||
)
|
||||
from utils.helpers import generate_uuid
|
||||
from app import config
|
||||
from services.notifications import notify_parking_assigned
|
||||
|
||||
|
||||
def get_spot_prefix(office: Office, db: Session) -> str:
|
||||
@@ -252,6 +253,20 @@ def assign_parking_fairly(office_id: str, pool_date: date, db: Session) -> dict:
|
||||
waitlist.append(candidate["user_id"])
|
||||
|
||||
db.commit()
|
||||
|
||||
# Send notifications to successful assignees
|
||||
for user_id in assigned:
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user:
|
||||
# Re-fetch the assignment to get the spot details
|
||||
assignment = db.query(DailyParkingAssignment).filter(
|
||||
DailyParkingAssignment.user_id == user_id,
|
||||
DailyParkingAssignment.date == pool_date
|
||||
).first()
|
||||
if assignment:
|
||||
spot_name = get_spot_display_name(assignment.spot_id, office_id, db)
|
||||
notify_parking_assigned(user, pool_date, spot_name)
|
||||
|
||||
return {"assigned": assigned, "waitlist": waitlist}
|
||||
|
||||
|
||||
@@ -317,15 +332,20 @@ def handle_presence_change(user_id: str, change_date: date, old_status: Presence
|
||||
# Check booking window
|
||||
should_assign = True
|
||||
if office.booking_window_enabled:
|
||||
from zoneinfo import ZoneInfo
|
||||
tz = ZoneInfo(config.TIMEZONE)
|
||||
now = datetime.now(tz)
|
||||
|
||||
# Allocation time is Day-1 at cutoff hour
|
||||
cutoff_dt = datetime.combine(change_date - timedelta(days=1), datetime.min.time())
|
||||
cutoff_dt = cutoff_dt.replace(
|
||||
hour=office.booking_window_end_hour,
|
||||
minute=office.booking_window_end_minute
|
||||
minute=office.booking_window_end_minute,
|
||||
tzinfo=tz
|
||||
)
|
||||
|
||||
# If now is before cutoff, do not assign yet (wait for batch job)
|
||||
if datetime.utcnow() < cutoff_dt:
|
||||
if now < cutoff_dt:
|
||||
should_assign = False
|
||||
config.logger.debug(f"Queuing parking request for user {user_id} on {change_date} (Window open until {cutoff_dt})")
|
||||
|
||||
@@ -362,3 +382,35 @@ def run_batch_allocation(office_id: str, pool_date: date, db: Session) -> dict:
|
||||
|
||||
# 2. Run fair allocation
|
||||
return assign_parking_fairly(office_id, pool_date, db)
|
||||
|
||||
|
||||
def process_daily_allocations(db: Session):
|
||||
"""
|
||||
Check if any office's booking window has just closed and run batch allocation.
|
||||
Run by scheduler every minute.
|
||||
HALT: Checks if the cutoff time for TOMORROW has been reached.
|
||||
"""
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
# Use configured timezone
|
||||
tz = ZoneInfo(config.TIMEZONE)
|
||||
now = datetime.now(tz)
|
||||
|
||||
# Check all offices with window enabled
|
||||
offices = db.query(Office).filter(Office.booking_window_enabled == True).all()
|
||||
|
||||
config.logger.debug(f"[SCHEDULER] Checking booking windows for {len(offices)} offices - Current Time: {now.strftime('%H:%M')}")
|
||||
|
||||
for office in offices:
|
||||
# Cutoff is defined as "Previous Day" (today) at Booking End Hour
|
||||
# If NOW matches the cutoff time, we run allocation for TOMORROW
|
||||
if now.hour == office.booking_window_end_hour and now.minute == office.booking_window_end_minute:
|
||||
target_date = now.date() + timedelta(days=1)
|
||||
config.logger.info(f"[SCHEDULER] CUTOFF REACHED for {office.name} (Cutoff: {office.booking_window_end_hour}:{office.booking_window_end_minute}). Starting Assegnazione Giornaliera parcheggi for {target_date}")
|
||||
|
||||
try:
|
||||
run_batch_allocation(office.id, target_date, db)
|
||||
config.logger.info(f"[SCHEDULER] Assegnazione Giornaliera parcheggi completed for {office.name} on {target_date}")
|
||||
except Exception as e:
|
||||
config.logger.error(f"[SCHEDULER] Failed Assegnazione Giornaliera parcheggi for {office.name}: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user