PoC: plot visitors on sites index

pull/3463/head
Adam Rutkowski 3 years ago committed by Adrian Gruntkowski
parent 0918492d48
commit 5d7fe64a92

@ -0,0 +1,42 @@
defmodule PlausibleWeb.Live.Components.Visitors do
use Phoenix.Component
# Proof of concept, TODOs:
# - make it asynchronous,
# - move under Live.Components or .Components depending on how it turns out,
# - make it render once per site - currently both mounts call it
attr :site, Plausible.Site, required: true
attr :height, :integer, default: 50
def chart(assigns) do
site = assigns.site
q = Plausible.Stats.Query.from(site, %{"period" => "day", "interval" => "hour"})
points =
site
|> Plausible.Stats.timeseries(q, [:visitors])
|> scale(assigns.height)
|> Enum.with_index(fn scaled_value, index -> "#{(index - 1) * 20},#{scaled_value}" end)
|> Enum.join(" ")
assigns = assign(assigns, :points, points)
~H"""
<svg viewBox={"0 0 #{24 * 20} #{@height + 1}"} class="chart w-full mb-2">
<polyline fill="none" stroke="#6366f1" stroke-width="3" points={@points} />
</svg>
"""
end
defp scale(data, target_range) do
max_value = Enum.max_by(data, fn %{visitors: visitors} -> visitors end)
scaling_factor =
if max_value.visitors > 0, do: div(target_range, max_value.visitors), else: 0
Enum.map(data, fn %{visitors: visitors} ->
target_range - visitors * scaling_factor
end)
end
end

@ -144,6 +144,7 @@ defmodule PlausibleWeb.Live.Sites do
<div class="pl-8 mt-2 flex items-center justify-between">
<span class="text-gray-600 dark:text-gray-400 text-sm truncate">
<span class="text-gray-800 dark:text-gray-200">
<PlausibleWeb.Live.Components.Visitors.chart site={site} />
<b>
<%= PlausibleWeb.StatsView.large_number_format(
Map.get(@visitors, site.domain, 0)

Loading…
Cancel
Save