diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 2b599fc83..47f71c278 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -1,7 +1,6 @@ defmodule Plausible.Billing do use Plausible.Repo alias Plausible.Billing.{Subscription, PaddleApi} - use Plausible.ClickhouseRepo def active_subscription_for(user_id) do Repo.get_by(Subscription, user_id: user_id, status: "active") @@ -143,25 +142,19 @@ defmodule Plausible.Billing do pageviews + custom_events end - 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) } end diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index 393a45508..8a44d9bda 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -175,16 +175,24 @@ defmodule Plausible.Stats.Clickhouse do end def 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) do Enum.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)