Skip to content

Commit

Permalink
parse alert cause, use in priority
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinLMoore committed Aug 21, 2023
1 parent 1acdbda commit 8c9cea4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
2 changes: 2 additions & 0 deletions apps/alerts/lib/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Alerts.Alert do
effect: :unknown,
severity: 5,
lifecycle: :unknown,
cause: "",
updated_at: Timex.now(),
description: "",
priority: :low,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions apps/alerts/lib/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions apps/alerts/lib/priority.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions apps/alerts/test/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -239,7 +241,8 @@ defmodule Alerts.ParserTest do
assert %Alerts.Alert{
lifecycle: :ongoing,
severity: 3,
effect: :delay
effect: :delay,
cause: :unknown_cause
} = alert
end

Expand All @@ -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
Expand Down
35 changes: 33 additions & 2 deletions apps/alerts/test/priority_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 8c9cea4

Please sign in to comment.