Fix breakdown call

This commit is contained in:
Uku Taht 2021-02-22 14:55:42 +02:00
parent a29ac44434
commit 3c4931598b
2 changed files with 22 additions and 51 deletions

View File

@ -5,24 +5,34 @@ defmodule Plausible.Stats.Breakdown do
@event_metrics ["visitors", "pageviews"]
@session_metrics ["bounce_rate", "visit_duration"]
# use join once this is solved: https://github.com/ClickHouse/ClickHouse/issues/10276
# https://github.com/ClickHouse/ClickHouse/issues/17319
def breakdown(site, query, "event:page", metrics, pagination) do
event_metrics = Enum.filter(metrics, &(&1 in @event_metrics))
session_metrics = Enum.filter(metrics, &(&1 in @session_metrics))
event_result =
breakdown_events(site, query, event_metrics, pagination)
|> Enum.map(fn row -> {row[:page], Map.delete(row, :page)} end)
|> Enum.into(%{})
event_result = breakdown_events(site, query, event_metrics, pagination)
pages = Enum.map(event_result, fn r -> r[:page] end)
session_result =
breakdown(site, query, "visit:entry_page", session_metrics, pagination)
|> Enum.map(fn row -> {row[:entry_page], Map.delete(row, :entry_page)} end)
|> Enum.into(%{})
if Enum.any?(session_metrics) do
session_result =
from(s in query_sessions(site, query),
group_by: s.entry_page,
where: s.entry_page in ^pages,
select: %{entry_page: s.entry_page}
)
|> select_metrics(session_metrics)
|> ClickhouseRepo.all()
Map.merge(event_result, session_result, fn _page, v1, v2 ->
Map.merge(v1, v2)
end)
|> Enum.map(fn {k, v} -> Map.put(v, :page, k) end)
session_metrics_atoms = Enum.map(session_metrics, &String.to_atom/1)
Enum.map(event_result, fn row ->
session_row = Enum.find(session_result, fn row2 -> row2[:entry_page] == row[:page] end)
Map.merge(row, Map.take(session_row, session_metrics_atoms))
end)
else
event_result
end
end
def breakdown(_, _, _, [], _), do: %{}

View File

@ -568,44 +568,5 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
]
}
end
test "just bounce rate for event:page", %{conn: conn, site: site} do
populate_stats([
build(:pageview,
user_id: 1,
pathname: "/",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
user_id: 1,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:10:00]
),
build(:pageview, pathname: "/", domain: site.domain, timestamp: ~N[2021-01-01 00:25:00]),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"metrics" => "bounce_rate"
})
assert json_response(conn, 200) == %{
"results" => [
%{"page" => "/", "bounce_rate" => 50},
%{"page" => "/plausible.io", "bounce_rate" => 100}
]
}
end
end
end