Skip to content

Commit

Permalink
refactor: get_route_details function in crud.py to include stop_id in…
Browse files Browse the repository at this point in the history
… stop_times list
  • Loading branch information
albertkun committed May 21, 2024
1 parent b525b7f commit 2eb8b3f
Showing 1 changed file with 12 additions and 8 deletions.
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

0 comments on commit 2eb8b3f

Please sign in to comment.