Fix sampling in JOIN queries

This commit is contained in:
Uku Taht 2021-08-31 11:13:09 +03:00
parent 77d3d8695d
commit 01412d090f
6 changed files with 30 additions and 15 deletions

View File

@ -60,6 +60,12 @@ defmodule Plausible.Stats.Filters do
end
end)
new_filters =
case new_filters["event:goal"] do
nil -> Map.put(new_filters, "event:name", {:is, "pageview"})
_ -> new_filters
end
%Plausible.Stats.Query{query | filters: new_filters}
end

View File

@ -23,6 +23,8 @@ defmodule Plausible.Stats.Props do
end)
end
else
query = Plausible.Stats.Query.remove_goal(query)
ClickhouseRepo.all(
from e in base_event_query(site, query),
inner_lateral_join: meta in fragment("meta as m"),

View File

@ -176,7 +176,9 @@ defmodule Plausible.Stats.Query do
def treat_page_filter_as_entry_page(%__MODULE__{filters: %{"visit:entry_page" => _}} = q), do: q
def treat_page_filter_as_entry_page(%__MODULE__{filters: %{"event:page" => f}} = q) do
put_filter(q, "visit:entry_page", f)
q
|> put_filter("visit:entry_page", f)
|> put_filter("event:page", nil)
end
def treat_page_filter_as_entry_page(q), do: q
@ -190,6 +192,7 @@ defmodule Plausible.Stats.Query do
query.filters
|> Map.drop(props)
|> Map.delete("event:goal")
|> Map.delete("event:name")
%__MODULE__{query | filters: new_filters}
end

View File

@ -9,12 +9,6 @@ defmodule PlausibleWeb.Api.StatsController do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()
query =
case query.filters["event:goal"] do
nil -> Query.put_filter(query, "event:name", {:is, "pageview"})
_ -> query
end
timeseries_query =
if query.period == "realtime" do
%Query{query | period: "30m"}
@ -329,12 +323,6 @@ defmodule PlausibleWeb.Api.StatsController do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()
query =
case query.filters["event:goal"] do
nil -> Query.put_filter(query, "event:name", {:is, "pageview"})
_ -> query
end
metrics =
if params["detailed"],
do: ["visitors", "pageviews", "bounce_rate", "time_on_page"],

View File

@ -122,6 +122,22 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
%{"name" => "DuckDuckGo", "count" => 1}
]
end
test "shows sources for a page", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/page1", referrer_source: "Google"),
build(:pageview, pathname: "/page2", referrer_source: "Google"),
build(:pageview, user_id: 1, pathname: "/page2", referrer_source: "DuckDuckGo"),
build(:pageview, user_id: 1, pathname: "/page1", referrer_source: "DuckDuckGo")
])
filters = Jason.encode!(%{"page" => "/page1"})
conn = get(conn, "/api/stats/#{site.domain}/sources?filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "Google", "count" => 1}
]
end
end
describe "GET /api/stats/:domain/utm_mediums" do

View File

@ -128,10 +128,10 @@ defmodule Plausible.Workers.CheckUsageTest do
insert(:subscription,
user: user,
paddle_plan_id: @paddle_id_10k,
last_bill_date: Timex.shift(Timex.today(), months: -2, days: -1)
last_bill_date: ~D[2021-06-29]
)
CheckUsage.perform(nil, billing_stub)
CheckUsage.perform(nil, billing_stub, ~D[2021-08-30])
assert_email_delivered_with(
to: [user],