Skip to content

Commit

Permalink
🍎 Surface update_type through API (#811)
Browse files Browse the repository at this point in the history
* surface update_type on the predictions resource

* Update prediction.ex doc

* move update_type key into correct spot in update
  • Loading branch information
bfauble authored Jun 5, 2024
1 parent afa99e3 commit 0a1da10
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 20 deletions.
11 changes: 11 additions & 0 deletions apps/api_web/lib/api_web/controllers/prediction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ defmodule ApiWeb.PredictionController do
""",
example: "REVENUE"
)

update_type(
:string,
"""
| Value | Description |
|------------------|-------------|
| `"MID_TRIP"` | Prediction is for the trip the vehicle is currently on. |
| `"AT_TERMINAL"` | Prediction is for a terminal trip that hasn't started yet. |
| `"REVERSE_TRIP"` | Prediction is for a trip that hasn't started and the train that will be servicing this trip is currently in the middle of a previous trip. |
"""
)
end

direction_id_attribute()
Expand Down
26 changes: 13 additions & 13 deletions apps/api_web/lib/api_web/views/prediction_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule ApiWeb.PredictionView do
:status,
:stop_sequence,
:track,
:revenue
:revenue,
:update_type
])

def preload(predictions, conn, include_opts) do
Expand Down Expand Up @@ -48,10 +49,11 @@ defmodule ApiWeb.PredictionView do
departure_uncertainty: p.departure_uncertainty,
direction_id: p.direction_id,
last_trip: p.last_trip?,
schedule_relationship: schedule_relationship(p),
schedule_relationship: upcase_atom_to_string(p.schedule_relationship),
status: p.status,
stop_sequence: p.stop_sequence,
revenue: revenue(p)
revenue: revenue(p),
update_type: upcase_atom_to_string(p.update_type)
}

add_legacy_attributes(attributes, p, conn.assigns.api_version)
Expand Down Expand Up @@ -202,20 +204,18 @@ defmodule ApiWeb.PredictionView do
Map.put(acc, :datetime, time)
end

def schedule_relationship(%{schedule_relationship: nil}) do
nil
end

def schedule_relationship(%{schedule_relationship: atom}) do
atom
|> Atom.to_string()
|> String.upcase()
end

def revenue(%{revenue: atom}) do
Atom.to_string(atom)
end

def format_time(%DateTime{} = dt), do: DateTime.to_iso8601(dt)
def format_time(nil), do: nil

defp upcase_atom_to_string(nil), do: nil

defp upcase_atom_to_string(atom) do
atom
|> Atom.to_string()
|> String.upcase()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule ApiWeb.PredictionControllerTest do
trip_id: "trip",
vehicle_id: "vehicle",
status: "On Time",
direction_id: 1
direction_id: 1,
update_type: :mid_trip
}
@cr_prediction hd(@cr_predictions)

Expand Down
6 changes: 4 additions & 2 deletions apps/api_web/test/api_web/views/prediction_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule ApiWeb.PredictionViewTest do
last_trip?: false,
schedule_relationship: :added,
status: "All Aboard",
stop_sequence: 5
stop_sequence: 5,
update_type: :mid_trip
}
@route %Model.Route{id: "CR-Lowell"}
@stop %Model.Stop{id: "North Station-02", parent_station: "place-north", platform_code: "2"}
Expand Down Expand Up @@ -86,7 +87,8 @@ defmodule ApiWeb.PredictionViewTest do
"status" => "All Aboard",
"schedule_relationship" => "ADDED",
"stop_sequence" => 5,
"revenue" => "REVENUE"
"revenue" => "REVENUE",
"update_type" => "MID_TRIP"
}
end

Expand Down
5 changes: 4 additions & 1 deletion apps/model/lib/model/prediction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Model.Prediction do
:stop_sequence,
:schedule_relationship,
:status,
:update_type,
trip_match?: false,
last_trip?: false,
revenue: :REVENUE
Expand Down Expand Up @@ -95,6 +96,7 @@ defmodule Model.Prediction do
* `:trip_match?` - a boolean indicating whether the prediction is for a trip in the GTFS file
* `:last_trip?` - a boolean indicating whether the prediction is for the last trip in a given service day
* `:revenue` - An indication of whether or not the prediction is for a revenue trip
* `:update_type` - An identifier for the type of prediction for the associated vehicle.
"""
@type t :: %__MODULE__{
arrival_time: DateTime.t() | nil,
Expand All @@ -112,7 +114,8 @@ defmodule Model.Prediction do
trip_id: Model.Trip.id(),
trip_match?: boolean,
last_trip?: boolean,
revenue: :REVENUE | :NON_REVENUE
revenue: :REVENUE | :NON_REVENUE,
update_type: :mid_trip | :at_terminal | :reverse_trip
}

@spec trip_id(t) :: Model.Trip.id()
Expand Down
8 changes: 7 additions & 1 deletion apps/parse/lib/parse/commuter_rail_departures/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule Parse.CommuterRailDepartures.JSON do
direction_id: Map.get(trip, "direction_id"),
vehicle_id: vehicle_id(raw),
schedule_relationship: schedule_relationship(Map.get(trip, "schedule_relationship")),
revenue: parse_revenue(Map.get(trip, "revenue", true))
revenue: parse_revenue(Map.get(trip, "revenue", true)),
update_type: parse_update_type(Map.get(raw, "update_type"))
}
end

Expand Down Expand Up @@ -94,4 +95,9 @@ defmodule Parse.CommuterRailDepartures.JSON do
defp parse_revenue(false), do: :NON_REVENUE

defp parse_revenue(_), do: :REVENUE

defp parse_update_type("mid_trip"), do: :mid_trip
defp parse_update_type("at_terminal"), do: :at_terminal
defp parse_update_type("reverse_trip"), do: :reverse_trip
defp parse_update_type(_), do: nil
end
8 changes: 7 additions & 1 deletion apps/parse/lib/parse/trip_updates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ defmodule Parse.TripUpdates do
vehicle_id: vehicle_id(update.vehicle),
schedule_relationship: trip_relationship(update.trip.schedule_relationship),
revenue: parse_revenue(Map.get(update.trip, :revenue, true)),
last_trip?: Map.get(update.trip, :last_trip?, false)
last_trip?: Map.get(update.trip, :last_trip?, false),
update_type: parse_update_type(update.update_type)
}

update.stop_time_update
Expand Down Expand Up @@ -120,4 +121,9 @@ defmodule Parse.TripUpdates do
defp parse_revenue(false), do: :NON_REVENUE

defp parse_revenue(_), do: :REVENUE

defp parse_update_type("mid_trip"), do: :mid_trip
defp parse_update_type("at_terminal"), do: :at_terminal
defp parse_update_type("reverse_trip"), do: :reverse_trip
defp parse_update_type(_), do: nil
end
60 changes: 59 additions & 1 deletion apps/parse/test/parse/trip_updates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule Parse.TripUpdatesTest do
schedule_relationship: :SCHEDULED
}
],
update_type: "mid_trip",
vehicle: %{
id: "vehicle"
}
Expand All @@ -84,7 +85,8 @@ defmodule Parse.TripUpdatesTest do
departure_time: nil,
departure_uncertainty: nil,
revenue: :REVENUE,
last_trip?: false
last_trip?: false,
update_type: :mid_trip
} = actual
end

Expand All @@ -107,6 +109,7 @@ defmodule Parse.TripUpdatesTest do
schedule_relationship: :SCHEDULED
}
],
update_type: "mid_trip",
vehicle: nil
}

Expand Down Expand Up @@ -137,6 +140,7 @@ defmodule Parse.TripUpdatesTest do
schedule_relationship: :SCHEDULED
}
],
update_type: "mid_trip",
vehicle: nil
}

Expand Down Expand Up @@ -168,6 +172,60 @@ defmodule Parse.TripUpdatesTest do
schedule_relationship: :SCHEDULED
}
],
update_type: "mid_trip",
vehicle: nil
}

[actual] = parse_trip_update(update)

assert %Model.Prediction{
vehicle_id: nil,
last_trip?: true,
update_type: :mid_trip
} = actual

update = put_in(update.update_type, "at_terminal")

[actual] = parse_trip_update(update)

assert %Model.Prediction{
vehicle_id: nil,
last_trip?: true,
update_type: :at_terminal
} = actual

update = put_in(update.update_type, "reverse_trip")

[actual] = parse_trip_update(update)

assert %Model.Prediction{
vehicle_id: nil,
last_trip?: true,
update_type: :reverse_trip
} = actual
end

test "parses update_type values for trip" do
update = %{
trip: %{
trip_id: "trip",
route_id: "route",
direction_id: 0,
schedule_relationship: :SCHEDULED,
last_trip?: true
},
stop_time_update: [
%{
stop_id: "stop",
stop_sequence: 5,
arrival: %{
time: 1
},
departure: nil,
schedule_relationship: :SCHEDULED
}
],
update_type: "mid_trip",
vehicle: nil
}

Expand Down

0 comments on commit 0a1da10

Please sign in to comment.