Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/hex/server/ash_postgres-1.5.23
Browse files Browse the repository at this point in the history
  • Loading branch information
skanderm committed Apr 2, 2024
2 parents f247cd6 + 4a6b9d2 commit 06b84a2
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 47 deletions.
56 changes: 42 additions & 14 deletions server/lib/orcasite/notifications/resources/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ defmodule Orcasite.Notifications.Notification do
constraints one_of: Event.types()
end

attribute :target_count, :integer
attribute :notified_count, :integer, default: 0
attribute :notified_count_updated_at, :utc_datetime

create_timestamp :inserted_at, private?: false, writable?: false
update_timestamp :updated_at
end

calculations do
calculate :progress,
:float,
expr(if(target_count == 0, do: 1, else: notified_count / target_count))

calculate :finished, :boolean, expr(notified_count == target_count)
end

relationships do
has_many :notification_instances, NotificationInstance

Expand Down Expand Up @@ -103,6 +115,11 @@ defmodule Orcasite.Notifications.Notification do
end
end

update :increment_notified_count do
change atomic_update(:notified_count, expr(notified_count + 1))
change atomic_update(:notified_count_updated_at, expr(now()))
end

create :notify_confirmed_candidate do
description "Create a notification for confirmed candidate (i.e. detection group)"
accept [:candidate_id, :message]
Expand Down Expand Up @@ -172,6 +189,8 @@ defmodule Orcasite.Notifications.Notification do
define :notify_confirmed_candidate,
action: :notify_confirmed_candidate,
args: [:candidate_id]

define :increment_notified_count
end

resource do
Expand All @@ -197,21 +216,26 @@ defmodule Orcasite.Notifications.Notification do
changeset
|> Ash.Changeset.after_action(fn _, notification ->
Task.Supervisor.async_nolink(Orcasite.TaskSupervisor, fn ->
Orcasite.Notifications.Subscription
|> Ash.Query.for_read(:available_for_notification, %{
notification_id: notification.id,
event_type: notification.event_type
})
|> Orcasite.Notifications.stream!()
|> Stream.map(fn subscription ->
Orcasite.Notifications.NotificationInstance
|> Ash.Changeset.for_create(:create_with_relationships, %{
notification: notification.id,
subscription: subscription.id
target_count =
Orcasite.Notifications.Subscription
|> Ash.Query.for_read(:available_for_notification, %{
notification_id: notification.id,
event_type: notification.event_type
})
|> Orcasite.Notifications.create!()
end)
|> Stream.run()
|> Orcasite.Notifications.stream!()
|> Stream.map(fn subscription ->
Orcasite.Notifications.NotificationInstance
|> Ash.Changeset.for_create(:create_with_relationships, %{
notification: notification.id,
subscription: subscription.id
})
|> Orcasite.Notifications.create!()
end)
|> Enum.reduce(0, fn _, sum -> sum + 1 end)

notification
|> Ash.Changeset.for_update(:update, %{target_count: target_count})
|> Orcasite.Notifications.update()
end)

{:ok, notification}
Expand All @@ -220,6 +244,10 @@ defmodule Orcasite.Notifications.Notification do
on: :create
end

preparations do
prepare build(load: [:progress, :finished])
end

graphql do
type :notification

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do

Task.Supervisor.async_nolink(Orcasite.TaskSupervisor, fn ->
NotificationInstance
|> Notifications.get!(notification_instance_id)
|> Notifications.get(notification_instance_id)
|> case do
nil ->
{:error, _} ->
nil

notif_instance ->
{:ok, notif_instance} ->
notif_instance
|> Ash.Changeset.for_destroy(:destroy)
|> Notifications.destroy!()
end
end)

Orcasite.Notifications.Notification.increment_notified_count(notification)

:ok
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Orcasite.Repo.Migrations.AddTrackingToNotifications do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
alter table(:notifications) do
add :target_count, :bigint
add :notified_count, :bigint, default: 0
add :notified_count_updated_at, :utc_datetime
end
end

def down do
alter table(:notifications) do
remove :notified_count_updated_at
remove :notified_count
remove :target_count
end
end
end
132 changes: 132 additions & 0 deletions server/priv/resource_snapshots/repo/notifications/20240328232225.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"attributes": [
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": true
},
{
"default": "%{}",
"size": null,
"type": "map",
"source": "meta",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "true",
"size": null,
"type": "boolean",
"source": "active",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "event_type",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "target_count",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "0",
"size": null,
"type": "bigint",
"source": "notified_count",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "utc_datetime",
"source": "notified_count_updated_at",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "inserted_at",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "updated_at",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": false
}
],
"table": "notifications",
"hash": "4DDEA23AF74FABB1F511CBC23120CEA61649F82506BBA3C9F2D41CE6DA873C36",
"repo": "Elixir.Orcasite.Repo",
"identities": [],
"schema": null,
"multitenancy": {
"global": null,
"strategy": null,
"attribute": null
},
"custom_indexes": [
{
"message": null,
"name": null,
"table": null,
"include": null,
"prefix": null,
"where": null,
"fields": [
{
"type": "atom",
"value": "meta"
}
],
"unique": false,
"all_tenants?": false,
"concurrently": false,
"error_fields": [
"meta"
],
"nulls_distinct": true,
"using": "gin"
}
],
"base_filter": null,
"check_constraints": [],
"custom_statements": [],
"has_create_action": true
}
22 changes: 8 additions & 14 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"@types/leaflet": "^1.9.8",
"@types/node": "20.11.30",
"@types/phoenix": "^1.6.4",
"@types/react": "18.2.70",
"@types/react-dom": "18.2.22",
"@types/react": "18.2.73",
"@types/react-dom": "18.2.23",
"@types/video.js": "^7.3.57",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"eslint": "8.57.0",
Expand Down
Loading

0 comments on commit 06b84a2

Please sign in to comment.