From 5d7fe64a92f9cb7f6d3876b8b180ba4a583e2269 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 24 Oct 2023 15:29:00 +0200 Subject: [PATCH] PoC: plot visitors on sites index --- .../live/components/visitors24.ex | 42 +++++++++++++++++++ lib/plausible_web/live/sites.ex | 1 + 2 files changed, 43 insertions(+) create mode 100644 lib/plausible_web/live/components/visitors24.ex diff --git a/lib/plausible_web/live/components/visitors24.ex b/lib/plausible_web/live/components/visitors24.ex new file mode 100644 index 000000000..1b737a66e --- /dev/null +++ b/lib/plausible_web/live/components/visitors24.ex @@ -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""" + + + + """ + 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 diff --git a/lib/plausible_web/live/sites.ex b/lib/plausible_web/live/sites.ex index fa053656d..578218c00 100644 --- a/lib/plausible_web/live/sites.ex +++ b/lib/plausible_web/live/sites.ex @@ -144,6 +144,7 @@ defmodule PlausibleWeb.Live.Sites do
+ <%= PlausibleWeb.StatsView.large_number_format( Map.get(@visitors, site.domain, 0)