Skip to content

Commit

Permalink
refactor: Extract add_alerts and convert_alerts_to_statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson committed Jan 23, 2025
1 parent a7cf2ce commit 13459ff
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/dotcom/system_status/groups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,33 @@ defmodule Dotcom.SystemStatus.Groups do
"""
def groups(alerts, time) do
@routes
|> Map.new(&{&1, alerts_for_route(alerts, &1)})
|> Enum.map(fn {route_id, alerts} ->
statuses = alerts_to_statuses(alerts, time)

%{route_id: route_id, sub_routes: [], statuses: statuses}
end)
|> add_alerts(alerts)
|> convert_alerts_to_statuses(time)
|> combine_green_line_branches()
|> combine_mattapan_with_red_line()
|> sort_routes_and_sub_routes()
end

# Maps the provided `routes` to a struct containing the route ID and
# the alerts associated with that route.
defp add_alerts(routes, alerts) do
routes
|> Enum.map(fn route_id ->
%{route_id: route_id, alerts: alerts_for_route(alerts, route_id)}
end)
end

# Maps the provided `routes_with_alerts` to a new struct that
# contains `statuses` that are constructed from the alerts, as well
# as empty `sub_routes` lists (which may get populated by the two
# `combine_*` functions below if needed).
defp convert_alerts_to_statuses(routes_with_alerts, time) do
routes_with_alerts
|> Enum.map(fn %{route_id: route_id, alerts: alerts} ->
%{route_id: route_id, sub_routes: [], statuses: alerts_to_statuses(alerts, time)}
end)
end

# Given `alerts` and `route_id`, filters out only the alerts
# applicable to the given route, using the alert's "informed
# entities".
Expand Down

0 comments on commit 13459ff

Please sign in to comment.