Skip to content

Commit

Permalink
Revert "fix: disable the realtime LineAPI"
Browse files Browse the repository at this point in the history
This reverts commit 5ff14ff. It also
moves the endpoint to a different route. This is so that any browsers
out there that still have the polling code will not be hitting this
endpoint.
  • Loading branch information
phildarnowsky committed Feb 26, 2020
1 parent 5ff14ff commit 5ef4fde
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ const LineDiagram = ({
});

const { data: maybeLiveData } = useFetch(
`/schedules/line_api/realtime?id=${route.id}&direction_id=${directionId}`,
`/schedules/line_api/predictions_and_vehicles?id=${
route.id
}&direction_id=${directionId}`,
{},
{ json: true, watch: directionId }
);
Expand Down
75 changes: 70 additions & 5 deletions apps/site/lib/site_web/controllers/schedule/line_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ defmodule SiteWeb.ScheduleController.LineApi do

alias Alerts.Stop, as: AlertsStop
alias Routes.Route
alias Schedules.Repo, as: SchedulesRepo
alias Site.TransitNearMe
alias SiteWeb.ScheduleController.Line.DiagramHelpers
alias SiteWeb.ScheduleController.Line.Helpers, as: LineHelpers
alias Stops.Repo, as: StopsRepo
alias Stops.RouteStop
alias Vehicles.Repo, as: VehiclesRepo
alias Vehicles.Vehicle

import SiteWeb.StopController, only: [json_safe_alerts: 2]

@typep simple_vehicle :: %{
id: String.t(),
headsign: String.t() | nil,
status: String.t(),
trip_name: String.t() | nil
}

@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
def show(conn, %{"id" => route_id, "direction_id" => direction_id}) do
line_data =
Expand Down Expand Up @@ -38,12 +49,38 @@ defmodule SiteWeb.ScheduleController.LineApi do
@doc """
Provides predictions and vehicle information for a given route and direction, organized by stop.
The line diagram polls this endpoint for its real-time data.
Currently disabled due to performance issues.
"""
@spec realtime(Plug.Conn.t(), map()) :: Plug.Conn.t()
def realtime(conn, _params) do
json(conn, %{})
@spec predictions_and_vehicles(Plug.Conn.t(), map()) :: Plug.Conn.t()
def predictions_and_vehicles(conn, %{"id" => route_id, "direction_id" => direction_id}) do
headsigns_by_stop =
TransitNearMe.time_data_for_route_by_stop(
route_id,
String.to_integer(direction_id),
date: conn.assigns.date,
now: conn.assigns.date_time
)

vehicles_by_stop =
route_id
|> expand_route_id()
|> Stream.flat_map(&VehiclesRepo.route(&1, direction_id: String.to_integer(direction_id)))
|> Stream.map(&update_vehicle_with_parent_stop(&1))
|> Enum.group_by(& &1.stop_id)

combined_data_by_stop =
Map.keys(headsigns_by_stop)
|> Stream.concat(Map.keys(vehicles_by_stop))
|> Stream.uniq()
|> Stream.map(fn stop_id ->
{stop_id,
%{
headsigns: Map.get(headsigns_by_stop, stop_id, []),
vehicles: Map.get(vehicles_by_stop, stop_id, []) |> Enum.map(&simple_vehicle_map(&1))
}}
end)
|> Enum.into(%{})

json(conn, combined_data_by_stop)
end

@spec update_route_stop_data({any, RouteStop.t()}, any, DateTime.t()) :: map()
Expand All @@ -55,6 +92,10 @@ defmodule SiteWeb.ScheduleController.LineApi do
}
end

@spec expand_route_id(Route.id_t()) :: [Route.id_t()]
defp expand_route_id("Green"), do: GreenLine.branch_ids()
defp expand_route_id(route_id), do: [route_id]

@spec get_line_data(Route.id_t(), LineHelpers.direction_id(), Route.branch_name(), boolean()) ::
[DiagramHelpers.stop_with_bubble_info()]
defp get_line_data(route_id, direction_id, variant, redesign_enabled?) do
Expand All @@ -67,4 +108,28 @@ defmodule SiteWeb.ScheduleController.LineApi do

DiagramHelpers.build_stop_list(branches, direction_id, redesign_enabled?)
end

@spec simple_vehicle_map(Vehicle.t()) :: simple_vehicle
defp simple_vehicle_map(%Vehicle{id: id, status: status, trip_id: trip_id}) do
case SchedulesRepo.trip(trip_id) do
nil ->
%{id: id, status: status}

%{headsign: headsign, name: name} ->
%{
id: id,
headsign: headsign,
status: status,
trip_name: name
}
end
end

@spec update_vehicle_with_parent_stop(Vehicle.t()) :: Vehicle.t()
defp update_vehicle_with_parent_stop(vehicle) do
case StopsRepo.get_parent(vehicle.stop_id) do
nil -> vehicle
parent_stop -> %{vehicle | stop_id: parent_stop.id}
end
end
end
8 changes: 7 additions & 1 deletion apps/site/lib/site_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ defmodule SiteWeb.Router do
get("/schedules/schedule_api", ScheduleController.ScheduleApi, :show)
get("/schedules/map_api", ScheduleController.MapApi, :show)
get("/schedules/line_api", ScheduleController.LineApi, :show)
get("/schedules/line_api/realtime", ScheduleController.LineApi, :realtime)

get(
"/schedules/line_api/predictions_and_vehicles",
ScheduleController.LineApi,
:predictions_and_vehicles
)

get("/schedules/subway", ModeController, :subway)
get("/schedules/bus", ModeController, :bus)
get("/schedules/ferry", ModeController, :ferry)
Expand Down
10 changes: 7 additions & 3 deletions apps/site/test/site_web/controllers/line_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ defmodule SiteWeb.LineApiTest do
end
end

describe "realtime" do
describe "predictions_and_vehicles" do
test "success response", %{conn: conn} do
conn = get(conn, line_api_path(conn, :realtime, %{"id" => "Red", "direction_id" => "1"}))
conn =
get(
conn,
line_api_path(conn, :predictions_and_vehicles, %{"id" => "Red", "direction_id" => "1"})
)

assert %{} = json_response(conn, 200)
assert %{"place-brdwy" => %{"headsigns" => _, "vehicles" => _}} = json_response(conn, 200)
end
end
end

0 comments on commit 5ef4fde

Please sign in to comment.