Skip to content

Commit

Permalink
cache realtime data, reduce TNM polling time (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-mahoney authored Sep 5, 2019
1 parent ce00087 commit 38dec7a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/site/assets/ts/tnm/components/TransitNearMe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const TransitNearMe = ({
stopsWithDistances.stops.map(stop => stop.id),
dispatch
),
45000
30000
);

return (
Expand Down
3 changes: 2 additions & 1 deletion apps/site/lib/site/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule Site.Application do
supervisor(SiteWeb.Endpoint, []),
supervisor(Site.GreenLine.Supervisor, []),
supervisor(Site.Stream.Vehicles, []),
supervisor(Site.React, [])
supervisor(Site.React, []),
supervisor(Site.RealtimeSchedule, [])
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand Down
22 changes: 15 additions & 7 deletions apps/site/lib/site/realtime_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Site.RealtimeSchedule do
are considering route patterns with the same name to be effectively the same
"""

use RepoCache, ttl: :timer.seconds(30)

alias Predictions.Prediction
alias Predictions.Repo, as: PredictionsRepo
alias RoutePatterns.RoutePattern
Expand Down Expand Up @@ -42,29 +44,35 @@ defmodule Site.RealtimeSchedule do

@spec stop_data([Stop.id_t()], DateTime.t(), Keyword.t()) :: [map]
def stop_data(stop_ids, now, opts \\ []) do
cache(stop_ids, fn _ ->
do_stop_data(stop_ids, now, opts)
end)
end

@spec do_stop_data([Stop.id_t()], DateTime.t(), Keyword.t()) :: [map]
defp do_stop_data(stop_ids, now, opts) do
opts = Keyword.merge(@default_opts, opts)
stops_fn = Keyword.fetch!(opts, :stops_fn)
routes_fn = Keyword.fetch!(opts, :routes_fn)
predictions_fn = Keyword.fetch!(opts, :predictions_fn)
schedules_fn = Keyword.fetch!(opts, :schedules_fn)
alerts_fn = Keyword.fetch!(opts, :alerts_fn)

# stage 1, get stops and routes
stops_task = Task.async(fn -> get_stops(stop_ids, stops_fn) end)
# stage 1, get routes
routes_task = Task.async(fn -> get_routes(stop_ids, routes_fn) end)
[stops, route_with_patterns] = Enum.map([stops_task, routes_task], &Task.await/1)
route_with_patterns = Task.await(routes_task)

# stage 2, get predictions, schedules, and alerts
# stage 2, get stops, predictions, schedules, and alerts
stops_task = Task.async(fn -> get_stops(stop_ids, stops_fn) end)
predictions_task = Task.async(fn -> get_predictions(route_with_patterns, predictions_fn) end)

schedules_task = Task.async(fn -> get_schedules(route_with_patterns, now, schedules_fn) end)

alert_counts_task =
Task.async(fn -> get_alert_counts(route_with_patterns, now, alerts_fn) end)

[predictions, schedules, alert_counts] =
[stops, predictions, schedules, alert_counts] =
Enum.map(
[predictions_task, schedules_task, alert_counts_task],
[stops_task, predictions_task, schedules_task, alert_counts_task],
&Task.await(&1, @long_timeout)
)

Expand Down
3 changes: 2 additions & 1 deletion apps/site/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ defmodule Site.Mixfile do
{:predictions, in_umbrella: true},
{:trip_plan, in_umbrella: true},
{:services, in_umbrella: true},
{:route_patterns, in_umbrella: true}
{:route_patterns, in_umbrella: true},
{:repo_cache, in_umbrella: true}
]
end
end
1 change: 1 addition & 0 deletions apps/site/test/site/realtime_schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ defmodule Site.RealtimeScheduleTest do
}
]

RealtimeSchedule.clear_cache()
actual = RealtimeSchedule.stop_data(stops, @now, opts)

assert actual == expected
Expand Down

0 comments on commit 38dec7a

Please sign in to comment.