Skip to content

Commit

Permalink
Add detections json api endpoint (#501)
Browse files Browse the repository at this point in the history
* Add detection json api, update some deps

* Allow relationship includes for detections' feed, candidate relationships. Add feed_streams to includes for feed
  • Loading branch information
skanderm committed Jun 13, 2024
1 parent 0185eaf commit e4babda
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 26 deletions.
5 changes: 5 additions & 0 deletions server/lib/orcasite/extensions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO: Try removing this once upgrading ash > 3.0 and ash_json_api > 1
defimpl Jason.Encoder, for: Ash.ForbiddenField do
def encode(_, _), do: "null"
def encode!(_, _), do: "null"
end
33 changes: 28 additions & 5 deletions server/lib/orcasite/radio/detection.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Orcasite.Radio.Detection do
use Ash.Resource,
extensions: [AshAdmin.Resource, AshUUID, AshGraphql.Resource],
extensions: [AshAdmin.Resource, AshUUID, AshGraphql.Resource, AshJsonApi.Resource],
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer]

Expand Down Expand Up @@ -283,23 +283,46 @@ defmodule Orcasite.Radio.Detection do
end
end

admin do
table_columns [
:id,
:feed_id,
:playlist_timestamp,
:player_offset,
:listener_count,
:description,
:candidate_id,
:inserted_at
]
end

code_interface do
define_for Orcasite.Radio

define :submit_detection
end

admin do
table_columns [
json_api do
type "detection"

default_fields [
:id,
:feed_id,
:playlist_timestamp,
:player_offset,
:listener_count,
:timestamp,
:description,
:candidate_id,
:category,
:inserted_at
]

includes [:feed, :candidate]

routes do
base "/detections"

index :index
end
end

graphql do
Expand Down
2 changes: 2 additions & 0 deletions server/lib/orcasite/radio/feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ defmodule Orcasite.Radio.Feed do
json_api do
type "feed"

includes [:feed_streams]

routes do
base "/feeds"

Expand Down
3 changes: 2 additions & 1 deletion server/lib/orcasite/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ defmodule Orcasite.Repo do
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
end

def installed_extensions, do: ["citext", "uuid-ossp", "postgis", AshUUID.PostgresExtension]
def installed_extensions,
do: ["citext", "uuid-ossp", "postgis", AshUUID.PostgresExtension, "pg_stat_statements"]
end
6 changes: 5 additions & 1 deletion server/lib/orcasite_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ defmodule OrcasiteWeb.Router do

scope "/" do
pipe_through [:browser, :require_admin]
live_dashboard "/admin/dashboard", metrics: OrcasiteWeb.Telemetry

live_dashboard "/admin/dashboard",
metrics: OrcasiteWeb.Telemetry,
ecto_repos: [Orcasite.Repo],
ecto_psql_extras_options: [long_running_queries: [threshold: "200 milliseconds"]]

sign_in_route(
path: "/admin/sign-in",
Expand Down
2 changes: 2 additions & 0 deletions server/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ defmodule Orcasite.Mixfile do
{:sweet_xml, "~> 0.6"},
{:configparser_ex, "~> 4.0", only: :dev},
{:broadway_sqs, "~> 0.7"},
{:recon, "~> 2.5"},
{:ecto_psql_extras, "~> 0.6"}
]
end

Expand Down
39 changes: 21 additions & 18 deletions server/mix.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Orcasite.Repo.Migrations.Install1Extensions do
@moduledoc """
Installs any extensions that are mentioned in the repo's `installed_extensions/0` callback
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
execute("CREATE EXTENSION IF NOT EXISTS \"pg_stat_statements\"")
end

def down do
# Uncomment this if you actually want to uninstall the extensions
# when this migration is rolled back:
# execute("DROP EXTENSION IF EXISTS \"pg_stat_statements\"")
end
end
3 changes: 2 additions & 1 deletion server/priv/resource_snapshots/repo/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"citext",
"uuid-ossp",
"postgis",
"ash-uuid_v1"
"ash-uuid_v1",
"pg_stat_statements"
]
}

0 comments on commit e4babda

Please sign in to comment.