-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
251 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
defmodule Canary.Sources.Document.CreateOpenAPI do | ||
use Ash.Resource.Change | ||
|
||
alias Canary.Sources.Document | ||
alias Canary.Sources.OpenAPI | ||
|
||
@impl true | ||
def init(opts) do | ||
if [ | ||
:source_id_argument, | ||
:fetcher_result_argument, | ||
:chunks_attribute, | ||
:meta_attribute | ||
] | ||
|> Enum.any?(&is_nil(opts[&1])) do | ||
:error | ||
else | ||
{:ok, opts} | ||
end | ||
end | ||
|
||
@impl true | ||
def change(changeset, opts, _context) do | ||
source_id = Ash.Changeset.get_argument(changeset, opts[:source_id_argument]) | ||
|
||
%OpenAPI.FetcherResult{schema: %OpenApiSpex.OpenApi{} = schema} = | ||
Ash.Changeset.get_argument(changeset, opts[:fetcher_result_argument]) | ||
|
||
changeset | ||
|> Ash.Changeset.change_attribute(opts[:meta_attribute], wrap_union(%OpenAPI.DocumentMeta{})) | ||
|> Ash.Changeset.change_attribute(opts[:chunks_attribute], []) | ||
|> Ash.Changeset.after_action(fn _, record -> | ||
hash = | ||
schema | ||
|> Jason.encode!() | ||
|> then(&:crypto.hash(:sha256, &1)) | ||
|> Base.encode16(case: :lower) | ||
|
||
chunks_create_result = | ||
schema.paths | ||
|> Enum.map(fn | ||
{path, %OpenApiSpex.PathItem{get: get, post: post, put: put, delete: delete}} -> | ||
%{ | ||
source_id: source_id, | ||
document_id: record.id, | ||
url: "TODO", | ||
path: path, | ||
get: render_operation(get), | ||
post: render_operation(post), | ||
put: render_operation(put), | ||
delete: render_operation(delete) | ||
} | ||
end) | ||
|> Ash.bulk_create(OpenAPI.Chunk, :create, | ||
return_errors?: true, | ||
return_records?: true | ||
) | ||
|
||
meta = %OpenAPI.DocumentMeta{hash: hash} | ||
|
||
case chunks_create_result do | ||
%Ash.BulkResult{status: :success, records: records} -> | ||
case Document.update(record, wrap_union(meta), Enum.map(records, &wrap_union/1)) do | ||
{:ok, updated_record} -> {:ok, updated_record} | ||
error -> error | ||
end | ||
|
||
%Ash.BulkResult{errors: errors} -> | ||
{:error, errors} | ||
end | ||
end) | ||
end | ||
|
||
defp render_operation(nil), do: nil | ||
|
||
defp render_operation(%OpenApiSpex.Operation{} = op) do | ||
[op.summary, op.description, op.operationId] | ||
|> Enum.reject(&is_nil/1) | ||
|> Enum.join(" / ") | ||
end | ||
|
||
defp wrap_union(%Ash.Union{} = v), do: v | ||
defp wrap_union(v), do: %Ash.Union{type: :openapi, value: v} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
defmodule Canary.Sources.OpenAPI.Chunk do | ||
use Ash.Resource, data_layer: :embedded | ||
|
||
@ops [:get, :post, :put, :delete] | ||
|
||
attributes do | ||
attribute :index_id, :uuid, allow_nil?: false | ||
attribute :source_id, :uuid, allow_nil?: false | ||
attribute :document_id, :string, allow_nil?: false | ||
attribute :is_parent, :boolean, allow_nil?: false | ||
|
||
attribute :tags, {:array, :string}, default: [] | ||
attribute :url, :string, allow_nil?: false | ||
attribute :path, :string, allow_nil?: false | ||
|
||
Enum.each(@ops, fn op -> | ||
attribute op, :string, allow_nil?: true | ||
end) | ||
end | ||
|
||
actions do | ||
defaults [:read, :destroy] | ||
defaults [:read] | ||
|
||
create :create do | ||
primary? true | ||
accept [:index_id, :source_id, :document_id] ++ [:url, :path] ++ @ops | ||
change {Canary.Change.AddToIndex, index_id_attribute: :index_id} | ||
end | ||
|
||
update :update do | ||
destroy :destroy do | ||
primary? true | ||
|
||
change { | ||
Canary.Change.RemoveFromIndex, | ||
source_type: :openapi, index_id_attribute: :index_id | ||
} | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Canary.Sources.OpenAPI.Syncer do | ||
alias Canary.Sources.Document | ||
alias Canary.Sources.OpenAPI | ||
|
||
require Ash.Query | ||
|
||
def run(source_id, %OpenAPI.FetcherResult{} = incomings) do | ||
existing_doc = | ||
Document | ||
|> Ash.Query.filter(source_id == ^source_id) | ||
|> Ash.Query.build(select: [:id, :meta]) | ||
|> Ash.read!() | ||
|> Enum.at(0, nil) | ||
|
||
if hash_eq?(existing_doc, incomings) do | ||
:ok | ||
else | ||
create_changeset = | ||
Ash.Changeset.for_create(Document, :create_openapi, %{ | ||
source_id: source_id, | ||
fetcher_result: incomings | ||
}) | ||
|
||
with {:ok, %{id: id}} <- Ash.create(create_changeset), | ||
:ok <- remove_docs(source_id, exclude_id: id) do | ||
:ok | ||
end | ||
end | ||
end | ||
|
||
defp remove_docs(source_id, opts) do | ||
exclude_id = opts[:exclude_id] || "" | ||
|
||
case Document | ||
|> Ash.Query.filter(source_id == ^source_id and id != ^exclude_id) | ||
|> Ash.bulk_destroy(:destroy, %{}, return_errors?: true) do | ||
%Ash.BulkResult{status: :success} -> :ok | ||
%Ash.BulkResult{errors: errors} -> {:error, errors} | ||
end | ||
end | ||
|
||
defp hash_eq?(nil, _), do: false | ||
|
||
defp hash_eq?( | ||
%Document{meta: %Ash.Union{type: :openapi, value: %OpenAPI.DocumentMeta{hash: hash_a}}}, | ||
%OpenAPI.FetcherResult{schema: %OpenApiSpex.OpenApi{} = schema} | ||
) do | ||
hash_b = | ||
schema | ||
|> Jason.encode!() | ||
|> then(&:crypto.hash(:sha256, &1)) | ||
|> Base.encode16(case: :lower) | ||
|
||
hash_a == hash_b | ||
end | ||
end |
Oops, something went wrong.