-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PA message table migration and postgres configuration (#321)
* Added modules needed to keep live alerts in memory. * Added new processes to supervision tree. * Added store test module. * Made fetcher more testable. * Added Fetcher test module. * Added moduledocs. * Credo. * Consolidated logic so fetching and storing happens in the same module. * Removed unnecessary pattern match. * Changed reduce to map. * Fixed AuthManager so users can have more than one role. * Changed attribute name to be more specific. * Added new role for PA Message workflow. * Renamed a role so it better represents how it's used. * Added a plug for verifying permission. * Added scope with an auth check for PA message creation. * Added role to fake Keycloak config. * Added new role to metadata. * Renamed modules and files to better describe functionality. * Changed references of admin to emergency admin. * Simplified how roles are mapped to access levels. * Removed explicit readonly permission. Can be implied. * Added roles list to metadata to cut down on function calls. * Fixed tests now that roles are in assigns. * Added test for new plug. * Use Plug instead of manual adding assigns. * Added a doc for the endpoint RTS will use to retrieve active messages. * add migration and dependencies * Add migration and database configuration * Add documentation for postgres * Add migrations process * Update config/test.exs Co-authored-by: Christian Maddox <[email protected]> * remove async migrations method * Add postgres service to CI * formatting * fetch cert * move cert file to priv * fix typo --------- Co-authored-by: cmaddox5 <[email protected]>
- Loading branch information
Showing
15 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# List Active Messages | ||
|
||
Lists all PA messages that are currently eligible to play. | ||
|
||
**URL** : `/api/active_pa_messages` | ||
|
||
**Method** : `GET` | ||
|
||
**Parameters**: None | ||
|
||
**API key required** : YES | ||
|
||
## Success Responses | ||
|
||
**Code** : `200 OK` | ||
|
||
**Response**: | ||
|
||
```json | ||
[ | ||
{ | ||
"id": 1, | ||
"sign_ids": ["sign_1", "sign2"], | ||
"priority": 0, | ||
"interval_in_minutes": 4, | ||
"visual_text": "This message will be played.", | ||
"audio_text": "This message will be played." | ||
}, | ||
{ | ||
"id": 2, | ||
"sign_ids": ["sign_3", "sign4"], | ||
"priority": 0, | ||
"interval_in_minutes": 3, | ||
"visual_text": "This message will be played.", | ||
"audio_text": "This message will be played." | ||
}, | ||
{ | ||
"id": 3, | ||
"sign_ids": ["sign_1"], | ||
"priority": 0, | ||
"interval_in_minutes": 2, | ||
"visual_text": "This message will be played.", | ||
"audio_text": "This message will be played." | ||
} | ||
] | ||
``` | ||
|
||
## Failure Responses | ||
|
||
**Code** : `403 Forbidden` | ||
|
||
**Response**: | ||
|
||
`Invalid API key` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Screenplay.Migrate do | ||
@moduledoc """ | ||
GenServer which runs on startup to run Ecto migrations. All migrations | ||
stored in the "migrations" directory are run during init. Migrations stored | ||
in the "async_migrations" directory will be run after the regular migrations | ||
complete and will only log a warning on failure. | ||
""" | ||
use GenServer, restart: :transient | ||
require Logger | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts) | ||
end | ||
|
||
@impl GenServer | ||
def init(opts) do | ||
Logger.info("#{__MODULE__} synchronous migrations starting") | ||
Keyword.get(opts, :sync_migrate_fn, &default_migrate_fn/1).("migrations") | ||
|
||
Logger.info("#{__MODULE__} synchronous migrations finished") | ||
{:ok, opts} | ||
end | ||
|
||
defp default_migrate_fn(migration_directory) do | ||
Ecto.Migrator.run( | ||
Screenplay.Repo, | ||
Ecto.Migrator.migrations_path(Screenplay.Repo, migration_directory), | ||
:up, | ||
all: true | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Screenplay.Repo do | ||
require Logger | ||
|
||
use Ecto.Repo, | ||
otp_app: :screenplay, | ||
adapter: Ecto.Adapters.Postgres | ||
|
||
def add_prod_credentials(config, auth_token_fn \\ &ExAws.RDS.generate_db_auth_token/4) do | ||
host = System.get_env("DATABASE_HOST") | ||
port = String.to_integer(System.get_env("DATABASE_PORT", "5432")) | ||
user = System.get_env("DATABASE_USER") | ||
|
||
token = | ||
auth_token_fn.( | ||
host, | ||
user, | ||
port, | ||
%{} | ||
) | ||
|
||
if is_nil(token) do | ||
Logger.info("#{__MODULE__} add_prod_credentials token_is_nil") | ||
else | ||
hash_string = Base.encode16(:crypto.hash(:sha3_256, token)) | ||
|
||
Logger.info("#{__MODULE__} add_prod_credentials token_hash=#{hash_string}") | ||
end | ||
|
||
Keyword.merge(config, | ||
hostname: host, | ||
username: user, | ||
port: port, | ||
password: token, | ||
ssl_opts: [ | ||
cacertfile: "priv/aws-cert-bundle.pem", | ||
verify: :verify_peer, | ||
server_name_indication: String.to_charlist(host), | ||
verify_fun: | ||
{&:ssl_verify_hostname.verify_fun/3, [check_hostname: String.to_charlist(host)]} | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
priv/repo/migrations/20240422170503_create_pa_message_table.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Screenplay.Repo.Migrations.CreatePaMessageTable do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table("pa_message") do | ||
add :alert_id, :string | ||
add :start_time, :utc_datetime | ||
add :end_time, :utc_datetime | ||
add :days_of_week, {:array, :string} | ||
add :sign_ids, {:array, :string}, null: false | ||
add :priority, :integer, null: false | ||
add :interval_in_minutes, :integer, null: false | ||
add :visual_text, :text, null: false | ||
add :audio_text, :text, null: false | ||
add :paused, :boolean | ||
add :saved, :boolean | ||
add :message_type, :string | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
create index("pa_message", [:start_time, :end_time]) | ||
create index("pa_message", ["(to_tsvector('english', visual_text))"], using: "GIN") | ||
end | ||
end |