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

feat(TripPlanner): Show more itineraries #2278

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/dotcom/trip_plan/input_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Dotcom.TripPlan.InputForm do
toPlace: PlanParams.to_place_param(to),
arriveBy: datetime_type == "arrive_by",
date: PlanParams.to_date_param(datetime),
numItineraries: 100,
time: PlanParams.to_time_param(datetime),
transportModes: __MODULE__.Modes.selected_mode_keys(modes) |> PlanParams.to_modes_param(),
wheelchair: wheelchair
Expand Down
15 changes: 13 additions & 2 deletions lib/dotcom/trip_plan/itinerary_groups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Dotcom.TripPlan.ItineraryGroups do
alias OpenTripPlannerClient.ItineraryTag

@short_walk_threshold_minutes 5
@max_per_group 5

@type summarized_leg :: %{
routes: [Routes.Route.t()],
Expand All @@ -35,8 +36,10 @@ defmodule Dotcom.TripPlan.ItineraryGroups do
def from_itineraries(itineraries) do
itineraries
|> Enum.group_by(&unique_legs_to_hash/1)
|> Enum.map(&drop_hash/1)
|> Enum.reject(&Enum.empty?/1)
|> Enum.map(&limit_itinerary_count/1)
|> Enum.map(&to_summarized_group/1)
|> Enum.reject(&Enum.empty?(&1.itineraries))
|> Enum.sort_by(fn
%{itineraries: [%{tag: tag} | _] = _} ->
Enum.find_index(ItineraryTag.tag_priority_order(), &(&1 == tag))
Expand All @@ -61,13 +64,21 @@ defmodule Dotcom.TripPlan.ItineraryGroups do
{Routes.Route.type_atom(route.type), leg.from.name, leg.to.name}
end

defp to_summarized_group({_hash, grouped_itineraries}) do
defp drop_hash({_hash, grouped_itineraries}) do
grouped_itineraries
end

defp to_summarized_group(grouped_itineraries) do
%{
itineraries: ItineraryTag.sort_tagged(grouped_itineraries),
summary: summary(grouped_itineraries)
}
end

defp limit_itinerary_count(itineraries) do
Enum.take(itineraries, @max_per_group)
end

defp summary(itineraries) do
itineraries
|> Enum.map(&to_map_with_fare/1)
Expand Down
15 changes: 15 additions & 0 deletions test/dotcom/trip_plan/itinerary_groups_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ defmodule Dotcom.TripPlan.ItineraryGroupsTest do
assert Kernel.length(grouped_itineraries) == 1
end

test "only includes the first five itineraries in a group", %{stops: [a, b, c]} do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this new test sticks with the pattern of other tests in the file.

# SETUP
bus_a_b_leg = TripPlanner.build(:bus_leg, from: a, to: b)
subway_b_c_leg = TripPlanner.build(:subway_leg, from: b, to: c)

itineraries =
TripPlanner.build_list(10, :itinerary, legs: [bus_a_b_leg, subway_b_c_leg])

# EXERCISE
[group] = ItineraryGroups.from_itineraries(itineraries)

# VERIFY
assert Kernel.length(group.itineraries) == 5
end

test "does not group itineraries with different modes", %{stops: [a, b, c]} do
# SETUP
bus_a_b_leg = TripPlanner.build(:bus_leg, from: a, to: b)
Expand Down
Loading