Add the sync for DBIServer.

This commit is contained in:
2025-05-05 21:30:27 +03:00
parent 4291a3090d
commit 981e1f0b92
9 changed files with 254 additions and 2 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ defmodule DecentralisedBookIndex.Metadata do
end
resource DecentralisedBookIndex.Metadata.DBIServer do
define :create_dbi_server, action: :create
define :create_dbi_server, args: [:name, :url, :sync_on?], action: :create
define :list_dbi_server, action: :read
define :get_dbi_server_by_id, args: [:id], action: :by_id
define :search_dbi_server, action: :search, args: [:name]
@@ -29,11 +29,19 @@ defmodule DecentralisedBookIndex.Metadata.DBIServer do
accept [:name, :url, :sync_on?]
end
create :sync_create do
accept [:id, :name, :url, :inserted_at, :updated_at, :dbi_server_id]
end
update :update do
primary? true
accept [:name, :url, :sync_on?]
end
update :sync do
accept [:name, :url, :inserted_at, :updated_at, :dbi_server_id]
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
@@ -63,7 +71,7 @@ defmodule DecentralisedBookIndex.Metadata.DBIServer do
end
attributes do
uuid_primary_key :id
uuid_primary_key :id, writable?: true
attribute :name, :string do
allow_nil? false
@@ -0,0 +1,21 @@
defmodule DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformer do
def from_json(json_body) do
json_body =
if Map.has_key?(json_body, "data") do
json_body["data"]
else
json_body
end
attrs =
%{
id: get_in(json_body, ["id"]),
name: get_in(json_body, ["attributes", "name"]),
url: get_in(json_body, ["attributes", "url"]),
inserted_at: get_in(json_body, ["attributes", "inserted_at"]),
updated_at: get_in(json_body, ["attributes", "updated_at"])
}
{:ok, attrs}
end
end
@@ -0,0 +1,31 @@
defmodule DecentralisedBookIndex.Sync.DBIServerSync do
alias DecentralisedBookIndex.Metadata
alias DecentralisedBookIndex.Metadata.DBIServer
def create_update(attrs, server_id) do
case Metadata.get_dbi_server_by_id(attrs.id) do
{:ok, dbi_server} ->
attrs =
attrs
|> Map.delete(:id)
|> Map.put(:dbi_server_id, server_id)
dbi_server
|> Ash.Changeset.for_update(:sync, attrs)
|> Ash.update!(authorize?: false)
:ok
{:error, %Ash.Error.Query.NotFound{}} ->
attrs =
attrs
|> Map.put(:dbi_server_id, server_id)
DBIServer
|> Ash.Changeset.for_create(:sync_create, attrs)
|> Ash.create!(authorize?: false)
:ok
end
end
end
@@ -0,0 +1,36 @@
defmodule DecentralisedBookIndex.SyncTasks.SyncDBIServerTask do
alias DecentralisedBookIndex.Sync.ApiClients.FetchJsons
alias DecentralisedBookIndex.Sync.DataTransformers.DBIServerTransformer
alias DecentralisedBookIndex.Sync.DBIServerSync
alias DecentralisedBookIndex.Metadata.DBIServer
require Logger
def sync(%DBIServer{} = server, url_params \\ "") do
url = "#{server.url}/api/v1/json/servers#{url_params}"
FetchJsons.get(url, sync_closure(server))
server
end
def sync_chunk(json_chunk, server_id) do
for json <- json_chunk do
with {:ok, attrs} <- DBIServerTransformer.from_json(json),
:ok <- DBIServerSync.create_update(attrs, server_id) do
:ok
else
{:error, reason} ->
Logger.error("Pipeline error: #{inspect(reason)}")
end
end
[]
end
def sync_closure(server) do
fn json_chunk ->
sync_chunk(json_chunk, server.id)
end
end
end
@@ -2,6 +2,7 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncServerTask do
alias DecentralisedBookIndex.Metadata
alias DecentralisedBookIndex.Metadata.DBIServer
alias DecentralisedBookIndex.SyncTasks.SyncDBIServerTask
alias DecentralisedBookIndex.SyncTasks.SyncAuthorsTask
alias DecentralisedBookIndex.SyncTasks.SyncPublishersTask
alias DecentralisedBookIndex.SyncTasks.SyncBooksTask
@@ -20,6 +21,7 @@ defmodule DecentralisedBookIndex.SyncTasks.SyncServerTask do
def sync_one(%DBIServer{} = server, url_params \\ "") do
server
|> SyncDBIServerTask.sync(url_params)
|> SyncAuthorsTask.sync(url_params)
|> SyncPublishersTask.sync(url_params)
|> SyncBooksTask.sync(url_params)