Skip to content

Commit

Permalink
feat(TripPlanner): Show more itineraries (#2278)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored Dec 19, 2024
1 parent 7675dcc commit c35dbff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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
# 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

0 comments on commit c35dbff

Please sign in to comment.