Skip to content

Commit

Permalink
Handle invalid imported region codes in suggestions gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed Jun 3, 2024
1 parent 482cfc9 commit efcb7ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
37 changes: 31 additions & 6 deletions lib/plausible/stats/filter_suggestions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,52 @@ defmodule Plausible.Stats.FilterSuggestions do
|> Enum.map(fn c ->
subdiv = Location.get_subdivision(c)

%{
value: c,
label: subdiv.name
}
if subdiv do
%{
value: c,
label: subdiv.name
}
else
%{
value: c,
label: c
}
end
end)
end

def filter_suggestions(site, query, "region", filter_search) do
matches = Location.search_subdivision(filter_search)
filter_search = String.downcase(filter_search)

q =
from(
e in query_sessions(site, query),
group_by: e.subdivision1_code,
order_by: [desc: fragment("count(*)")],
select: e.subdivision1_code
select: e.subdivision1_code,
where: e.subdivision1_code != ""
)
|> Imported.merge_imported_region_suggestions(site, query)

ClickhouseRepo.all(q)
|> Enum.map(fn c -> Enum.find(matches, fn x -> x.code == c end) end)
|> Enum.map(fn c ->
match = Enum.find(matches, fn x -> x.code == c end)

cond do
match ->
match

String.contains?(String.downcase(c), filter_search) ->
%{
code: c,
name: c
}

true ->
nil
end
end)
|> Enum.filter(& &1)
|> Enum.slice(0..24)
|> Enum.map(fn subdiv ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,44 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
end
end

test "handles invalid region codes in imported data gracefully (GA4)", %{
conn: conn,
site: site,
site_import: site_import
} do
# NOTE: Currently, the regions imported from GA4 do not conform to region code standard
# we are using. Instead, literal region names are persisted. Those names often do not
# match the names from our region databases either. Regardless of that, we still consider
# them when filtering suggestions.

populate_stats(site, site_import.id, [
build(:imported_locations, country: "EE", region: "EE-37", pageviews: 2),
build(:imported_locations, country: "EE", region: "Hiiumaa", pageviews: 1)
])

conn =
get(
conn,
"/api/stats/#{site.domain}/suggestions/region?q=&with_imported=true"
)

assert json_response(conn, 200) == [
%{"value" => "EE-37", "label" => "Harjumaa"},
%{"value" => "Hiiumaa", "label" => "Hiiumaa"}
]

conn2 =
get(
conn,
"/api/stats/#{site.domain}/suggestions/region?q=H&with_imported=true"
)

assert json_response(conn2, 200) == [
%{"value" => "EE-37", "label" => "Harjumaa"},
%{"value" => "Hiiumaa", "label" => "Hiiumaa"}
]
end

test "ignores imported data in region suggestions when a different property is filtered by",
%{
conn: conn,
Expand Down

0 comments on commit efcb7ea

Please sign in to comment.