Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 500 when getting vehicle with OL crowding data #663

Merged
merged 6 commits into from
Sep 1, 2023
Merged
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
3 changes: 1 addition & 2 deletions apps/api_web/lib/api_web/controllers/vehicle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ defmodule ApiWeb.VehicleController do
%{
"label" => "some-carriage",
"occupancy_status" => "MANY_SEATS_AVAILABLE",
"occupancy_percentage" => 80,
"carriage_sequence" => 1
"occupancy_percentage" => 80
}
]
)
Expand Down
4 changes: 0 additions & 4 deletions apps/api_web/lib/api_web/swagger_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ defmodule ApiWeb.SwaggerHelpers do
type: :string,
description: "Carriage-specific label, used as an identifier"
},
carriage_sequence: %Schema{
type: :integer,
description: "Provides a reliable order"
},
occupancy_status: %Schema{
type: :string,
description: occupancy_status_description(),
Expand Down
23 changes: 20 additions & 3 deletions apps/api_web/lib/api_web/views/vehicle_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule ApiWeb.VehicleView do
vehicle
|> super(conn)
|> backwards_compatible_attributes(vehicle, conn.assigns.api_version)
|> encode_carriages()
end

for status <- ~w(in_transit_to incoming_at stopped_at)a do
Expand All @@ -62,26 +63,42 @@ defmodule ApiWeb.VehicleView do
end

for status <-
~w(empty many_seats_available few_seats_available standing_room_only crushed_standing_room_only full not_accepting_passengers)a do
~w(empty many_seats_available few_seats_available standing_room_only crushed_standing_room_only full not_accepting_passengers no_data_available not_boardable)a do
status_binary =
status
|> Atom.to_string()
|> String.upcase()

def occupancy_status(%{occupancy_status: unquote(status)}, _conn) do
def occupancy_status(%{occupancy_status: unquote(status)}) do
unquote(status_binary)
end
end

def occupancy_status(_, _) do
def occupancy_status(_) do
nil
end

def occupancy_status(conveyance, _conn) do
occupancy_status(conveyance)
end

defp backwards_compatible_attributes(attributes, vehicle, "2017-11-28") do
Map.put(attributes, :last_updated, vehicle.updated_at)
end

defp backwards_compatible_attributes(attributes, _, _) do
attributes
end

defp encode_carriages(vehicle) do
Map.put(vehicle, :carriages, Enum.map(vehicle.carriages || [], &encode_carriage/1))
end

defp encode_carriage(carriage) do
%{
label: carriage.label,
occupancy_status: occupancy_status(carriage, nil),
occupancy_percentage: carriage.occupancy_percentage
}
end
end
36 changes: 33 additions & 3 deletions apps/api_web/test/api_web/controllers/vehicle_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ defmodule ApiWeb.VehicleControllerTest do
latitude: 42.01,
longitude: -71.15,
speed: 75,
stop_id: "current_stop"
stop_id: "current_stop",
occupancy_status: :empty,
carriages: [
%Vehicle.Carriage{
label: "carriage_1",
occupancy_status: :empty,
occupancy_percentage: 0,
carriage_sequence: 1
},
%Vehicle.Carriage{
label: "carriage_2",
occupancy_status: :empty,
occupancy_percentage: 0,
carriage_sequence: 2
}
]
}
@stop %Model.Stop{id: "current_stop"}
@vehicle hd(@vehicles)
Expand Down Expand Up @@ -255,7 +270,7 @@ defmodule ApiWeb.VehicleControllerTest do
"current_stop_sequence" => nil,
"updated_at" => nil,
"occupancy_status" => nil,
"carriages" => nil
"carriages" => []
}
}
end
Expand Down Expand Up @@ -333,7 +348,22 @@ defmodule ApiWeb.VehicleControllerTest do
latitude: 42.01,
longitude: -71.15,
speed: 75,
stop_id: "current_stop"
stop_id: "current_stop",
occupancy_status: :many_seats_available,
carriages: [
%Vehicle.Carriage{
label: "carriage_1",
occupancy_status: :empty,
occupancy_percentage: 0,
carriage_sequence: 1
},
%Vehicle.Carriage{
label: "carriage_2",
occupancy_status: :empty,
occupancy_percentage: 0,
carriage_sequence: 2
}
]
}

State.Vehicle.new_state([vehicle])
Expand Down
2 changes: 2 additions & 0 deletions apps/model/lib/model/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ defmodule Model.Vehicle do
| :crushed_standing_room_only
| :full
| :not_accepting_passengers
| :no_data_available
| :not_boardable

@typedoc """
Meters per second
Expand Down
4 changes: 4 additions & 0 deletions apps/parse/lib/parse/vehicle_positions_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ defmodule Parse.VehiclePositionsJson do

defp parse_occupancy_status("NOT_ACCEPTING_PASSENGERS"), do: :not_accepting_passengers

defp parse_occupancy_status("NO_DATA_AVAILABLE"), do: :no_data_available

defp parse_occupancy_status("NOT_BOARDABLE"), do: :not_boardable

defp unix_to_local(timestamp) when is_integer(timestamp) do
Parse.Timezone.unix_to_local(timestamp)
end
Expand Down
Loading