Skip to content

Commit

Permalink
Don't need to store route_id
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJKim committed Apr 25, 2024
1 parent 473e2f6 commit cb25ac7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
11 changes: 4 additions & 7 deletions lib/engine/last_trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule Engine.LastTrip do
@hour_in_seconds 3600

@type state :: %{
last_modified: nil,
recent_departures: :ets.tab(),
last_trips: :ets.tab()
}
Expand Down Expand Up @@ -49,8 +48,7 @@ defmodule Engine.LastTrip do

state = %{
recent_departures: @recent_departures_table,
last_trips: @last_trips_table,
last_modified: nil
last_trips: @last_trips_table
}

create_tables(state)
Expand All @@ -66,8 +64,7 @@ defmodule Engine.LastTrip do
def handle_cast({:update_last_trips, last_trips}, %{last_trips: last_trips_table} = state) do
current_time = Timex.now()

last_trips =
Enum.map(last_trips, fn {trip_id, route_id} -> {trip_id, route_id, current_time} end)
last_trips = Enum.map(last_trips, fn trip_id -> {trip_id, current_time} end)

:ets.insert(last_trips_table, last_trips)

Expand All @@ -84,10 +81,10 @@ defmodule Engine.LastTrip do
|> Stream.map(&{elem(&1, 0), elem(&1, 1)})
|> Map.new()

Enum.reduce(new_recent_departures, current_recent_departures, fn {stop_id, trip_id, route_id,
Enum.reduce(new_recent_departures, current_recent_departures, fn {stop_id, trip_id,
departure_time},
acc ->
Map.get_and_update(acc, {stop_id, route_id}, fn recent_departures ->
Map.get_and_update(acc, stop_id, fn recent_departures ->
if recent_departures do
{recent_departures, Map.put(recent_departures, trip_id, departure_time)}
else
Expand Down
6 changes: 3 additions & 3 deletions lib/predictions/last_trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ defmodule Predictions.LastTrip do

predictions_by_trip =
get_running_trips(predictions_feed)
|> Enum.map(&{&1["trip"]["trip_id"], &1["trip"]["route_id"], &1["stop_time_update"]})
|> Enum.map(&{&1["trip"]["trip_id"], &1["stop_time_update"]})

for {trip_id, route_id, predictions} <- predictions_by_trip,
for {trip_id, predictions} <- predictions_by_trip,
prediction <- predictions,
prediction["departure"] do
seconds_until_departure = prediction["departure"]["time"] - DateTime.to_unix(current_time)

if seconds_until_departure in -@hour_in_seconds..0 do
{prediction["stop_id"], trip_id, route_id, prediction["departure"]["time"]}
{prediction["stop_id"], trip_id, prediction["departure"]["time"]}
end
end
|> Enum.reject(&is_nil/1)
Expand Down

0 comments on commit cb25ac7

Please sign in to comment.