Skip to content

Commit

Permalink
chore: consolidate trip planner modules (#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Sep 17, 2024
1 parent 2f309bf commit 357dd15
Show file tree
Hide file tree
Showing 41 changed files with 219 additions and 207 deletions.
2 changes: 1 addition & 1 deletion lib/dotcom/trip_plan/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Dotcom.TripPlan.Alerts do

alias Alerts.Alert
alias Alerts.InformedEntity, as: IE
alias TripPlan.{Itinerary, Leg, TransitDetail}
alias Dotcom.TripPlan.{Itinerary, Leg, TransitDetail}

@doc "Filters a list of Alerts to those relevant to the Itinerary"
@spec filter_for_itinerary([Alert.t()], Itinerary.t()) :: [Alert.t()]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Dotcom.TripPlanner.FarePasses do
defmodule Dotcom.TripPlan.FarePasses do
@moduledoc """
Computing fare passes and prices for trip plan itineraries.
"""
alias Dotcom.TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}
alias Fares.{Fare, Month, OneWay}
alias TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}

@spec with_passes(Itinerary.t()) :: Itinerary.t()
def with_passes(itinerary) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TripPlanner.InputForm do
defmodule Dotcom.TripPlan.InputForm do
@moduledoc """
Describes the inputs users can fill to request trip plans.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TripPlan.Itinerary do
defmodule Dotcom.TripPlan.Itinerary do
@moduledoc """
A trip at a particular time.
Expand All @@ -7,11 +7,11 @@ defmodule TripPlan.Itinerary do
at different times of day.
"""

alias Dotcom.TripPlan.{Leg, NamedPosition, TransitDetail}
alias Fares.Fare
alias Routes.Route
alias Schedules.Trip
alias Stops.Stop
alias TripPlan.{Leg, NamedPosition, TransitDetail}

@derive {Jason.Encoder, except: [:passes]}
@enforce_keys [:start, :stop]
Expand Down Expand Up @@ -119,8 +119,8 @@ defmodule TripPlan.Itinerary do
end
end

defimpl Enumerable, for: TripPlan.Itinerary do
alias TripPlan.Leg
defimpl Enumerable, for: Dotcom.TripPlan.Itinerary do
alias Dotcom.TripPlan.Leg

def count(_itinerary) do
{:error, __MODULE__}
Expand Down
12 changes: 9 additions & 3 deletions lib/dotcom/trip_plan/itinerary_row.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
defmodule Dotcom.TripPlan.ItineraryRow do
@moduledoc false

alias Dotcom.TripPlan.IntermediateStop
alias Dotcom.TripPlan.{
IntermediateStop,
Leg,
NamedPosition,
PersonalDetail,
PersonalDetail.Step,
TransitDetail
}

alias Routes.Route
alias TripPlan.{Leg, NamedPosition, PersonalDetail, TransitDetail}
alias TripPlan.PersonalDetail.Step

defstruct stop: {nil, nil},
route: nil,
Expand Down
3 changes: 1 addition & 2 deletions lib/dotcom/trip_plan/itinerary_row_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ defmodule Dotcom.TripPlan.ItineraryRowList do
An optional to and from name can be passed in.
"""

alias Dotcom.TripPlan.ItineraryRow
alias Dotcom.TripPlan.{Itinerary, ItineraryRow}
alias Stops.Stop
alias TripPlan.Itinerary

defstruct rows: [],
destination: nil,
Expand Down
4 changes: 2 additions & 2 deletions lib/trip_plan/leg.ex → lib/dotcom/trip_plan/leg.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule TripPlan.Leg do
defmodule Dotcom.TripPlan.Leg do
@moduledoc """
A single-mode part of an Itinerary
An Itinerary can take multiple modes of transportation (walk, bus,
train, &c). Leg represents a single mode of travel during journey.
"""

alias TripPlan.{NamedPosition, PersonalDetail, TransitDetail}
alias Dotcom.TripPlan.{NamedPosition, PersonalDetail, TransitDetail}

@derive {Jason.Encoder, only: [:from, :to, :mode]}
defstruct start: Timex.now(),
Expand Down
5 changes: 2 additions & 3 deletions lib/dotcom/trip_plan/location.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Dotcom.TripPlan.Location do
alias Dotcom.TripPlan.Query
alias Dotcom.TripPlan.{NamedPosition, Query}
alias Phoenix.HTML
alias TripPlan.NamedPosition

@location_service Application.compile_env!(:dotcom, :location_service)
@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]
Expand Down Expand Up @@ -124,7 +123,7 @@ defmodule Dotcom.TripPlan.Location do
@spec do_validate_by_name(LocationService.Behaviour.result(), :to | :from, Query.t(), map) ::
Query.t()
defp do_validate_by_name({:ok, [%LocationService.Address{} = result | _]}, field, query, params) do
pos = TripPlan.NamedPosition.new(result)
pos = NamedPosition.new(result)

query
|> Map.put(field, pos)
Expand Down
2 changes: 1 addition & 1 deletion lib/dotcom/trip_plan/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ defmodule Dotcom.TripPlan.Map do
@moduledoc """
Handles generating the maps displayed within the TripPlan Controller
"""
alias Dotcom.TripPlan.{Leg, NamedPosition, TransitDetail}
alias Leaflet.{MapData, MapData.Marker}
alias Leaflet.MapData.Polyline, as: LeafletPolyline
alias Routes.Route
alias TripPlan.{Leg, NamedPosition, TransitDetail}
alias Util.Position

@type t :: MapData.t()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TripPlan.NamedPosition do
defmodule Dotcom.TripPlan.NamedPosition do
@moduledoc "Defines a position for a trip plan as a stop and/or lat/lon"

@derive Jason.Encoder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule TripPlanner.OpenTripPlanner do
defmodule Dotcom.TripPlan.OpenTripPlanner do
@moduledoc """
Makes requests to OpenTripPlanner via the OpenTripPlannerClient library, and
parses the result.
"""

alias Dotcom.TripPlanner.Parser
alias Dotcom.TripPlan.{NamedPosition, Parser}

alias OpenTripPlannerClient.ItineraryTag.{
EarliestArrival,
Expand All @@ -13,8 +13,6 @@ defmodule TripPlanner.OpenTripPlanner do
ShortestTrip
}

alias TripPlan.NamedPosition

@otp_module Application.compile_env!(:dotcom, :otp_module)

@doc """
Expand Down
5 changes: 2 additions & 3 deletions lib/trip_planner/parser.ex → lib/dotcom/trip_plan/parser.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Dotcom.TripPlanner.Parser do
defmodule Dotcom.TripPlan.Parser do
@moduledoc """
Parse results from OpenTripPlanner:
Expand All @@ -10,9 +10,8 @@ defmodule Dotcom.TripPlanner.Parser do
MBTA system.
"""

alias Dotcom.TripPlanner.FarePasses
alias Dotcom.TripPlan.{FarePasses, Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}
alias OpenTripPlannerClient.Schema
alias TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TripPlan.PersonalDetail do
defmodule Dotcom.TripPlan.PersonalDetail do
@moduledoc """
Additional information for legs which are taken on personal transportation
"""
Expand All @@ -12,7 +12,7 @@ defmodule TripPlan.PersonalDetail do
}
end

defmodule TripPlan.PersonalDetail.Step do
defmodule Dotcom.TripPlan.PersonalDetail.Step do
@moduledoc """
A turn-by-turn direction
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/dotcom/trip_plan/query.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Dotcom.TripPlan.Query do
@moduledoc "Fetch trip plan via OTP and handle response"

alias TripPlan.{Itinerary, NamedPosition}
alias TripPlanner.OpenTripPlanner
alias Dotcom.TripPlan.{Itinerary, NamedPosition, OpenTripPlanner}

defstruct [
:from,
Expand Down
2 changes: 1 addition & 1 deletion lib/dotcom/trip_plan/related_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule Dotcom.TripPlan.RelatedLink do
import DotcomWeb.Router.Helpers
import PhoenixHTMLHelpers.Link, only: [link: 2]

alias Dotcom.TripPlan.{Itinerary, Leg, TransitDetail}
alias DotcomWeb.PartialView.SvgIconWithCircle
alias Routes.Route
alias TripPlan.{Itinerary, Leg, TransitDetail}

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule TripPlan.Transfer do
defmodule Dotcom.TripPlan.Transfer do
@moduledoc """
Tools for handling logic around transfers between transit legs and modes.
The MBTA allows transfers between services depending on the fare media used
and the amount paid.
This logic may be superseded by the upcoming fares work.
"""
alias TripPlan.{Leg, NamedPosition, TransitDetail}
alias Dotcom.TripPlan.{Leg, NamedPosition, TransitDetail}

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TripPlan.TransitDetail do
defmodule Dotcom.TripPlan.TransitDetail do
@moduledoc """
Additional information for legs taken on public transportation
"""
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions lib/dotcom_web/components/trip_planner.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
defmodule DotcomWeb.Components.TripPlanner do
defmodule DotcomWeb.Components.TripPlannerForm do
@moduledoc """
Reusable components mainly used for the Trip Planner
"""
use Phoenix.Component
use DotcomWeb, :component

import DotcomWeb.Components, only: [algolia_autocomplete: 1]

alias TripPlanner.InputForm
alias Dotcom.TripPlan.InputForm

attr :id, :string
attr :params, :map, default: %{}
Expand Down
2 changes: 1 addition & 1 deletion lib/dotcom_web/controllers/trip_plan/feedback_csv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule DotcomWeb.TripPlan.FeedbackCSV do

require Logger

alias TripPlan.PersonalDetail.Step
alias Dotcom.TripPlan.PersonalDetail.Step

@headers [
"generated_time",
Expand Down
17 changes: 13 additions & 4 deletions lib/dotcom_web/controllers/trip_plan_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ defmodule DotcomWeb.TripPlanController do

require Logger

alias Dotcom.TripPlan.{ItineraryRowList, Query, RelatedLink}
alias Dotcom.TripPlan.{
Itinerary,
ItineraryRowList,
Leg,
NamedPosition,
PersonalDetail,
Query,
RelatedLink,
TransitDetail
}

alias Dotcom.TripPlan.Map, as: TripPlanMap
alias Routes.Route
alias TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}

@location_service Application.compile_env!(:dotcom, :location_service)

Expand Down Expand Up @@ -44,7 +53,7 @@ defmodule DotcomWeb.TripPlanController do
if String.match?(address, ~r/^(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),.*$/) do
[latitude, longitude, name] = String.split(address, ",", parts: 3)
# Avoid extra geocode call, just use these coordinates
destination = %TripPlan.NamedPosition{
destination = %NamedPosition{
latitude: String.to_float(latitude),
longitude: String.to_float(longitude),
name: name,
Expand Down Expand Up @@ -108,7 +117,7 @@ defmodule DotcomWeb.TripPlanController do
if String.match?(address, ~r/^(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),.*$/) do
[latitude, longitude, name] = String.split(address, ",", parts: 3)
# Avoid extra geocode call, just use these coordinates
destination = %TripPlan.NamedPosition{
destination = %NamedPosition{
latitude: String.to_float(latitude),
longitude: String.to_float(longitude),
name: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule DotcomWeb.Live.TripPlanner do

use DotcomWeb, :live_view

import DotcomWeb.Components.TripPlanner
import DotcomWeb.Components.TripPlannerForm

@form_id "trip-planner-form"

Expand Down
2 changes: 1 addition & 1 deletion lib/dotcom_web/templates/trip_plan/_itinerary_tab.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<%= @routes |> Enum.map(&icon_for_route/1) |> Enum.intersperse(fa "angle-right") %>
<span class="m-trip-plan-results__itinerary-length-distance">
<%= svg "walk.svg" %>
<%= @itinerary |> TripPlan.Itinerary.walking_distance %> mi
<%= @itinerary |> Dotcom.TripPlan.Itinerary.walking_distance %> mi
</span>
</div>
21 changes: 10 additions & 11 deletions lib/dotcom_web/views/trip_plan_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ defmodule DotcomWeb.TripPlanView do
require Routes.Route
alias Fares.{Fare, Format}
alias Routes.Route
alias Dotcom.TripPlan.{ItineraryRow, Query}
alias DotcomWeb.PartialView.SvgIconWithCircle
alias DotcomWeb.Plugs.Cookies
alias TripPlan.{Itinerary, Leg, Transfer}
alias Dotcom.StopBubble
alias Dotcom.TripPlan.{Itinerary, ItineraryRow, Leg, NamedPosition, Query, Transfer}

import Schedules.Repo, only: [end_of_rating: 0]

Expand Down Expand Up @@ -79,10 +78,10 @@ defmodule DotcomWeb.TripPlanView do
Fetches value to show in input field, preferring to use the geocoded
Query value if one is available.
"""
@spec get_input_value(Query.t() | nil, map, :to | :from) :: TripPlan.NamedPosition.t()
@spec get_input_value(Query.t() | nil, map, :to | :from) :: NamedPosition.t()
def get_input_value(%Query{} = query, params, field) do
case Map.get(query, field) do
pos = %TripPlan.NamedPosition{} ->
pos = %NamedPosition{} ->
pos

{:error, _} ->
Expand All @@ -91,7 +90,7 @@ defmodule DotcomWeb.TripPlanView do
end

def get_input_value(nil, params, field) do
%TripPlan.NamedPosition{
%NamedPosition{
name: Map.get(params, Atom.to_string(field))
}
end
Expand Down Expand Up @@ -322,13 +321,13 @@ defmodule DotcomWeb.TripPlanView do

defp format_green_line_name("Green Line " <> branch), do: "Green Line (#{branch})"

@spec accessibility_icon(TripPlan.Itinerary.t()) :: Phoenix.HTML.Safe.t()
defp accessibility_icon(%TripPlan.Itinerary{accessible?: nil}) do
@spec accessibility_icon(Itinerary.t()) :: Phoenix.HTML.Safe.t()
defp accessibility_icon(%Itinerary{accessible?: nil}) do
# Unknown accessibilityScore, so can't show a value
{:safe, ""}
end

defp accessibility_icon(%TripPlan.Itinerary{accessible?: accessible?}) do
defp accessibility_icon(%Itinerary{accessible?: accessible?}) do
content_tag(
:span,
[
Expand Down Expand Up @@ -369,7 +368,7 @@ defmodule DotcomWeb.TripPlanView do
svg_icon_with_circle(%SvgIconWithCircle{icon: route})
end

@spec datetime_from_query(nil | Dotcom.TripPlan.Query.t()) :: any()
@spec datetime_from_query(nil | Query.t()) :: any()
def datetime_from_query(%Query{time: {:error, _}}), do: datetime_from_query(nil)
def datetime_from_query(%Query{time: {_depart_or_arrive, dt}}), do: dt
def datetime_from_query(nil), do: Util.now() |> Dotcom.TripPlan.DateTime.round_minute()
Expand Down Expand Up @@ -581,7 +580,7 @@ defmodule DotcomWeb.TripPlanView do
We have to check if there is a bus to subway transfer and manually add the transfer cost of $0.70.
"""
@spec get_one_way_total_by_type(TripPlan.Itinerary.t(), Fares.fare_type()) :: non_neg_integer
@spec get_one_way_total_by_type(Itinerary.t(), Fares.fare_type()) :: non_neg_integer
def get_one_way_total_by_type(itinerary, fare_type) do
transit_legs =
itinerary.legs
Expand Down Expand Up @@ -635,7 +634,7 @@ defmodule DotcomWeb.TripPlanView do
defp cr_prefix(%Fare{mode: :commuter_rail}), do: "Commuter Rail "
defp cr_prefix(_), do: ""

@spec get_calculated_fares(TripPlan.Itinerary.t()) :: %{mode: fare_calculation}
@spec get_calculated_fares(Itinerary.t()) :: %{mode: fare_calculation}
def get_calculated_fares(itinerary) do
itinerary.legs
|> Enum.filter(fn leg -> Leg.transit?(leg) end)
Expand Down
Loading

0 comments on commit 357dd15

Please sign in to comment.