Skip to content
Open
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
44 changes: 44 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,47 @@ 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) |> 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} ->
cond do
is_nil(start) and DateTime.after?(stop, now) ->
true

is_nil(start) ->
false

is_nil(stop) and DateTime.before?(start, now) ->
true

is_nil(stop) ->
false

DateTime.before?(start, now) and DateTime.after?(stop, now) ->
true

true ->
false
end
end)
end

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

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
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