feat: issue #25 implemented - cli
This commit is contained in:
@@ -142,4 +142,106 @@ defmodule BDS.Maintenance do
|
||||
|
||||
{:ok, %{diff_reports: diff_reports, orphan_reports: orphan_reports}}
|
||||
end
|
||||
|
||||
@doc """
|
||||
The canonical full-rebuild sequence, shared by the GUI "Rebuild Database"
|
||||
command and the CLI `rebuild` command so the two can never drift. Each step
|
||||
is `%{name: String.t(), work: (report -> result)}` where `report` is a
|
||||
2-arity progress callback and a `{:error, message}` result marks failure.
|
||||
"""
|
||||
def full_rebuild_steps(project_id) when is_binary(project_id) do
|
||||
[
|
||||
%{
|
||||
name: "Rebuild Posts From Files",
|
||||
work: fn report ->
|
||||
{:ok, posts} =
|
||||
rebuild_from_filesystem(project_id, "post",
|
||||
on_progress: report,
|
||||
rebuild_embeddings: false
|
||||
)
|
||||
|
||||
report.(1.0, "Post rebuild complete")
|
||||
%{project_id: project_id, counts: %{posts: length(posts)}}
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Rebuild Media From Files",
|
||||
work: fn report ->
|
||||
{:ok, media} = rebuild_from_filesystem(project_id, "media", on_progress: report)
|
||||
report.(1.0, "Media rebuild complete")
|
||||
%{project_id: project_id, counts: %{media: length(media)}}
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Rebuild Scripts From Files",
|
||||
work: fn report ->
|
||||
{:ok, scripts} = rebuild_from_filesystem(project_id, "script", on_progress: report)
|
||||
report.(1.0, "Script rebuild complete")
|
||||
%{project_id: project_id, counts: %{scripts: length(scripts)}}
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Rebuild Templates From Files",
|
||||
work: fn report ->
|
||||
{:ok, templates} = rebuild_from_filesystem(project_id, "template", on_progress: report)
|
||||
report.(1.0, "Template rebuild complete")
|
||||
%{project_id: project_id, counts: %{templates: length(templates)}}
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Rebuild Post Links",
|
||||
work: fn report ->
|
||||
:ok = BDS.Posts.rebuild_post_links(project_id, on_progress: report)
|
||||
report.(1.0, "Post links rebuilt")
|
||||
%{project_id: project_id}
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Regenerate Missing Thumbnails",
|
||||
work: fn report ->
|
||||
result = BDS.Media.regenerate_missing_thumbnails(project_id, on_progress: report)
|
||||
report.(1.0, "Missing thumbnails regenerated")
|
||||
Map.put(result, :project_id, project_id)
|
||||
end
|
||||
},
|
||||
%{
|
||||
name: "Rebuild Embedding Index",
|
||||
work: fn report -> rebuild_embedding_index(project_id, on_progress: report) end
|
||||
}
|
||||
]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rebuilds the embedding index for the project, mapping backend failures to a
|
||||
user-facing message (`{:error, message}`).
|
||||
"""
|
||||
def rebuild_embedding_index(project_id, opts \\ []) when is_binary(project_id) do
|
||||
on_progress = progress_callback(opts)
|
||||
|
||||
case Embeddings.rebuild_project(project_id, on_progress: on_progress) do
|
||||
{:ok, rebuilt_post_ids} ->
|
||||
on_progress.(1.0, "Embedding index rebuilt")
|
||||
|
||||
%{
|
||||
project_id: project_id,
|
||||
rebuilt_post_ids: rebuilt_post_ids,
|
||||
rebuilt_count: length(rebuilt_post_ids)
|
||||
}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, embedding_error_message(reason)}
|
||||
end
|
||||
end
|
||||
|
||||
defp embedding_error_message(reason) do
|
||||
detail =
|
||||
case reason do
|
||||
message when is_binary(message) -> message
|
||||
{:embedding_backend_unavailable, _inner} -> "the embedding service did not start"
|
||||
other -> inspect(other)
|
||||
end
|
||||
|
||||
"Could not build the embedding index: #{detail}. The model is downloaded on first use, " <>
|
||||
"so check your internet connection — or turn off semantic similarity in Settings."
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user