From e5c4110db0a5c5a60c31d108cc67bdf0cf691176 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Tue, 29 Aug 2023 09:23:17 -0400 Subject: [PATCH 01/17] Added a Carriage config so more data can be saved for logging. --- .../v2/widget_instance/train_crowding.ex | 2 +- lib/screens/vehicles/carriage.ex | 21 +++++++++++++++++++ lib/screens/vehicles/parser.ex | 6 +++++- lib/screens/vehicles/vehicle.ex | 4 +++- .../widgets/train_crowding_test.exs | 9 ++++++-- .../widget_instance/train_crowding_test.exs | 14 ++++++------- 6 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 lib/screens/vehicles/carriage.ex diff --git a/lib/screens/v2/widget_instance/train_crowding.ex b/lib/screens/v2/widget_instance/train_crowding.ex index d40ff6b3b..54b005f2d 100644 --- a/lib/screens/v2/widget_instance/train_crowding.ex +++ b/lib/screens/v2/widget_instance/train_crowding.ex @@ -49,7 +49,7 @@ defmodule Screens.V2.WidgetInstance.TrainCrowding do defp serialize_carriages(nil), do: nil defp serialize_carriages(carriages), - do: Enum.map(carriages, fn car -> serialize_occupancy_status(car) end) + do: Enum.map(carriages, fn car -> serialize_occupancy_status(car.occupancy_status) end) defp serialize_occupancy_status(:no_data_available), do: :no_data defp serialize_occupancy_status(:many_seats_available), do: :not_crowded diff --git a/lib/screens/vehicles/carriage.ex b/lib/screens/vehicles/carriage.ex new file mode 100644 index 000000000..365b77a16 --- /dev/null +++ b/lib/screens/vehicles/carriage.ex @@ -0,0 +1,21 @@ +defmodule Screens.Vehicles.Carriage do + @moduledoc false + + defstruct car_number: nil, + occupancy_status: nil + + @type occupancy_status :: + :many_seats_available + | :few_seats_available + | :standing_room_only + | :crushed_standing_room_only + | :full + | :no_data_available + | :not_accepting_passengers + | nil + + @type t :: %__MODULE__{ + car_number: String.t(), + occupancy_status: occupancy_status | nil + } +end diff --git a/lib/screens/vehicles/parser.ex b/lib/screens/vehicles/parser.ex index a9c25a901..350ed95d9 100644 --- a/lib/screens/vehicles/parser.ex +++ b/lib/screens/vehicles/parser.ex @@ -50,9 +50,13 @@ defmodule Screens.Vehicles.Parser do defp parse_carriages(data), do: Enum.map(data, &parse_car_crowding/1) defp parse_car_crowding(%{ + "label" => car_number, "occupancy_status" => occupancy_status }), - do: parse_occupancy_status(occupancy_status) + do: %Screens.Vehicles.Carriage{ + car_number: car_number, + occupancy_status: parse_occupancy_status(occupancy_status) + } defp trip_id_from_trip_data(%{"data" => %{"id" => trip_id}}), do: trip_id defp trip_id_from_trip_data(_), do: nil diff --git a/lib/screens/vehicles/vehicle.ex b/lib/screens/vehicles/vehicle.ex index 6c64d43a4..2b22c97d3 100644 --- a/lib/screens/vehicles/vehicle.ex +++ b/lib/screens/vehicles/vehicle.ex @@ -1,6 +1,8 @@ defmodule Screens.Vehicles.Vehicle do @moduledoc false + alias Screens.Vehicles.Carriage + defstruct id: nil, direction_id: nil, current_status: nil, @@ -29,7 +31,7 @@ defmodule Screens.Vehicles.Vehicle do stop_id: Screens.Stops.Stop.id() | nil, parent_stop_id: Screens.Stops.Stop.id() | nil, occupancy_status: occupancy_status, - carriages: list(occupancy_status) | nil + carriages: list(Carriage.t()) } def by_route_and_direction(route_id, direction_id) do diff --git a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs index 197af2a47..408619744 100644 --- a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs +++ b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs @@ -6,7 +6,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do alias Screens.Config.Screen alias Screens.Config.V2.{TrainCrowding, Triptych} alias Screens.Predictions.Prediction - alias Screens.Vehicles.Vehicle + alias Screens.Vehicles.{Carriage, Vehicle} alias Screens.V2.WidgetInstance.TrainCrowding, as: CrowdingWidget setup :setup_base @@ -31,7 +31,12 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do next_train_prediction = struct(Prediction, %{ - vehicle: struct(Vehicle, %{stop_id: "10001", current_status: :incoming_at}) + vehicle: + struct(Vehicle, %{ + stop_id: "10001", + current_status: :incoming_at, + carriages: [struct(Carriage)] + }) }) location_context = %Screens.LocationContext{ diff --git a/test/screens/v2/widget_instance/train_crowding_test.exs b/test/screens/v2/widget_instance/train_crowding_test.exs index b0e51d9cd..d8883ae5f 100644 --- a/test/screens/v2/widget_instance/train_crowding_test.exs +++ b/test/screens/v2/widget_instance/train_crowding_test.exs @@ -3,7 +3,7 @@ defmodule Screens.V2.WidgetInstance.TrainCrowdingTest do alias Screens.Predictions.Prediction alias Screens.V2.WidgetInstance.TrainCrowding, as: WidgetInstance - alias Screens.Vehicles.Vehicle + alias Screens.Vehicles.{Carriage, Vehicle} setup do config = @@ -30,12 +30,12 @@ defmodule Screens.V2.WidgetInstance.TrainCrowdingTest do stop_id: "10001", current_status: :incoming_at, carriages: [ - :crushed_standing_room_only, - :few_seats_available, - :standing_room_only, - :many_seats_available, - :full, - :not_accepting_passengers + %Carriage{car_number: "1", occupancy_status: :crushed_standing_room_only}, + %Carriage{car_number: "2", occupancy_status: :few_seats_available}, + %Carriage{car_number: "3", occupancy_status: :standing_room_only}, + %Carriage{car_number: "4", occupancy_status: :many_seats_available}, + %Carriage{car_number: "5", occupancy_status: :full}, + %Carriage{car_number: "6", occupancy_status: :not_accepting_passengers} ] }) }) From 09d5f13794cd7f3707161ab6edd37b04ed7ea796 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Tue, 29 Aug 2023 09:45:30 -0400 Subject: [PATCH 02/17] Added logging for post launch research. --- .../candidate_generator/widgets/train_crowding.ex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index fa9398083..ba97ca117 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -1,6 +1,8 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do @moduledoc false + require Logger + alias Screens.Alerts.Alert alias Screens.Config.Screen alias Screens.Config.V2.{TrainCrowding, Triptych} @@ -59,6 +61,8 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do train_crowding.station_id and next_train_prediction.vehicle.carriages != [] and not any_alert_makes_this_a_terminal?(alerts, location_context) do + log_crowding_info(next_train_prediction, train_crowding) + [ %CrowdingWidget{ screen: config, @@ -88,4 +92,15 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do localized_alert.alert.effect in [:suspension, :shuttle] and LocalizedAlert.location(localized_alert) in [:boundary_downstream, :boundary_upstream] end + + defp log_crowding_info(prediction, crowding_config) do + Enum.each( + prediction.vehicle.carriages, + fn %Screens.Vehicles.Carriage{} = carriage -> + Logger.info( + "[train_crowding] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} car_number=#{carriage.car_number} vehicle_id=#{prediction.vehicle.id} crowding_level=#{carriage.occupancy_status} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id}" + ) + end + ) + end end From a3e6bae18e91316ab5b5351af4999cf11081e689 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 1 Sep 2023 08:06:16 -0400 Subject: [PATCH 03/17] Added another log. --- .../v2/candidate_generator/widgets/train_crowding.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index ba97ca117..a1f88686f 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -53,6 +53,10 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do params |> Map.to_list() |> fetch_alerts_fn.() do next_train_prediction = List.first(predictions) + Logger.info( + "[train_crowding next_prediction] station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" + ) + # If there is an upcoming train, it's headed to this station, and we're not at a temporary terminal, # show the widget if not is_nil(next_train_prediction) and @@ -98,7 +102,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do prediction.vehicle.carriages, fn %Screens.Vehicles.Carriage{} = carriage -> Logger.info( - "[train_crowding] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} car_number=#{carriage.car_number} vehicle_id=#{prediction.vehicle.id} crowding_level=#{carriage.occupancy_status} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id}" + "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} car_number=#{carriage.car_number} vehicle_id=#{prediction.vehicle.id} crowding_level=#{carriage.occupancy_status} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id}" ) end ) From f6871e93b25b6fccfb2365727d407d30eeba1b27 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 1 Sep 2023 11:52:43 -0400 Subject: [PATCH 04/17] Added an opts list to CandidateGenerator.candidate_instances. --- lib/screens/v2/candidate_generator.ex | 3 ++- lib/screens/v2/candidate_generator/bus_eink.ex | 1 + lib/screens/v2/candidate_generator/bus_shelter.ex | 1 + lib/screens/v2/candidate_generator/dup.ex | 1 + lib/screens/v2/candidate_generator/gl_eink.ex | 1 + lib/screens/v2/candidate_generator/pre_fare.ex | 1 + lib/screens/v2/candidate_generator/solari.ex | 1 + lib/screens/v2/candidate_generator/solari_large.ex | 1 + lib/screens/v2/candidate_generator/triptych.ex | 5 +++-- .../candidate_generator/widgets/train_crowding.ex | 5 ++++- lib/screens/v2/screen_data.ex | 6 +++--- .../controllers/v2/screen_api_controller.ex | 2 +- .../v2/candidate_generator/bus_eink_test.exs | 2 ++ .../v2/candidate_generator/bus_shelter_test.exs | 4 ++++ test/screens/v2/candidate_generator/dup_test.exs | 2 ++ .../v2/candidate_generator/solari_large_test.exs | 4 +++- .../screens/v2/candidate_generator/solari_test.exs | 3 ++- .../widgets/train_crowding_test.exs | 14 ++++++++++++-- 18 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/screens/v2/candidate_generator.ex b/lib/screens/v2/candidate_generator.ex index a3a1ba3cf..a837ffc6e 100644 --- a/lib/screens/v2/candidate_generator.ex +++ b/lib/screens/v2/candidate_generator.ex @@ -12,7 +12,8 @@ defmodule Screens.V2.CandidateGenerator do Fetches data and returns a list of candidate widget instances to be considered for placement on the template. """ - @callback candidate_instances(ScreenData.config()) :: ScreenData.candidate_instances() + @callback candidate_instances(ScreenData.config(), keyword()) :: + ScreenData.candidate_instances() @doc """ Receives the finalized list of widget instances that were placed on diff --git a/lib/screens/v2/candidate_generator/bus_eink.ex b/lib/screens/v2/candidate_generator/bus_eink.ex index 57939703b..17aa1c564 100644 --- a/lib/screens/v2/candidate_generator/bus_eink.ex +++ b/lib/screens/v2/candidate_generator/bus_eink.ex @@ -44,6 +44,7 @@ defmodule Screens.V2.CandidateGenerator.BusEink do # credo:disable-for-next-line Credo.Check.Design.DuplicatedCode def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), fetch_stop_name_fn \\ &Stop.fetch_stop_name/1, departures_instances_fn \\ &Widgets.Departures.departures_instances/1, diff --git a/lib/screens/v2/candidate_generator/bus_shelter.ex b/lib/screens/v2/candidate_generator/bus_shelter.ex index 3dfc29300..c2ccdf7c6 100644 --- a/lib/screens/v2/candidate_generator/bus_shelter.ex +++ b/lib/screens/v2/candidate_generator/bus_shelter.ex @@ -47,6 +47,7 @@ defmodule Screens.V2.CandidateGenerator.BusShelter do # credo:disable-for-next-line Credo.Check.Design.DuplicatedCode def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), fetch_stop_name_fn \\ &Stop.fetch_stop_name/1, departures_instances_fn \\ &Widgets.Departures.departures_instances/1, diff --git a/lib/screens/v2/candidate_generator/dup.ex b/lib/screens/v2/candidate_generator/dup.ex index 2aa905e56..ad155ad46 100644 --- a/lib/screens/v2/candidate_generator/dup.ex +++ b/lib/screens/v2/candidate_generator/dup.ex @@ -76,6 +76,7 @@ defmodule Screens.V2.CandidateGenerator.Dup do @impl CandidateGenerator def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), fetch_stop_name_fn \\ &Stop.fetch_stop_name/1, evergreen_content_instances_fn \\ &Widgets.Evergreen.evergreen_content_instances/1, diff --git a/lib/screens/v2/candidate_generator/gl_eink.ex b/lib/screens/v2/candidate_generator/gl_eink.ex index a567dd12e..62344ce57 100644 --- a/lib/screens/v2/candidate_generator/gl_eink.ex +++ b/lib/screens/v2/candidate_generator/gl_eink.ex @@ -67,6 +67,7 @@ defmodule Screens.V2.CandidateGenerator.GlEink do @impl CandidateGenerator def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), fetch_destination_fn \\ &fetch_destination/2, departures_instances_fn \\ &Widgets.Departures.departures_instances/3, diff --git a/lib/screens/v2/candidate_generator/pre_fare.ex b/lib/screens/v2/candidate_generator/pre_fare.ex index 3cf71794e..019d67c51 100644 --- a/lib/screens/v2/candidate_generator/pre_fare.ex +++ b/lib/screens/v2/candidate_generator/pre_fare.ex @@ -64,6 +64,7 @@ defmodule Screens.V2.CandidateGenerator.PreFare do # credo:disable-for-next-line def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), subway_status_instance_fn \\ &Widgets.SubwayStatus.subway_status_instances/2, reconstructed_alert_instances_fn \\ &Widgets.ReconstructedAlert.reconstructed_alert_instances/1, diff --git a/lib/screens/v2/candidate_generator/solari.ex b/lib/screens/v2/candidate_generator/solari.ex index 5e9e5712f..fdd349e9a 100644 --- a/lib/screens/v2/candidate_generator/solari.ex +++ b/lib/screens/v2/candidate_generator/solari.ex @@ -25,6 +25,7 @@ defmodule Screens.V2.CandidateGenerator.Solari do # credo:disable-for-next-line Credo.Check.Design.DuplicatedCode def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), departures_instances_fn \\ &Widgets.Departures.departures_instances/1 ) do diff --git a/lib/screens/v2/candidate_generator/solari_large.ex b/lib/screens/v2/candidate_generator/solari_large.ex index 3f6924593..9e2583647 100644 --- a/lib/screens/v2/candidate_generator/solari_large.ex +++ b/lib/screens/v2/candidate_generator/solari_large.ex @@ -25,6 +25,7 @@ defmodule Screens.V2.CandidateGenerator.SolariLarge do # credo:disable-for-next-line Credo.Check.Design.DuplicatedCode def candidate_instances( config, + _opts, now \\ DateTime.utc_now(), departures_instances_fn \\ &Widgets.Departures.departures_instances/1 ) do diff --git a/lib/screens/v2/candidate_generator/triptych.ex b/lib/screens/v2/candidate_generator/triptych.ex index c432acdb0..cf2f55072 100644 --- a/lib/screens/v2/candidate_generator/triptych.ex +++ b/lib/screens/v2/candidate_generator/triptych.ex @@ -22,11 +22,12 @@ defmodule Screens.V2.CandidateGenerator.Triptych do @impl CandidateGenerator def candidate_instances( config, - crowding_widget_instances_fn \\ &Widgets.TrainCrowding.crowding_widget_instances/1, + opts, + crowding_widget_instances_fn \\ &Widgets.TrainCrowding.crowding_widget_instances/2, evergreen_content_instances_fn \\ &Widgets.Evergreen.evergreen_content_instances/1 ) do [ - fn -> crowding_widget_instances_fn.(config) end, + fn -> crowding_widget_instances_fn.(config, opts) end, fn -> evergreen_content_instances_fn.(config) end, fn -> placeholder_instances() end ] diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index a1f88686f..5f9384063 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -11,9 +11,10 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do alias Screens.V2.LocalizedAlert alias Screens.V2.WidgetInstance.TrainCrowding, as: CrowdingWidget - @spec crowding_widget_instances(Screen.t()) :: list(CrowdingWidget.t()) + @spec crowding_widget_instances(Screen.t(), keyword()) :: list(CrowdingWidget.t()) def crowding_widget_instances( config, + opts, now \\ DateTime.utc_now(), fetch_predictions_fn \\ &Prediction.fetch/1, fetch_location_context_fn \\ &Stop.fetch_location_context/3, @@ -27,6 +28,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do _, _, _, + _, _ ) do [] @@ -34,6 +36,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do def crowding_widget_instances( %Screen{app_params: %Triptych{train_crowding: train_crowding}} = config, + opts, now, fetch_predictions_fn, fetch_location_context_fn, diff --git a/lib/screens/v2/screen_data.ex b/lib/screens/v2/screen_data.ex index 0d82a1c89..a1fbce692 100644 --- a/lib/screens/v2/screen_data.ex +++ b/lib/screens/v2/screen_data.ex @@ -41,7 +41,7 @@ defmodule Screens.V2.ScreenData do layout_and_widgets = config - |> fetch_data() + |> fetch_data(opts) |> tap(&cache_visible_alert_widgets(&1, screen_id, config.hidden_from_screenplay)) |> resolve_paging(refresh_rate) @@ -64,13 +64,13 @@ defmodule Screens.V2.ScreenData do end @spec fetch_data(Screens.Config.Screen.t()) :: {Template.layout(), selected_instances_map()} - def fetch_data(config) do + def fetch_data(config, opts \\ []) do candidate_generator = Parameters.get_candidate_generator(config) screen_template = candidate_generator.screen_template() candidate_instances = config - |> candidate_generator.candidate_instances() + |> candidate_generator.candidate_instances(opts) |> Enum.filter(&WidgetInstance.valid_candidate?/1) pick_instances(screen_template, candidate_instances) diff --git a/lib/screens_web/controllers/v2/screen_api_controller.ex b/lib/screens_web/controllers/v2/screen_api_controller.ex index 4b65ee1d2..2c34f22e5 100644 --- a/lib/screens_web/controllers/v2/screen_api_controller.ex +++ b/lib/screens_web/controllers/v2/screen_api_controller.ex @@ -75,7 +75,7 @@ defmodule ScreensWeb.V2.ScreenApiController do screen_side ) - json(conn, ScreenData.by_screen_id(screen_id)) + json(conn, ScreenData.by_screen_id(screen_id, is_real_screen: is_screen)) end end diff --git a/test/screens/v2/candidate_generator/bus_eink_test.exs b/test/screens/v2/candidate_generator/bus_eink_test.exs index 3ebe0dc97..00b5293a0 100644 --- a/test/screens/v2/candidate_generator/bus_eink_test.exs +++ b/test/screens/v2/candidate_generator/bus_eink_test.exs @@ -59,10 +59,12 @@ defmodule Screens.V2.CandidateGenerator.BusEinkTest do now = ~U[2020-04-06T10:00:00Z] evergreen_content_instances_fn = fn _ -> [] end subway_status_instances_fn = fn _, _ -> [] end + opts = [] actual_instances = BusEink.candidate_instances( config, + opts, now, fetch_stop_fn, departures_instances_fn, diff --git a/test/screens/v2/candidate_generator/bus_shelter_test.exs b/test/screens/v2/candidate_generator/bus_shelter_test.exs index d6312efb1..c2d1039c2 100644 --- a/test/screens/v2/candidate_generator/bus_shelter_test.exs +++ b/test/screens/v2/candidate_generator/bus_shelter_test.exs @@ -80,6 +80,7 @@ defmodule Screens.V2.CandidateGenerator.BusShelterTest do now = ~U[2020-04-06T10:00:00Z] evergreen_content_instances_fn = fn _ -> [] end subway_status_instances_fn = fn _, _ -> [] end + opts = [] expected_header = %NormalHeader{ screen: config, @@ -93,6 +94,7 @@ defmodule Screens.V2.CandidateGenerator.BusShelterTest do actual_instances = BusShelter.candidate_instances( config, + opts, now, fetch_stop_fn, departures_instances_fn, @@ -115,6 +117,7 @@ defmodule Screens.V2.CandidateGenerator.BusShelterTest do now = ~U[2020-04-06T10:00:00Z] evergreen_content_instances_fn = fn _ -> [] end subway_status_instances_fn = fn _, _ -> [] end + opts = [] expected_header = %NormalHeader{ screen: config, @@ -126,6 +129,7 @@ defmodule Screens.V2.CandidateGenerator.BusShelterTest do actual_instances = BusShelter.candidate_instances( config, + opts, now, fetch_stop_fn, departures_instances_fn, diff --git a/test/screens/v2/candidate_generator/dup_test.exs b/test/screens/v2/candidate_generator/dup_test.exs index ba2716161..57fe6baf8 100644 --- a/test/screens/v2/candidate_generator/dup_test.exs +++ b/test/screens/v2/candidate_generator/dup_test.exs @@ -90,6 +90,7 @@ defmodule Screens.V2.CandidateGenerator.DupTest do departures_instances_fn = fn _, _ -> [] end evergreen_content_instances_fn = fn _ -> [] end alerts_instances_fn = fn _, _ -> [] end + opts = [] expected_headers = List.duplicate( @@ -105,6 +106,7 @@ defmodule Screens.V2.CandidateGenerator.DupTest do actual_instances = Dup.candidate_instances( config, + opts, now, fetch_stop_fn, evergreen_content_instances_fn, diff --git a/test/screens/v2/candidate_generator/solari_large_test.exs b/test/screens/v2/candidate_generator/solari_large_test.exs index 3fb63ec93..e8867c919 100644 --- a/test/screens/v2/candidate_generator/solari_large_test.exs +++ b/test/screens/v2/candidate_generator/solari_large_test.exs @@ -34,6 +34,7 @@ defmodule Screens.V2.CandidateGenerator.SolariLargeTest do test "returns expected header", %{config: config} do departures_instances_fn = fn _ -> [] end now = ~U[2020-04-06T10:00:00Z] + opts = [] expected_header = %NormalHeader{ screen: config, @@ -42,7 +43,8 @@ defmodule Screens.V2.CandidateGenerator.SolariLargeTest do time: ~U[2020-04-06T10:00:00Z] } - actual_instances = SolariLarge.candidate_instances(config, now, departures_instances_fn) + actual_instances = + SolariLarge.candidate_instances(config, opts, now, departures_instances_fn) assert expected_header in actual_instances end diff --git a/test/screens/v2/candidate_generator/solari_test.exs b/test/screens/v2/candidate_generator/solari_test.exs index f884494be..5eddcf4b6 100644 --- a/test/screens/v2/candidate_generator/solari_test.exs +++ b/test/screens/v2/candidate_generator/solari_test.exs @@ -34,6 +34,7 @@ defmodule Screens.V2.CandidateGenerator.SolariTest do test "returns expected header", %{config: config} do departures_instances_fn = fn _ -> [] end now = ~U[2020-04-06T10:00:00Z] + opts = [] expected_header = %NormalHeader{ screen: config, @@ -42,7 +43,7 @@ defmodule Screens.V2.CandidateGenerator.SolariTest do time: ~U[2020-04-06T10:00:00Z] } - actual_instances = Solari.candidate_instances(config, now, departures_instances_fn) + actual_instances = Solari.candidate_instances(config, opts, now, departures_instances_fn) assert expected_header in actual_instances end diff --git a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs index 408619744..5f1f64e4b 100644 --- a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs +++ b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs @@ -146,6 +146,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do %{ config: config, + opts: [], now: ~U[2023-08-16 21:04:00Z], next_train_prediction: next_train_prediction, fetch_predictions_fn: fn _ -> {:ok, [next_train_prediction]} end, @@ -170,6 +171,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns crowding widget if train is on the way to this station", context do assert crowding_widget_instances( context.config, + context.opts, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -192,6 +194,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, + context.opts, context.now, fn _ -> {:ok, [alt_prediction]} end, context.fetch_location_context_fn, @@ -208,6 +211,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, + context.opts, context.now, fn _ -> {:ok, [alt_prediction]} end, context.fetch_location_context_fn, @@ -220,6 +224,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do context do assert crowding_widget_instances( context.config, + context.opts, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -231,6 +236,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns empty if there are no predictions", context do assert crowding_widget_instances( context.config, + context.opts, context.now, fn _ -> {:ok, []} end, context.fetch_location_context_fn, @@ -242,6 +248,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns empty if any fetches fail", context do assert crowding_widget_instances( context.config, + context.opts, context.now, fn _ -> :error end, context.fetch_location_context_fn, @@ -251,6 +258,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, + context.opts, context.now, context.fetch_predictions_fn, fn _, _, _ -> :error end, @@ -260,6 +268,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, + context.opts, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -269,6 +278,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, + context.opts, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -277,10 +287,10 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do ) == [] end - test "returns empty if widget is disabled", %{config: config} do + test "returns empty if widget is disabled", %{config: config, opts: opts} do config = disable_widget(config) - assert crowding_widget_instances(config) == [] + assert crowding_widget_instances(config, opts) == [] end end end From 3f1c328b329bdbe6da4b823e35a12f3a71652618 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 1 Sep 2023 11:53:23 -0400 Subject: [PATCH 05/17] Only log on real screens. --- .../widgets/train_crowding.ex | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 5f9384063..d6db6c6ad 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -56,9 +56,11 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do params |> Map.to_list() |> fetch_alerts_fn.() do next_train_prediction = List.first(predictions) - Logger.info( - "[train_crowding next_prediction] station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" - ) + if opts[:is_real_screen] do + Logger.info( + "[train_crowding next_prediction] station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" + ) + end # If there is an upcoming train, it's headed to this station, and we're not at a temporary terminal, # show the widget @@ -68,7 +70,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do train_crowding.station_id and next_train_prediction.vehicle.carriages != [] and not any_alert_makes_this_a_terminal?(alerts, location_context) do - log_crowding_info(next_train_prediction, train_crowding) + log_crowding_info(next_train_prediction, train_crowding, opts[:is_real_screen]) [ %CrowdingWidget{ @@ -100,14 +102,16 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do LocalizedAlert.location(localized_alert) in [:boundary_downstream, :boundary_upstream] end - defp log_crowding_info(prediction, crowding_config) do - Enum.each( - prediction.vehicle.carriages, - fn %Screens.Vehicles.Carriage{} = carriage -> - Logger.info( - "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} car_number=#{carriage.car_number} vehicle_id=#{prediction.vehicle.id} crowding_level=#{carriage.occupancy_status} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id}" - ) - end + defp log_crowding_info(prediction, crowding_config, true) do + crowding_levels = + prediction.vehicle.carriages + |> Enum.sort_by(& &1.car_number) + |> Enum.map(& &1.occupancy_status) + + Logger.info( + "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" ) end + + defp log_crowding_info(_, _, _), do: :ok end From 9f2a37ed4dece11fcc899b179a0fc0b8755f84a8 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Tue, 5 Sep 2023 09:52:15 -0400 Subject: [PATCH 06/17] Fixed error from merge. --- lib/screens/v2/candidate_generator/triptych.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/screens/v2/candidate_generator/triptych.ex b/lib/screens/v2/candidate_generator/triptych.ex index d60e8482a..a140d1bf3 100644 --- a/lib/screens/v2/candidate_generator/triptych.ex +++ b/lib/screens/v2/candidate_generator/triptych.ex @@ -21,7 +21,7 @@ defmodule Screens.V2.CandidateGenerator.Triptych do def candidate_instances( config, opts, - crowding_widget_instances_fn \\ &Widgets.TrainCrowding.crowding_widget_instances/1, + crowding_widget_instances_fn \\ &Widgets.TrainCrowding.crowding_widget_instances/2, evergreen_content_instances_fn \\ &Widgets.Evergreen.evergreen_content_instances/1, local_evergreen_set_instances_fn \\ &Widgets.LocalEvergreenSet.local_evergreen_set_instances/1 ) do From 6f8b9f1d2626ce9ead8615fc439ad9c87c55dfa7 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 10:14:28 -0400 Subject: [PATCH 07/17] Changed to list of strings. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index d6db6c6ad..62166091e 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -106,7 +106,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) - |> Enum.map(& &1.occupancy_status) + |> Enum.map(&to_string/1) Logger.info( "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" From 0f3ac3cc54306928b90bbf474d7afe3bb34e8e42 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 10:31:49 -0400 Subject: [PATCH 08/17] Forgot to only to_string occupancy_status. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 62166091e..ba150d471 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -106,7 +106,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) - |> Enum.map(&to_string/1) + |> Enum.map(&to_string(&1.occupancy_status)) Logger.info( "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" From a3dee5b30dc3b69dbb451d9235e94e3747f71308 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 11:44:13 -0400 Subject: [PATCH 09/17] Adjusted display of crowding levels. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index ba150d471..ae52ee658 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -106,7 +106,8 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) - |> Enum.map(&to_string(&1.occupancy_status)) + |> Enum.map(& &1.occupancy_status) + |> Enum.join(",") Logger.info( "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" From 673540313d871d7cabcfb43e80d4434cf3628f37 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 11:45:38 -0400 Subject: [PATCH 10/17] Added screen_id and pane to logs. --- .../widgets/train_crowding.ex | 16 +++++++++++----- .../controllers/v2/screen_api_controller.ex | 9 ++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index ae52ee658..80f429961 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -58,7 +58,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do if opts[:is_real_screen] do Logger.info( - "[train_crowding next_prediction] station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" + "[train_crowding next_prediction] screen_id=#{opts[:screen_id]} triptych_pane=#{opts[:triptych_pane]} station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" ) end @@ -70,7 +70,13 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do train_crowding.station_id and next_train_prediction.vehicle.carriages != [] and not any_alert_makes_this_a_terminal?(alerts, location_context) do - log_crowding_info(next_train_prediction, train_crowding, opts[:is_real_screen]) + log_crowding_info( + next_train_prediction, + train_crowding, + opts[:is_real_screen], + opts[:screen_id], + opts[:triptych_pane] + ) [ %CrowdingWidget{ @@ -102,7 +108,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do LocalizedAlert.location(localized_alert) in [:boundary_downstream, :boundary_upstream] end - defp log_crowding_info(prediction, crowding_config, true) do + defp log_crowding_info(prediction, crowding_config, true, screen_id, triptych_pane) do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) @@ -110,9 +116,9 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do |> Enum.join(",") Logger.info( - "[train_crowding car_crowding_info] station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" + "[train_crowding car_crowding_info] screen_id=#{screen_id} triptych_pane=#{triptych_pane} station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" ) end - defp log_crowding_info(_, _, _), do: :ok + defp log_crowding_info(_, _, _, _, _), do: :ok end diff --git a/lib/screens_web/controllers/v2/screen_api_controller.ex b/lib/screens_web/controllers/v2/screen_api_controller.ex index b92fd15d0..a98344464 100644 --- a/lib/screens_web/controllers/v2/screen_api_controller.ex +++ b/lib/screens_web/controllers/v2/screen_api_controller.ex @@ -80,7 +80,14 @@ defmodule ScreensWeb.V2.ScreenApiController do screen_side ) - json(conn, ScreenData.by_screen_id(screen_id, is_real_screen: is_screen)) + json( + conn, + ScreenData.by_screen_id(screen_id, + is_real_screen: is_screen, + screen_id: screen_id, + triptych_pane: triptych_pane + ) + ) end end From 1ffe934b87f8f683fd06f66529b559106ce13479 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 11:50:05 -0400 Subject: [PATCH 11/17] Used better Enum function. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 80f429961..3022d1ca7 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -112,8 +112,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) - |> Enum.map(& &1.occupancy_status) - |> Enum.join(",") + |> Enum.map_join(",", & &1.occupancy_status) Logger.info( "[train_crowding car_crowding_info] screen_id=#{screen_id} triptych_pane=#{triptych_pane} station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" From f3940efe3a9e9c0487b37690ad59891f12f0d3a4 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 18:04:09 -0400 Subject: [PATCH 12/17] Removed unnecessary log values. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 3022d1ca7..68de39cce 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -58,7 +58,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do if opts[:is_real_screen] do Logger.info( - "[train_crowding next_prediction] screen_id=#{opts[:screen_id]} triptych_pane=#{opts[:triptych_pane]} station_id=#{train_crowding.station_id} direction_id=#{train_crowding.direction_id} next_prediction_id=#{next_train_prediction.id} next_trip_id=#{next_train_prediction.trip.id}" + "[train_crowding next_prediction] screen_id=#{opts[:screen_id]} triptych_pane=#{opts[:triptych_pane]} next_trip_id=#{next_train_prediction.trip.id}" ) end @@ -115,7 +115,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do |> Enum.map_join(",", & &1.occupancy_status) Logger.info( - "[train_crowding car_crowding_info] screen_id=#{screen_id} triptych_pane=#{triptych_pane} station_id=#{crowding_config.station_id} direction_id=#{crowding_config.direction_id} trip_id=#{prediction.trip.id} prediction_id=#{prediction.id} vehicle_id=#{prediction.vehicle.id} car_crowding_levels=#{crowding_levels}" + "[train_crowding car_crowding_info] screen_id=#{screen_id} triptych_pane=#{triptych_pane} trip_id=#{prediction.trip.id} car_crowding_levels=#{crowding_levels}" ) end From 24155be1cb2d9dd7d093429ecb9d24e30b3f9995 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 18:05:02 -0400 Subject: [PATCH 13/17] Removed unused parameter. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 68de39cce..fa321967f 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -72,7 +72,6 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do not any_alert_makes_this_a_terminal?(alerts, location_context) do log_crowding_info( next_train_prediction, - train_crowding, opts[:is_real_screen], opts[:screen_id], opts[:triptych_pane] @@ -108,7 +107,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do LocalizedAlert.location(localized_alert) in [:boundary_downstream, :boundary_upstream] end - defp log_crowding_info(prediction, crowding_config, true, screen_id, triptych_pane) do + defp log_crowding_info(prediction, true, screen_id, triptych_pane) do crowding_levels = prediction.vehicle.carriages |> Enum.sort_by(& &1.car_number) @@ -119,5 +118,5 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do ) end - defp log_crowding_info(_, _, _, _, _), do: :ok + defp log_crowding_info(_, _, _, _), do: :ok end From 4861948dbba9db46f17927c0429b234b6f25010c Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 6 Sep 2023 18:05:33 -0400 Subject: [PATCH 14/17] Removed unnecessary sort. --- lib/screens/v2/candidate_generator/widgets/train_crowding.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index fa321967f..658f40420 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -108,10 +108,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do end defp log_crowding_info(prediction, true, screen_id, triptych_pane) do - crowding_levels = - prediction.vehicle.carriages - |> Enum.sort_by(& &1.car_number) - |> Enum.map_join(",", & &1.occupancy_status) + crowding_levels = Enum.map_join(prediction.vehicle.carriages, ",", & &1.occupancy_status) Logger.info( "[train_crowding car_crowding_info] screen_id=#{screen_id} triptych_pane=#{triptych_pane} trip_id=#{prediction.trip.id} car_crowding_levels=#{crowding_levels}" From d679aba763da9a4ed7fb6a345cfb3b64b1934cf4 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 8 Sep 2023 11:30:09 -0400 Subject: [PATCH 15/17] Created a logging_options map to hold the new options. --- .../widgets/train_crowding.ex | 21 +++++++++---------- .../controllers/v2/screen_api_controller.ex | 8 ++++--- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 658f40420..e8256267d 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -36,7 +36,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do def crowding_widget_instances( %Screen{app_params: %Triptych{train_crowding: train_crowding}} = config, - opts, + [logging_options: logging_options], now, fetch_predictions_fn, fetch_location_context_fn, @@ -56,9 +56,9 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do params |> Map.to_list() |> fetch_alerts_fn.() do next_train_prediction = List.first(predictions) - if opts[:is_real_screen] do + if logging_options.is_real_screen do Logger.info( - "[train_crowding next_prediction] screen_id=#{opts[:screen_id]} triptych_pane=#{opts[:triptych_pane]} next_trip_id=#{next_train_prediction.trip.id}" + "[train_crowding next_prediction] screen_id=#{logging_options.screen_id} triptych_pane=#{logging_options.triptych_pane} next_trip_id=#{next_train_prediction.trip.id}" ) end @@ -70,12 +70,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do train_crowding.station_id and next_train_prediction.vehicle.carriages != [] and not any_alert_makes_this_a_terminal?(alerts, location_context) do - log_crowding_info( - next_train_prediction, - opts[:is_real_screen], - opts[:screen_id], - opts[:triptych_pane] - ) + log_crowding_info(next_train_prediction, logging_options) [ %CrowdingWidget{ @@ -107,7 +102,11 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do LocalizedAlert.location(localized_alert) in [:boundary_downstream, :boundary_upstream] end - defp log_crowding_info(prediction, true, screen_id, triptych_pane) do + defp log_crowding_info(prediction, %{ + is_real_screen: true, + screen_id: screen_id, + triptych_pane: triptych_pane + }) do crowding_levels = Enum.map_join(prediction.vehicle.carriages, ",", & &1.occupancy_status) Logger.info( @@ -115,5 +114,5 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do ) end - defp log_crowding_info(_, _, _, _), do: :ok + defp log_crowding_info(_, _), do: :ok end diff --git a/lib/screens_web/controllers/v2/screen_api_controller.ex b/lib/screens_web/controllers/v2/screen_api_controller.ex index a98344464..a6d058802 100644 --- a/lib/screens_web/controllers/v2/screen_api_controller.ex +++ b/lib/screens_web/controllers/v2/screen_api_controller.ex @@ -83,9 +83,11 @@ defmodule ScreensWeb.V2.ScreenApiController do json( conn, ScreenData.by_screen_id(screen_id, - is_real_screen: is_screen, - screen_id: screen_id, - triptych_pane: triptych_pane + logging_options: %{ + is_real_screen: is_screen, + screen_id: screen_id, + triptych_pane: triptych_pane + } ) ) end From c6df566dfb80a9dfd5d8d6bfbfdf83d08b493519 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 8 Sep 2023 12:06:56 -0400 Subject: [PATCH 16/17] Tweaked when logging_options is extracted from opts. --- lib/screens/v2/candidate_generator/triptych.ex | 2 +- .../v2/candidate_generator/widgets/train_crowding.ex | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/screens/v2/candidate_generator/triptych.ex b/lib/screens/v2/candidate_generator/triptych.ex index a140d1bf3..cb64637e3 100644 --- a/lib/screens/v2/candidate_generator/triptych.ex +++ b/lib/screens/v2/candidate_generator/triptych.ex @@ -26,7 +26,7 @@ defmodule Screens.V2.CandidateGenerator.Triptych do local_evergreen_set_instances_fn \\ &Widgets.LocalEvergreenSet.local_evergreen_set_instances/1 ) do [ - fn -> crowding_widget_instances_fn.(config, opts) end, + fn -> crowding_widget_instances_fn.(config, opts[:logging_options]) end, fn -> evergreen_content_instances_fn.(config) end, fn -> local_evergreen_set_instances_fn.(config) end ] diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index e8256267d..5a5b45352 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -11,10 +11,10 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do alias Screens.V2.LocalizedAlert alias Screens.V2.WidgetInstance.TrainCrowding, as: CrowdingWidget - @spec crowding_widget_instances(Screen.t(), keyword()) :: list(CrowdingWidget.t()) + @spec crowding_widget_instances(Screen.t(), map()) :: list(CrowdingWidget.t()) def crowding_widget_instances( config, - opts, + logging_options, now \\ DateTime.utc_now(), fetch_predictions_fn \\ &Prediction.fetch/1, fetch_location_context_fn \\ &Stop.fetch_location_context/3, @@ -36,7 +36,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do def crowding_widget_instances( %Screen{app_params: %Triptych{train_crowding: train_crowding}} = config, - [logging_options: logging_options], + logging_options, now, fetch_predictions_fn, fetch_location_context_fn, From d596f36847b22bbadae3d2ec4397817866887e78 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 8 Sep 2023 12:07:08 -0400 Subject: [PATCH 17/17] Fixed tests. --- .../widgets/train_crowding_test.exs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs index bf6544580..bcc814ca2 100644 --- a/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs +++ b/test/screens/v2/candidate_generator/widgets/train_crowding_test.exs @@ -146,7 +146,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do %{ config: config, - opts: [], + logging_options: %{is_real_screen: false, screen_id: "TEST", triptych_pane: nil}, now: ~U[2023-08-16 21:04:00Z], next_train_prediction: next_train_prediction, fetch_predictions_fn: fn _ -> {:ok, [next_train_prediction]} end, @@ -171,7 +171,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns crowding widget if train is on the way to this station", context do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -194,7 +194,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, fn _ -> {:ok, [alt_prediction]} end, context.fetch_location_context_fn, @@ -211,7 +211,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, fn _ -> {:ok, [alt_prediction]} end, context.fetch_location_context_fn, @@ -224,7 +224,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do context do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -236,7 +236,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns empty if there are no predictions", context do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, fn _ -> {:ok, []} end, context.fetch_location_context_fn, @@ -248,7 +248,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do test "returns empty if any fetches fail", context do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, fn _ -> :error end, context.fetch_location_context_fn, @@ -258,7 +258,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, context.fetch_predictions_fn, fn _, _, _ -> :error end, @@ -268,7 +268,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -278,7 +278,7 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do assert crowding_widget_instances( context.config, - context.opts, + context.logging_options, context.now, context.fetch_predictions_fn, context.fetch_location_context_fn, @@ -287,10 +287,13 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowdingTest do ) == [] end - test "returns empty if widget is disabled", %{config: config, opts: opts} do + test "returns empty if widget is disabled", %{ + config: config, + logging_options: logging_options + } do config = disable_widget(config) - assert crowding_widget_instances(config, opts) == [] + assert crowding_widget_instances(config, logging_options) == [] end end end