Refactor filter suggestions with a more DRY approach (#4090)

* refactor filter suggestions with a more DRY approach

* Avoid DRYing props string->atom translation

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
This commit is contained in:
RobertJoonas 2024-05-10 08:12:02 +01:00 committed by GitHub
parent 4e7e932a75
commit 370aeb4b02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -210,132 +210,42 @@ defmodule Plausible.Stats.FilterSuggestions do
"operating_system_version" -> :operating_system_version
"screen_size" -> :screen_size
"hostname" -> :hostname
_ -> :unknown
end
q =
if(filter_name == :pathname or filter_name == :hostname,
do: base_event_query(site, query),
else: query_sessions(site, query)
)
|> from(
group_by: ^filter_name,
order_by: [desc: fragment("count(*)")],
limit: 25
)
q =
case filter_name do
:pathname ->
from(e in q,
select: e.pathname,
where: fragment("? ilike ?", e.pathname, ^filter_query)
)
:hostname ->
q =
from(e in q,
select: e.hostname,
where: fragment("? ilike ?", e.hostname, ^filter_query)
)
case Plausible.Shields.allowed_hostname_patterns(site.domain) do
:all ->
q
limited_to when is_list(limited_to) ->
from(e in q,
where: fragment("multiMatchAny(?, ?)", e.hostname, ^limited_to)
)
end
:entry_page ->
from(e in q,
select: e.entry_page,
where: fragment("? ilike ?", e.entry_page, ^filter_query)
)
:exit_page ->
from(e in q,
select: e.exit_page,
where: fragment("? ilike ?", e.exit_page, ^filter_query)
)
:referrer_source ->
from(e in q,
select: e.referrer_source,
where: fragment("? ilike ?", e.referrer_source, ^filter_query)
)
:utm_medium ->
from(e in q,
select: e.utm_medium,
where: fragment("? ilike ?", e.utm_medium, ^filter_query)
)
:utm_source ->
from(e in q,
select: e.utm_source,
where: fragment("? ilike ?", e.utm_source, ^filter_query)
)
:utm_campaign ->
from(e in q,
select: e.utm_campaign,
where: fragment("? ilike ?", e.utm_campaign, ^filter_query)
)
:utm_content ->
from(e in q,
select: e.utm_content,
where: fragment("? ilike ?", e.utm_content, ^filter_query)
)
:utm_term ->
from(e in q,
select: e.utm_term,
where: fragment("? ilike ?", e.utm_term, ^filter_query)
)
:referrer ->
from(e in q,
select: e.referrer,
where: fragment("? ilike ?", e.referrer, ^filter_query)
)
:browser ->
from(e in q, select: e.browser, where: fragment("? ilike ?", e.browser, ^filter_query))
:browser_version ->
from(e in q,
select: e.browser_version,
where: fragment("? ilike ?", e.browser_version, ^filter_query)
)
:operating_system ->
from(e in q,
select: e.operating_system,
where: fragment("? ilike ?", e.operating_system, ^filter_query)
)
:operating_system_version ->
from(e in q,
select: e.operating_system_version,
where: fragment("? ilike ?", e.operating_system_version, ^filter_query)
)
:screen_size ->
from(e in q,
select: e.screen_size,
where: fragment("? ilike ?", e.screen_size, ^filter_query)
)
base_q =
if filter_name in [:pathname, :hostname] do
base_event_query(site, query)
else
query_sessions(site, query)
end
ClickhouseRepo.all(q)
from(e in base_q,
where: fragment("? ilike ?", field(e, ^filter_name), ^filter_query),
select: field(e, ^filter_name),
group_by: ^filter_name,
order_by: [desc: fragment("count(*)")],
limit: 25
)
|> apply_additional_filters(filter_name, site)
|> ClickhouseRepo.all()
|> Enum.filter(fn suggestion -> suggestion != "" end)
|> wrap_suggestions()
end
defp apply_additional_filters(q, :hostname, site) do
case Plausible.Shields.allowed_hostname_patterns(site.domain) do
:all ->
q
limited_to when is_list(limited_to) ->
from(e in q,
where: fragment("multiMatchAny(?, ?)", e.hostname, ^limited_to)
)
end
end
defp apply_additional_filters(q, _, _), do: q
defp wrap_suggestions(list) do
Enum.map(list, fn val -> %{value: val, label: val} end)
end