From f2df63f80fc9003dedb1734a807ffd1fedb56f54 Mon Sep 17 00:00:00 2001 From: Erin Moore Date: Wed, 25 Nov 2026 10:09:48 -0500 Subject: [PATCH 1/4] first pass association validator --- config/config.exs | 4 +- .../alert_association_validator.ex | 117 +++++++++++++++ .../jobs/alert_association_daily_worker.ex | 23 +++ .../alert_association_validator_test.exs | 134 ++++++++++++++++++ .../alert_association_daily_worker_test.exs | 12 ++ 5 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 lib/alerts_ui/alert_validation/alert_association_validator.ex create mode 100644 lib/alerts_ui/jobs/alert_association_daily_worker.ex create mode 100644 test/alerts_ui/alert_validation/alert_association_validator_test.exs create mode 100644 test/alerts_ui/jobs/alert_association_daily_worker_test.exs diff --git a/config/config.exs b/config/config.exs index b467c7b5e..a1d6f855b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -129,7 +129,9 @@ config :alerts_ui, Oban, # Update recurring alerts every day at 3:01 am EST {"1 8 * * *", AlertsUI.Jobs.RecurringAlertUpdater}, # Validate alerts against production every day at 4:00 am EST - {"0 9 * * *", AlertsUI.Jobs.AlertValidationDailyWorker} + {"0 9 * * *", AlertsUI.Jobs.AlertValidationDailyWorker}, + # Validate alert associations against production every day at 4:30 am EST + {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} ]} ], queues: [default: 10] diff --git a/lib/alerts_ui/alert_validation/alert_association_validator.ex b/lib/alerts_ui/alert_validation/alert_association_validator.ex new file mode 100644 index 000000000..d01d19b94 --- /dev/null +++ b/lib/alerts_ui/alert_validation/alert_association_validator.ex @@ -0,0 +1,117 @@ +defmodule AlertsUI.AlertValidation.AlertAssociationValidator do + @moduledoc """ + Checks open alerts that have duration certainties of KNOWN or UNKNOWN against + provided API url. If they have any stops or trips that are not associated with their routes, + the alert IDs are logged so Splunk can alert + interested parties. If you want to make this run for upcoming, + then the api library functions need updated to take the api key. + """ + + alias AlertsUI.Alerts + alias AlertsUI.Models.Alert + alias MBTAV3API.Schedules.Repo, as: SchedulesRepo + alias MBTAV3API.Stops.Repo, as: StopsRepo + + @type invalid_alert_t :: %{ + alert_id: String.t(), + invalid_stops: [String.t()], + invalid_trips: [String.t()] + } + + @spec validate_alerts() :: [invalid_alert_t()] + def validate_alerts do + Alerts.list_all_open() + |> Enum.reject(&(&1.primary_service_type == "2")) + |> gather_alerts() + end + + @spec validate_commuter_rail_alerts() :: [invalid_alert_t()] + def validate_commuter_rail_alerts do + Alerts.list_all_open() + |> Enum.filter(&(&1.primary_service_type == "2")) + |> gather_alerts() + end + + defp gather_alerts(alerts) do + alerts + |> Enum.map(fn alert -> + %{ + alert_id: alert.alert_id, + route_ids: Alert.gather_route_ids(alert), + stop_ids: Alert.gather_stop_ids(alert), + trip_ids: Alert.gather_trip_ids(alert) + } + end) + |> Enum.reduce([], fn alert_map, acc -> + invalid_stops = invalid_stops(alert_map.stop_ids, alert_map.route_ids) + invalid_trips = invalid_trips(alert_map.trip_ids, alert_map.route_ids) + + if Enum.all?( + [invalid_stops, invalid_trips], + &Enum.empty?/1 + ) do + acc + else + entity_results = %{ + alert_id: alert_map.alert_id, + invalid_stops: invalid_stops, + invalid_trips: invalid_trips + } + + [entity_results | acc] + end + end) + end + + @spec format_invalid_alerts([invalid_alert_t()], String.t()) :: String.t() + # use to format a list of invalid alert structs for logging purposes + def format_invalid_alerts(invalid_alerts, error_message_word, mode_word \\ "") + def format_invalid_alerts([], _, _), do: "No invalid alerts found" + + def format_invalid_alerts(invalid_alerts, error_message_word, mode_word) do + invalid_alert_info = + Enum.map(invalid_alerts, fn invalid_alert -> + entity_results = + [ + format_entity_strings(invalid_alert.invalid_stops, "Stops"), + format_entity_strings(invalid_alert.invalid_trips, "Trips") + ] + |> Enum.reject(&is_nil/1) + + "Alert #{invalid_alert.alert_id}:|" <> Enum.join(entity_results, "|") + end) + + "The following #{mode_word}alerts have entities that are no longer associated with each other in #{error_message_word} GTFS:|" <> + Enum.join(invalid_alert_info, "||") + end + + defp invalid_stops([], _), do: [] + defp invalid_stops(_, []), do: [] + + defp invalid_stops(stop_ids, route_ids) do + found_stop_ids = + StopsRepo.all("filter[route]": Enum.join(route_ids, ",")) + |> Enum.map(& &1.id) + |> Enum.filter(&(&1 in stop_ids)) + + stop_ids -- found_stop_ids + end + + defp invalid_trips([], _), do: [] + defp invalid_trips(_, []), do: [] + + defp invalid_trips(trip_ids, route_ids) do + found_trip_ids = + SchedulesRepo.by_route_ids(route_ids) + |> Enum.map(& &1.trip.id) + |> Enum.filter(&(&1 in trip_ids)) + + trip_ids -- found_trip_ids + end + + defp format_entity_strings([], _), do: nil + + defp format_entity_strings(items, category) do + "#{category}: #{Enum.join(items, ", ")}" + end +end diff --git a/lib/alerts_ui/jobs/alert_association_daily_worker.ex b/lib/alerts_ui/jobs/alert_association_daily_worker.ex new file mode 100644 index 000000000..6e5d3a0b5 --- /dev/null +++ b/lib/alerts_ui/jobs/alert_association_daily_worker.ex @@ -0,0 +1,23 @@ +defmodule AlertsUI.Jobs.AlertAssociationDailyWorker do + @moduledoc """ + Oban worker that checks the stops and trips of open alerts for + association validity once a day, against the production API. + (eg that they are still associated with their routes) + """ + use Oban.Worker + + require Logger + + alias AlertsUI.AlertValidation.AlertAssociationValidator + + @impl Oban.Worker + def perform(_job) do + AlertAssociationValidator.validate_alerts() + |> AlertAssociationValidator.format_invalid_alerts("current") + |> Logger.info() + + AlertAssociationValidator.validate_commuter_rail_alerts() + |> AlertAssociationValidator.format_invalid_alerts("current", "Commuter Rail ") + |> Logger.info() + end +end diff --git a/test/alerts_ui/alert_validation/alert_association_validator_test.exs b/test/alerts_ui/alert_validation/alert_association_validator_test.exs new file mode 100644 index 000000000..0083e3d26 --- /dev/null +++ b/test/alerts_ui/alert_validation/alert_association_validator_test.exs @@ -0,0 +1,134 @@ +defmodule AlertsUI.AlertValidation.AlertAssociationValidatorTest do + @moduledoc false + use AlertsUIWeb.ConnCase + import AlertsUI.Support.Factory + import Mock + + alias AlertsUI.AlertValidation.AlertAssociationValidator + + alias MBTAV3API.Schedules.Repo, as: SchedulesRepo + alias MBTAV3API.Schedules.{Schedule, Trip} + alias MBTAV3API.Stops.Repo, as: StopsRepo + alias MBTAV3API.Stops.Stop + + @cr_alert_params %{ + alert_id: "345", + primary_service_type: "2", + original_route_selections: ["Lowell Line|CR-Lowell"], + original_stop_selections: ["Wedgemere|place-NHRML-0073", "Bad Stop|place-nope"], + original_trip_selections: ["595|CR-683322-595", "000|nope-444"] + } + + @closed_alert_params %{ + alert_id: "234", + primary_service_type: "1", + closed_timestamp: ~U[2023-10-14 00:01:00Z], + original_route_selections: ["Red Line|Red"], + original_stop_selections: ["Alewife|place-alfcl", "Bad Stop|place-nope"], + original_trip_selections: ["595|CR-683322-595", "000|nope-444"] + } + + @ferry_alert_params %{ + alert_id: "123", + primary_service_type: "4", + original_route_selections: ["Quincy Ferry|Boat-F7"], + original_stop_selections: ["Central Wharf|Boat-Aquarium"], + original_trip_selections: ["595|CR-683322-595", "000|nope-444"] + } + + @subway_alert_params %{ + alert_id: "456", + primary_service_type: "1", + original_route_selections: ["Red Line|Red"], + original_stop_selections: ["Alewife|place-alfcl", "Bad Stop|place-nope"], + original_trip_selections: [] + } + + describe("validate_alerts/0") do + test "finds alerts with stops no longer associated with their routes" do + insert(:alert, @cr_alert_params) + insert(:alert, @closed_alert_params) + insert(:alert, @subway_alert_params) + + with_mocks([ + {StopsRepo, [], + [ + all: fn ["filter[route]": "Red"] -> + [%Stop{id: "place-alfcl", name: "Alewife"}] + end + ]} + ]) do + assert AlertAssociationValidator.validate_alerts() == + [ + %{ + alert_id: "456", + invalid_stops: ["place-nope"], + invalid_trips: [] + } + ] + end + end + + test "finds alerts with trips no longer associated with their routes" do + insert(:alert, @cr_alert_params) + insert(:alert, @closed_alert_params) + insert(:alert, @ferry_alert_params) + + with_mocks([ + {StopsRepo, [], + [ + all: fn ["filter[route]": "Boat-F7"] -> + [%Stop{id: "Boat-Aquarium", name: "Central Wharf"}] + end + ]}, + {SchedulesRepo, [], + [ + by_route_ids: fn ["Boat-F7"] -> + [%Schedule{trip: %Trip{id: "CR-683322-595", name: "595"}}] + end + ]} + ]) do + assert AlertAssociationValidator.validate_alerts() == + [ + %{ + alert_id: "123", + invalid_stops: [], + invalid_trips: ["nope-444"] + } + ] + end + end + end + + describe("validate_commute_rail_alerts/0") do + test "finds CR alert with stops and trips no longer associated with their routes" do + insert(:alert, @cr_alert_params) + insert(:alert, @closed_alert_params) + insert(:alert, @ferry_alert_params) + + with_mocks([ + {StopsRepo, [], + [ + all: fn ["filter[route]": "CR-Lowell"] -> + [%Stop{id: "place-NHRML-0073", name: "Wedgemere"}] + end + ]}, + {SchedulesRepo, [], + [ + by_route_ids: fn ["CR-Lowell"] -> + [%Schedule{trip: %Trip{id: "CR-683322-595", name: "595"}}] + end + ]} + ]) do + assert AlertAssociationValidator.validate_commuter_rail_alerts() == + [ + %{ + alert_id: "345", + invalid_stops: ["place-nope"], + invalid_trips: ["nope-444"] + } + ] + end + end + end +end diff --git a/test/alerts_ui/jobs/alert_association_daily_worker_test.exs b/test/alerts_ui/jobs/alert_association_daily_worker_test.exs new file mode 100644 index 000000000..1ce641e9f --- /dev/null +++ b/test/alerts_ui/jobs/alert_association_daily_worker_test.exs @@ -0,0 +1,12 @@ +defmodule AlertsUI.Jobs.AlertAssociationDailyWorkerTest do + use AlertsUI.DataCase, async: false + + alias AlertsUI.Jobs.AlertAssociationDailyWorker + alias Oban.Job + + describe "perform" do + test "runs successfully" do + assert AlertAssociationDailyWorker.perform(%Job{args: %{}}) == :ok + end + end +end From c4c37b175524a4a6173dcb13b37f3299bf66a5db Mon Sep 17 00:00:00 2001 From: Erin Moore Date: Wed, 25 Nov 2026 10:11:09 -0500 Subject: [PATCH 2/4] every 5 minute for dev testing purposes --- config/config.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index a1d6f855b..64f6c7710 100644 --- a/config/config.exs +++ b/config/config.exs @@ -131,7 +131,8 @@ config :alerts_ui, Oban, # Validate alerts against production every day at 4:00 am EST {"0 9 * * *", AlertsUI.Jobs.AlertValidationDailyWorker}, # Validate alert associations against production every day at 4:30 am EST - {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} + {"*/5 * * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} + # {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} ]} ], queues: [default: 10] From d9914c653f25e1fcd78d95bc249d89500156f8c8 Mon Sep 17 00:00:00 2001 From: Erin Moore Date: Wed, 25 Nov 2026 12:20:26 -0500 Subject: [PATCH 3/4] green line fix --- config/config.exs | 6 +- .../alert_association_validator.ex | 3 +- .../alert_association_validator_test.exs | 58 +++++++++++++++---- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/config/config.exs b/config/config.exs index 64f6c7710..73c9883d9 100644 --- a/config/config.exs +++ b/config/config.exs @@ -130,9 +130,9 @@ config :alerts_ui, Oban, {"1 8 * * *", AlertsUI.Jobs.RecurringAlertUpdater}, # Validate alerts against production every day at 4:00 am EST {"0 9 * * *", AlertsUI.Jobs.AlertValidationDailyWorker}, - # Validate alert associations against production every day at 4:30 am EST - {"*/5 * * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} - # {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} + # Validate alert associations against production every day at 4:30 am EST + {"*/5 * * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} + # {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} ]} ], queues: [default: 10] diff --git a/lib/alerts_ui/alert_validation/alert_association_validator.ex b/lib/alerts_ui/alert_validation/alert_association_validator.ex index d01d19b94..3aef65d8e 100644 --- a/lib/alerts_ui/alert_validation/alert_association_validator.ex +++ b/lib/alerts_ui/alert_validation/alert_association_validator.ex @@ -9,6 +9,7 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidator do alias AlertsUI.Alerts alias AlertsUI.Models.Alert + alias AlertsUI.Routes alias MBTAV3API.Schedules.Repo, as: SchedulesRepo alias MBTAV3API.Stops.Repo, as: StopsRepo @@ -37,7 +38,7 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidator do |> Enum.map(fn alert -> %{ alert_id: alert.alert_id, - route_ids: Alert.gather_route_ids(alert), + route_ids: alert |> Alert.gather_route_ids() |> Routes.expanding_green_line(), stop_ids: Alert.gather_stop_ids(alert), trip_ids: Alert.gather_trip_ids(alert) } diff --git a/test/alerts_ui/alert_validation/alert_association_validator_test.exs b/test/alerts_ui/alert_validation/alert_association_validator_test.exs index 0083e3d26..de72bbb95 100644 --- a/test/alerts_ui/alert_validation/alert_association_validator_test.exs +++ b/test/alerts_ui/alert_validation/alert_association_validator_test.exs @@ -11,11 +11,11 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidatorTest do alias MBTAV3API.Stops.Repo, as: StopsRepo alias MBTAV3API.Stops.Stop - @cr_alert_params %{ - alert_id: "345", - primary_service_type: "2", - original_route_selections: ["Lowell Line|CR-Lowell"], - original_stop_selections: ["Wedgemere|place-NHRML-0073", "Bad Stop|place-nope"], + @ferry_alert_params %{ + alert_id: "123", + primary_service_type: "4", + original_route_selections: ["Quincy Ferry|Boat-F7"], + original_stop_selections: ["Central Wharf|Boat-Aquarium"], original_trip_selections: ["595|CR-683322-595", "000|nope-444"] } @@ -28,11 +28,11 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidatorTest do original_trip_selections: ["595|CR-683322-595", "000|nope-444"] } - @ferry_alert_params %{ - alert_id: "123", - primary_service_type: "4", - original_route_selections: ["Quincy Ferry|Boat-F7"], - original_stop_selections: ["Central Wharf|Boat-Aquarium"], + @cr_alert_params %{ + alert_id: "345", + primary_service_type: "2", + original_route_selections: ["Lowell Line|CR-Lowell"], + original_stop_selections: ["Wedgemere|place-NHRML-0073", "Bad Stop|place-nope"], original_trip_selections: ["595|CR-683322-595", "000|nope-444"] } @@ -44,6 +44,18 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidatorTest do original_trip_selections: [] } + @green_line_alert_params %{ + alert_id: "567", + primary_service_type: "1", + original_route_selections: ["Green Line|Green"], + original_stop_selections: [ + "Kent Street|place-kntst", + "Lechmere|place-lech", + "Bad Stop|place-nope" + ], + original_trip_selections: [] + } + describe("validate_alerts/0") do test "finds alerts with stops no longer associated with their routes" do insert(:alert, @cr_alert_params) @@ -69,6 +81,32 @@ defmodule AlertsUI.AlertValidation.AlertAssociationValidatorTest do end end + test "correctly expands the green line" do + insert(:alert, @green_line_alert_params) + + with_mocks([ + {StopsRepo, [], + [ + all: fn + ["filter[route]": "Green-B,Green-C,Green-D,Green-E"] -> + [ + %Stop{id: "place-kntst", name: "Kent Street"}, + %Stop{id: "place-lech", name: "Lechmere"} + ] + end + ]} + ]) do + assert AlertAssociationValidator.validate_alerts() == + [ + %{ + alert_id: "567", + invalid_stops: ["place-nope"], + invalid_trips: [] + } + ] + end + end + test "finds alerts with trips no longer associated with their routes" do insert(:alert, @cr_alert_params) insert(:alert, @closed_alert_params) From ff46169c7338879e491e347a20ad4d2fd863636c Mon Sep 17 00:00:00 2001 From: Erin Moore Date: Wed, 25 Nov 2026 12:48:17 -0500 Subject: [PATCH 4/4] put config back --- config/config.exs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 73c9883d9..8468b2e1c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -131,8 +131,7 @@ config :alerts_ui, Oban, # Validate alerts against production every day at 4:00 am EST {"0 9 * * *", AlertsUI.Jobs.AlertValidationDailyWorker}, # Validate alert associations against production every day at 4:30 am EST - {"*/5 * * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} - # {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} + {"30 9 * * *", AlertsUI.Jobs.AlertAssociationDailyWorker} ]} ], queues: [default: 10]