defmodule Integrations.Dragonfly.Display do
@moduledoc """
Dashboard panel for `Integrations.Dragonfly` (same package).
Nested under the monitor's own namespace deliberately — the package's
top-level module name never changes, and this module's name is
guaranteed distinct from any separately-published standalone display
package. `Display.BundledDefault` auto-hooks this whenever
`Integrations.Dragonfly` starts, the same as if they were still one module.
"""
use CodeNameRaven.Display, category: :infrastructure, size_hint: :medium
@default_port 6379
@impl true
def display_name, do: "Dragonfly"
@impl true
def compatible_monitors, do: [Integrations.Dragonfly]
@impl true
def render(assigns) do
result = assigns[:result]
assigns = Map.merge(assigns, %{result: result})
~H"""
<%= @config.name || instance_title(@config.params) %>
<%= instance_title(@config.params) %>
<%= if @result && @result[:version], do: " · v#{@result.version}" %>
Evicted: <%= @result.evicted_keys %>
Expired: <%= @result.expired_keys %>
Replication lag: <%= @result.replication_lag %> bytes
"""
end
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
defp instance_title(params) do
host = params[:host] || params["host"] || "?"
port = parse_int(params[:port] || params["port"], @default_port)
"#{host}:#{port}"
end
defp format_memory(%{used_memory_bytes: used, maxmemory_bytes: max})
when is_integer(max) and max > 0 do
pct = round(used / max * 100)
"#{format_bytes(used)} / #{format_bytes(max)} (#{pct}%)"
end
defp format_memory(%{used_memory_bytes: used}), do: format_bytes(used)
defp format_memory(_), do: "?"
defp format_bytes(bytes) when is_integer(bytes) do
cond do
bytes >= 1_073_741_824 -> "#{Float.round(bytes / 1_073_741_824, 1)} GB"
bytes >= 1_048_576 -> "#{Float.round(bytes / 1_048_576, 1)} MB"
bytes >= 1024 -> "#{Float.round(bytes / 1024, 1)} KB"
true -> "#{bytes} B"
end
end
defp format_bytes(_), do: "?"
defp parse_int(nil, default), do: default
defp parse_int(v, _) when is_integer(v), do: v
defp parse_int(v, default) when is_binary(v) do
case Integer.parse(v) do
{n, _} -> n
:error -> default
end
end
defp parse_int(_, default), do: default
end