|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +import requests |
| 4 | +from asgiref.sync import async_to_sync |
| 5 | +from bs4 import BeautifulSoup |
1 | 6 | from celery import shared_task |
2 | 7 | from celery.utils.log import get_task_logger |
| 8 | +from channels.layers import get_channel_layer |
| 9 | +from django.conf import settings |
| 10 | +from django.utils import timezone |
3 | 11 |
|
| 12 | +from ..schedule.models import Day |
4 | 13 | from .models import Route |
5 | 14 |
|
6 | 15 | logger = get_task_logger(__name__) |
|
9 | 18 | @shared_task |
10 | 19 | def reset_routes() -> None: |
11 | 20 | logger.info("Resetting bus routes") |
12 | | - |
13 | 21 | for route in Route.objects.all(): |
14 | 22 | route.reset_status() |
| 23 | + |
| 24 | + |
| 25 | +@shared_task |
| 26 | +def schedule_all_bus_delay_fetches(): |
| 27 | + # This task runs once daily (12:01am) and schedules all bus delay fetches for the day |
| 28 | + day = Day.objects.today() |
| 29 | + if day is None: |
| 30 | + logger.error("No Day found for today") |
| 31 | + return |
| 32 | + |
| 33 | + tz = timezone.get_current_timezone() |
| 34 | + start_datetime = timezone.make_aware(day.start_time.date_obj(day.date), tz) |
| 35 | + end_datetime = timezone.make_aware(day.end_time.date_obj(day.date), tz) |
| 36 | + |
| 37 | + # 1 minute intervals (idle windows) |
| 38 | + # Idle window 1: 2.5h before start to 1h after start |
| 39 | + first_window_start = start_datetime - timedelta(hours=2.5) |
| 40 | + first_window_end = start_datetime + timedelta(hours=1) |
| 41 | + # Idle window 2: 1h before end to 5m before end |
| 42 | + second_window_start = end_datetime - timedelta(hours=1) |
| 43 | + second_window_end = end_datetime - timedelta(minutes=5) |
| 44 | + # Idle window 3: 20m after end to 2h after end |
| 45 | + third_window_start = end_datetime + timedelta(minutes=20) |
| 46 | + third_window_end = end_datetime + timedelta(hours=2) |
| 47 | + |
| 48 | + # Helper to schedule at 1 minute intervals |
| 49 | + def schedule_minutely(start, end): |
| 50 | + t = start |
| 51 | + while t <= end: |
| 52 | + fetch_fcps_bus_delays.apply_async(eta=t) |
| 53 | + t += timedelta(minutes=1) |
| 54 | + |
| 55 | + schedule_minutely(first_window_start, first_window_end) |
| 56 | + schedule_minutely(second_window_start, second_window_end) |
| 57 | + schedule_minutely(third_window_start, third_window_end) |
| 58 | + |
| 59 | + # 15 second intervals (active window) |
| 60 | + active_window_start = end_datetime - timedelta(minutes=5) |
| 61 | + active_window_end = end_datetime + timedelta(minutes=20) |
| 62 | + t = active_window_start |
| 63 | + while t <= active_window_end: |
| 64 | + fetch_fcps_bus_delays.apply_async(eta=t) |
| 65 | + t += timedelta(seconds=15) |
| 66 | + |
| 67 | + logger.info("Scheduled all bus delay fetches for today.") |
| 68 | + |
| 69 | + |
| 70 | +@shared_task |
| 71 | +def fetch_fcps_bus_delays(): |
| 72 | + url = settings.BUS_DELAY_URL |
| 73 | + |
| 74 | + try: |
| 75 | + response = requests.get(url, timeout=10) |
| 76 | + response.raise_for_status() |
| 77 | + except Exception as e: |
| 78 | + logger.error("Error fetching URL: %s", e) |
| 79 | + return |
| 80 | + |
| 81 | + try: |
| 82 | + soup = BeautifulSoup(response.text, "html.parser") |
| 83 | + rows = soup.select("table tr") |
| 84 | + except Exception as e: |
| 85 | + logger.error("Error parsing HTML: %s", e) |
| 86 | + return |
| 87 | + |
| 88 | + if not rows or len(rows) < 2: |
| 89 | + logger.warning("Not a complete row with all bus information") |
| 90 | + return |
| 91 | + |
| 92 | + # Sort out the JEFFERSON HIGH bus delays |
| 93 | + try: |
| 94 | + for row in rows[1:]: |
| 95 | + cells = row.find_all("td") |
| 96 | + if len(cells) >= 4 and cells[0].text.strip() == "JEFFERSON HIGH": |
| 97 | + route_name = cells[1].text.strip().split()[0][:100] |
| 98 | + reason = cells[3].text.strip()[:150] |
| 99 | + estimated_time_delay = cells[2].text.strip()[:10] |
| 100 | + try: |
| 101 | + obj = Route.objects.get(route_name=route_name) |
| 102 | + # Only update if current status isn't "on time" |
| 103 | + if obj.status != "a": |
| 104 | + obj.status = "d" |
| 105 | + obj.reason = reason |
| 106 | + obj.estimated_time_delay = estimated_time_delay |
| 107 | + obj.save(update_fields=["status", "reason", "estimated_time_delay"]) |
| 108 | + logger.info("Updated route %s with delay: %s and ETA: %s", route_name, reason, estimated_time_delay) |
| 109 | + channel_layer = get_channel_layer() |
| 110 | + all_routes = list(Route.objects.values()) |
| 111 | + async_to_sync(channel_layer.group_send)( |
| 112 | + "bus", |
| 113 | + { |
| 114 | + "type": "bus.update", |
| 115 | + "message": { |
| 116 | + "allRoutes": all_routes, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ) |
| 120 | + except Route.DoesNotExist: |
| 121 | + logger.error("Route with route_name %s does not exist", route_name) |
| 122 | + except Exception as e: |
| 123 | + logger.error("Error processing bus delays: %s", e) |
| 124 | + return |
0 commit comments