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_trip_departure_times function to handle current_time parameter #504

Merged
merged 1 commit into from
Feb 29, 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
38 changes: 31 additions & 7 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ async def get_trip_departure_times(
route_code: str,
direction_id: int,
day_type: str,
time: Optional[str] = None,
current_time: Optional[str] = None,
async_db: AsyncSession = Depends(get_async_db)
):
"""
Expand All @@ -883,12 +883,36 @@ async def get_trip_departure_times(
fields = {'route_code': route_code, 'direction_id': direction_id, 'day_type': day_type}
result = await crud.get_data_from_many_fields_async(async_db, model, agency_id, fields)

if time is not None:
# Convert the time string to a datetime.time object
time_obj = datetime.strptime(time, "%H:%M:%S").time()
# Filter the result to get the closest time
result = min(result, key=lambda r: abs(datetime.combine(date.today(), r['start_time']) - datetime.combine(date.today(), time_obj)))
else:
# Group the results by trip_id
trips = defaultdict(list)
for record in result:
trips[record['trip_id']].append(record)

# Iterate over each trip
for trip_id, records in trips.items():
if current_time is not None:
# Convert the current time string to a datetime.time object
current_time_obj = datetime.strptime(current_time, "%H:%M:%S").time()

for record in records:
# Convert each time in the departure_times list to a datetime.time object
departure_times = [datetime.strptime(t, "%H:%M:%S").time() for t in record['departure_times']]

# Calculate the difference between the current time and each time in the departure_times list
time_diffs = [abs(datetime.combine(date.today(), t) - datetime.combine(date.today(), current_time_obj)) for t in departure_times]

# Find the minimum difference and the corresponding time in the departure_times list
min_diff, closest_time = min(zip(time_diffs, departure_times), key=lambda x: x[0])

# If the current time is not within the range of start_time and end_time, return the next closest time
if not (record['start_time'] <= current_time_obj <= record['end_time']):
record['closest_time'] = closest_time.strftime("%H:%M:%S")
else:
record['closest_time'] = current_time

result = dict(trips)

if not result:
# If no time is provided, return the first record
result = result[0] if result else None

Expand Down
Loading