Skip to content

Commit

Permalink
write per-stop prediction flagging data (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Feb 22, 2024
1 parent 78573fd commit e4f3928
Show file tree
Hide file tree
Showing 7 changed files with 9,838 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ yarn-error.log
/config/*.secret.exs
test/mock_write.json
/priv/config.json
/priv/stops-config.json

# local direnv configuration
/.envrc
1 change: 1 addition & 0 deletions lib/signs_ui/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule SignsUi.Application do
defp set_runtime_config do
Config.update_env(:aws_signs_bucket, System.get_env("AWS_SIGNS_BUCKET"))
Config.update_env(:aws_signs_path, System.get_env("AWS_SIGNS_PATH"))
Config.update_env(:aws_signs_stops_path, System.get_env("AWS_SIGNS_STOPS_PATH"))
Config.update_env(:messages_api_keys, System.get_env("MESSAGES_API_KEYS"))
Config.update_env(:sentry, :dsn, System.get_env("SENTRY_DSN"))
Config.update_env(:sentry, :environment_name, System.get_env("SENTRY_ENV"))
Expand Down
5 changes: 5 additions & 0 deletions lib/signs_ui/config/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ defmodule SignsUi.Config.Local do
File.write!(path(), content)
end

# sobelow_skip ["Traversal"]
def write_stops(content) do
:code.priv_dir(:signs_ui) |> Path.join("stops-config.json") |> File.write!(content)
end

# sobelow_skip ["Traversal"]
def read do
case File.read(path()) do
Expand Down
5 changes: 5 additions & 0 deletions lib/signs_ui/config/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ defmodule SignsUi.Config.S3 do
{:ok, _} = ExAws.S3.put_object(bucket_name(), path_name(), content) |> ExAws.request()
end

def write_stops(content) do
path = Application.get_env(:signs_ui, :aws_signs_stops_path)
{:ok, _} = ExAws.S3.put_object(bucket_name(), path, content) |> ExAws.request()
end

def read do
{:ok, %{body: content}} = ExAws.S3.get_object(bucket_name(), path_name()) |> ExAws.request()
content
Expand Down
53 changes: 50 additions & 3 deletions lib/signs_ui/config/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule SignsUi.Config.State do
signs: %{Config.Sign.id() => Config.Sign.t()},
configured_headways: ConfiguredHeadways.t(),
chelsea_bridge_announcements: String.t(),
sign_groups: SignGroups.t()
sign_groups: SignGroups.t(),
sign_stops: map()
}

def start_link(opts \\ []) do
Expand Down Expand Up @@ -86,7 +87,8 @@ defmodule SignsUi.Config.State do
|> Map.get("configured_headways", %{})
|> ConfiguredHeadways.parse_configured_headways_json(),
chelsea_bridge_announcements: Map.get(response, "chelsea_bridge_announcements", "off"),
sign_groups: response |> Map.get("sign_groups", %{}) |> SignGroups.from_json()
sign_groups: response |> Map.get("sign_groups", %{}) |> SignGroups.from_json(),
sign_stops: parse_signs_json()
}

{:ok, state}
Expand Down Expand Up @@ -194,7 +196,8 @@ defmodule SignsUi.Config.State do
signs: signs,
configured_headways: configured_headways,
chelsea_bridge_announcements: chelsea_bridge_announcements,
sign_groups: sign_groups
sign_groups: sign_groups,
sign_stops: sign_stops
}) do
config_store = Application.get_env(:signs_ui, :config_store)

Expand All @@ -209,6 +212,50 @@ defmodule SignsUi.Config.State do
pretty: true
)
|> config_store.write()

for {%{stop_id: stop_id, route_id: route_id, direction_id: direction_id}, ids} <- sign_stops do
%{
stop_id: stop_id,
route_id: route_id,
direction_id: direction_id,
predictions:
if Enum.any?(ids, fn id ->
case signs[id] do
nil -> false
sign -> sign.config.mode == :headway
end
end) do
"flagged"
else
"normal"
end
}
end
|> then(&%{"stops" => &1})
|> Jason.encode!(pretty: true)
|> config_store.write_stops()
end

# sobelow_skip ["Traversal"]
defp parse_signs_json do
signs_json =
:code.priv_dir(:signs_ui)
|> Path.join("signs.json")
|> File.read!()
|> Jason.decode!(keys: :atoms)

for %{id: id, source_config: %{sources: sources}} <- signs_json,
%{stop_id: stop_id, routes: routes, direction_id: direction_id} <- sources,
route_id <- routes,
reduce: %{} do
acc ->
Map.update(
acc,
%{stop_id: stop_id, route_id: route_id, direction_id: direction_id},
[id],
&[id | &1]
)
end
end

defp schedule_clean(pid, ms) do
Expand Down
2 changes: 2 additions & 0 deletions lib/signs_ui/mock/config_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule SignsUi.Mock.ConfigStore do
def write(_) do
end

def write_stops(_), do: nil

def read do
%{
"signs" => %{
Expand Down
Loading

0 comments on commit e4f3928

Please sign in to comment.