towerops/lib/towerops_web/live/admin/dashboard_live.ex

49 lines
1.3 KiB
Elixir

defmodule ToweropsWeb.Admin.DashboardLive do
@moduledoc """
Admin dashboard showing system statistics and recent audit logs.
"""
use ToweropsWeb, :live_view
alias Towerops.Admin
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, t("Dashboard"))
|> assign(:timezone, socket.assigns.current_scope.timezone)}
end
@impl true
def handle_params(params, _url, socket) do
page = params |> Map.get("page", "1") |> String.to_integer()
per_page = 10
user_count = Admin.count_users()
org_count = Admin.count_organizations()
# Fetch all recent logs (limited to last 100 for performance)
all_logs = Admin.list_audit_logs(limit: 100)
total_count = length(all_logs)
total_pages = ceil(total_count / per_page)
# Ensure page is within valid range
page = max(1, min(page, max(1, total_pages)))
# Slice logs for current page
offset = (page - 1) * per_page
recent_logs = Enum.slice(all_logs, offset, per_page)
{:noreply,
socket
|> assign(:user_count, user_count)
|> assign(:org_count, org_count)
|> assign(:recent_logs, recent_logs)
|> assign(:pagination, %{
page: page,
per_page: per_page,
total_count: total_count,
total_pages: total_pages
})}
end
end