fix: better progress reporting on tasks
This commit is contained in:
@@ -122,32 +122,28 @@ defmodule BDS.Desktop.ShellCommands do
|
||||
|
||||
{:ok, posts_task} =
|
||||
Tasks.submit_task("Rebuild Posts From Files", fn report ->
|
||||
report.(0.0, "Scanning post files")
|
||||
{:ok, posts} = Maintenance.rebuild_from_filesystem(project.id, "post")
|
||||
{:ok, posts} = Maintenance.rebuild_from_filesystem(project.id, "post", on_progress: report)
|
||||
report.(1.0, "Post rebuild complete")
|
||||
%{project_id: project.id, counts: %{posts: length(posts)}}
|
||||
end, attrs)
|
||||
|
||||
{:ok, _media_task} =
|
||||
Tasks.submit_task("Rebuild Media From Files", fn report ->
|
||||
report.(0.0, "Scanning media files")
|
||||
{:ok, media} = Maintenance.rebuild_from_filesystem(project.id, "media")
|
||||
{:ok, media} = Maintenance.rebuild_from_filesystem(project.id, "media", on_progress: report)
|
||||
report.(1.0, "Media rebuild complete")
|
||||
%{project_id: project.id, counts: %{media: length(media)}}
|
||||
end, attrs)
|
||||
|
||||
{:ok, _scripts_task} =
|
||||
Tasks.submit_task("Rebuild Scripts From Files", fn report ->
|
||||
report.(0.0, "Scanning script files")
|
||||
{:ok, scripts} = Maintenance.rebuild_from_filesystem(project.id, "script")
|
||||
{:ok, scripts} = Maintenance.rebuild_from_filesystem(project.id, "script", on_progress: report)
|
||||
report.(1.0, "Script rebuild complete")
|
||||
%{project_id: project.id, counts: %{scripts: length(scripts)}}
|
||||
end, attrs)
|
||||
|
||||
{:ok, _templates_task} =
|
||||
Tasks.submit_task("Rebuild Templates From Files", fn report ->
|
||||
report.(0.0, "Scanning template files")
|
||||
{:ok, templates} = Maintenance.rebuild_from_filesystem(project.id, "template")
|
||||
{:ok, templates} = Maintenance.rebuild_from_filesystem(project.id, "template", on_progress: report)
|
||||
report.(1.0, "Template rebuild complete")
|
||||
%{project_id: project.id, counts: %{templates: length(templates)}}
|
||||
end, attrs)
|
||||
|
||||
@@ -15,12 +15,12 @@ defmodule BDS.Maintenance do
|
||||
alias BDS.Sidecar
|
||||
alias BDS.Templates.Template
|
||||
|
||||
def rebuild_from_filesystem(project_id, entity_type) do
|
||||
def rebuild_from_filesystem(project_id, entity_type, opts \\ []) do
|
||||
case normalize_entity_type(entity_type) do
|
||||
:post -> BDS.Posts.rebuild_posts_from_files(project_id)
|
||||
:media -> BDS.Media.rebuild_media_from_files(project_id)
|
||||
:script -> BDS.Scripts.rebuild_scripts_from_files(project_id)
|
||||
:template -> BDS.Templates.rebuild_templates_from_files(project_id)
|
||||
:post -> BDS.Posts.rebuild_posts_from_files(project_id, opts)
|
||||
:media -> BDS.Media.rebuild_media_from_files(project_id, opts)
|
||||
:script -> BDS.Scripts.rebuild_scripts_from_files(project_id, opts)
|
||||
:template -> BDS.Templates.rebuild_templates_from_files(project_id, opts)
|
||||
:embedding -> Embeddings.rebuild_project(project_id)
|
||||
:unsupported -> {:error, :unsupported_entity_type}
|
||||
end
|
||||
|
||||
@@ -311,8 +311,9 @@ defmodule BDS.Media do
|
||||
end)
|
||||
end
|
||||
|
||||
def rebuild_media_from_files(project_id) do
|
||||
def rebuild_media_from_files(project_id, opts \\ []) do
|
||||
project = Projects.get_project!(project_id)
|
||||
on_progress = progress_callback(opts)
|
||||
|
||||
canonical_sidecars =
|
||||
project
|
||||
@@ -322,19 +323,36 @@ defmodule BDS.Media do
|
||||
|> Enum.filter(&canonical_sidecar?/1)
|
||||
|> Enum.filter(&binary_exists_for_sidecar?/1)
|
||||
|
||||
media_items = Enum.map(canonical_sidecars, &upsert_media_from_sidecar(project, &1))
|
||||
translation_sidecars =
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("media")
|
||||
|> list_matching_files("*.meta")
|
||||
|> Enum.filter(&translation_sidecar?/1)
|
||||
|
||||
total_files = length(canonical_sidecars) + length(translation_sidecars)
|
||||
:ok = report_rebuild_started(on_progress, total_files, "media files")
|
||||
|
||||
media_items =
|
||||
canonical_sidecars
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {sidecar_path, index} ->
|
||||
media = upsert_media_from_sidecar(project, sidecar_path)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "media files")
|
||||
media
|
||||
end)
|
||||
|
||||
canonical_media_by_binary_path =
|
||||
Map.new(media_items, fn media ->
|
||||
{Path.join(Projects.project_data_dir(project), media.file_path), media}
|
||||
end)
|
||||
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("media")
|
||||
|> list_matching_files("*.meta")
|
||||
|> Enum.filter(&translation_sidecar?/1)
|
||||
|> Enum.each(&upsert_translation_from_sidecar(project, canonical_media_by_binary_path, &1))
|
||||
translation_sidecars
|
||||
|> Enum.with_index(length(canonical_sidecars) + 1)
|
||||
|> Enum.each(fn {sidecar_path, index} ->
|
||||
upsert_translation_from_sidecar(project, canonical_media_by_binary_path, sidecar_path)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "media files")
|
||||
end)
|
||||
|
||||
{:ok, media_items}
|
||||
end
|
||||
@@ -629,4 +647,31 @@ defmodule BDS.Media do
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp progress_callback(opts) do
|
||||
case Keyword.get(opts, :on_progress) do
|
||||
callback when is_function(callback, 2) -> callback
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp report_rebuild_started(nil, _total, _label), do: :ok
|
||||
|
||||
defp report_rebuild_started(callback, 0, label) do
|
||||
callback.(1.0, "No #{label} found")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_started(callback, total, label) do
|
||||
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||
defp report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||
|
||||
defp report_rebuild_progress(callback, current, total, label) do
|
||||
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@@ -136,8 +136,9 @@ defmodule BDS.Posts do
|
||||
end
|
||||
end
|
||||
|
||||
def rebuild_posts_from_files(project_id) do
|
||||
def rebuild_posts_from_files(project_id, opts \\ []) do
|
||||
project = Projects.get_project!(project_id)
|
||||
on_progress = progress_callback(opts)
|
||||
|
||||
rebuild_files =
|
||||
project
|
||||
@@ -146,14 +147,26 @@ defmodule BDS.Posts do
|
||||
|> list_matching_files("*.md")
|
||||
|> Enum.map(&parse_rebuild_file(project, &1))
|
||||
|
||||
total_files = length(rebuild_files)
|
||||
:ok = report_rebuild_started(on_progress, total_files, "post files")
|
||||
|
||||
{translation_files, post_files} = Enum.split_with(rebuild_files, &translation_rebuild_file?/1)
|
||||
|
||||
posts =
|
||||
post_files
|
||||
|> Enum.map(&upsert_post_from_rebuild_file(project_id, &1))
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {file, index} ->
|
||||
post = upsert_post_from_rebuild_file(project_id, file)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "post files")
|
||||
post
|
||||
end)
|
||||
|
||||
translation_files
|
||||
|> Enum.map(&upsert_post_translation_from_rebuild_file(project_id, &1))
|
||||
|> Enum.with_index(length(post_files) + 1)
|
||||
|> Enum.each(fn {file, index} ->
|
||||
upsert_post_translation_from_rebuild_file(project_id, file)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "post files")
|
||||
end)
|
||||
|
||||
{:ok, posts}
|
||||
end
|
||||
@@ -941,4 +954,31 @@ defmodule BDS.Posts do
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp progress_callback(opts) do
|
||||
case Keyword.get(opts, :on_progress) do
|
||||
callback when is_function(callback, 2) -> callback
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp report_rebuild_started(nil, _total, _label), do: :ok
|
||||
|
||||
defp report_rebuild_started(callback, 0, label) do
|
||||
callback.(1.0, "No #{label} found")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_started(callback, total, label) do
|
||||
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||
defp report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||
|
||||
defp report_rebuild_progress(callback, current, total, label) do
|
||||
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@@ -115,15 +115,27 @@ defmodule BDS.Scripts do
|
||||
end
|
||||
end
|
||||
|
||||
def rebuild_scripts_from_files(project_id) do
|
||||
def rebuild_scripts_from_files(project_id, opts \\ []) do
|
||||
project = Projects.get_project!(project_id)
|
||||
|
||||
scripts =
|
||||
script_paths =
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("scripts")
|
||||
|> list_matching_files("*.lua")
|
||||
|> Enum.map(&upsert_script_from_file(project_id, project, &1))
|
||||
|
||||
total_files = length(script_paths)
|
||||
on_progress = progress_callback(opts)
|
||||
:ok = report_rebuild_started(on_progress, total_files, "script files")
|
||||
|
||||
scripts =
|
||||
script_paths
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {path, index} ->
|
||||
script = upsert_script_from_file(project_id, project, path)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "script files")
|
||||
script
|
||||
end)
|
||||
|
||||
{:ok, scripts}
|
||||
end
|
||||
@@ -259,4 +271,31 @@ defmodule BDS.Scripts do
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp progress_callback(opts) do
|
||||
case Keyword.get(opts, :on_progress) do
|
||||
callback when is_function(callback, 2) -> callback
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp report_rebuild_started(nil, _total, _label), do: :ok
|
||||
|
||||
defp report_rebuild_started(callback, 0, label) do
|
||||
callback.(1.0, "No #{label} found")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_started(callback, total, label) do
|
||||
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||
defp report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||
|
||||
defp report_rebuild_progress(callback, current, total, label) do
|
||||
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@@ -133,15 +133,27 @@ defmodule BDS.Templates do
|
||||
end
|
||||
end
|
||||
|
||||
def rebuild_templates_from_files(project_id) do
|
||||
def rebuild_templates_from_files(project_id, opts \\ []) do
|
||||
project = Projects.get_project!(project_id)
|
||||
|
||||
templates =
|
||||
template_paths =
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("templates")
|
||||
|> list_matching_files("*.liquid")
|
||||
|> Enum.map(&upsert_template_from_file(project_id, project, &1))
|
||||
|
||||
total_files = length(template_paths)
|
||||
on_progress = progress_callback(opts)
|
||||
:ok = report_rebuild_started(on_progress, total_files, "template files")
|
||||
|
||||
templates =
|
||||
template_paths
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {path, index} ->
|
||||
template = upsert_template_from_file(project_id, project, path)
|
||||
:ok = report_rebuild_progress(on_progress, index, total_files, "template files")
|
||||
template
|
||||
end)
|
||||
|
||||
{:ok, templates}
|
||||
end
|
||||
@@ -410,4 +422,31 @@ defmodule BDS.Templates do
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp progress_callback(opts) do
|
||||
case Keyword.get(opts, :on_progress) do
|
||||
callback when is_function(callback, 2) -> callback
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp report_rebuild_started(nil, _total, _label), do: :ok
|
||||
|
||||
defp report_rebuild_started(callback, 0, label) do
|
||||
callback.(1.0, "No #{label} found")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_started(callback, total, label) do
|
||||
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||
:ok
|
||||
end
|
||||
|
||||
defp report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||
defp report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||
|
||||
defp report_rebuild_progress(callback, current, total, label) do
|
||||
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user