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
Apply usage optimization in background job
Uku Taht4eae40cd71ad
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex 2b599fc837..47f71c2784 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -1,7 +1,6 @@defmodule Plausible.Billing douse Plausible.Repoalias Plausible.Billing.{Subscription, PaddleApi}- use Plausible.ClickhouseRepodef active_subscription_for(user_id) doRepo.get_by(Subscription, user_id: user_id, status: "active")@@ -143,25 +142,19 @@ defmodule Plausible.Billing dopageviews + custom_eventsend- defp get_usage_for_billing_cycle(sites, cycle) do- domains = Enum.map(sites, & &1.domain)-- ClickhouseRepo.one(- from e in "events",- where: e.domain in ^domains,- where: fragment("toDate(?)", e.timestamp) >= ^cycle.first,- where: fragment("toDate(?)", e.timestamp) <= ^cycle.last,- select: fragment("count(*)")- )- end-def last_two_billing_months_usage(user, today \\ Timex.today()) do{first, second} = last_two_billing_cycles(user, today)sites = Plausible.Sites.owned_by(user)+ usage_for_sites = fn sites, date_range ->+ domains = Enum.map(sites, & &1.domain)+ {pageviews, custom_events} = Plausible.Stats.Clickhouse.usage_breakdown(domains, date_range)+ pageviews + custom_events+ end+{- get_usage_for_billing_cycle(sites, first),- get_usage_for_billing_cycle(sites, second)+ usage_for_sites.(sites, first),+ usage_for_sites.(sites, second)}enddiff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.exindex 393a455087..8a44d9bda1 100644--- a/lib/plausible/stats/clickhouse.ex+++ b/lib/plausible/stats/clickhouse.ex@@ -175,16 +175,24 @@ defmodule Plausible.Stats.Clickhouse doenddef usage_breakdown(domains) do- q = Plausible.Stats.Query.from("UTC", %{"period" => "30d"})- {first_datetime, last_datetime} = utc_boundaries(q, "UTC")+ range =+ Date.range(+ Timex.shift(Timex.today(), days: -30),+ Timex.today()+ )++ usage_breakdown(domains, range)+ end+ def usage_breakdown(domains, date_range) doEnum.chunk_every(domains, 300)|> Enum.reduce({0, 0}, fn domains, {pageviews_total, custom_events_total} ->{chunk_pageviews, chunk_custom_events} =ClickhouseRepo.one(from e in "events",where: e.domain in ^domains,- where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime,+ where: fragment("toDate(?)", e.timestamp) >= ^date_range.first,+ where: fragment("toDate(?)", e.timestamp) <= ^date_range.last,select: {fragment("countIf(? = 'pageview')", e.name),fragment("countIf(? != 'pageview')", e.name)