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) |> filter_by_visit_props(Filters.visit_props(), query)
end 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 defp filter_by_visit_props(q, visit_props, query) do
Enum.reduce(visit_props, q, fn prop_name, sessions_q -> Enum.reduce(visit_props, q, fn prop_name, sessions_q ->
filter_key = "visit:" <> prop_name filter_key = "visit:" <> prop_name
db_field = String.to_existing_atom(prop_name)
db_field =
Map.get(@api_prop_name_to_db, prop_name, prop_name)
|> String.to_existing_atom()
from(s in sessions_q, from(s in sessions_q,
where: ^dynamic_filter_condition(query, filter_key, db_field) where: ^dynamic_filter_condition(query, filter_key, db_field)
@ -431,7 +416,7 @@ defmodule Plausible.Stats.Base do
end end
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(:referrer, @no_ref), do: ""
defp db_field_val(:utm_medium, @no_ref), do: "" defp db_field_val(:utm_medium, @no_ref), do: ""
defp db_field_val(:utm_source, @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 defp do_group_by(q, "visit:source") do
from( from(
s in q, s in q,
group_by: s.referrer_source, group_by: s.source,
select_merge: %{ 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 end
defp do_group_by(q, "visit:country") do defp do_group_by(q, "visit:country") do
from( from(
s in q, s in q,
where: s.country_code != "\0\0" and s.country_code != "ZZ", where: s.country != "\0\0" and s.country != "ZZ",
group_by: s.country_code, group_by: s.country,
select_merge: %{country: s.country_code}, select_merge: %{country: s.country},
order_by: {:asc, s.country_code} order_by: {:asc, s.country}
) )
end end
defp do_group_by(q, "visit:region") do defp do_group_by(q, "visit:region") do
from( from(
s in q, s in q,
where: s.subdivision1_code != "", where: s.region != "",
group_by: s.subdivision1_code, group_by: s.region,
select_merge: %{region: s.subdivision1_code}, select_merge: %{region: s.region},
order_by: {:asc, s.subdivision1_code} order_by: {:asc, s.region}
) )
end end
defp do_group_by(q, "visit:city") do defp do_group_by(q, "visit:city") do
from( from(
s in q, s in q,
where: s.city_geoname_id != 0, where: s.city != 0,
group_by: s.city_geoname_id, group_by: s.city,
select_merge: %{city: s.city_geoname_id}, select_merge: %{city: s.city},
order_by: {:asc, s.city_geoname_id} order_by: {:asc, s.city}
) )
end end
@ -605,40 +605,40 @@ defmodule Plausible.Stats.Breakdown do
defp do_group_by(q, "visit:device") do defp do_group_by(q, "visit:device") do
from( from(
s in q, s in q,
group_by: s.screen_size, group_by: s.device,
select_merge: %{ 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 end
defp do_group_by(q, "visit:os") do defp do_group_by(q, "visit:os") do
from( from(
s in q, s in q,
group_by: s.operating_system, group_by: s.os,
select_merge: %{ 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 end
defp do_group_by(q, "visit:os_version") do defp do_group_by(q, "visit:os_version") do
from( from(
s in q, s in q,
group_by: [s.operating_system, s.operating_system_version], group_by: [s.os, s.os_version],
select_merge: %{ 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: os_version:
fragment( fragment(
"if(empty(?), ?, ?)", "if(empty(?), ?, ?)",
s.operating_system_version, s.os_version,
@not_set, @not_set,
s.operating_system_version s.os_version
) )
}, },
order_by: {:asc, s.operating_system_version} order_by: {:asc, s.os_version}
) )
end end