Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get_route_details function to include stop_id in stop_times list #528

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions fastapi/app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
Loading