Disable sampling in the API

This commit is contained in:
Uku Taht 2021-08-19 13:13:13 +03:00
parent 5df15e9169
commit 63b7abad58
3 changed files with 30 additions and 9 deletions

View File

@ -30,11 +30,19 @@ defmodule Plausible.Stats.Base do
q =
from(
e in "events",
hints: [sample: query.sample_threshold],
where: e.domain == ^site.domain,
where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime
)
q =
case query.sample_threshold do
"infinite" ->
q
threshold ->
from(e in q, hints: [sample: threshold])
end
q =
case query.filters["event:page"] do
{:is, page} ->

View File

@ -5,6 +5,8 @@ defmodule Plausible.Stats.Query do
filters: %{},
sample_threshold: 10_000_000
@default_sample_threshold 10_000_000
def shift_back(%__MODULE__{period: "month"} = query, site) do
# Querying current month to date
{new_first, new_last} =
@ -42,7 +44,8 @@ defmodule Plausible.Stats.Query do
period: "30m",
interval: "minute",
date_range: Date.range(date, date),
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -53,7 +56,8 @@ defmodule Plausible.Stats.Query do
period: "day",
date_range: Date.range(date, date),
interval: "hour",
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -65,7 +69,8 @@ defmodule Plausible.Stats.Query do
period: "7d",
date_range: Date.range(start_date, end_date),
interval: "date",
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -77,7 +82,8 @@ defmodule Plausible.Stats.Query do
period: "30d",
date_range: Date.range(start_date, end_date),
interval: "date",
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -91,7 +97,8 @@ defmodule Plausible.Stats.Query do
period: "month",
date_range: Date.range(start_date, end_date),
interval: "date",
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -108,7 +115,8 @@ defmodule Plausible.Stats.Query do
period: "6mo",
date_range: Date.range(start_date, end_date),
interval: Map.get(params, "interval", "month"),
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -125,7 +133,8 @@ defmodule Plausible.Stats.Query do
period: "12mo",
date_range: Date.range(start_date, end_date),
interval: Map.get(params, "interval", "month"),
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end
@ -148,7 +157,8 @@ defmodule Plausible.Stats.Query do
period: "custom",
date_range: Date.range(from_date, to_date),
interval: Map.get(params, "interval", "date"),
filters: parse_filters(params)
filters: parse_filters(params),
sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold)
}
end

View File

@ -12,6 +12,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def aggregate(conn, params) do
site = conn.assigns[:site]
params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),
@ -52,6 +53,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def breakdown(conn, params) do
site = conn.assigns[:site]
params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),
@ -124,6 +126,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController do
def timeseries(conn, params) do
site = conn.assigns[:site]
params = Map.put(params, "sample_threshold", "infinite")
with :ok <- validate_period(params),
:ok <- validate_date(params),