Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add usage progress bar for plan limits (#6058)
Sanne de Vriesa740b7ea2ec1
diff --git a/lib/plausible_web/components/billing/billing.ex b/lib/plausible_web/components/billing/billing.exindex 1afbe02501..61c13a2b24 100644--- a/lib/plausible_web/components/billing/billing.ex+++ b/lib/plausible_web/components/billing/billing.ex@@ -220,6 +220,76 @@ defmodule PlausibleWeb.Components.Billing do"""end+ @doc """+ Renders a usage progress bar with color coding based on usage percentage.++ Color scheme:+ - 0-90%: Green (healthy usage)+ - 91-99%: Gradient from green through yellow to orange (approaching limit)+ - 100%: Gradient from green through orange to red (at limit)+ """+ attr(:title, :string, required: true)+ attr(:usage, :integer, required: true)+ attr(:limit, :any, required: true)+ attr(:rest, :global)++ def usage_progress_row(assigns) do+ percentage = calculate_percentage(assigns.usage, assigns.limit)++ assigns =+ assigns+ |> assign(:percentage, percentage)+ |> assign(:color_class, progress_bar_color_from_percentage(percentage, assigns.limit))++ ~H"""+ <div class="flex flex-col gap-y-2" {@rest}>+ <div class="w-full bg-gray-200 rounded-full h-1.5 dark:bg-gray-700">+ <div+ class={["h-1.5 rounded-full transition-all duration-300", @color_class]}+ style={"width: #{@percentage}%"}+ >+ </div>+ </div>+ <div class="flex justify-between items-center text-sm font-medium text-gray-900 dark:text-gray-100">+ <span>+ {@title}+ </span>+ <span>+ {PlausibleWeb.TextHelpers.number_format(@usage)}+ {if is_number(@limit), do: "/ #{PlausibleWeb.TextHelpers.number_format(@limit)}"}+ {if @limit == :unlimited, do: "/ Unlimited"}+ </span>+ </div>+ </div>+ """+ end++ defp calculate_percentage(_usage, :unlimited), do: 0+ defp calculate_percentage(_usage, 0), do: 0++ defp calculate_percentage(usage, limit) when is_number(limit) do+ percentage = usage / limit * 100+ min(percentage, 100.0) |> Float.round(1)+ end++ defp progress_bar_color_from_percentage(_percentage, :unlimited),+ do: "bg-green-500 dark:bg-green-600"++ defp progress_bar_color_from_percentage(_percentage, 0), do: "bg-gray-200 dark:bg-gray-700"++ defp progress_bar_color_from_percentage(percentage, _limit) when is_number(percentage) do+ cond do+ percentage >= 100.0 ->+ "bg-gradient-to-r from-green-500 via-orange-500 via-[80%] to-red-500 dark:from-green-600 dark:via-orange-600 dark:to-red-600"++ percentage >= 91 ->+ "bg-gradient-to-r from-green-500 via-yellow-500 via-[80%] to-orange-500 dark:from-green-600 dark:via-yellow-600 dark:to-orange-600"++ true ->+ "bg-green-500 dark:bg-green-600"+ end+ end+def monthly_quota_box(assigns) do~H"""<divdiff --git a/test/plausible_web/components/billing/billing_test.exs b/test/plausible_web/components/billing/billing_test.exsindex e201ca53ec..c254231067 100644--- a/test/plausible_web/components/billing/billing_test.exs+++ b/test/plausible_web/components/billing/billing_test.exs@@ -119,4 +119,95 @@ defmodule PlausibleWeb.Components.BillingTest do"""|> rendered_to_string()end++ describe "usage_progress_row/1" do+ test "renders progress bar with green color at 0% usage" do+ html = render_progress_row(0, 10_000)++ assert html =~ "0"+ assert html =~ "10,000"+ assert html =~ "width: 0"+ assert html =~ "bg-green-500 dark:bg-green-600"+ end++ test "renders progress bar with green color at 50% usage" do+ html = render_progress_row(5_000, 10_000)++ assert html =~ "5,000"+ assert html =~ "10,000"+ assert html =~ "width: 50.0%"+ assert html =~ "bg-green-500 dark:bg-green-600"+ end++ test "renders progress bar with green color at 90% usage" do+ html = render_progress_row(9_000, 10_000)++ assert html =~ "9,000"+ assert html =~ "10,000"+ assert html =~ "width: 90.0%"+ assert html =~ "bg-green-500 dark:bg-green-600"+ end++ test "renders progress bar with gradient at 95% usage" do+ html = render_progress_row(9_500, 10_000)++ assert html =~ "9,500"+ assert html =~ "10,000"+ assert html =~ "width: 95.0%"+ assert html =~ "bg-gradient-to-r from-green-500 via-yellow-500 via-[80%] to-orange-500"+ end++ test "renders progress bar with red gradient at 100% usage" do+ html = render_progress_row(10_000, 10_000)++ assert html =~ "10,000"+ assert html =~ "width: 100.0%"+ assert html =~ "bg-gradient-to-r from-green-500 via-orange-500 via-[80%] to-red-500"+ end++ test "caps percentage at 100% when usage exceeds limit" do+ html = render_progress_row(15_000, 10_000)++ assert html =~ "15,000"+ assert html =~ "10,000"+ assert html =~ "width: 100.0%"+ assert html =~ "bg-gradient-to-r from-green-500 via-orange-500 via-[80%] to-red-500"+ end++ test "handles unlimited limit" do+ html = render_progress_row(5_000, :unlimited)++ assert html =~ "5,000"+ assert html =~ "Unlimited"+ assert html =~ "width: 0%"+ assert html =~ "bg-green-500 dark:bg-green-600"+ end++ test "handles zero limit" do+ html = render_progress_row(0, 0)++ assert html =~ "0"+ assert html =~ "width: 0%"+ assert html =~ "bg-gray-200 dark:bg-gray-700"+ end++ test "includes title in output" do+ html = render_progress_row(100, 1000, "Test title")++ assert html =~ "Test title"+ end+ end++ defp render_progress_row(usage, limit, title \\ "Pageviews") do+ assigns = %{title: title, usage: usage, limit: limit}++ ~H"""+ <PlausibleWeb.Components.Billing.usage_progress_row+ title={@title}+ usage={@usage}+ limit={@limit}+ />+ """+ |> rendered_to_string()+ endend