Spaces:
Running
Running
Upload app.py
Browse files- src/app.py +7 -3
src/app.py
CHANGED
|
@@ -107,11 +107,12 @@ async def get_route_view(route_id: str):
|
|
| 107 |
placeholders = ','.join(['?'] * len(trip_ids))
|
| 108 |
|
| 109 |
# We use CAST(? AS VARCHAR) to force DuckDB to match strings to strings
|
|
|
|
| 110 |
query = f"""
|
| 111 |
SELECT
|
| 112 |
CAST(st.trip_id AS VARCHAR),
|
| 113 |
CAST(st.stop_id AS VARCHAR),
|
| 114 |
-
st.arrival_time,
|
| 115 |
t.trip_headsign
|
| 116 |
FROM stop_times st
|
| 117 |
JOIN trips t ON CAST(st.trip_id AS VARCHAR) = CAST(t.trip_id AS VARCHAR)
|
|
@@ -182,7 +183,7 @@ async def get_vehicle_view(vehicle_id: str):
|
|
| 182 |
query = """
|
| 183 |
SELECT
|
| 184 |
t.trip_headsign,
|
| 185 |
-
st.arrival_time
|
| 186 |
FROM trips t
|
| 187 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 188 |
WHERE CAST(t.trip_id AS VARCHAR) = ?
|
|
@@ -196,6 +197,8 @@ async def get_vehicle_view(vehicle_id: str):
|
|
| 196 |
scheduled_hms = row[1]
|
| 197 |
|
| 198 |
# Math: Reality (Unix Time) - Plan (Service Day + Scheduled Seconds)
|
|
|
|
|
|
|
| 199 |
if predicted_time:
|
| 200 |
service_day_ts = get_service_day_start_ts()
|
| 201 |
plan_ts = service_day_ts + hms_to_seconds(scheduled_hms)
|
|
@@ -256,8 +259,9 @@ async def get_stop_view(stop_code: str):
|
|
| 256 |
if now <= pred_time <= two_hours_out:
|
| 257 |
|
| 258 |
# 4. Handshake with DB for destination and schedule
|
|
|
|
| 259 |
query = """
|
| 260 |
-
SELECT t.trip_headsign, st.arrival_time, r.route_short_name
|
| 261 |
FROM trips t
|
| 262 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 263 |
JOIN routes r ON t.route_id = r.route_id
|
|
|
|
| 107 |
placeholders = ','.join(['?'] * len(trip_ids))
|
| 108 |
|
| 109 |
# We use CAST(? AS VARCHAR) to force DuckDB to match strings to strings
|
| 110 |
+
# Use COALESCE(departure_time, arrival_time) to match prediction logic
|
| 111 |
query = f"""
|
| 112 |
SELECT
|
| 113 |
CAST(st.trip_id AS VARCHAR),
|
| 114 |
CAST(st.stop_id AS VARCHAR),
|
| 115 |
+
COALESCE(st.departure_time, st.arrival_time) as scheduled_time,
|
| 116 |
t.trip_headsign
|
| 117 |
FROM stop_times st
|
| 118 |
JOIN trips t ON CAST(st.trip_id AS VARCHAR) = CAST(t.trip_id AS VARCHAR)
|
|
|
|
| 183 |
query = """
|
| 184 |
SELECT
|
| 185 |
t.trip_headsign,
|
| 186 |
+
COALESCE(st.departure_time, st.arrival_time) as scheduled_time
|
| 187 |
FROM trips t
|
| 188 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 189 |
WHERE CAST(t.trip_id AS VARCHAR) = ?
|
|
|
|
| 197 |
scheduled_hms = row[1]
|
| 198 |
|
| 199 |
# Math: Reality (Unix Time) - Plan (Service Day + Scheduled Seconds)
|
| 200 |
+
# Note: predicted_time uses departure.time if available, else arrival.time
|
| 201 |
+
# So we use COALESCE(departure_time, arrival_time) to match
|
| 202 |
if predicted_time:
|
| 203 |
service_day_ts = get_service_day_start_ts()
|
| 204 |
plan_ts = service_day_ts + hms_to_seconds(scheduled_hms)
|
|
|
|
| 259 |
if now <= pred_time <= two_hours_out:
|
| 260 |
|
| 261 |
# 4. Handshake with DB for destination and schedule
|
| 262 |
+
# Use COALESCE(departure_time, arrival_time) to match prediction logic
|
| 263 |
query = """
|
| 264 |
+
SELECT t.trip_headsign, COALESCE(st.departure_time, st.arrival_time) as scheduled_time, r.route_short_name
|
| 265 |
FROM trips t
|
| 266 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 267 |
JOIN routes r ON t.route_id = r.route_id
|