defmodule Oban.Web.Queues.TableComponent do use Oban.Web, :live_component import Oban.Web.Helpers.QueueHelper alias Oban.Web.{Queue, Timing} @impl Phoenix.LiveComponent def render(assigns) do ~H"""
No queues running match the current set of filters.
""" end # Components attr :label, :string, required: true attr :class, :string, default: "" defp queue_header(assigns) do ~H""" {@label} """ end attr :access, :map, required: true attr :myself, :any, required: true attr :queue, :string, required: true attr :selected, :boolean, default: false defp queue_row(assigns) do ~H"""
  • <.link patch={oban_path([:queues, @queue.name])} class="py-5 flex flex-grow items-center">
    {@queue.name}
    {length(@queue.checks)} {@queue.counts.executing} {integer_to_estimate(@queue.counts.available)} {local_limit(@queue)} {global_limit(@queue)} {rate_limit(@queue)} {started_at(@queue)}
  • """ end # Handlers @impl Phoenix.LiveComponent def handle_event("toggle-select", %{"id" => queue}, socket) do send(self(), {:toggle_select, queue}) {:noreply, socket} end # Helpers defp local_limit(queue) do queue.checks |> Enum.map(& &1["local_limit"]) |> Enum.min_max() |> case do {min, min} -> min {min, max} -> "#{min}..#{max}" end end defp global_limit(queue) do Queue.global_limit(queue) || "-" end defp rate_limit(queue) do queue.checks |> Enum.map(& &1["rate_limit"]) |> Enum.reject(&is_nil/1) |> case do [head_limit | _rest] = rate_limits -> %{"allowed" => allowed, "period" => period, "window_time" => time} = head_limit {prev_total, curr_total} = rate_limits |> Enum.flat_map(& &1["windows"]) |> Enum.reduce({0, 0}, fn %{"prev_count" => pcnt, "curr_count" => ccnt}, {pacc, cacc} -> {pacc + pcnt, cacc + ccnt} end) unix_now = DateTime.to_unix(DateTime.utc_now(), :second) ellapsed = unix_now - time_to_unix(time) weight = div(max(period - ellapsed, 0), period) remaining = prev_total * weight + curr_total period_in_words = Timing.to_words(period, relative: false) "#{remaining}/#{allowed} per #{period_in_words}" [] -> "-" end end defp time_to_unix(unix) when is_integer(unix), do: unix defp time_to_unix(time) do Date.utc_today() |> DateTime.new!(Time.from_iso8601!(time)) |> DateTime.to_unix(:second) end end