Skip to content

Commit

Permalink
Add progress tracking for reindexing as well
Browse files Browse the repository at this point in the history
  • Loading branch information
scottming committed Jul 13, 2024
1 parent dfd7767 commit 2910c54
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ defmodule Lexical.RemoteControl.CodeMod.Rename do
uri_with_expected_operation =
uri_with_expected_operation(client_name, document_changes_list)

{paths_to_delete, paths_to_remind} =
for %Document.Changes{rename_file: rename_file, document: document} <- document_changes_list do
if rename_file do
{rename_file.old_uri, rename_file.new_uri}
else
{nil, document.uri}
end
end
|> Enum.unzip()

paths_to_delete = Enum.reject(paths_to_delete, &is_nil/1)
renaming_operation_count = Enum.count(uri_with_expected_operation)

total_operation_count =
renaming_operation_count + length(paths_to_delete) + length(paths_to_remind)

{report_progress_func, complete_func} =
Progress.begin_percent("Renaming", Enum.count(uri_with_expected_operation))
Progress.begin_percent("Renaming", total_operation_count)

Commands.RenameSupervisor.start_renaming(
uri_with_expected_operation,
paths_to_remind,
paths_to_delete,
report_progress_func,
complete_func
)
Expand Down
73 changes: 60 additions & 13 deletions apps/remote_control/lib/lexical/remote_control/commands/rename.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Lexical.RemoteControl.Commands.Rename do
# Therefore, we need this module to tell us if lexical is currently in the process of renaming.

alias Lexical.RemoteControl.Api.Messages
alias Lexical.RemoteControl.Search.Store
alias Lexical.RemoteControl.Commands.Reindex
require Logger
import Messages
Expand All @@ -16,19 +17,28 @@ defmodule Lexical.RemoteControl.Commands.Rename do

@type t :: %__MODULE__{
uri_to_expected_operation: uri_to_expected_operation(),
raw_uri_to_expected_operation: uri_to_expected_operation(),
paths_to_remind: list(Lexical.path()),
paths_to_delete: list(Lexical.path()),
on_update_progess: fun(),
on_complete: fun()
}
defstruct uri_to_expected_operation: %{},
raw_uri_to_expected_operation: %{},
paths_to_remind: [],
paths_to_delete: [],
on_update_progess: nil,
on_complete: nil

def new(uri_to_expected_operation, on_update_progess, on_complete) do
def new(
uri_to_expected_operation,
paths_to_remind,
paths_to_delete,
on_update_progess,
on_complete
) do
%__MODULE__{
uri_to_expected_operation: uri_to_expected_operation,
raw_uri_to_expected_operation: uri_to_expected_operation,
paths_to_remind: paths_to_remind,
paths_to_delete: paths_to_delete,
on_update_progess: on_update_progess,
on_complete: on_complete
}
Expand All @@ -52,8 +62,8 @@ defmodule Lexical.RemoteControl.Commands.Rename do
)

if Enum.empty?(new_uri_with_expected_operation) do
reindex_all_modified_files(state)
state.on_complete.()
reindex_all_renamed_files(state)
end

%__MODULE__{state | uri_to_expected_operation: new_uri_with_expected_operation}
Expand All @@ -74,10 +84,18 @@ defmodule Lexical.RemoteControl.Commands.Rename do
end
end

defp reindex_all_renamed_files(%__MODULE__{} = state) do
state.raw_uri_to_expected_operation
|> Map.keys()
|> Enum.each(&Reindex.uri/1)
defp reindex_all_modified_files(%__MODULE__{} = state) do
Enum.each(state.paths_to_remind, fn
path ->
Reindex.uri(path)
state.on_update_progess.(1, "reindexing")
end)

Enum.each(state.paths_to_delete, fn
path ->
Store.clear(path)
state.on_update_progess.(1, "deleting old index")
end)
end
end

Expand All @@ -86,20 +104,49 @@ defmodule Lexical.RemoteControl.Commands.Rename do

@spec child_spec(
%{Lexical.uri() => Messages.file_changed() | Messages.file_saved()},
list(Lexical.path()),
list(Lexical.path()),
fun(),
fun()
) :: Supervisor.child_spec()
def child_spec(uri_to_expected_operation, on_update_progess, on_complete) do
def child_spec(
uri_to_expected_operation,
paths_to_remind,
paths_to_delete,
on_update_progess,
on_complete
) do
%{
id: __MODULE__,
start:
{__MODULE__, :start_link, [uri_to_expected_operation, on_update_progess, on_complete]},
{__MODULE__, :start_link,
[
uri_to_expected_operation,
paths_to_remind,
paths_to_delete,
on_update_progess,
on_complete
]},
restart: :transient
}
end

def start_link(uri_to_expected_operation, on_update_progess, on_complete) do
state = State.new(uri_to_expected_operation, on_update_progess, on_complete)
def start_link(
uri_to_expected_operation,
paths_to_remind,
paths_to_delete,
on_update_progess,
on_complete
) do
state =
State.new(
uri_to_expected_operation,
paths_to_remind,
paths_to_delete,
on_update_progess,
on_complete
)

GenServer.start_link(__MODULE__, state, name: __MODULE__)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ defmodule Lexical.RemoteControl.Commands.RenameSupervisor do
DynamicSupervisor.start_link(name: __MODULE__, strategy: :one_for_one)
end

def start_renaming(uri_with_expected_operation, report_progress_func, complete_func) do
def start_renaming(
uri_with_expected_operation,
paths_to_remind,
paths_to_delete,
report_progress_func,
complete_func
) do
DynamicSupervisor.start_child(
__MODULE__,
Rename.child_spec(uri_with_expected_operation, report_progress_func, complete_func)
Rename.child_spec(
uri_with_expected_operation,
paths_to_remind,
paths_to_delete,
report_progress_func,
complete_func
)
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
alias Lexical.RemoteControl.Api.Proxy
alias Lexical.RemoteControl.Commands.Rename
alias Lexical.RemoteControl.Commands.RenameSupervisor
alias Lexical.RemoteControl.Search.Store

import Lexical.RemoteControl.Api.Messages
import Lexical.Test.EventualAssertions
Expand Down Expand Up @@ -30,10 +31,13 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
} do
uri = "file://file.ex"
uri_with_expected_operation = %{uri => file_changed(uri: uri)}
file_to_remind = [uri]

{:ok, _pid} =
RenameSupervisor.start_renaming(
uri_with_expected_operation,
file_to_remind,
[],
on_report_progress,
on_complete
)
Expand All @@ -47,10 +51,13 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
on_complete: on_complete
} do
uri = "file://file.ex"
file_to_remind = [uri]

{:ok, _pid} =
RenameSupervisor.start_renaming(
%{uri => file_saved(uri: uri)},
file_to_remind,
[],
on_report_progress,
on_complete
)
Expand All @@ -68,16 +75,22 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
on_complete: on_complete
} do
uri1 = "file://file1.ex"
uri2 = "file://file2.ex"
old_uri = "file://file2.ex"
new_uri = "file://file3.ex"

uri_with_expected_operation = %{
uri1 => file_changed(uri: uri1),
uri2 => file_saved(uri: uri2)
new_uri => file_saved(uri: new_uri)
}

file_to_remind = [uri1, new_uri]
file_to_delete = [old_uri]

{:ok, _pid} =
RenameSupervisor.start_renaming(
uri_with_expected_operation,
file_to_remind,
file_to_delete,
on_report_progress,
on_complete
)
Expand All @@ -87,6 +100,45 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
refute_receive :complete_progress
end

test "it should reindex the new file and delete the old file when all the files are modified.",
%{
on_report_progress: on_report_progress,
on_complete: on_complete
} do
patch(Store, :clear, :ok)
old_uri = "file://old_file.ex"
new_uri = "file://new_file.ex"
another_uri = "file://file.ex"

uri_with_expected_operation = %{
old_uri => file_changed(uri: old_uri),
new_uri => file_saved(uri: new_uri)
}

file_to_remind = [new_uri, another_uri]
file_to_delete = [old_uri]

{:ok, _pid} =
RenameSupervisor.start_renaming(
uri_with_expected_operation,
file_to_remind,
file_to_delete,
on_report_progress,
on_complete
)

Rename.update_progress(file_changed(uri: old_uri))
assert_receive {:update_progress, 1, ""}
refute_receive :complete_progress

Rename.update_progress(file_saved(uri: new_uri))
assert_receive {:update_progress, 1, ""}

assert_receive {:update_progress, 1, "reindexing"}
assert_receive {:update_progress, 1, "deleting old index"}
assert_receive :complete_progress
end

test "it should return :error when updating the progress if the process is not alive" do
assert {:error, :not_in_rename_progress} =
Rename.update_progress(file_changed(uri: "file://file.ex"))
Expand Down

0 comments on commit 2910c54

Please sign in to comment.