defmodule EventStoreDashboard.Components.SubscriptionModal do @moduledoc false use Phoenix.Component alias EventStoreDashboard.Components.EventLink alias EventStoreDashboard.Params alias EventStoreDashboard.Repo alias EventStoreDashboard.Repo.Context alias Phoenix.LiveDashboard.PageBuilder alias Phoenix.LiveView.Socket attr(:page, PageBuilder, required: true) attr(:ctx, Context, required: true) attr(:subscription_id, :integer, required: true) attr(:socket, Socket, required: true) @pending_limit 10 def render(assigns) do subscription = fetch_subscription(assigns.page.node, assigns.ctx, assigns.subscription_id) pending = fetch_pending(assigns.page.node, assigns.ctx, subscription) assigns = assign(assigns, subscription: subscription, pending: pending, pending_limit: @pending_limit ) ~H"""
<%= if @subscription do %>
ID
{@subscription.subscription_id}
Name
{@subscription.subscription_name}
Stream <.link patch={stream_modal_path(@socket, @page, @ctx, @subscription.stream_uuid)} class="es-modal-trigger" > {@subscription.stream_uuid}
Last seen
Stream Version
Lag
{@subscription.lag}
Created at
{@subscription.created_at}
0} class="mt-3">
Next pending events (showing up to {@pending_limit} of {@subscription.lag})

Could not load pending events.

{position_header(@subscription.stream_uuid)} Type Created at
{event.event_type}
{event.created_at}
<% else %> <% end %>
""" end defp fetch_subscription(node, %Context{} = ctx, subscription_id) do case Repo.fetch_subscription_by_id(node, ctx, subscription_id) do {:ok, subscription} -> subscription _ -> nil end end defp fetch_pending(_node, _ctx, nil), do: [] defp fetch_pending(_node, _ctx, %{lag: 0}), do: [] defp fetch_pending(node, %Context{} = ctx, subscription) do with {:ok, stream} <- Repo.fetch_stream(node, ctx, subscription.stream_uuid), {:ok, rows} <- Repo.query_events_after_version( node, ctx, stream.stream_id, subscription.last_seen || 0, @pending_limit ) do Enum.map(rows, &Repo.row_to_event(&1, ctx)) else _ -> [] end end defp position_header("$all"), do: "Position" defp position_header(_), do: "Stream Version" defp view_events_path(socket, page, %Context{} = ctx, stream_uuid) do Params.to_live_dashboard_path(socket, page, %Params{ eventstore: ctx.event_store, nav: "events", stream: stream_uuid }) end defp stream_modal_path(socket, page, %Context{} = ctx, stream_uuid) do Params.to_live_dashboard_path(socket, page, %Params{ eventstore: ctx.event_store, stream_modal: stream_uuid, subscription_id: nil }) end end