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
Memoize has_stats? (#1302)
Uku Taht33ba5d79b635
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.exindex ac3c78643b..3dd8250485 100644--- a/lib/plausible/auth/auth.ex+++ b/lib/plausible/auth/auth.ex@@ -1,7 +1,6 @@defmodule Plausible.Auth douse Plausible.Repoalias Plausible.Auth- alias Plausible.Stats.Clickhouse, as: Statsdef issue_email_verification(user) doRepo.update_all(from(c in "email_verification_codes", where: c.user_id == ^user.id),@@ -68,7 +67,7 @@ defmodule Plausible.Auth doenddef has_active_sites?(user, roles \\ [:owner, :admin, :viewer]) do- domains =+ sites =Repo.all(from u in Plausible.Auth.User,where: u.id == ^user.id,@@ -77,10 +76,10 @@ defmodule Plausible.Auth dowhere: sm.role in ^roles,join: s in Plausible.Site,on: s.id == sm.site_id,- select: s.domain+ select: s)- Stats.has_pageviews?(domains)+ Enum.any?(sites, &Plausible.Sites.has_stats?/1)enddef user_owns_sites?(user) dodiff --git a/lib/plausible/site/schema.ex b/lib/plausible/site/schema.exindex ade0ffc2c8..625c5bcd6d 100644--- a/lib/plausible/site/schema.ex+++ b/lib/plausible/site/schema.ex@@ -10,6 +10,7 @@ defmodule Plausible.Site dofield :timezone, :string, default: "Etc/UTC"field :public, :booleanfield :locked, :boolean+ field :has_stats, :booleanmany_to_many :members, User, join_through: Plausible.Site.Membershiphas_many :memberships, Plausible.Site.Membership@@ -42,6 +43,10 @@ defmodule Plausible.Site dochange(site, public: false)end+ def set_has_stats(site, has_stats_val) do+ change(site, has_stats: has_stats_val)+ end+defp clean_domain(changeset) doclean_domain =(get_field(changeset, :domain) || "")diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.exindex f50f034dcd..e073fc7f1a 100644--- a/lib/plausible/sites.ex+++ b/lib/plausible/sites.ex@@ -26,6 +26,23 @@ defmodule Plausible.Sites doendend+ def has_stats?(site) do+ if site.has_stats do+ true+ else+ has_stats = Plausible.Stats.Clickhouse.has_pageviews?(site)++ if has_stats do+ Plausible.Site.set_has_stats(site, true)+ |> Repo.update()++ true+ else+ false+ end+ end+ end+def create_shared_link(site, name, password \\ nil) dochanges =SharedLink.changeset(diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.exindex 60607f6a83..ac5b2a678f 100644--- a/lib/plausible/stats/clickhouse.ex+++ b/lib/plausible/stats/clickhouse.ex@@ -892,7 +892,9 @@ defmodule Plausible.Stats.Clickhouse doenddef has_pageviews?(site) do- ClickhouseRepo.exists?(from e in "events", where: e.domain == ^site.domain)+ ClickhouseRepo.exists?(+ from e in "events", where: e.domain == ^site.domain and e.name == "pageview"+ )enddef all_props(site, %Query{filters: %{"props" => meta}} = query) when is_map(meta) dodiff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.exindex cdd2023f44..00c0e9ae7d 100644--- a/lib/plausible_web/controllers/stats_controller.ex+++ b/lib/plausible_web/controllers/stats_controller.ex@@ -1,16 +1,15 @@defmodule PlausibleWeb.StatsController douse PlausibleWeb, :controlleruse Plausible.Repo- alias Plausible.Stats.Clickhouse, as: Statsalias Plausible.Stats.Queryplug PlausibleWeb.AuthorizeSiteAccess when action in [:stats, :csv_export]def stats(%{assigns: %{site: site}} = conn, _params) do- has_pageviews = Stats.has_pageviews?(site)+ has_stats = Plausible.Sites.has_stats?(site)cond do- !site.locked && has_pageviews ->+ !site.locked && has_stats ->demo = site.domain == PlausibleWeb.Endpoint.host()offer_email_report = get_session(conn, site.domain <> "_offer_email_report")@@ -26,7 +25,7 @@ defmodule PlausibleWeb.StatsController dodemo: demo)- !site.locked && !has_pageviews ->+ !site.locked && !has_stats ->conn|> assign(:skip_plausible_tracking, true)|> render("waiting_first_pageview.html", site: site)diff --git a/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex b/lib/plausible_web/templates/stats/waiting_first_pageview.html.eexindex b262ca0b7d..e66ebf684f 100644--- a/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex+++ b/lib/plausible_web/templates/stats/waiting_first_pageview.html.eex@@ -10,7 +10,7 @@})}- setInterval(updateStatus, 1500)+ setInterval(updateStatus, 5000)</script><div class="w-full max-w-md mx-auto mt-8">diff --git a/lib/workers/send_site_setup_emails.ex b/lib/workers/send_site_setup_emails.exindex 74c95546c0..d4250aac41 100644--- a/lib/workers/send_site_setup_emails.ex+++ b/lib/workers/send_site_setup_emails.ex@@ -2,7 +2,6 @@ defmodule Plausible.Workers.SendSiteSetupEmails douse Plausible.Repouse Oban.Worker, queue: :site_setup_emailsrequire Logger- alias Plausible.Stats.Clickhouse, as: Stats@impl Oban.Workerdef perform(_job) do@@ -46,7 +45,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails doPlausible.Sites.owner_for(site)|> Repo.preload(:subscription)- setup_completed = Stats.has_pageviews?(site)+ setup_completed = Plausible.Sites.has_stats?(site)hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)if !setup_completed && hours_passed > 47 do@@ -69,7 +68,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails doPlausible.Sites.owner_for(site)|> Repo.preload(:subscription)- if Stats.has_pageviews?(site) do+ if Plausible.Sites.has_stats?(site) dosend_setup_success_email(owner, site)endenddiff --git a/priv/repo/migrations/20210906102736_memoize_setup_complete.exs b/priv/repo/migrations/20210906102736_memoize_setup_complete.exsnew file mode 100644index 0000000000..a1b5ebc0ee--- /dev/null+++ b/priv/repo/migrations/20210906102736_memoize_setup_complete.exs@@ -0,0 +1,9 @@+defmodule Plausible.Repo.Migrations.MemoizeSetupComplete do+ use Ecto.Migration++ def change do+ alter table(:sites) do+ add :has_stats, :boolean, null: false, default: false+ end+ end+enddiff --git a/test/plausible/sites_test.exs b/test/plausible/sites_test.exsindex b4f4be59df..077c694d47 100644--- a/test/plausible/sites_test.exs+++ b/test/plausible/sites_test.exs@@ -1,5 +1,6 @@defmodule Plausible.SitesTest douse Plausible.DataCase+ import Plausible.TestUtilsalias Plausible.Sitesdescribe "is_member?" do@@ -17,4 +18,35 @@ defmodule Plausible.SitesTest dorefute Sites.is_member?(user.id, site)endend++ describe "has_stats?" do+ test "is false if site has no stats" do+ site = insert(:site)++ refute Sites.has_stats?(site)+ end++ test "is true if site has stats" do+ site = insert(:site)++ populate_stats(site, [+ build(:pageview)+ ])++ assert Sites.has_stats?(site)+ end++ test "memoizes has_stats value" do+ site = insert(:site)++ populate_stats(site, [+ build(:pageview)+ ])++ refute site.has_stats++ assert Sites.has_stats?(site)+ assert Repo.reload!(site).has_stats+ end+ endend