defmodule VigilWeb.PerformanceComponents do
@moduledoc """
Pieces the performance pages share: duration, throughput and error-rate
formatting, a transaction's op and method badges, and a stat tile.
"""
use Phoenix.Component
@doc "A duration in ms for people: 850µs, 4.2ms, 120ms, 1.35s, 2m 5s; nil is a dash."
def duration(nil), do: "—"
def duration(ms) when ms < 1, do: "#{round(ms * 1000)}µs"
def duration(ms) when ms < 10, do: "#{:erlang.float_to_binary(ms / 1, decimals: 1)}ms"
def duration(ms) when ms < 1000, do: "#{round(ms)}ms"
def duration(ms) when ms < 60_000,
do: "#{:erlang.float_to_binary(ms / 1000, decimals: if(ms < 10_000, do: 2, else: 1))}s"
def duration(ms) do
s = round(ms / 1000)
"#{div(s, 60)}m #{rem(s, 60)}s"
end
@doc "Transactions a minute: 0, <0.01, 0.42, 12.3, 120."
def tpm(t) when t == 0, do: "0"
def tpm(t) when t < 0.01, do: "<0.01"
def tpm(t) when t < 10, do: :erlang.float_to_binary(t / 1, decimals: 2)
def tpm(t) when t < 100, do: :erlang.float_to_binary(t / 1, decimals: 1)
def tpm(t), do: Integer.to_string(round(t))
@doc "An error rate (0..1) as a percentage."
def rate(r) when r == 0, do: "0%"
def rate(r) when r < 0.001, do: "<0.1%"
def rate(r), do: "#{:erlang.float_to_binary(r * 100, decimals: 1)}%"
@doc "Whether a span's status means it failed."
def failed?(status), do: status not in [nil, "", "ok", "unset"]
attr :group, :map, required: true
@doc "A transaction group's method and op, as small badges."
def txn_badges(assigns) do
~H"""
<span
:if={@group.method not in [nil, ""]}
class="badge badge-outline badge-xs font-mono"
>
{@group.method}
</span>
<span :if={@group.op not in [nil, ""]} class="badge badge-ghost badge-xs font-mono">
{@group.op}
</span>
"""
end
attr :id, :string, required: true
attr :label, :string, required: true
attr :hint, :string, default: nil
attr :tone, :atom, default: nil, values: [nil, :error]
slot :inner_block, required: true
@doc "A stat tile."
def stat(assigns) do
~H"""
<div
id={@id}
class="rounded-box border border-base-300 bg-base-100 px-4 py-3 transition hover:border-base-content/20"
title={@hint}
>
<dt class="text-xs font-medium uppercase tracking-wide text-base-content/60">{@label}</dt>
<dd class={["mt-1 text-lg font-semibold tabular-nums", @tone == :error && "text-error"]}>
{render_slot(@inner_block)}
</dd>
</div>
"""
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
⭐
Log in to mark this repository.
2.5 KiB · text
History
bbb2a48