Apply usage optimization in background job

This commit is contained in:
Uku Taht 2021-10-22 12:20:11 +02:00
parent 26e34596c8
commit 4eae40cd71
2 changed files with 19 additions and 18 deletions

View File

@ -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

View File

@ -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)