Skip to content

Commit

Permalink
feat: small dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher2K committed Dec 1, 2024
1 parent e5d04da commit 7cd634c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions swapify_api/lib/swapify_api/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ defmodule SwapifyApi.Accounts do
end)
end

@spec count_users() :: {:ok, pos_integer()}
def count_users(), do: UserRepo.count()

@doc """
Returns a map with 2 keys
%{users: list(Users.t()), count: pos_integer()}
Expand Down
2 changes: 2 additions & 0 deletions swapify_api/lib/swapify_api/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ defmodule SwapifyApi.Tasks do
end
end
end

def count_transfers(), do: TransferRepo.count()
end
16 changes: 16 additions & 0 deletions swapify_api/lib/swapify_api/tasks/transfer_repo.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule SwapifyApi.Tasks.TransferRepo do
@doc "Transfer table repository"
use OpenTelemetryDecorator

import Ecto.Query

alias SwapifyApi.MusicProviders.Playlist
Expand Down Expand Up @@ -197,4 +199,18 @@ defmodule SwapifyApi.Tasks.TransferRepo do
)
|> Repo.one()
|> Utils.from_nullable_to_tuple()

@doc """
Transfer count
"""
@spec count() :: {:ok, pos_integer()}
@decorate with_span("transfer_repo.count")
def count() do
query =
from transfer in Transfer,
select: count("*")

Repo.one(query)
|> Utils.from_nullable_to_tuple()
end
end
12 changes: 12 additions & 0 deletions swapify_api/lib/swapify_api_web/components/ui.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@ defmodule SwapifyApiWeb.UI do
</dl>
"""
end

attr :name, :string, required: true
attr :value, :string, required: true

def card_metric(assigns) do
~H"""
<div class="flex flex-col items-start justify-start gap-2 rounded-lg border border-solid border-gray-200 bg-white px-4 py-2">
<p class="w-full text-center text-xs"><%= @name %></p>
<p class="w-full text-center text-lg font-medium"><%= @value %></p>
</div>
"""
end
end
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
Need to do something meaningful here
<div class="flex w-full flex-col items-start justify-start gap-10">
<header class="flex flex-col items-start justify-start gap-2">
<.h1>Dashboard</.h1>
</header>

<.h2>App metrics</.h2>

<div class="flex w-full flex-row flex-wrap items-start justify-start gap-2">
<.card_metric name="Users" value={@user_count} />
<.card_metric name="Transfers" value={@transfer_count} />
</div>
</div>

Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
defmodule SwapifyApiWeb.AdminDashboardController do
use SwapifyApiWeb, :controller

alias SwapifyApi.Tasks
alias SwapifyApi.Accounts

plug :put_layout, {SwapifyApiWeb.Layouts, :admin}

def index(conn, _), do: conn |> render(:index)
def index(conn, _) do
with {:ok, user_count} <- Accounts.count_users(),
{:ok, transfer_count} <- Tasks.count_transfers() do
conn |> render(:index, user_count: user_count, transfer_count: transfer_count)
end
end
end

0 comments on commit 7cd634c

Please sign in to comment.