Skip to content

Commit

Permalink
rework takeover alert state handling (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored May 2, 2024
1 parent 2c4c6ec commit 23a97fd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 84 deletions.
1 change: 0 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ config :screenplay,
sftp_remote_path: System.get_env("SFTP_REMOTE_PATH"),
sftp_local_path: System.get_env("SFTP_LOCAL_PATH"),
alerts_fetch_module: Screenplay.OutfrontTakeoverTool.Alerts.LocalFetch,
local_alerts_path_spec: {:priv, "alerts.json"},
sftp_client_module: Screenplay.Outfront.FakeSFTPClient,
config_fetcher: Screenplay.Config.LocalFetch,
local_config_file_spec: {:priv, "places_and_screens.json"},
Expand Down
3 changes: 1 addition & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ config :screenplay, ScreenplayWeb.Endpoint,

config :screenplay,
redirect_http?: false,
alerts_fetch_module: Screenplay.OutfrontTakeoverTool.Alerts.LocalFetch,
local_alerts_path_spec: {:test, "alerts.json"},
alerts_fetch_module: Screenplay.OutfrontTakeoverTool.Alerts.TestFetch,
config_fetcher: Screenplay.Config.LocalFetch,
api_v3_url: [:no_api_requests_allowed_during_testing],
sftp_client_module: Screenplay.Outfront.FakeSFTPClient
Expand Down
46 changes: 8 additions & 38 deletions lib/screenplay/outfront_takeover_tool/alerts/local_fetch.ex
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
defmodule Screenplay.OutfrontTakeoverTool.Alerts.LocalFetch do
@moduledoc false

alias Screenplay.OutfrontTakeoverTool.Alerts.State

@spec get_state() :: {:ok, State.t()} | :error
def get_state do
with {:ok, file_contents} <- do_get(),
{:ok, json} <- Jason.decode(file_contents) do
{:ok, State.from_json(json)}
else
_ -> :error
end
end

# sobelow_skip ["Traversal.FileModule"]
defp do_get do
path = local_alerts_path()

case File.read(path) do
{:ok, contents} -> {:ok, contents}
_ -> :error
end
end

@spec put_state(State.t()) :: :ok | :error
def put_state(state) do
contents = state |> State.to_json() |> Jason.encode!(pretty: true)
do_put(contents)
@spec get_state!() :: binary()
def get_state! do
file_path() |> File.read!()
end

# sobelow_skip ["Traversal.FileModule"]
defp do_put(contents) do
path = local_alerts_path()

case File.write(path, contents) do
:ok -> :ok
_ -> :error
end
@spec put_state!(binary()) :: :ok
def put_state!(state) do
file_path() |> File.write!(state)
:ok
end

defp local_alerts_path do
case Application.get_env(:screenplay, :local_alerts_path_spec) do
{:priv, file_name} -> Path.join(:code.priv_dir(:screenplay), file_name)
{:test, file_name} -> Path.join(~w[#{File.cwd!()} test fixtures #{file_name}])
end
end
defp file_path, do: :code.priv_dir(:screenplay) |> Path.join("alerts.json")
end
51 changes: 10 additions & 41 deletions lib/screenplay/outfront_takeover_tool/alerts/s3_fetch.ex
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
defmodule Screenplay.OutfrontTakeoverTool.Alerts.S3Fetch do
@moduledoc false

require Logger

alias Screenplay.OutfrontTakeoverTool.Alerts.State

@spec get_state() :: {:ok, State.t()} | :error
def get_state do
with {:ok, body} <- do_get(),
{:ok, json} <- Jason.decode(body) do
{:ok, State.from_json(json)}
else
_ -> :error
end
end

defp do_get do
bucket = Application.get_env(:screenplay, :alerts_s3_bucket)
path = Application.get_env(:screenplay, :alerts_s3_path)
get_operation = ExAws.S3.get_object(bucket, path)

case ExAws.request(get_operation) do
{:ok, %{body: body, status_code: 200}} ->
{:ok, body}

e ->
Logger.error(e)
:error
end
@spec get_state!() :: binary()
def get_state! do
%{body: body, status_code: 200} = ExAws.S3.get_object(bucket(), path()) |> ExAws.request!()
body
end

@spec put_state(State.t()) :: :ok | :error
def put_state(state) do
contents = state |> State.to_json() |> Jason.encode!(pretty: true)
do_put(contents)
@spec put_state!(binary()) :: :ok
def put_state!(state) do
%{status_code: 200} = ExAws.S3.put_object(bucket(), path(), state) |> ExAws.request!()
:ok
end

defp do_put(contents) do
bucket = Application.get_env(:screenplay, :alerts_s3_bucket)
path = Application.get_env(:screenplay, :alerts_s3_path)
put_operation = ExAws.S3.put_object(bucket, path, contents)

case ExAws.request(put_operation) do
{:ok, %{status_code: 200}} -> :ok
_ -> :error
end
end
defp bucket, do: Application.get_env(:screenplay, :alerts_s3_bucket)
defp path, do: Application.get_env(:screenplay, :alerts_s3_path)
end
4 changes: 2 additions & 2 deletions lib/screenplay/outfront_takeover_tool/alerts/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule Screenplay.OutfrontTakeoverTool.Alerts.State do
case opts do
:ok ->
fetch_module = Application.get_env(:screenplay, :alerts_fetch_module)
{:ok, init_state} = fetch_module.get_state()
init_state = fetch_module.get_state!() |> Jason.decode!() |> from_json()

{:ok, init_state}

Expand Down Expand Up @@ -250,7 +250,7 @@ defmodule Screenplay.OutfrontTakeoverTool.Alerts.State do

defp save_state(new_state) do
fetch_module = Application.get_env(:screenplay, :alerts_fetch_module)
fetch_module.put_state(new_state)
new_state |> to_json() |> Jason.encode!(pretty: true) |> fetch_module.put_state!()
end

defp get_overlapping_stations(existing_alerts, new_id, new_stations) do
Expand Down
13 changes: 13 additions & 0 deletions lib/screenplay/outfront_takeover_tool/alerts/test_fetch.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Screenplay.OutfrontTakeoverTool.Alerts.TestFetch do
@moduledoc false

@spec get_state!() :: binary()
def get_state! do
File.read!("test/fixtures/alerts.json")
end

@spec put_state!(binary()) :: :ok
def put_state!(_state) do
:ok
end
end

0 comments on commit 23a97fd

Please sign in to comment.