Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read-only jobs view with optional filters #359

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Orcasite.Notifications.Changes.ExtractNotificationInstanceMeta do
{:get_notif, Notifications.get(Notification, changeset.arguments.notification)} do
changeset
|> Ash.Changeset.change_attribute(:meta, %{
email: Map.get(subscription.meta, "email"),
subscriber_name: Map.get(subscription.meta, "name"),
channel: Map.get(subscription.meta, "channel"),
event_type: notification.event_type,
Expand Down
1 change: 1 addition & 0 deletions server/lib/orcasite/notifications/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ defmodule Orcasite.Notifications.Registry do
entry Orcasite.Notifications.Subscription
entry Orcasite.Notifications.NotificationInstance
entry Orcasite.Notifications.Token
entry Orcasite.Notifications.Job
end
end
124 changes: 124 additions & 0 deletions server/lib/orcasite/notifications/resources/job.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
defmodule Orcasite.Notifications.Job do
use Ash.Resource,
extensions: [AshAdmin.Resource, AshGraphql.Resource],
data_layer: AshPostgres.DataLayer

@states [:available, :scheduled, :executing, :retryable, :completed, :discarded, :cancelled]

postgres do
table "oban_jobs"
repo Orcasite.Repo
migrate? false
end

attributes do
integer_primary_key :id

attribute :state, :atom do
constraints one_of: @states

writable? false
end

attribute :queue, :string, writable?: false
attribute :worker, :string, writable?: false
attribute :args, :map, writable?: false
attribute :errors, {:array, :map}, writable?: false
attribute :attempt, :integer, writable?: false
attribute :max_attempts, :integer, writable?: false

attribute :inserted_at, :utc_datetime, writable?: false
attribute :scheduled_at, :utc_datetime, writable?: false
attribute :attempted_at, :utc_datetime, writable?: false
attribute :completed_at, :utc_datetime, writable?: false
attribute :cancelled_at, :utc_datetime, writable?: false
attribute :discarded_at, :utc_datetime, writable?: false

attribute :attempted_by, {:array, :string}, writable?: false
attribute :priority, :integer, writable?: false
attribute :tags, {:array, :string}, writable?: false
attribute :meta, :map, writable?: false
end

actions do
defaults [:read]

read :index do
pagination do
offset? true
countable true
default_limit 100
end

argument :state, :atom do
constraints one_of: @states
end

argument :notification_id, :string
argument :subscription_id, :string
argument :email, :string
argument :node, :string

argument :event_type, :atom do
constraints one_of: Orcasite.Notifications.Event.types()
end

filter expr(
if(is_nil(^arg(:notification_id)),
do: true,
else: args[:notification_id] == ^arg(:notification_id)
) and
if(is_nil(^arg(:subscription_id)),
do: true,
else: args[:subscription_id] == ^arg(:subscription_id)
) and
if(is_nil(^arg(:email)),
do: true,
else: args[:meta][:email] == ^arg(:email)
) and
if(is_nil(^arg(:node)),
do: true,
else: args[:meta][:node] == ^arg(:node)
) and
if(is_nil(^arg(:event_type)),
do: true,
else: args[:meta][:event_type] == ^arg(:event_type)
) and
if(is_nil(^arg(:state)),
do: true,
else: state == ^arg(:state)
)
)
end
end

admin do
table_columns [
:id,
:state,
:args,
:attempt,
:max_attempts,
:inserted_at,
:scheduled_at,
:completed_at,
:tags
]

format_fields args: {__MODULE__, :fetch_meta, []},
meta: {Jason, :encode!, []},
inserted_at: {Calendar, :strftime, ["%m/%d %H:%M:%S %Z"]},
scheduled_at: {Calendar, :strftime, ["%m/%d %H:%M:%S %Z"]},
completed_at: {Calendar, :strftime, ["%m/%d %H:%M:%S %Z"]}
skanderm marked this conversation as resolved.
Show resolved Hide resolved
end

preparations do
prepare build(sort: [inserted_at: :desc])
end

def fetch_meta(args) do
args
|> Map.get("meta")
|> Jason.encode!()
end
end
16 changes: 8 additions & 8 deletions server/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Orcasite.Mixfile do
{:phoenix_ecto, "~> 4.4"},
{:ecto_sql, "~> 3.6"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 3.3"},
{:phoenix_html, "~> 4.0"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.0"},
{:phoenix_live_dashboard, "~> 0.8.0"},
Expand All @@ -57,12 +57,12 @@ defmodule Orcasite.Mixfile do
{:finch, "~> 0.13"},
{:floki, ">= 0.30.0", only: :test},
{:gettext, "~> 0.20"},
{:plug_cowboy, "~> 2.5"},
{:plug_cowboy, "~> 2.7"},
{:plug, "~> 1.7"},
{:absinthe, "~> 1.7"},
{:absinthe_plug, "~> 1.5"},
# Provides helper functions for easy batching of Ecto associations
{:dataloader, "~> 1.0.0"},
{:dataloader, "~> 2.0"},
# Algorithm used by Comeonin to hash password
{:bcrypt_elixir, "~> 3.0"},
# JSON parser, works with Absinthe out of the box
Expand All @@ -71,24 +71,24 @@ defmodule Orcasite.Mixfile do
{:geo_postgis, "~> 3.0"},
{:jason, "~> 1.2"},
# Reverse proxy for proxying to nextjs app
{:reverse_proxy_plug, "~> 2.1"},
{:reverse_proxy_plug, "~> 2.3.0"},
{:httpoison, "~> 2.2"},
{:corsica, "~> 1.0"},
{:corsica, "~> 2.1"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:ash, "~> 2.6", override: true},
{:ash_admin, "~> 0.10.0"},
{:ash_admin, github: "ash-project/ash_admin", branch: "main"},
{:ash_postgres, "~> 1.3"},
{:heroicons, "~> 0.5"},
{:oban, "~> 2.14"},
{:gen_smtp, "~> 1.0"},
{:ash_authentication, "~> 3.12.0"},
{:ash_authentication_phoenix, "~> 1.9.0"},
{:ash_authentication_phoenix, "~> 1.9.3"},
{:syn, "~> 3.3"},
{:mjml, "~> 3.0.1"},
{:zappa, github: "skanderm/zappa", branch: "master"},
{:ash_uuid, "~> 0.4"},
{:ash_graphql, "~> 0.26.6"},
{:ash_graphql, "~> 0.27.0"},
{:ash_json_api, "~> 0.34.0"},
{:open_api_spex, "~> 3.16"},
{:redoc_ui_plug, "~> 0.2.1"},
Expand Down
Loading
Loading