Skip to content
Open
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
24 changes: 1 addition & 23 deletions lib/dotcom_web/controllers/schedule/green.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule DotcomWeb.ScheduleController.Green do
import DotcomWeb.Schedule.Line, only: [line_direction: 2]
import DotcomWeb.Schedule.RouteBreadcrumbs, only: [assign_breadcrumbs: 2]

alias DotcomWeb.Schedule.{Predictions, VehicleLocations}
alias DotcomWeb.Schedule.VehicleLocations
alias DotcomWeb.ScheduleController.LineController
alias DotcomWeb.ScheduleView

Expand All @@ -27,7 +27,6 @@ defmodule DotcomWeb.ScheduleController.Green do
plug(:assign_next_holidays)
plug(:stops_on_routes)
plug(:vehicle_locations)
plug(:predictions)
plug(DotcomWeb.ScheduleController.VehicleTooltips)
plug(:assign_breadcrumbs)
plug(DotcomWeb.ScheduleController.ScheduleError)
Expand Down Expand Up @@ -76,27 +75,6 @@ defmodule DotcomWeb.ScheduleController.Green do
assign(conn, :stops_on_routes, GreenLine.stops_on_routes(direction_id, date))
end

def predictions(conn, _opts) do
{predictions, vehicle_predictions} =
conn
|> conn_with_branches
|> Task.async_stream(
fn branch_conn ->
Predictions.all_predictions(branch_conn)
end,
timeout: @task_timeout
)
|> Enum.reduce({[], []}, fn {:ok, branch_conn},
{acc_predictions, acc_vehicle_predictions} ->
{branch_conn.assigns.predictions ++ acc_predictions,
branch_conn.assigns.vehicle_predictions ++ acc_vehicle_predictions}
end)

conn
|> assign(:predictions, predictions)
|> assign(:vehicle_predictions, vehicle_predictions)
end

def vehicle_locations(conn, opts) do
vehicle_locations =
conn
Expand Down
124 changes: 0 additions & 124 deletions lib/dotcom_web/controllers/schedule/predictions.ex

This file was deleted.

5 changes: 2 additions & 3 deletions lib/dotcom_web/controllers/schedule/vehicle_tooltips.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule DotcomWeb.ScheduleController.VehicleTooltips do
@moduledoc """
Assigns :vehicle_tooltips based on previously requested :route, :vehicle_locations and :vehicle_predictions.
Assigns :vehicle_tooltips based on previously requested :route, :vehicle_locations
"""

import Plug.Conn, only: [assign: 3]
Expand All @@ -21,8 +21,7 @@ defmodule DotcomWeb.ScheduleController.VehicleTooltips do
:vehicle_tooltips,
VehicleHelpers.build_tooltip_index(
conn.assigns.route,
conn.assigns.vehicle_locations,
conn.assigns.vehicle_predictions
conn.assigns.vehicle_locations
)
)
end
Expand Down
37 changes: 5 additions & 32 deletions lib/vehicle_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule VehicleHelpers do
alias Schedules.Trip
alias Vehicles.Vehicle

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

@type tooltip_index_key :: {Trip.id_t() | nil, Stop.id_t()} | Stop.id_t()
Expand All @@ -27,32 +28,19 @@ defmodule VehicleHelpers do
construct a convenient map that can be used in views / templates to determine if a tooltip is available
and to fetch all of the required data
"""
@spec build_tooltip_index(Route.t(), VehicleLocations.t(), [Prediction.t()]) :: tooltip_index
def build_tooltip_index(route, vehicle_locations, vehicle_predictions) do
indexed_predictions = index_vehicle_predictions(vehicle_predictions)

@spec build_tooltip_index(Route.t(), VehicleLocations.t()) :: tooltip_index
def build_tooltip_index(route, vehicle_locations) do
vehicle_locations
|> Stream.reject(fn {{_trip_id, stop_id}, _status} -> is_nil(stop_id) end)
|> Enum.reduce(%{}, fn vehicle_location, output ->
{{trip_id, stop_id}, vehicle} = vehicle_location

{prediction, trip} =
if trip_id do
{
prediction_for_stop(indexed_predictions, trip_id, vehicle.stop_id),
Schedules.Repo.trip(trip_id)
}
else
{nil, nil}
end

stop_name = @stops_repo.get(vehicle.stop_id) |> stop_name()

tooltip = %VehicleTooltip{
vehicle: vehicle,
prediction: prediction,
prediction: nil,
stop_name: stop_name,
trip: trip,
trip: if(trip_id, do: @schedules_repo.trip(trip_id)),
route: route
}

Expand All @@ -62,21 +50,6 @@ defmodule VehicleHelpers do
end)
end

@spec prediction_for_stop(VehicleLocations.t(), String.t(), String.t()) :: Prediction.t() | nil
defp prediction_for_stop(vehicle_predictions, trip_id, stop_id) do
Map.get(vehicle_predictions, {trip_id, stop_id})
end

@spec index_vehicle_predictions([Prediction.t()]) :: %{
{String.t(), String.t()} => Prediction.t()
}
defp index_vehicle_predictions(predictions) do
predictions
|> Stream.filter(&(&1.trip && &1.stop))
|> Stream.map(&{{&1.trip.id, &1.stop.id}, &1})
|> Enum.into(Map.new())
end

@spec stop_name(Stops.Stop.t() | nil) :: String.t()
defp stop_name(nil), do: ""
defp stop_name(stop), do: stop.name
Expand Down
Loading
Loading