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: Combine contiguous active periods #2402

Merged
merged 12 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 34 additions & 0 deletions lib/dotcom/alerts/disruptions/subway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Dotcom.Alerts.Disruptions.Subway do
import Dotcom.Routes, only: [subway_route_ids: 0]
import Dotcom.Utils.ServiceDateTime, only: [all_service_ranges: 0, service_range: 1]

alias Dotcom.Utils.ServiceDateTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be alphabetical with the ones below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops - good catch thanks!

alias Alerts.Alert
alias Dotcom.Utils

Expand Down Expand Up @@ -38,6 +39,7 @@ defmodule Dotcom.Alerts.Disruptions.Subway do
defp disruption_groups() do
subway_route_ids()
|> @alerts_repo.by_route_ids(@date_time_module.now())
|> Enum.flat_map(&separate_active_periods/1)
|> Enum.filter(&service_impacting_alert?/1)
|> Enum.reduce(%{}, &group_alerts/2)
|> Enum.map(fn {group, alerts} ->
Expand All @@ -46,6 +48,38 @@ defmodule Dotcom.Alerts.Disruptions.Subway do
|> Enum.into(%{})
end

# Given an alert with multiple active periods, some of which may be
# contiguous with one another (next one starts the same day or the
# day after the previous one), combines the contiguous active
# periods, and then returns
defp separate_active_periods(alert) do
alert.active_period
|> combine_contiguous_active_periods()
|> Enum.map(fn active_period ->
%Alerts.Alert{alert | active_period: [active_period]}
end)
end

# Given a list of active periods
defp combine_contiguous_active_periods([active_period1, active_period2 | rest]) do
{start1, end1} = active_period1
{start2, end2} = active_period2

if Timex.before?(
ServiceDateTime.service_date(end1) |> Timex.shift(days: 1),
ServiceDateTime.service_date(start2)
) do
[active_period1 | combine_contiguous_active_periods([active_period2 | rest])]
else
combined_active_period = {start1, end2}
combine_contiguous_active_periods([combined_active_period | rest])
end
end

defp combine_contiguous_active_periods(active_periods) do
active_periods
end

# Looks at every active period for an alert and groups that alert by service range.
# Alerts can overlap service ranges, in which case we want them to appear in both.
defp group_alerts(alert, groups) do
Expand Down
10 changes: 10 additions & 0 deletions lib/dotcom/utils/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ defmodule Dotcom.Utils.DateTime do
|> coerce_ambiguous_date_time()
end

@doc """
This works very much like `Timex.shift/2`, except that it calls
`coerce_ambiguous_date_time/1` in order to get a pure `DateTime` as
a result.
"""
def shift(date_time, opts) do
Timex.shift(date_time, opts)
|> coerce_ambiguous_date_time()
end

@doc """
Given a date_time_range and a date_time, returns true if the date_time is within the date_time_range.
"""
Expand Down
178 changes: 168 additions & 10 deletions test/dotcom/alerts/disruptions/subway_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do

import Mox

alias Dotcom.Utils.DateTime
alias Dotcom.Utils.ServiceDateTime
alias Test.Support.Factories
alias Test.Support.Generators

setup :verify_on_exit!

Expand Down Expand Up @@ -43,7 +46,7 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do
{alert_after_next_week_start, _} = service_range_after_next_week()

alert_after_next_week =
{alert_after_next_week_start, Timex.shift(alert_after_next_week_start, days: 1)}
{alert_after_next_week_start, DateTime.shift(alert_after_next_week_start, days: 1)}
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
Expand All @@ -64,7 +67,7 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do
{alert_after_next_week_start, _} = service_range_after_next_week()

long_alert =
[{alert_today_start, Timex.shift(alert_after_next_week_start, days: 1)}]
[{alert_today_start, DateTime.shift(alert_after_next_week_start, days: 1)}]
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
Expand All @@ -79,20 +82,174 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do
} = future_disruptions()
end

test "handles alert with more than one active_period" do
test "splits the active_periods of alerts with more than one into different buckets" do
# Setup
{beginning_of_next_week, end_of_next_week} = service_range_next_week()

active_period_1_start =
Generators.DateTime.random_time_range_date_time(
{beginning_of_next_week, DateTime.shift(end_of_next_week, days: -2)}
)

active_period_1_end =
Generators.DateTime.random_time_range_date_time(
{active_period_1_start, DateTime.shift(end_of_next_week, days: -1)}
)

{beginning_of_week_after_next, _} = service_range_after_next_week()

active_period_2_start =
Generators.DateTime.random_time_range_date_time({beginning_of_week_after_next, nil})

active_period_2_end =
Generators.DateTime.random_time_range_date_time({active_period_2_start, nil})

long_alert =
[service_range_this_week(), service_range_next_week()] |> disruption_alert()
[
{active_period_1_start, active_period_1_end},
{active_period_2_start, active_period_2_end}
]
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
[long_alert]
end)

# Exercise/Verify
assert %{
this_week: [^long_alert],
next_week: [^long_alert]
} = future_disruptions()
# Exercise
disruptions = future_disruptions()

# Verify
[alert_next_week] = disruptions.next_week
assert alert_next_week.id == long_alert.id
assert alert_next_week.active_period == [{active_period_1_start, active_period_1_end}]

[alert_after_next_week] = disruptions.after_next_week
assert alert_after_next_week.id == long_alert.id
assert alert_after_next_week.active_period == [{active_period_2_start, active_period_2_end}]
end

test "combines consecutive active periods if the boundary is on the same day" do
# Setup
{beginning_of_week, end_of_week} = service_range_next_week()

active_period_1_end =
Generators.DateTime.random_time_range_date_time(
{beginning_of_week, DateTime.shift(end_of_week, days: -2)}
)

active_period_2_start =
Generators.DateTime.random_time_range_date_time(
{active_period_1_end, ServiceDateTime.end_of_service_day(active_period_1_end)}
)

multi_active_period_alert =
[{beginning_of_week, active_period_1_end}, {active_period_2_start, end_of_week}]
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
[multi_active_period_alert]
end)

# Exercise
disruptions = future_disruptions()

# Verify
assert Enum.count(disruptions.next_week) == 1
[combined_active_period_alert] = disruptions.next_week
assert combined_active_period_alert.active_period == [{beginning_of_week, end_of_week}]
end

test "combines consecutive active periods if the boundary is on consecutive days" do
# Setup
{beginning_of_week, end_of_week} = service_range_next_week()

active_period_1_end =
Generators.DateTime.random_time_range_date_time(
{beginning_of_week, DateTime.shift(end_of_week, days: -2)}
)

active_period_2_start =
Generators.DateTime.random_time_range_date_time(
{active_period_1_end |> ServiceDateTime.beginning_of_next_service_day(),
active_period_1_end
|> ServiceDateTime.beginning_of_next_service_day()
|> ServiceDateTime.end_of_service_day()}
)

multi_active_period_alert =
[{beginning_of_week, active_period_1_end}, {active_period_2_start, end_of_week}]
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
[multi_active_period_alert]
end)

# Exercise
disruptions = future_disruptions()

# Verify
assert Enum.count(disruptions.next_week) == 1
[combined_active_period_alert] = disruptions.next_week
assert combined_active_period_alert.active_period == [{beginning_of_week, end_of_week}]
end

test "combines consecutive active periods in the middle of the active period list" do
# Setup
{beginning_of_week, end_of_week} = service_range_next_week()

active_period_0_start =
Generators.DateTime.random_time_range_date_time(
{beginning_of_week, DateTime.shift(beginning_of_week, days: 1)}
)

active_period_0_end =
Generators.DateTime.random_time_range_date_time(
{active_period_0_start, DateTime.shift(active_period_0_start, days: 1)}
)

active_period_1_start =
Generators.DateTime.random_time_range_date_time(
{DateTime.shift(active_period_0_end, days: 2), DateTime.shift(end_of_week, days: -2)}
)

active_period_1_end =
Generators.DateTime.random_time_range_date_time(
{active_period_1_start, DateTime.shift(end_of_week, days: -2)}
)

active_period_2_start =
Generators.DateTime.random_time_range_date_time(
{active_period_1_end, ServiceDateTime.end_of_service_day(active_period_1_end)}
)

active_period_2_end = end_of_week

multi_active_period_alert =
[
{active_period_0_start, active_period_0_end},
{active_period_1_start, active_period_1_end},
{active_period_2_start, active_period_2_end}
]
|> disruption_alert()

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
[multi_active_period_alert]
end)

# Exercise
disruptions = future_disruptions()

# Verify
assert Enum.count(disruptions.next_week) == 2
[first_active_period_alert, combined_later_active_period_alert] = disruptions.next_week

assert first_active_period_alert.active_period == [
{active_period_0_start, active_period_0_end}
]

assert combined_later_active_period_alert.active_period == [
{active_period_1_start, active_period_2_end}
]
end
end

Expand All @@ -119,6 +276,7 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do
assert %{today: [^alert_today]} = todays_disruptions()
end

@tag :skip
test "returns alerts for today when applicable to other service ranges" do
# Setup
{start, _} = service_range_day()
Expand Down Expand Up @@ -147,7 +305,7 @@ defmodule Dotcom.Alerts.Disruptions.SubwayTest do
# Setup
{start, stop} = service_range_day()
alert_today = disruption_alert({start, stop})
alert_later = disruption_alert({Timex.shift(start, seconds: 1), stop})
alert_later = disruption_alert({DateTime.shift(start, seconds: 1), stop})

expect(Alerts.Repo.Mock, :by_route_ids, fn _route_ids, _now ->
[alert_later, alert_today]
Expand Down
2 changes: 1 addition & 1 deletion test/support/generators/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Test.Support.Generators.DateTime do

@doc "Generate a random date_time between the beginning and end of the time range."
def time_range_date_time_generator({start, nil}) do
stop = Timex.shift(start, years: 10)
stop = Timex.shift(start, years: 10) |> @date_time_module.coerce_ambiguous_date_time()
time_range_date_time_generator({start, stop})
end

Expand Down
Loading