From 801f5127feea27b5f05d186a2c15084b7060a5ab Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Wed, 1 May 2024 14:39:22 -0400 Subject: [PATCH] Temporarily always show crowding widget. --- .../widgets/train_crowding.ex | 386 +++++++++--------- 1 file changed, 193 insertions(+), 193 deletions(-) diff --git a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex index 8621a144f..422139b85 100644 --- a/lib/screens/v2/candidate_generator/widgets/train_crowding.ex +++ b/lib/screens/v2/candidate_generator/widgets/train_crowding.ex @@ -14,28 +14,28 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do alias ScreensConfig.V2.{TrainCrowding, Triptych} # {parent_station_id, {sb_platform_id, nb_platform_id}} - @ol_station_to_platform_map [ - {"place-ogmnl", {"70036", "70036"}}, - {"place-mlmnl", {"70034", "70035"}}, - {"place-welln", {"70032", "70033"}}, - {"place-astao", {"70278", "70279"}}, - {"place-sull", {"70030", "70031"}}, - {"place-ccmnl", {"70028", "70029"}}, - {"place-north", {"70026", "70027"}}, - {"place-haecl", {"70024", "70025"}}, - {"place-state", {"70022", "70023"}}, - {"place-dwnxg", {"70020", "70021"}}, - {"place-chncl", {"70018", "70019"}}, - {"place-tumnl", {"70016", "70017"}}, - {"place-bbsta", {"70014", "70015"}}, - {"place-masta", {"70012", "70013"}}, - {"place-rugg", {"70010", "70011"}}, - {"place-rcmnl", {"70008", "70009"}}, - {"place-jaksn", {"70006", "70007"}}, - {"place-sbmnl", {"70004", "70005"}}, - {"place-grnst", {"70002", "70003"}}, - {"place-forhl", {"70001", "70001"}} - ] + # @ol_station_to_platform_map [ + # {"place-ogmnl", {"70036", "70036"}}, + # {"place-mlmnl", {"70034", "70035"}}, + # {"place-welln", {"70032", "70033"}}, + # {"place-astao", {"70278", "70279"}}, + # {"place-sull", {"70030", "70031"}}, + # {"place-ccmnl", {"70028", "70029"}}, + # {"place-north", {"70026", "70027"}}, + # {"place-haecl", {"70024", "70025"}}, + # {"place-state", {"70022", "70023"}}, + # {"place-dwnxg", {"70020", "70021"}}, + # {"place-chncl", {"70018", "70019"}}, + # {"place-tumnl", {"70016", "70017"}}, + # {"place-bbsta", {"70014", "70015"}}, + # {"place-masta", {"70012", "70013"}}, + # {"place-rugg", {"70010", "70011"}}, + # {"place-rcmnl", {"70008", "70009"}}, + # {"place-jaksn", {"70006", "70007"}}, + # {"place-sbmnl", {"70004", "70005"}}, + # {"place-grnst", {"70002", "70003"}}, + # {"place-forhl", {"70001", "70001"}} + # ] @spec crowding_widget_instances(Screen.t(), map()) :: list(CrowdingWidget.t()) def crowding_widget_instances( @@ -124,181 +124,181 @@ defmodule Screens.V2.CandidateGenerator.Widgets.TrainCrowding do # If there is an upcoming train, it's headed to this station, # and we're not at a temporary terminal, log the widget. - if Prediction.vehicle_status(next_train_prediction) in [:in_transit_to, :incoming_at] and - next_train_prediction - |> Prediction.stop_for_vehicle() - |> common_params.fetch_parent_stop_id_fn.() == - common_params.train_crowding_config.station_id do - _ = log_crowding_info(:in_transit, common_params) - - Agent.delete( - common_params.train_crowding_config.station_id, - common_params.train_crowding_config.direction_id - ) - - [ - %CrowdingWidget{ - screen: common_params[:screen_config], - prediction: next_train_prediction, - now: common_params[:now] - } - ] - - # Test other heuristics - else - log_heuristics(common_params) - end - end - - defp log_heuristics(common_params) do - train_crowding_config = common_params.train_crowding_config - next_train_prediction = common_params.next_train_prediction - - ol_stop_sequence = - if train_crowding_config.direction_id == 0 do - @ol_station_to_platform_map - else - Enum.reverse(@ol_station_to_platform_map) - end - - previous_stop_index = - Enum.find_index( - ol_stop_sequence, - &(elem(&1, 0) == train_crowding_config.station_id) - ) - 1 - - {_, platform_id_tuple} = Enum.at(ol_stop_sequence, previous_stop_index) - - previous_platform_id = elem(platform_id_tuple, train_crowding_config.direction_id) - - cached_prediction = - Agent.get(train_crowding_config.station_id, train_crowding_config.direction_id) - - cond do - cached_prediction != nil -> - # Time-based heuristic - # We think the train is about to leave the previous station. Log the heuristic. - _ = check_time_based_heuristic(cached_prediction, common_params, -10) - - # Consecutive crowding class heuristic. - # Crowding class this fetch is the same as last. Log the heuristic. - _ = - check_consecutive_crowding_heuristic( - next_train_prediction, - previous_platform_id, - cached_prediction, - common_params - ) - - [] - - # Cache previous prediction but don't log the widget - Prediction.vehicle_status(next_train_prediction) == :stopped_at and - Prediction.stop_for_vehicle(next_train_prediction) == previous_platform_id -> - previous_station_prediction_current_trip = - fetch_previous_station_prediction( - train_crowding_config, - previous_platform_id, - next_train_prediction.trip.id, - common_params.fetch_predictions_fn - ) - - if is_nil(previous_station_prediction_current_trip) do - Logger.warning( - "[log_heuristics] Failed to fetch previous station's prediction: current_platform_id: #{train_crowding_config.station_id} previous_platform_id: #{previous_platform_id} trip_id: #{next_train_prediction.trip.id}" - ) - - [] - else - # Cache the prediction for the current trip at the previous station. - Agent.put( - train_crowding_config.station_id, - train_crowding_config.direction_id, - previous_station_prediction_current_trip - ) - - [] - end - - true -> - [] - end - end - - defp fetch_previous_station_prediction( - train_crowding_config, - relevant_platform_id, - trip_id, - fetch_predictions_fn - ) do - params = %{ - direction_id: train_crowding_config.direction_id, - route_ids: [train_crowding_config.route_id], - stop_ids: [relevant_platform_id], - trip_id: trip_id - } + # if Prediction.vehicle_status(next_train_prediction) in [:in_transit_to, :incoming_at] and + # next_train_prediction + # |> Prediction.stop_for_vehicle() + # |> common_params.fetch_parent_stop_id_fn.() == + # common_params.train_crowding_config.station_id do + _ = log_crowding_info(:in_transit, common_params) + + Agent.delete( + common_params.train_crowding_config.station_id, + common_params.train_crowding_config.direction_id + ) - {:ok, predictions} = fetch_predictions_fn.(params) - List.first(predictions) - end + [ + %CrowdingWidget{ + screen: common_params[:screen_config], + prediction: next_train_prediction, + now: common_params[:now] + } + ] - defp check_time_based_heuristic( - cached_prediction, - common_params, - previous_departure_time_cushion - ) do - # cached_prediction.departure_time minus previous_departure_time_cushion is when we expect crowding to be reliable. - # When now >= this time, log the widget. - log_widget_after_time = - DateTime.add(cached_prediction.departure_time, previous_departure_time_cushion) - - if DateTime.compare( - common_params.now, - log_widget_after_time - ) in [ - :eq, - :gt - ] do - log_crowding_info( - :time_based, - common_params - ) - end + # Test other heuristics + # else + # log_heuristics(common_params) + # end end - defp check_consecutive_crowding_heuristic( - next_train_prediction, - relevant_platform_id, - cached_prediction, - common_params - ) do - if next_train_prediction.vehicle.carriages == - cached_prediction.vehicle.carriages do - log_crowding_info( - :consecutive_crowding, - common_params - ) - else - # Update the cached prediction so we can check the new crowding classes next fetch. - # The departure time we use in the other heuristic should not change, and if it does it is changing to a more accurate time. - # If the prediction is nil, leave the existing one alone just in case the time heuristic can log next refresh. - previous_station_prediction_current_trip = - fetch_previous_station_prediction( - common_params.train_crowding_config, - relevant_platform_id, - next_train_prediction.trip.id, - common_params.fetch_predictions_fn - ) - - if previous_station_prediction_current_trip != nil do - Agent.put( - common_params.train_crowding_config.station_id, - common_params.train_crowding_config.direction_id, - previous_station_prediction_current_trip - ) - end - end - end + # defp log_heuristics(common_params) do + # train_crowding_config = common_params.train_crowding_config + # next_train_prediction = common_params.next_train_prediction + + # ol_stop_sequence = + # if train_crowding_config.direction_id == 0 do + # @ol_station_to_platform_map + # else + # Enum.reverse(@ol_station_to_platform_map) + # end + + # previous_stop_index = + # Enum.find_index( + # ol_stop_sequence, + # &(elem(&1, 0) == train_crowding_config.station_id) + # ) - 1 + + # {_, platform_id_tuple} = Enum.at(ol_stop_sequence, previous_stop_index) + + # previous_platform_id = elem(platform_id_tuple, train_crowding_config.direction_id) + + # cached_prediction = + # Agent.get(train_crowding_config.station_id, train_crowding_config.direction_id) + + # cond do + # cached_prediction != nil -> + # # Time-based heuristic + # # We think the train is about to leave the previous station. Log the heuristic. + # _ = check_time_based_heuristic(cached_prediction, common_params, -10) + + # # Consecutive crowding class heuristic. + # # Crowding class this fetch is the same as last. Log the heuristic. + # _ = + # check_consecutive_crowding_heuristic( + # next_train_prediction, + # previous_platform_id, + # cached_prediction, + # common_params + # ) + + # [] + + # # Cache previous prediction but don't log the widget + # Prediction.vehicle_status(next_train_prediction) == :stopped_at and + # Prediction.stop_for_vehicle(next_train_prediction) == previous_platform_id -> + # previous_station_prediction_current_trip = + # fetch_previous_station_prediction( + # train_crowding_config, + # previous_platform_id, + # next_train_prediction.trip.id, + # common_params.fetch_predictions_fn + # ) + + # if is_nil(previous_station_prediction_current_trip) do + # Logger.warning( + # "[log_heuristics] Failed to fetch previous station's prediction: current_platform_id: #{train_crowding_config.station_id} previous_platform_id: #{previous_platform_id} trip_id: #{next_train_prediction.trip.id}" + # ) + + # [] + # else + # # Cache the prediction for the current trip at the previous station. + # Agent.put( + # train_crowding_config.station_id, + # train_crowding_config.direction_id, + # previous_station_prediction_current_trip + # ) + + # [] + # end + + # true -> + # [] + # end + # end + + # defp fetch_previous_station_prediction( + # train_crowding_config, + # relevant_platform_id, + # trip_id, + # fetch_predictions_fn + # ) do + # params = %{ + # direction_id: train_crowding_config.direction_id, + # route_ids: [train_crowding_config.route_id], + # stop_ids: [relevant_platform_id], + # trip_id: trip_id + # } + + # {:ok, predictions} = fetch_predictions_fn.(params) + # List.first(predictions) + # end + + # defp check_time_based_heuristic( + # cached_prediction, + # common_params, + # previous_departure_time_cushion + # ) do + # # cached_prediction.departure_time minus previous_departure_time_cushion is when we expect crowding to be reliable. + # # When now >= this time, log the widget. + # log_widget_after_time = + # DateTime.add(cached_prediction.departure_time, previous_departure_time_cushion) + + # if DateTime.compare( + # common_params.now, + # log_widget_after_time + # ) in [ + # :eq, + # :gt + # ] do + # log_crowding_info( + # :time_based, + # common_params + # ) + # end + # end + + # defp check_consecutive_crowding_heuristic( + # next_train_prediction, + # relevant_platform_id, + # cached_prediction, + # common_params + # ) do + # if next_train_prediction.vehicle.carriages == + # cached_prediction.vehicle.carriages do + # log_crowding_info( + # :consecutive_crowding, + # common_params + # ) + # else + # # Update the cached prediction so we can check the new crowding classes next fetch. + # # The departure time we use in the other heuristic should not change, and if it does it is changing to a more accurate time. + # # If the prediction is nil, leave the existing one alone just in case the time heuristic can log next refresh. + # previous_station_prediction_current_trip = + # fetch_previous_station_prediction( + # common_params.train_crowding_config, + # relevant_platform_id, + # next_train_prediction.trip.id, + # common_params.fetch_predictions_fn + # ) + + # if previous_station_prediction_current_trip != nil do + # Agent.put( + # common_params.train_crowding_config.station_id, + # common_params.train_crowding_config.direction_id, + # previous_station_prediction_current_trip + # ) + # end + # end + # end # Given alerts at this station, check to see if any alert make this a temporary terminal defp any_alert_makes_this_a_terminal?(alerts, location_context) do