diff --git a/apps/alerts/lib/alert.ex b/apps/alerts/lib/alert.ex index 46c598fabb..086de35dfb 100644 --- a/apps/alerts/lib/alert.ex +++ b/apps/alerts/lib/alert.ex @@ -10,6 +10,7 @@ defmodule Alerts.Alert do effect: :unknown, severity: 5, lifecycle: :unknown, + cause: "", updated_at: Timex.now(), description: "", priority: :low, @@ -57,6 +58,7 @@ defmodule Alerts.Alert do header: String.t(), informed_entity: IESet.t(), active_period: [period_pair], + cause: String.t(), effect: effect, severity: severity, lifecycle: lifecycle, diff --git a/apps/alerts/lib/parser.ex b/apps/alerts/lib/parser.ex index 1e24977f32..40d43327e0 100644 --- a/apps/alerts/lib/parser.ex +++ b/apps/alerts/lib/parser.ex @@ -14,6 +14,7 @@ defmodule Alerts.Parser do informed_entity: parse_informed_entity(attributes["informed_entity"]), active_period: Enum.map(attributes["active_period"], &active_period/1), effect: effect(attributes), + cause: cause(attributes["cause"]), severity: severity(attributes["severity"]), lifecycle: lifecycle(attributes["lifecycle"]), updated_at: parse_time(attributes["updated_at"]), @@ -134,6 +135,34 @@ defmodule Alerts.Parser do defp do_effect("SUMMARY"), do: :summary defp do_effect(_), do: :unknown + @spec cause(String.t() | nil) :: Alerts.Alert.cause() + def cause(nil), do: :unknown_cause + + def cause(cause) when is_binary(cause) do + cause + |> String.replace(" ", "_") + |> String.upcase() + |> do_cause() + end + + @spec do_cause(String.t()) :: Alerts.Alert.cause() + defp do_cause("TRAFFIC"), do: :traffic + defp do_cause("TRACK_WORK"), do: :track_work + defp do_cause("OTHER_CAUSE"), do: :other_cause + defp do_cause("TECHNICAL_PROBLEM"), do: :technical_problem + defp do_cause("STRIKE"), do: :strike + defp do_cause("DEMONSTRATION"), do: :demonstration + defp do_cause("ACCIDENT"), do: :accident + defp do_cause("HOLIDAY"), do: :holiday + defp do_cause("WEATHER"), do: :weather + defp do_cause("MAINTENANCE"), do: :maintenance + defp do_cause("CONSTRUCTION"), do: :construction + defp do_cause("POLICE_ACTIVITY"), do: :police_activity + defp do_cause("MEDICAL_EMERGENCY"), do: :medical_emergency + defp do_cause("UNKNOWN_CAUSE"), do: :unknown_cause + defp do_cause(_), do: :unknown_cause + + @spec severity(String.t() | integer) :: Alerts.Alert.severity() def severity(binary) when is_binary(binary) do case String.upcase(binary) do diff --git a/apps/alerts/lib/priority.ex b/apps/alerts/lib/priority.ex index 47d59c54fb..e4c41ae68e 100644 --- a/apps/alerts/lib/priority.ex +++ b/apps/alerts/lib/priority.ex @@ -17,12 +17,13 @@ defmodule Alerts.Priority do end def priority( - %{effect: :delay, severity: severity, informed_entity: informed_entities}, + %{effect: :delay, severity: severity, informed_entity: informed_entities, cause: cause}, _time ) - when severity < 6 do - # delay alerts for bus routes under severity 6 are low priority - case length(Enum.filter(informed_entities, &(&1.route_type == 3))) > 0 do + when severity < 6 and cause === :traffic do + # delay alerts for bus routes with a cause of traffic under severity 6 are low priority + is_bus_alert = Enum.any?(informed_entities, & &1.route_type == 3) + case is_bus_alert do true -> :low false -> :high end diff --git a/apps/alerts/test/parser_test.exs b/apps/alerts/test/parser_test.exs index fd729aab4f..e4ece7120c 100644 --- a/apps/alerts/test/parser_test.exs +++ b/apps/alerts/test/parser_test.exs @@ -32,6 +32,7 @@ defmodule Alerts.ParserTest do "severity" => "Minor", "lifecycle" => "Ongoing", "effect_name" => "Delay", + "cause" => "TRAFFIC", "updated_at" => "2016-06-20T16:09:29-04:00", "description" => "Affected routes: 18", "banner" => "Test banner copy", @@ -64,6 +65,7 @@ defmodule Alerts.ParserTest do severity: 3, lifecycle: :ongoing, effect: :delay, + cause: :traffic, updated_at: ~N[2016-06-20T16:09:29] |> Timex.to_datetime("Etc/GMT+4"), description: "Affected routes: 18", priority: :low, @@ -239,7 +241,8 @@ defmodule Alerts.ParserTest do assert %Alerts.Alert{ lifecycle: :ongoing, severity: 3, - effect: :delay + effect: :delay, + cause: :unknown_cause } = alert end @@ -263,7 +266,8 @@ defmodule Alerts.ParserTest do assert %Alerts.Alert{ lifecycle: :ongoing, severity: 3, - effect: :access_issue + effect: :access_issue, + cause: :unknown_cause } = alert end end diff --git a/apps/alerts/test/priority_test.exs b/apps/alerts/test/priority_test.exs index 86977bdacf..9fbe6a64f1 100644 --- a/apps/alerts/test/priority_test.exs +++ b/apps/alerts/test/priority_test.exs @@ -9,9 +9,10 @@ defmodule Alerts.PriorityTest do @now Util.to_local_time(~N[2018-01-15T12:00:00]) describe "priority/2" do - test "Delay alerts are low if severity is under 5 and the route type is bus" do + test "Delay alerts are low if severity is under 5 and the route type is bus and the cause is traffic" do alert = %Alert{ effect: :delay, + cause: :traffic, severity: 4, informed_entity: [ %InformedEntity{ @@ -36,9 +37,38 @@ defmodule Alerts.PriorityTest do assert priority(alert, @now) == :low end - test "Delay alerts are high if type is bus but severity is 6 or over" do + test "Delay alerts are high if severity is under 5 and the route type is bus and the cause is NOT traffic" do alert = %Alert{ effect: :delay, + cause: :unknown_cause, + severity: 4, + informed_entity: [ + %InformedEntity{ + direction_id: 1, + facility: nil, + route: "CR-Newburyport", + route_type: 2, + stop: nil, + trip: "CR-597929-148" + }, + %InformedEntity{ + direction_id: 1, + facility: nil, + route: "CR-Newburyport", + route_type: 3, + stop: nil, + trip: "CR-597929-148" + } + ] + } + + assert priority(alert, @now) == :high + end + + test "Delay alerts are high if type is bus but severity is 6 or over, regardless of cause" do + alert = %Alert{ + effect: :delay, + cause: :traffic, severity: 6, informed_entity: [ %InformedEntity{ @@ -58,6 +88,7 @@ defmodule Alerts.PriorityTest do test "Delay alerts are high for any severity if route type is not bus" do alert = %Alert{ effect: :delay, + cause: :unknown_cause, severity: 4, informed_entity: [ %InformedEntity{