Use ALIAS columns to simplify querying API (#4059)

* Cleanup: remove @api_prop_name_to_db-related cruft

* Use aliases elsewhere in code
This commit is contained in:
Karl-Aksel Puulmann 2024-05-05 12:03:00 +03:00 committed by GitHub
parent 17f812443d
commit 035cb3a379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 43 deletions

View File

@ -150,25 +150,10 @@ defmodule Plausible.Stats.Base do
|> filter_by_visit_props(Filters.visit_props(), query)
end
@api_prop_name_to_db %{
"source" => "referrer_source",
"device" => "screen_size",
"screen" => "screen_size",
"os" => "operating_system",
"os_version" => "operating_system_version",
"country" => "country_code",
"region" => "subdivision1_code",
"city" => "city_geoname_id",
"entry_page_hostname" => "hostname"
}
defp filter_by_visit_props(q, visit_props, query) do
Enum.reduce(visit_props, q, fn prop_name, sessions_q ->
filter_key = "visit:" <> prop_name
db_field =
Map.get(@api_prop_name_to_db, prop_name, prop_name)
|> String.to_existing_atom()
db_field = String.to_existing_atom(prop_name)
from(s in sessions_q,
where: ^dynamic_filter_condition(query, filter_key, db_field)
@ -431,7 +416,7 @@ defmodule Plausible.Stats.Base do
end
end
defp db_field_val(:referrer_source, @no_ref), do: ""
defp db_field_val(:source, @no_ref), do: ""
defp db_field_val(:referrer, @no_ref), do: ""
defp db_field_val(:utm_medium, @no_ref), do: ""
defp db_field_val(:utm_source, @no_ref), do: ""

View File

@ -476,41 +476,41 @@ defmodule Plausible.Stats.Breakdown do
defp do_group_by(q, "visit:source") do
from(
s in q,
group_by: s.referrer_source,
group_by: s.source,
select_merge: %{
source: fragment("if(empty(?), ?, ?)", s.referrer_source, @no_ref, s.referrer_source)
source: fragment("if(empty(?), ?, ?)", s.source, @no_ref, s.source)
},
order_by: {:asc, s.referrer_source}
order_by: {:asc, s.source}
)
end
defp do_group_by(q, "visit:country") do
from(
s in q,
where: s.country_code != "\0\0" and s.country_code != "ZZ",
group_by: s.country_code,
select_merge: %{country: s.country_code},
order_by: {:asc, s.country_code}
where: s.country != "\0\0" and s.country != "ZZ",
group_by: s.country,
select_merge: %{country: s.country},
order_by: {:asc, s.country}
)
end
defp do_group_by(q, "visit:region") do
from(
s in q,
where: s.subdivision1_code != "",
group_by: s.subdivision1_code,
select_merge: %{region: s.subdivision1_code},
order_by: {:asc, s.subdivision1_code}
where: s.region != "",
group_by: s.region,
select_merge: %{region: s.region},
order_by: {:asc, s.region}
)
end
defp do_group_by(q, "visit:city") do
from(
s in q,
where: s.city_geoname_id != 0,
group_by: s.city_geoname_id,
select_merge: %{city: s.city_geoname_id},
order_by: {:asc, s.city_geoname_id}
where: s.city != 0,
group_by: s.city,
select_merge: %{city: s.city},
order_by: {:asc, s.city}
)
end
@ -605,40 +605,40 @@ defmodule Plausible.Stats.Breakdown do
defp do_group_by(q, "visit:device") do
from(
s in q,
group_by: s.screen_size,
group_by: s.device,
select_merge: %{
device: fragment("if(empty(?), ?, ?)", s.screen_size, @not_set, s.screen_size)
device: fragment("if(empty(?), ?, ?)", s.device, @not_set, s.device)
},
order_by: {:asc, s.screen_size}
order_by: {:asc, s.device}
)
end
defp do_group_by(q, "visit:os") do
from(
s in q,
group_by: s.operating_system,
group_by: s.os,
select_merge: %{
os: fragment("if(empty(?), ?, ?)", s.operating_system, @not_set, s.operating_system)
os: fragment("if(empty(?), ?, ?)", s.os, @not_set, s.os)
},
order_by: {:asc, s.operating_system}
order_by: {:asc, s.os}
)
end
defp do_group_by(q, "visit:os_version") do
from(
s in q,
group_by: [s.operating_system, s.operating_system_version],
group_by: [s.os, s.os_version],
select_merge: %{
os: fragment("if(empty(?), ?, ?)", s.operating_system, @not_set, s.operating_system),
os: fragment("if(empty(?), ?, ?)", s.os, @not_set, s.os),
os_version:
fragment(
"if(empty(?), ?, ?)",
s.operating_system_version,
s.os_version,
@not_set,
s.operating_system_version
s.os_version
)
},
order_by: {:asc, s.operating_system_version}
order_by: {:asc, s.os_version}
)
end