Skip to content

Instantly share code, notes, and snippets.

@vegg89
Last active August 20, 2019 16:59
Show Gist options
  • Save vegg89/130278d0a1c0adec851489dc52bce59a to your computer and use it in GitHub Desktop.
Save vegg89/130278d0a1c0adec851489dc52bce59a to your computer and use it in GitHub Desktop.
increase decrease implementation
defmodule PhoenixObserverWeb.ObserverLive do
use Phoenix.LiveView
alias PhoenixObserverWeb.PageView
alias PhoenixObserver.Format
@initial_settings %{
timer_refresh_rate: 1000,
timer: nil,
system_info: %{
proc_count: 0,
proc_limit: 0,
smp_support: false,
mem_allocated: "0 MB",
port_count: 0,
port_limit: 0,
multi_scheduling: "disabled",
mem_used: "0 MB",
mem_used_perc: "0%",
atom_count: 0,
atom_limit: 0,
logical_processors: 0,
mem_unused: "0 MB",
mem_unused_perc: "0%"
}
}
def render(assigns) do
PageView.render("observer_live.html", assigns)
end
def mount(_session, socket) do
timer =
if connected?(socket),
do: set_timer(@initial_settings.timer, @initial_settings.timer_refresh_rate)
{:ok, set_initial_assigns(socket, timer)}
end
def handle_info(:update_info, socket) do
socket =
socket
|> assign(system_info: system_info())
{:noreply, socket}
end
def handle_event(
"refresh_rate_increase",
_path,
socket = %{assigns: %{timer: timer, timer_refresh_rate: timer_refresh_rate}}
) do
socket =
socket
|> assign(timer: set_timer(timer, timer_refresh_rate + 50))
|> assign(timer_refresh_rate: timer_refresh_rate + 50)
{:noreply, socket}
end
def handle_event(
"refresh_rate_decrease",
_path,
socket = %{assigns: %{timer_refresh_rate: timer_refresh_rate}}
)
when timer_refresh_rate <= 200,
do: {:noreply, socket}
def handle_event(
"refresh_rate_decrease",
_path,
socket = %{assigns: %{timer: timer, timer_refresh_rate: timer_refresh_rate}}
) do
socket =
socket
|> assign(timer: set_timer(timer, timer_refresh_rate - 50))
|> assign(timer_refresh_rate: timer_refresh_rate - 50)
{:noreply, socket}
end
defp set_initial_assigns(socket, timer) do
socket
|> assign(timer_refresh_rate: @initial_settings.timer_refresh_rate)
|> assign(timer: timer)
|> assign(system_info: @initial_settings.system_info)
end
defp set_timer(prev_timer, new_refresh_rate) do
if prev_timer, do: :timer.cancel(prev_timer)
case :timer.send_interval(new_refresh_rate, self(), :update_info) do
{:ok, timer} ->
timer
_ ->
nil
end
end
defp system_info do
mem_allocated = :recon_alloc.memory(:allocated)
mem_used = :recon_alloc.memory(:used)
mem_unused = mem_allocated - mem_used
mem_used_perc = Format.to_percentage(mem_used / mem_allocated)
mem_unused_perc = Format.to_percentage(mem_unused / mem_allocated)
mem_allocated = Format.number_to_human_size(mem_allocated)
mem_used = Format.number_to_human_size(mem_used)
mem_unused = Format.number_to_human_size(mem_unused)
%{
proc_count: :erlang.system_info(:process_count),
proc_limit: :erlang.system_info(:process_limit),
smp_support: :erlang.system_info(:smp_support),
mem_allocated: mem_allocated,
port_count: :erlang.system_info(:port_count),
port_limit: :erlang.system_info(:port_limit),
multi_scheduling: :erlang.system_info(:multi_scheduling),
mem_used: mem_used,
mem_used_perc: mem_used_perc,
atom_count: :erlang.system_info(:atom_count),
atom_limit: :erlang.system_info(:atom_limit),
logical_processors: :erlang.system_info(:logical_processors),
mem_unused: mem_unused,
mem_unused_perc: mem_unused_perc
}
end
end
<div class="interval-buttons">
<span>Interval:</span>
<span><%= @timer_refresh_rate %> ms</span>
<button class="button button-outline" phx-click="refresh_rate_decrease">-</button>
<button class="button button-outline" phx-click="refresh_rate_increase">+</button>
</div>
<h1>General System Information</h1>
<hr />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment