From 669091f2efbb3a395a79c8e8f9280ea43b6d079e Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 20 Sep 2022 15:02:14 +0300 Subject: [PATCH] Ignore unknown country in imported data (#2247) --- lib/plausible/stats/imported.ex | 2 +- .../controllers/api/stats_controller/countries_test.exs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/plausible/stats/imported.ex b/lib/plausible/stats/imported.ex index 198311c02..9888bba5f 100644 --- a/lib/plausible/stats/imported.ex +++ b/lib/plausible/stats/imported.ex @@ -179,7 +179,7 @@ defmodule Plausible.Stats.Imported do |> select_merge([i], %{exit_page: i.exit_page, visits: sum(i.exits)}) :country -> - imported_q |> select_merge([i], %{country: i.country}) + imported_q |> where([i], i.country != "ZZ") |> select_merge([i], %{country: i.country}) :region -> imported_q |> where([i], i.region != "") |> select_merge([i], %{region: i.region}) diff --git a/test/plausible_web/controllers/api/stats_controller/countries_test.exs b/test/plausible_web/controllers/api/stats_controller/countries_test.exs index 91e266b60..bdf2df11f 100644 --- a/test/plausible_web/controllers/api/stats_controller/countries_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/countries_test.exs @@ -68,9 +68,12 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do end test "ignores unknown country code ZZ", %{conn: conn, site: site} do - populate_stats(site, [build(:pageview, country_code: "ZZ")]) + populate_stats(site, [ + build(:pageview, country_code: "ZZ"), + build(:imported_locations, country: "ZZ") + ]) - conn = get(conn, "/api/stats/#{site.domain}/countries?period=day") + conn = get(conn, "/api/stats/#{site.domain}/countries?period=day&with_imported=true") assert json_response(conn, 200) == [] end