-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add a page with reindex functionality #101
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
Open
pyphilia
wants to merge
5
commits into
main
Choose a base branch
from
100-reindex
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| defmodule Admin.Publications.SearchIndex do | ||
| @moduledoc """ | ||
| Utilities to trigger a publication reindex on an external indexing endpoint. | ||
|
|
||
| It expects the following application config in `:admin`: | ||
|
|
||
| config :admin, :publications, | ||
| publication_index_url | ||
| publication_index_header_value | ||
|
|
||
| """ | ||
|
|
||
| require Logger | ||
|
|
||
| defp http_client, do: Application.get_env(:admin, :publication_index_http_client, Req) | ||
|
|
||
| @doc """ | ||
| Trigger a reindex by calling the configured endpoint with the configured header. | ||
|
|
||
| Returns `{:ok, %Req.Response{}}` on success or `{:error, error_code}` on failure. | ||
| """ | ||
| def reindex do | ||
| with {:ok, client, url, headers} <- build_reindex_request() do | ||
|
|
||
| req = client.new(method: :get, url: url, headers: headers) | ||
|
|
||
| IO.puts("Reindex request: #{inspect(req)}") | ||
|
|
||
| case client.request(req) do | ||
| %Req.Response{} = resp -> | ||
| if resp.status in 200..299 do | ||
| {:ok, resp} | ||
| else | ||
| Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") | ||
| {:error, resp.status} | ||
| end | ||
|
|
||
| {:ok, %Req.Response{} = resp} -> | ||
| if resp.status in 200..299 do | ||
| {:ok, resp} | ||
| else | ||
| Logger.error("SearchIndex.reindex failed: #{inspect(resp)}") | ||
| {:error, resp.status} | ||
| end | ||
|
|
||
| {:error, reason} -> | ||
| Logger.error("SearchIndex.reindex failed: #{inspect(reason)}") | ||
| {:error, 500} | ||
|
|
||
| other -> | ||
| Logger.error("SearchIndex.reindex unexpected response: #{inspect(other)}") | ||
| {:error, :unexpected_response} | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @doc false | ||
| # Build the HTTP client, url, and headers for the reindex request. | ||
| defp build_reindex_request do | ||
|
|
||
| case Application.get_env(:admin, :backend_host) do | ||
| nil -> | ||
| {:error, :missing_publication_index_url} | ||
|
|
||
| url -> | ||
| case Application.get_env(:admin, :publication_reindex_headers) do | ||
| :error -> | ||
| {:error, :missing_publication_index_header_value} | ||
|
|
||
| headers_value -> | ||
| client = http_client() | ||
| {:ok, client, "http://#{url}/items/collections/search/rebuild", headers_value} | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,59 @@ | ||
| defmodule AdminWeb.PublicationSearchIndexLive do | ||
| use AdminWeb, :live_view | ||
|
|
||
| alias Admin.Publications.SearchIndex | ||
|
|
||
| @impl true | ||
| def render(assigns) do | ||
| ~H""" | ||
| <Layouts.admin flash={@flash} current_scope={@current_scope}> | ||
| <div class="p-6"> | ||
| <.header> | ||
| Index Status | ||
| <:subtitle> | ||
| You can start a reindex of the library index. This is useful if your search engine is not up to date. | ||
| </:subtitle> | ||
| </.header> | ||
| <div role="alert" class="alert alert-warning"> | ||
| <.icon name="hero-exclamation-triangle" /> | ||
| <span>This operation is heavy on the database and might take some time to complete.</span> | ||
| </div> | ||
| <p class="my-4">{@status || ""}</p> | ||
| <div> | ||
| <button | ||
| phx-click="reindex" | ||
| class="px-4 py-2 disabled:bg-gray-600 bg-blue-600 text-white rounded" | ||
| disabled={@indexing} | ||
| > | ||
| Start Reindex | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </Layouts.admin> | ||
| """ | ||
| end | ||
|
|
||
| @impl true | ||
| def mount(_params, _session, socket) do | ||
| {:ok, assign(socket, status: nil, indexing: false)} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_event("reindex", _params, socket) do | ||
| case SearchIndex.reindex() do | ||
| {:ok, _resp} -> | ||
| {:noreply, | ||
| assign(socket, | ||
| indexing: true, | ||
| status: "Reindex has started. It might take some time to complete." | ||
| )} | ||
|
|
||
| {:error, reason} -> | ||
| {:noreply, | ||
| assign(socket, | ||
| indexing: false, | ||
| status: "Reindex failed with error #{inspect(reason)}" | ||
| )} | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 Admin.Publications.PublicationSearchIndexTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias Admin.Publications.SearchIndex | ||
|
|
||
| setup do | ||
| # ensure we start with a clean client config | ||
| Application.delete_env(:admin, :publication_index_http_client) | ||
| :ok | ||
| end | ||
|
|
||
| test "returns error when url is missing" do | ||
| Application.delete_env(:admin, :publication_index_url) | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| assert {:error, :missing_publication_index_url} = SearchIndex.reindex() | ||
| end | ||
|
|
||
| test "returns error when header value is missing" do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.delete_env(:admin, :publication_index_header_value) | ||
|
|
||
| assert {:error, :missing_publication_index_header_value} = SearchIndex.reindex() | ||
| end | ||
|
|
||
| test "returns ok on 2xx response" do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| client = | ||
| Module.concat([__MODULE__, :SuccessClient]) | ||
|
|
||
| defmodule client do | ||
| def new(_opts), do: :req_request | ||
|
|
||
| def request(_req) do | ||
| resp = %Req.Response{status: 200, body: "ok"} | ||
| {:ok, resp} | ||
| end | ||
| end | ||
|
|
||
| Application.put_env(:admin, :publication_index_http_client, client) | ||
|
|
||
| assert {:ok, resp} = SearchIndex.reindex() | ||
| assert resp.status == 200 | ||
| end | ||
|
|
||
| test "returns error on non-2xx response" do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| client = Module.concat([__MODULE__, :BadResponseClient]) | ||
|
|
||
| defmodule client do | ||
| def new(_opts), do: :req_request | ||
|
|
||
| def request(_req) do | ||
| resp = %Req.Response{status: 500, body: "nope"} | ||
| {:ok, resp} | ||
| end | ||
| end | ||
|
|
||
| Application.put_env(:admin, :publication_index_http_client, client) | ||
|
|
||
| assert {:error, 500} = SearchIndex.reindex() | ||
| end | ||
|
|
||
| test "returns error tuple when client errors" do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| client = Module.concat([__MODULE__, :ErrClient]) | ||
|
|
||
| defmodule client do | ||
| def new(_opts), do: :req_request | ||
|
|
||
| def request(_req), do: {:error, :econnrefused} | ||
| end | ||
|
|
||
| Application.put_env(:admin, :publication_index_http_client, client) | ||
|
|
||
| assert {:error, :econnrefused} = SearchIndex.reindex() | ||
| end | ||
| end |
This file contains hidden or 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 AdminWeb.PublicationIndexLiveTest do | ||
| use AdminWeb.ConnCase, async: true | ||
|
|
||
| import Phoenix.LiveViewTest | ||
|
|
||
| setup %{conn: conn} do | ||
| %{conn: conn} = register_and_log_in_user(%{conn: conn}) | ||
| :ok | ||
| {:ok, conn: conn} | ||
| end | ||
|
|
||
| test "shows error when reindex fails", %{conn: conn} do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| client = Module.concat([__MODULE__, :FailClient]) | ||
|
|
||
| defmodule client do | ||
| def new(_opts), do: :req_request | ||
| def request(_req), do: {:error, :econnrefused} | ||
| end | ||
|
|
||
| Application.put_env(:admin, :publication_index_http_client, client) | ||
|
|
||
| {:ok, view, _html} = live(conn, "/publications/reindex") | ||
|
|
||
| view |> element("button", "Start Reindex") |> render_click() | ||
|
|
||
| assert render(view) =~ "Reindex failed" | ||
| end | ||
|
|
||
| test "show waitingstatus on success", %{conn: conn} do | ||
| Application.put_env(:admin, :publication_index_url, "http://example") | ||
| Application.put_env(:admin, :publication_index_header_value, "token") | ||
|
|
||
| client = Module.concat([__MODULE__, :OkClient]) | ||
|
|
||
| defmodule client do | ||
| def new(_opts), do: :req_request | ||
| def request(_req) do | ||
| resp = %Req.Response{status: 200, body: "ok"} | ||
| {:ok, resp} | ||
| end | ||
| end | ||
|
|
||
| Application.put_env(:admin, :publication_index_http_client, client) | ||
|
|
||
| {:ok, view, _html} = live(conn, "/publications/reindex") | ||
|
|
||
| view |> element("button", "Start Reindex") |> render_click() | ||
|
|
||
| refute render(view) =~ "Reindex failed" | ||
|
|
||
| assert view |> element("button[disabled]", "Start Reindex") |> has_element?() | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure here.
In local I used
localhost:3000that automatically redirects to/api.Using
api.graasp.orgin prod would work as it redirects to/apitoo, but usinggraasp.orgwould require/api(so it's not a host anymore).