[WIP] Added PoC job table

This commit is contained in:
Kieran Eglin 2024-05-16 14:33:02 -07:00
parent bbdc56c656
commit 726d236540
No known key found for this signature in database
GPG key ID: 193984967FCF432D
4 changed files with 110 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -26,8 +26,16 @@
</div>
<div class="rounded-sm border shadow-default border-strokedark bg-boxdark mt-4 p-5">
<span class="text-2xl font-medium mb-4">History</span>
<span class="text-2xl font-medium mb-4">Active Tasks</span>
<section class="mt-6">
<%= live_render(@conn, Pinchflat.Pages.HistoryTableLive) %>
<%= live_render(@conn, Pinchflat.Pages.JobTableLive) %>
</section>
</div>
<div class="rounded-sm border shadow-default border-strokedark bg-boxdark mt-4 p-5">
<span class="text-2xl font-medium mb-4">Media History</span>
<section class="mt-6">
<%!-- TODO: revert --%>
<%!-- <%= live_render(@conn, Pinchflat.Pages.HistoryTableLive) %> --%>
</section>
</div>

View file

@ -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"""
<div class="mb-4 flex items-center">
<p class="ml-2">Nothing Here!</p>
</div>
"""
end
def render(assigns) do
~H"""
<div>
<div class="max-w-full overflow-x-auto">
<.table rows={@tasks} table_class="text-white">
<:col :let={task} label="Job ID">
<%= task.job_id %>
</:col>
</.table>
</div>
</div>
"""
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