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
Bootstrap the interface for consolidated sites (#5739)
RobertJoonas653aa6587719
diff --git a/extra/lib/plausible/consolidated_view.ex b/extra/lib/plausible/consolidated_view.exnew file mode 100644index 0000000000..ced2465b16--- /dev/null+++ b/extra/lib/plausible/consolidated_view.ex@@ -0,0 +1,83 @@+defmodule Plausible.ConsolidatedView do+ @moduledoc """+ Contextual interface for consolidated views,+ each implemented as Site object serving as+ pointers to team's regular sites.+ """++ use Plausible++ import Ecto.Query++ alias Plausible.Teams.Team+ alias Plausible.{Repo, Site}++ @spec enable(Team.t()) :: {:ok, Site.t()} | {:error, :upgrade_required}+ def enable(%Team{} = team) do+ if eligible?(team) do+ do_enable(team)+ else+ {:error, :upgrade_required}+ end+ end++ @spec disable(Team.t()) :: :ok+ def disable(%Team{} = team) do+ from(s in Site, where: s.consolidated and s.domain == ^make_id(team))+ |> Plausible.Repo.delete_all()++ :ok+ end++ @spec site_ids(Team.t()) :: [pos_integer()] | {:error, :not_found}+ def site_ids(%Team{} = team) do+ case get(team) do+ nil -> {:error, :not_found}+ _found -> {:ok, owned_site_ids(team)}+ end+ end++ @spec get(Team.t() | String.t()) :: Site.t() | nil+ def get(team_or_id)++ def get(%Team{} = team) do+ team |> make_id() |> get()+ end++ def get(id) when is_binary(id) do+ Repo.get_by(Site, domain: id, consolidated: true)+ end++ defp do_enable(%Team{} = team) do+ case get(team) do+ nil ->+ Site.new_for_team(team, %{consolidated: true, domain: make_id(team)})+ |> Repo.insert()++ cv ->+ {:ok, cv}+ end+ end++ defp make_id(%Team{} = team) do+ team.identifier+ end++ # TODO: Only active trials and business subscriptions should be eligible.+ # This function should call a new underlying feature module.+ defp eligible?(%Team{}), do: always(true)++ # TEMPORARY: Will be replaced with `Teams.owned_site_ids` once it starts+ # filtering out consolidated sites. There are many other list/count all+ # sites queries for which there will probably be a dedicated DB view that+ # applies the where clause.+ defp owned_site_ids(team) do+ Repo.all(+ from(s in Site,+ where: s.team_id == ^team.id and not s.consolidated,+ select: s.id,+ order_by: [desc: s.id]+ )+ )+ end+enddiff --git a/lib/plausible/site.ex b/lib/plausible/site.exindex 55b85d8a41..05470468a2 100644--- a/lib/plausible/site.ex+++ b/lib/plausible/site.ex@@ -101,9 +101,15 @@ defmodule Plausible.Site do"""end+ on_ee do+ @changeset_cast_fields [:domain, :consolidated, :timezone, :legacy_time_on_page_cutoff]+ else+ @changeset_cast_fields [:domain, :timezone, :legacy_time_on_page_cutoff]+ end+def changeset(site, attrs \\ %{}) dosite- |> cast(attrs, [:domain, :timezone, :legacy_time_on_page_cutoff])+ |> cast(attrs, @changeset_cast_fields)|> clean_domain()|> validate_required([:domain, :timezone])|> validate_timezone()diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.exindex c97f37f24e..0ffb65f089 100644--- a/lib/plausible/sites.ex+++ b/lib/plausible/sites.ex@@ -12,6 +12,20 @@ defmodule Plausible.Sites dorequire Plausible.Site.UserPreference+ on_ee do+ @spec regular?(Site.t()) :: boolean()+ def regular?(%Site{} = site), do: not site.consolidated++ @spec consolidated?(Site.t()) :: boolean()+ def consolidated?(%Site{} = site), do: site.consolidated+ else+ @spec regular?(Site.t()) :: boolean()+ def regular?(%Site{}), do: true++ @spec consolidated?(Site.t()) :: boolean()+ def consolidated?(%Site{}), do: false+ end+@shared_link_special_names ["WordPress - Shared Dashboard"]@doc """Special shared link names are used to distinguish between thosediff --git a/test/plausible/consolidated_view_test.exs b/test/plausible/consolidated_view_test.exsnew file mode 100644index 0000000000..3fa5153009--- /dev/null+++ b/test/plausible/consolidated_view_test.exs@@ -0,0 +1,88 @@+defmodule Plausible.ConsolidatedViewTest do+ use Plausible++ on_ee do+ use Plausible.DataCase, async: true+ import Ecto.Query+ alias Plausible.ConsolidatedView++ describe "enable/1" do+ setup [:create_user, :create_team]++ test "creates and persists a new consolidated site instance", %{team: team} do+ assert {:ok, %Plausible.Site{consolidated: true}} = ConsolidatedView.enable(team)+ assert ConsolidatedView.get(team)+ end++ test "is idempotent", %{team: team} do+ assert {:ok, s1} = ConsolidatedView.enable(team)+ assert {:ok, s2} = ConsolidatedView.enable(team)++ assert 1 =+ from(s in Plausible.Site, where: s.team_id == ^team.id)+ |> Plausible.Repo.aggregate(:count)++ assert s1.domain == s2.domain+ end++ @tag :skip+ test "returns {:error, :upgrade_required} when team ineligible for this feature"+ end++ describe "disable/1" do+ setup [:create_user, :create_team]++ setup %{team: team} do+ ConsolidatedView.enable(team)+ :ok+ end++ test "deletes an existing consolidated site instance", %{team: team} do+ assert ConsolidatedView.get(team)++ assert :ok = ConsolidatedView.disable(team)++ refute ConsolidatedView.get(team)+ end++ test "is idempotent", %{team: team} do+ assert :ok = ConsolidatedView.disable(team)+ assert :ok = ConsolidatedView.disable(team)++ refute ConsolidatedView.get(team)+ end+ end++ describe "site_ids/1" do+ setup [:create_user, :create_team, :create_site]++ test "returns {:error, :not_found} when no consolidated view exists", %{team: team} do+ assert {:error, :not_found} = ConsolidatedView.site_ids(team)+ end++ test "returns site_ids owned by the team when consolidated view exists", %{+ team: team,+ site: site+ } do+ ConsolidatedView.enable(team)+ assert ConsolidatedView.site_ids(team) == {:ok, [site.id]}+ end+ end++ describe "get/1" do+ setup [:create_user, :create_team]++ test "can get by team", %{team: team} do+ assert is_nil(ConsolidatedView.get(team))+ ConsolidatedView.enable(team)+ assert %Plausible.Site{} = ConsolidatedView.get(team)+ end++ test "can get by team.identifier", %{team: team} do+ assert is_nil(ConsolidatedView.get(team.identifier))+ ConsolidatedView.enable(team)+ assert %Plausible.Site{} = ConsolidatedView.get(team.identifier)+ end+ end+ end+enddiff --git a/test/plausible/site/sites_test.exs b/test/plausible/site/sites_test.exsindex 75d5fb79f4..6c7bdb5512 100644--- a/test/plausible/site/sites_test.exs+++ b/test/plausible/site/sites_test.exs@@ -922,4 +922,23 @@ defmodule Plausible.SitesTest doassert {:error, :too_many_pins} = Sites.toggle_pin(user, site)endend++ describe "predicates for site.consolidated" do+ on_ee do+ test "regular?/1" do+ assert Sites.regular?(%Plausible.Site{consolidated: false})+ refute Sites.regular?(%Plausible.Site{consolidated: true})+ end++ test "consolidated?/1" do+ assert Sites.consolidated?(%Plausible.Site{consolidated: true})+ refute Sites.consolidated?(%Plausible.Site{consolidated: false})+ end+ else+ test "all sites are regular on CE" do+ assert Sites.regular?(%Plausible.Site{})+ refute Sites.consolidated?(%Plausible.Site{})+ end+ end+ endend