From c35dbfff58c57194aba75299fb0b4de675981bcf Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 19 Dec 2024 16:05:25 -0500 Subject: [PATCH] feat(TripPlanner): Show more itineraries (#2278) --- lib/dotcom/trip_plan/input_form.ex | 1 + lib/dotcom/trip_plan/itinerary_groups.ex | 15 +++++++++++++-- test/dotcom/trip_plan/itinerary_groups_test.exs | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/dotcom/trip_plan/input_form.ex b/lib/dotcom/trip_plan/input_form.ex index b634f2f0ad..8d542444a2 100644 --- a/lib/dotcom/trip_plan/input_form.ex +++ b/lib/dotcom/trip_plan/input_form.ex @@ -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 diff --git a/lib/dotcom/trip_plan/itinerary_groups.ex b/lib/dotcom/trip_plan/itinerary_groups.ex index 5d0562fa0f..f59c84b619 100644 --- a/lib/dotcom/trip_plan/itinerary_groups.ex +++ b/lib/dotcom/trip_plan/itinerary_groups.ex @@ -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()], @@ -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)) @@ -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) diff --git a/test/dotcom/trip_plan/itinerary_groups_test.exs b/test/dotcom/trip_plan/itinerary_groups_test.exs index 064ac246b5..d5661419f8 100644 --- a/test/dotcom/trip_plan/itinerary_groups_test.exs +++ b/test/dotcom/trip_plan/itinerary_groups_test.exs @@ -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)