diff --git a/lib/pinchflat/tasks/tasks_query.ex b/lib/pinchflat/tasks/tasks_query.ex new file mode 100644 index 0000000..cb2da78 --- /dev/null +++ b/lib/pinchflat/tasks/tasks_query.ex @@ -0,0 +1,43 @@ +defmodule Pinchflat.Tasks.TasksQuery do + @moduledoc """ + Query helpers for the Tasks context. + + These methods are made to be one-ish liners used + to compose queries. Each method should strive to do + _one_ thing. These don't need to be tested as + they are just building blocks for other functionality + which, itself, will be tested. + """ + import Ecto.Query, warn: false + + alias Pinchflat.Tasks.Task + + # This allows the module to be aliased and query methods to be used + # all in one go + # usage: use Pinchflat.Tasks.TasksQuery + defmacro __using__(_opts) do + quote do + import Ecto.Query, warn: false + + alias unquote(__MODULE__) + end + end + + def new do + Task + end + + def join_job(query) do + join(query, :inner, [t], j in assoc(t, :job)) + end + + def in_state(states) when is_list(states) do + dynamic([t, j], j.state in ^states) + end + + def in_state(state), do: in_state([state]) + + def has_worker(worker_name) do + dynamic([t, j], fragment("? LIKE ?", j.worker, ^"%.#{worker_name}")) + end +end diff --git a/lib/pinchflat_web/controllers/pages/page_controller.ex b/lib/pinchflat_web/controllers/pages/page_controller.ex index a6e36da..bc990b4 100644 --- a/lib/pinchflat_web/controllers/pages/page_controller.ex +++ b/lib/pinchflat_web/controllers/pages/page_controller.ex @@ -20,14 +20,16 @@ defmodule PinchflatWeb.Pages.PageController do end defp render_home_page(conn) do + # TODO: revert conn |> render(:home, - media_profile_count: Repo.aggregate(MediaProfile, :count, :id), - source_count: Repo.aggregate(Source, :count, :id), + media_profile_count: 1 || Repo.aggregate(MediaProfile, :count, :id), + source_count: 1 || Repo.aggregate(Source, :count, :id), media_item_count: - MediaQuery.new() - |> MediaQuery.with_media_downloaded_at() - |> Repo.aggregate(:count, :id) + 1 || + MediaQuery.new() + |> MediaQuery.with_media_downloaded_at() + |> Repo.aggregate(:count, :id) ) end diff --git a/lib/pinchflat_web/controllers/pages/page_html/home.html.heex b/lib/pinchflat_web/controllers/pages/page_html/home.html.heex index d018bcb..bec4635 100644 --- a/lib/pinchflat_web/controllers/pages/page_html/home.html.heex +++ b/lib/pinchflat_web/controllers/pages/page_html/home.html.heex @@ -26,8 +26,16 @@
- History + Active Tasks
- <%= live_render(@conn, Pinchflat.Pages.HistoryTableLive) %> + <%= live_render(@conn, Pinchflat.Pages.JobTableLive) %> +
+
+ +
+ Media History +
+ <%!-- TODO: revert --%> + <%!-- <%= live_render(@conn, Pinchflat.Pages.HistoryTableLive) %> --%>
diff --git a/lib/pinchflat_web/controllers/pages/page_html/job_table_live.ex b/lib/pinchflat_web/controllers/pages/page_html/job_table_live.ex new file mode 100644 index 0000000..006d012 --- /dev/null +++ b/lib/pinchflat_web/controllers/pages/page_html/job_table_live.ex @@ -0,0 +1,50 @@ +defmodule Pinchflat.Pages.JobTableLive do + use PinchflatWeb, :live_view + use Pinchflat.Tasks.TasksQuery + + alias Pinchflat.Repo + # alias Pinchflat.Utils.NumberUtils + alias PinchflatWeb.CustomComponents.TextComponents + + def render(%{tasks: []} = assigns) do + ~H""" +
+

Nothing Here!

+
+ """ + end + + def render(assigns) do + ~H""" +
+
+ <.table rows={@tasks} table_class="text-white"> + <:col :let={task} label="Job ID"> + <%= task.job_id %> + + +
+
+ """ + end + + def mount(_params, _session, socket) do + tasks = get_tasks() + + {:ok, assign(socket, tasks: tasks)} + end + + defp get_tasks do + TasksQuery.new() + |> TasksQuery.join_job() + |> where(^TasksQuery.in_state("completed")) + |> limit(5) + |> Repo.all() + end + + defp format_datetime(nil), do: "" + + defp format_datetime(datetime) do + TextComponents.datetime_in_zone(%{datetime: datetime, format: "%Y-%m-%d %H:%M"}) + end +end