Move hostname filter appending to Breakdown module (#3998)

We currently update the query filtered by hostname to its
respective visit props in some cases.
This patch moves it down, from controllers, to the Breakdown module,
so any changes in logic will be also reflected in the Stats API.
This commit is contained in:
hq1 2024-04-10 09:49:09 +02:00 committed by GitHub
parent aee69e44c8
commit 39fdbb3a67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 21 deletions

View File

@ -195,6 +195,7 @@ defmodule Plausible.Stats.Breakdown do
end end
def breakdown(site, query, property, metrics, pagination, opts) do def breakdown(site, query, property, metrics, pagination, opts) do
query = maybe_update_breakdown_filters(property, query)
if !Keyword.get(opts, :skip_tracing), do: trace(query, property, metrics) if !Keyword.get(opts, :skip_tracing), do: trace(query, property, metrics)
metrics_to_select = Util.maybe_add_visitors_metric(metrics) -- @computed_metrics metrics_to_select = Util.maybe_add_visitors_metric(metrics) -- @computed_metrics
@ -212,6 +213,32 @@ defmodule Plausible.Stats.Breakdown do
end end
end end
defp maybe_update_breakdown_filters("visit:source", query) do
update_hostname(query, "visit:entry_page_hostname")
end
defp maybe_update_breakdown_filters("visit:entry_page", query) do
update_hostname(query, "visit:entry_page_hostname")
end
defp maybe_update_breakdown_filters("visit:exit_page", query) do
update_hostname(query, "visit:exit_page_hostname")
end
defp maybe_update_breakdown_filters(_, query) do
query
end
defp update_hostname(query, visit_prop) do
case query.filters["event:hostname"] do
nil ->
query
some ->
Plausible.Stats.Query.put_filter(query, visit_prop, some)
end
end
# Backwards compatibility # Backwards compatibility
defp breakdown_table(%Query{experimental_reduced_joins?: false}, _, _), do: :session defp breakdown_table(%Query{experimental_reduced_joins?: false}, _, _), do: :session

View File

@ -458,13 +458,6 @@ defmodule PlausibleWeb.Api.StatsController do
query = Query.from(site, params) query = Query.from(site, params)
pagination = parse_pagination(params) pagination = parse_pagination(params)
query =
if query.filters["event:hostname"] do
Query.put_filter(query, "visit:entry_page_hostname", query.filters["event:hostname"])
else
query
end
extra_metrics = extra_metrics =
if params["detailed"], do: [:bounce_rate, :visit_duration], else: [] if params["detailed"], do: [:bounce_rate, :visit_duration], else: []
@ -770,13 +763,6 @@ defmodule PlausibleWeb.Api.StatsController do
pagination = parse_pagination(params) pagination = parse_pagination(params)
metrics = breakdown_metrics(query, [:visits, :visit_duration]) metrics = breakdown_metrics(query, [:visits, :visit_duration])
query =
if query.filters["event:hostname"] do
Query.put_filter(query, "visit:entry_page_hostname", query.filters["event:hostname"])
else
query
end
entry_pages = entry_pages =
Stats.breakdown(site, query, "visit:entry_page", metrics, pagination) Stats.breakdown(site, query, "visit:entry_page", metrics, pagination)
|> transform_keys(%{entry_page: :name}) |> transform_keys(%{entry_page: :name})
@ -807,13 +793,6 @@ defmodule PlausibleWeb.Api.StatsController do
{limit, page} = parse_pagination(params) {limit, page} = parse_pagination(params)
metrics = breakdown_metrics(query, [:visits]) metrics = breakdown_metrics(query, [:visits])
query =
if query.filters["event:hostname"] do
Query.put_filter(query, "visit:exit_page_hostname", query.filters["event:hostname"])
else
query
end
exit_pages = exit_pages =
Stats.breakdown(site, query, "visit:exit_page", metrics, {limit, page}) Stats.breakdown(site, query, "visit:exit_page", metrics, {limit, page})
|> add_exit_rate(site, query, limit) |> add_exit_rate(site, query, limit)