Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions lib/alerts/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule Alerts.Alert do
lifecycle: :unknown,
priority: :low,
severity: 5,
stale?: false,
updated_at: Timex.now(),
url: ""

Expand Down Expand Up @@ -116,6 +117,7 @@ defmodule Alerts.Alert do
lifecycle: lifecycle,
priority: Priority.priority_level(),
severity: severity,
stale?: boolean(),
updated_at: DateTime.t(),
url: String.t() | nil
}
Expand All @@ -135,6 +137,7 @@ defmodule Alerts.Alert do
|> set_priority()
|> set_direction_ids()
|> ensure_entity_set()
|> check_freshness()
end

@spec update(t(), Keyword.t()) :: t()
Expand Down Expand Up @@ -178,6 +181,34 @@ defmodule Alerts.Alert do
%__MODULE__{alert | priority: Priority.priority(alert)}
end

defp check_freshness(%__MODULE__{} = alert) do
now = Timex.now()
five_weeks_ago = DateTime.add(now, -5 * 7, :day)
current_active_period = current_active_period(alert, now)

stale? =
if is_nil(current_active_period) do
false
else
(current_active_period |> elem(0) || now) |> DateTime.before?(five_weeks_ago)
end

%__MODULE__{alert | stale?: stale?}
end

# credo:disable-for-next-line
def current_active_period(%__MODULE__{} = alert, now) do
alert.active_period
|> Enum.find(fn {start, stop} ->
case {start, stop} do
# nil start should never happen, but some tests include it
{nil, stop} -> DateTime.after?(stop, now)
{start, nil} -> DateTime.before?(start, now)
{start, stop} -> DateTime.before?(start, now) and DateTime.after?(stop, now)
end
end)
end

@spec build_struct(Keyword.t()) :: t()
defp build_struct(keywords), do: struct!(__MODULE__, keywords)

Expand Down
9 changes: 8 additions & 1 deletion lib/dotcom/system_status/subway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ defmodule Dotcom.SystemStatus.Subway do
def subway_status(alerts, time) do
@lines
|> Map.new(fn line ->
{line, nested_statuses_for_line(line, alerts, time)}
{line, nested_statuses_for_line(line, alerts |> filter_stale_alerts(), time)}
end)
end

defp filter_stale_alerts(alerts) do
alerts
|> Enum.filter(fn %{stale?: stale?} ->
!stale?
end)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/templates/schedule/_line.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

<div class="page-section m-schedule-line">
{DotcomWeb.AlertView.group(
alerts: @alerts,
alerts:
@alerts
|> Enum.filter(fn %{stale?: stale?} -> !stale? end),
route: @route,
date_time: @date_time,
priority_filter: :high
Expand Down
56 changes: 56 additions & 0 deletions test/alerts/alerts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,62 @@ defmodule AlertsTest do
end
end

describe "check_freshness/1" do
test "Marks alerts with no end older than 5 weeks as stale" do
stale_start_date = Timex.now() |> DateTime.add(Enum.random(-10..-6) * 7, :day)
alert = new(effect: :delay, active_period: [{stale_start_date, nil}])

assert alert.stale?,
"Alert with no end was expected to be stale, but was not"
end

test "Marks alerts with an end older than 5 weeks as stale" do
stale_start_date = Timex.now() |> DateTime.add(Enum.random(-10..-6) * 7, :day)
stale_end_date = Timex.now() |> DateTime.add(Enum.random(1..10), :day)
alert = new(effect: :delay, active_period: [{stale_start_date, stale_end_date}])

assert alert.stale?,
"Alert with an end was expected to be stale, but was not #{stale_start_date}"
end

test "Handles alerts with multiple active periods correctly" do
stale_start_date = Timex.now() |> DateTime.add(Enum.random(-10..-6) * 7, :day)
stale_end_date = Timex.now() |> DateTime.add(Enum.random(1..10), :day)
future_start_date = Timex.now() |> DateTime.add(Enum.random(1..30), :day)
future_end_date = future_start_date |> DateTime.add(Enum.random(1..30), :day)

alert =
new(
effect: :delay,
active_period: [
{stale_start_date, stale_end_date},
{future_start_date, future_end_date}
]
)

assert alert.stale?,
"Alert with an end was expected to be stale, but was not #{stale_start_date}"
end

test "Marks alerts with no end younger than 5 weeks as fresh" do
fresh_start_date = Timex.now() |> DateTime.add(Enum.random(-4..-1) * 7, :day)
alert = new(effect: :delay, active_period: [{fresh_start_date, nil}])

assert !alert.stale?,
"Alert with no end was expected to be fresh, but was not"
end

test "Marks alerts with an end younger than 5 weeks as fresh" do
fresh_start_date = Timex.now() |> DateTime.add(Enum.random(-4..-1) * 7, :day)
fresh_end_date = Timex.now() |> DateTime.add(Enum.random(1..10), :day)

alert = new(effect: :delay, active_period: [{fresh_start_date, fresh_end_date}])

assert !alert.stale?,
"Alert with an end was expected to be fresh, but was not"
end
end

describe "ongoing_effects/0" do
test "returns a list" do
assert is_list(ongoing_effects())
Expand Down
23 changes: 11 additions & 12 deletions test/alerts/match_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ defmodule Alerts.MatchTest do
%InformedEntity{stop: "1"}
],
active_period: [
{nil, ~N[2016-06-01T00:00:00]},
{NaiveDateTime.from_erl!({{2016, 6, 2}, {0, 0, 0}}),
NaiveDateTime.from_erl!({{2016, 6, 2}, {1, 0, 0}})},
{NaiveDateTime.from_erl!({{2016, 6, 3}, {0, 0, 0}}), nil}
{nil, ~U(2016-06-01 00:00:00Z)},
{~U(2016-06-02 00:00:00Z), ~U(2016-06-02 01:00:00Z)},
{~U(2016-06-03 00:00:00Z), nil}
]
)
]
Expand All @@ -79,28 +78,28 @@ defmodule Alerts.MatchTest do

assert Alerts.Match.match(alerts, ie) == alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 1}, {0, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-01 00:00:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 2}, {0, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-02 00:00:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 2}, {0, 30, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-02 00:30:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 3}, {0, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-03 00:00:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 4}, {0, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-04 00:00:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 5, 20}, {0, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-05-20 00:00:00Z)) ==
alerts

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 1}, {12, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-01 12:00:00Z)) ==
[]

assert Alerts.Match.match(alerts, ie, NaiveDateTime.from_erl!({{2016, 6, 2}, {12, 0, 0}})) ==
assert Alerts.Match.match(alerts, ie, ~U(2016-06-02 12:00:00Z)) ==
[]
end

Expand Down
Loading