Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POF EctoPSQLExtras #191

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ defmodule DemoWeb.Router do
live_dashboard("/dashboard",
metrics: DemoWeb.Telemetry,
env_keys: ["USER", "ROOTDIR"],
metrics_history: {DemoWeb.History, :data, []}
metrics_history: {DemoWeb.History, :data, []},
additional_pages: [
{"ecto_psql_extras",
{Phoenix.LiveDashboard.Pages.EctoPsqlExtrasPage, %{repo: DemoWeb.Repo}}}
]
)
end
end
Expand Down Expand Up @@ -202,13 +206,29 @@ defmodule DemoWeb.Endpoint do
plug DemoWeb.Router
end

# Configures the endpoint
Application.put_env(:phoenix_live_dashboard, DemoWeb.Repo,
database: "PUT_YOUR_OWN_DATABASE",
username: "postgres",
password: "postgres",
hostname: "localhost"
)

defmodule DemoWeb.Repo do
use Ecto.Repo, otp_app: :phoenix_live_dashboard, adapter: Ecto.Adapters.Postgres
end

Application.ensure_all_started(:os_mon)
Application.ensure_all_started(:postgrex)
Application.ensure_all_started(:ecto_sql)
Application.ensure_all_started(:ecto)
Application.put_env(:phoenix, :serve_endpoints, true)

Task.start(fn ->
children = [
{Phoenix.PubSub, [name: Demo.PubSub, adapter: Phoenix.PubSub.PG2]},
{DemoWeb.History, DemoWeb.Telemetry.metrics()},
DemoWeb.Repo,
DemoWeb.Endpoint
]

Expand Down
10 changes: 7 additions & 3 deletions lib/phoenix/live_dashboard/components/tab_bar_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule Phoenix.LiveDashboard.TabBarComponent do
{:ok, render} when is_function(render, 0) ->
tab

{:ok, {component, args}} when is_atom(component) and is_list(args) ->
{:ok, {component, _args}} when is_atom(component) ->
tab

{:ok, _invalid} ->
Expand Down Expand Up @@ -127,7 +127,8 @@ defmodule Phoenix.LiveDashboard.TabBarComponent do
end

defp render_tab_link(socket, page, tab, current, id) do
path = live_dashboard_path(socket, page, tab: id)
params = [tab: id] |> maybe_put(:info, page.params[:info])
path = live_dashboard_path(socket, page.route, page.node, params)
class = "nav-link#{if current == id, do: " active"}"

case tab[:method] do
Expand All @@ -136,10 +137,13 @@ defmodule Phoenix.LiveDashboard.TabBarComponent do
end
end

defp maybe_put(keyword, _key, nil), do: keyword
defp maybe_put(keyword, key, value), do: [{key, value} | keyword]

defp render_content(socket, page, tabs, current) do
case tabs[current][:render] do
{component, component_assigns} ->
live_component(socket, component, [page: page] ++ component_assigns)
live_component(socket, component, Map.put(component_assigns, :page, page))

# Needed for the metrics page, should be removed soon
fun when is_function(fun, 0) ->
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix/live_dashboard/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule Phoenix.LiveDashboard.PageLive do
{:skip, socket}

{false, {:disabled, anchor}} ->
{{:disabled, anchor, nil}, socket}
{{:disabled, anchor}, socket}

{false, {:disabled, anchor, more_info_url}} ->
{{:disabled, anchor, more_info_url}, socket}
Expand Down
263 changes: 263 additions & 0 deletions lib/phoenix/live_dashboard/pages/ecto_psql_extras_page.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
if match?({:module, _}, Code.ensure_compiled(EctoPSQLExtras)) do
{:module, _} = Code.ensure_compiled(Postgrex)

defmodule Phoenix.LiveDashboard.Pages.EctoPsqlExtrasPage do
@moduledoc false
use Phoenix.LiveDashboard.PageBuilder

@menu_text "PsqlExtras"

@impl true
def init(%{repo: repo}) do
{:ok, %{repo: repo}, process: repo}
end

@impl true
def mount(_params, %{repo: repo}, socket) do
{:ok, assign(socket, repo: repo)}
end

@impl true
def menu_link(%{repo: repo}, capabilities) do
if repo in capabilities.processes do
{:ok, @menu_text}
else
{:disabled, @menu_text}
end
end

@impl true
def render_page(assigns) do
tab_bar(tabs: tabs(assigns) |> IO.inspect())
end

@tables [
:cache_hit,
:index_cache_hit,
:table_cache_hit,
:index_usage,
:locks,
:all_locks,
:outliers,
:calls,
:blocking,
:total_index_size,
:index_size,
:table_size,
:table_indexes_size,
:total_table_size,
:unused_indexes,
:seq_scans,
:long_running_queries,
:records_rank,
:bloat,
:vacuum_stats
]
defp tabs(%{repo: repo}) do
for table_name <- @tables do
{table_name,
name: Phoenix.Naming.humanize(table_name), render: render_table(table_name, repo)}
end
end

defp render_table(table, repo) do
table(
columns: table_columns(table),
id: :table_id,
# row_attrs: table_row_attrs(table),
row_fetcher: row_fetcher(table, repo),
title: Phoenix.Naming.humanize(table)
)
end

defp table_columns(:cache_hit) do
[%{field: :name, sortable: true}, %{field: :ratio, sortable: true}]
end

defp table_columns(:index_cache_hit) do
[
%{field: :name, sortable: true},
%{field: :buffer_hits, sortable: true},
%{field: :block_reads, sortable: true},
%{field: :total_read, sortable: true},
%{field: :ratio, sortable: true}
]
end

defp table_columns(:table_cache_hit) do
[
%{field: :name, sortable: true},
%{field: :buffer_hits, sortable: true},
%{field: :block_reads, sortable: true},
%{field: :total_read, sortable: true},
%{field: :ratio, sortable: true}
]
end

defp table_columns(:index_usage) do
[
%{field: :relname, sortable: true},
%{field: :percent_of_times_index_used, sortable: true},
%{field: :rows_in_table, sortable: true}
]
end

defp table_columns(:locks) do
[
%{field: :procpid, sortable: true},
%{field: :relname, sortable: true},
%{field: :transactionid, sortable: true},
%{field: :granted, sortable: true},
%{field: :query_snippet, sortable: true},
%{field: :mode, sortable: true},
%{field: :age, sortable: true}
]
end

defp table_columns(:all_locks) do
[
%{field: :pid, sortable: true},
%{field: :relname, sortable: true},
%{field: :transactionid, sortable: true},
%{field: :granted, sortable: true},
%{field: :query_snippet, sortable: true},
%{field: :mode, sortable: true},
%{field: :age, sortable: true}
]
end

defp table_columns(table) when table in [:outliers, :calls] do
[
%{field: :qry, sortable: true},
%{field: :exec_time, sortable: true},
%{field: :prop_exec_time, sortable: true},
%{field: :ncalls, sortable: true},
%{field: :sync_io_time, sortable: true}
]
end

defp table_columns(:blocking) do
[
%{field: :blocked_pid, sortable: true},
%{field: :blocking_statement, sortable: true},
%{field: :blocking_duration, sortable: true},
%{field: :blocking_pid, sortable: true},
%{field: :blocked_statement, sortable: true},
%{field: :blocked_duration, sortable: true}
]
end

defp table_columns(:total_index_size) do
[
%{field: :size, sortable: true}
]
end

defp table_columns(table)
when table in [
:index_size,
:index_size,
:total_indexes_size,
:table_size,
:total_table_size
] do
[
%{field: :name, sortable: true},
%{field: :size, sortable: true}
]
end

defp table_columns(:table_indexes_size) do
[
%{field: :table, sortable: true},
%{field: :size, sortable: true}
]
end

defp table_columns(:unused_indexes) do
[
%{field: :table, sortable: true},
%{field: :index, sortable: true},
%{field: :index_size, sortable: true},
%{field: :index_scans, sortable: true}
]
end

defp table_columns(:seq_scans) do
[
%{field: :name, sortable: true},
%{field: :count, sortable: true}
]
end

defp table_columns(:long_running_queries) do
[
%{field: :pid, sortable: true},
%{field: :duration, sortable: true},
%{field: :query, sortable: true}
]
end

defp table_columns(:records_rank) do
[
%{field: :name, sortable: true},
%{field: :estimated_count, sortable: true}
]
end

defp table_columns(:bloat) do
[
%{field: :type, sortable: true},
%{field: :schemaname, sortable: true},
%{field: :object_name, sortable: true},
%{field: :bloat, sortable: true},
%{field: :waste, sortable: true}
]
end

defp table_columns(:vacuum_stats) do
[
%{field: :schema, sortable: true},
%{field: :table, sortable: true},
%{field: :last_vacuum, sortable: true},
%{field: :last_autovacuum, sortable: true},
%{field: :rowcount, sortable: true},
%{field: :dead_rowcount, sortable: true},
%{field: :autovacuum_threshold, sortable: true},
%{field: :expect_autovacuum, sortable: true}
]
end

defp row_fetcher(name, repo) do
fn params, node ->
:rpc.call(node, EctoPSQLExtras, :query, [name, repo, :raw])
|> calc_rows(params)
end
end

defp calc_rows(%Postgrex.Result{} = result, params) do
%{search: _search, sort_by: sort_by, sort_dir: sort_dir, limit: limit} = params
sorter = if sort_dir == :asc, do: &<=/2, else: &>=/2
%{columns: columns, rows: rows} = result |> IO.inspect()

rows =
rows
|> Enum.map(&Enum.zip(columns, &1))
|> Enum.map(fn row ->
Map.new(row, fn {key, value} -> {String.to_atom(key), convert_value(value)} end)
end)

count = length(rows)
rows = rows |> Enum.sort_by(&Map.fetch!(&1, sort_by), sorter) |> Enum.take(limit)
{rows, count}
end

defp convert_value(%Decimal{} = decimal) do
Decimal.to_float(decimal)
end

defp convert_value(value) do
value
end
end
end
11 changes: 6 additions & 5 deletions lib/phoenix/live_dashboard/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ defmodule Phoenix.LiveDashboard.Router do

defp normalize_additional_pages(pages) do
Enum.map(pages, fn
module when is_atom(module) ->
{module, []}
{path, module} when is_binary(path) and is_atom(module) ->
{path, {module, %{}}}

{module, args} when is_atom(module) and is_list(args) ->
{module, args}
{path, {module, args}} when is_binary(path) and is_atom(module) ->
{path, {module, args}}

other ->
raise ArgumentError,
"invalid :additional_page, must be a tuple {module, args}, got: " <> inspect(other)
"invalid :additional_page, must be a tuple {path, {module, args}}, got: " <>
inspect(other)
end)
end

Expand Down
3 changes: 3 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ defmodule Phoenix.LiveDashboard.MixProject do
{:phoenix_live_view, "~> 0.14.3", phoenix_live_view_opts()},
{:telemetry_metrics, "~> 0.4.0 or ~> 0.5.0"},
{:phoenix_html, "~> 2.14.1 or ~> 2.15"},
{:ecto_psql_extras, "~> 0.1.4", optional: true},
{:ecto_sql, "~> 3.4", optional: true},
{:postgrex, "~> 0.0", optional: true},
{:circular_buffer, "~> 0.2", only: :dev},
{:telemetry_poller, "~> 0.4", only: :dev},
{:phoenix_live_reload, "~> 1.2", only: :dev},
Expand Down
Loading