defmodule EventStoreDashboard.Components.StreamModal 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(:stream_uuid, :string, required: true) attr(:socket, Socket, required: true) def render(assigns) do stream = fetch_stream(assigns.page.node, assigns.ctx, assigns.stream_uuid) snapshot = fetch_snapshot(assigns.page.node, assigns.ctx, assigns.stream_uuid) assigns = assign(assigns, stream: stream, snapshot: snapshot) ~H"""
<%= if @stream do %>
Stream
{@stream.stream_uuid}
ID
{@stream.stream_id}
Stream Version
Status <.status_badge status={@stream.status} />
Snapshot <.link patch={snapshot_modal_path(@socket, @page, @ctx, @snapshot.source_uuid)} class="es-modal-trigger" > @{@snapshot.source_version} 0} class="badge badge-warning ml-2"> {snapshot_lag(@stream, @snapshot)} events behind
Created at
{@stream.created_at}
Deleted at
{@stream.deleted_at}
<% else %> <% end %>
""" end attr(:status, :atom, required: true) defp status_badge(%{status: :deleted} = assigns) do ~H|deleted| end defp status_badge(%{status: :created} = assigns) do ~H|created| end defp status_badge(assigns) do ~H|unknown| end defp fetch_stream(node, %Context{} = ctx, stream_uuid) do case Repo.fetch_stream(node, ctx, stream_uuid) do {:ok, stream} -> stream _ -> nil end end defp fetch_snapshot(_node, _ctx, "$all"), do: nil defp fetch_snapshot(node, %Context{} = ctx, stream_uuid) do case Repo.fetch_snapshot_summary(node, ctx, stream_uuid) do {:ok, snapshot} -> snapshot _ -> nil end end defp snapshot_lag(stream, snapshot) do max((stream.stream_version || 0) - (snapshot.source_version || 0), 0) end defp snapshot_modal_path(socket, page, %Context{} = ctx, source_uuid) do Params.to_live_dashboard_path(socket, page, %Params{ eventstore: ctx.event_store, nav: "snapshots", snapshot_uuid: source_uuid, stream_modal: nil }) end 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 end