From 2eb8b3f48360e1c61b9a6c023f90bf1dfa0dfce0 Mon Sep 17 00:00:00 2001 From: albertkun Date: Tue, 21 May 2024 16:26:16 -0700 Subject: [PATCH] refactor: get_route_details function in crud.py to include stop_id in stop_times list --- fastapi/app/crud.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/fastapi/app/crud.py b/fastapi/app/crud.py index 369e631..ae4a6d7 100644 --- a/fastapi/app/crud.py +++ b/fastapi/app/crud.py @@ -163,17 +163,21 @@ async def get_route_details(db: AsyncSession, route_code: str, direction_id: int result = db.execute(query, {'p_route_code': route_code, 'p_direction_id': direction_id, 'p_day_type': day_type.value, 'p_input_time': p_time.strftime("%H:%M:%S"), 'p_num_results': num_results}) raw_data = result.fetchall() - stop_times = [] + stop_times = defaultdict(dict) shape_ids = set() for row in raw_data: - stop_name, arrival_times, shape_id, stop_id = row + stop_name, departure_times, shape_id = row shape_ids.add(shape_id) - times = [] - for time in arrival_times: - if time not in times: - times.append(time) - times.sort() - stop_times.append([stop_name, {'stop_id': stop_id, 'times': times, 'shape_id': shape_id}]) + if 'times' not in stop_times[stop_name]: + stop_times[stop_name]['times'] = [] + for time in departure_times: + if time not in stop_times[stop_name]['times']: + stop_times[stop_name]['times'].append(time) + stop_times[stop_name]['times'].sort() + stop_times[stop_name]['shape_id'] = shape_id + + # Prepare the list of stop times and shape_ids + stop_times_list = [(stop_name, times, shape_id) for stop_name, times in stop_times.items()] # Query the trip_shapes table for the geometries of the distinct shape_ids query = text("SELECT shape_id, ST_AsGeoJSON(geometry) FROM metro_api.trip_shapes WHERE shape_id IN :shape_ids") result = db.execute(query, {'shape_ids': tuple(shape_ids)})