[Draft] Improve location translations (#1526)

* WIP

* Use location library for search suggestions

* Remove unused code

* Remove Countries completely

* Fix tests
pull/1542/head
Uku Taht 5 years ago committed by GitHub
parent 178c8076d5
commit 1dba113e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,8 @@ RUN apk add --no-cache git nodejs yarn python3 npm ca-certificates wget gnupg ma
npm install npm@latest -g && \
npm install -g webpack
RUN wget https://s3.eu-central-1.wasabisys.com/plausible-application/geonames.csv -q
COPY mix.exs ./
COPY mix.lock ./
RUN mix local.hex --force && \
@ -42,7 +44,8 @@ RUN npm run deploy --prefix ./assets && \
mix phx.digest priv/static && \
mix download_country_database && \
# https://hexdocs.pm/sentry/Sentry.Sources.html#module-source-code-storage
mix sentry_recompile
mix sentry_recompile && \
mv geonames.csv ./priv/geonames.csv
WORKDIR /app
COPY rel rel
@ -66,6 +69,7 @@ COPY --from=buildcontainer /app/_build/prod/rel/plausible /app
RUN chown -R plausibleuser:plausibleuser /app
USER plausibleuser
WORKDIR /app
ENV GEONAMES_SOURCE_FILE=/app/lib/plausible-0.0.1/priv/geonames.csv
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000
CMD ["run"]

@ -56,7 +56,7 @@ class Countries extends React.Component {
])
this.state.countries.forEach(function(item){
dataset[item.code] = {numberOfThings: item.visitors, fillColor: paletteScale(item.visitors)};
dataset[item.alpha_3] = {numberOfThings: item.visitors, fillColor: paletteScale(item.visitors)};
});
return dataset
@ -107,12 +107,14 @@ class Countries extends React.Component {
},
done: (datamap) => {
datamap.svg.selectAll('.datamaps-subunit').on('click', (geography) => {
const country = this.state.countries.find(c => c.alpha_3 === geography.id)
navigateToQuery(
this.props.history,
this.props.query,
{
country: geography.id,
country_name: geography.properties.name
country: country.code,
country_name: country.name
}
)
})

@ -111,6 +111,7 @@ geolite2_country_db =
)
ip_geolocation_db = get_var_from_path_or_env(config_dir, "IP_GEOLOCATION_DB", geolite2_country_db)
geonames_source_file = get_var_from_path_or_env(config_dir, "GEONAMES_SOURCE_FILE")
disable_auth =
config_dir
@ -400,6 +401,10 @@ if config_env() != :test do
]
end
if geonames_source_file do
config :location, :geonames_source_file, geonames_source_file
end
config :logger,
level: log_level,
backends: [:console]

@ -218,7 +218,7 @@ defmodule Plausible.Stats.Clickhouse do
q =
if query.filters["country"] do
country = Plausible.Stats.CountryName.to_alpha2(query.filters["country"])
country = query.filters["country"]
from(s in q, where: s.country_code == ^country)
else
q
@ -312,7 +312,7 @@ defmodule Plausible.Stats.Clickhouse do
q =
if query.filters["country"] do
country = Plausible.Stats.CountryName.to_alpha2(query.filters["country"])
country = query.filters["country"]
from(s in q, where: s.country_code == ^country)
else
q
@ -528,147 +528,4 @@ defmodule Plausible.Stats.Clickhouse do
db_query
end
end
def make_suggestions(site, query, "country", filter_search) do
filter_search =
String.split(filter_search, ",")
|> Enum.map(fn c -> Plausible.Stats.CountryName.to_alpha2(c) end)
q =
from(
e in base_session_query(site, query),
group_by: e.country_code,
order_by: [desc: fragment("count(*)")],
select: {e.country_code}
)
ClickhouseRepo.all(q)
|> Enum.map(fn {x} -> x end)
|> Enum.filter(fn c -> Enum.find(filter_search, false, fn x -> x == c end) end)
|> Enum.map(fn c -> Plausible.Stats.CountryName.to_alpha3(c) end)
|> Enum.slice(0..24)
end
def make_suggestions(site, _query, "goal", filter_search) do
Repo.all(from g in Plausible.Goal, where: g.domain == ^site.domain)
|> Enum.map(fn x -> if x.event_name, do: x.event_name, else: "Visit #{x.page_path}" end)
|> Enum.filter(fn goal ->
String.contains?(
String.downcase(goal),
String.downcase(filter_search)
)
end)
end
def make_suggestions(site, query, filter_name, filter_search) do
filter_search = if filter_search == nil, do: "", else: filter_search
filter_query =
if Enum.member?(["entry_page", "page", "exit_page"], filter_name),
do: "%#{String.replace(filter_search, "*", "")}%",
else: "%#{filter_search}%"
filter_name =
case filter_name do
"page" -> "pathname"
"source" -> "referrer_source"
"os" -> "operating_system"
"os_version" -> "operating_system_version"
"screen" -> "screen_size"
_ -> filter_name
end
q =
if(filter_name == "pathname",
do: base_query(site, query),
else: base_session_query(site, query)
)
|> from(
group_by: ^String.to_atom(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)
)
"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)
)
"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)
)
end
ClickhouseRepo.all(q)
|> Enum.map(fn {suggestion} -> suggestion end)
|> Enum.filter(fn suggestion -> suggestion != "" end)
end
end

@ -1,575 +0,0 @@
defmodule Plausible.Stats.CountryName do
@iso3166_2_source Application.app_dir(:plausible, "priv/iso_3166-2.json")
@geonames_source Application.app_dir(:plausible, "priv/cities500.txt")
@subdivision_names File.read!(@iso3166_2_source)
|> Jason.decode!()
|> Map.get("3166-2")
|> Enum.map(fn %{"code" => code, "name" => name} -> {code, name} end)
|> Enum.into(%{})
@city_names File.stream!(@geonames_source)
|> Stream.map(&String.trim(&1))
|> Stream.map(&String.split(&1, "\t"))
|> Stream.map(fn [id, name | _rest] -> {String.to_integer(id), name} end)
|> Enum.into(%{})
@city_codes @city_names
|> Enum.map(fn {code, name} -> {name, code} end)
|> Enum.into(%{})
@country_codes_to_names %{
"AF" => "Afghanistan",
"AX" => "Aland Islands",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia And Herzegovina",
"BW" => "Botswana",
"BV" => "Bouvet Island",
"BR" => "Brazil",
"IO" => "British Indian Ocean Territory",
"BN" => "Brunei Darussalam",
"BG" => "Bulgaria",
"BF" => "Burkina Faso",
"BI" => "Burundi",
"KH" => "Cambodia",
"CM" => "Cameroon",
"CA" => "Canada",
"CV" => "Cape Verde",
"KY" => "Cayman Islands",
"CF" => "Central African Republic",
"TD" => "Chad",
"CL" => "Chile",
"CN" => "China",
"CX" => "Christmas Island",
"CC" => "Cocos (Keeling) Islands",
"CO" => "Colombia",
"KM" => "Comoros",
"CG" => "Congo",
"CD" => "Congo, Democratic Republic",
"CK" => "Cook Islands",
"CR" => "Costa Rica",
"CI" => "Cote D'Ivoire",
"HR" => "Croatia",
"CU" => "Cuba",
"CY" => "Cyprus",
"CZ" => "Czech Republic",
"DK" => "Denmark",
"DJ" => "Djibouti",
"DM" => "Dominica",
"DO" => "Dominican Republic",
"EC" => "Ecuador",
"EG" => "Egypt",
"SV" => "El Salvador",
"GQ" => "Equatorial Guinea",
"ER" => "Eritrea",
"EE" => "Estonia",
"ET" => "Ethiopia",
"FK" => "Falkland Islands (Malvinas)",
"FO" => "Faroe Islands",
"FJ" => "Fiji",
"FI" => "Finland",
"FR" => "France",
"GF" => "French Guiana",
"PF" => "French Polynesia",
"TF" => "French Southern Territories",
"GA" => "Gabon",
"GM" => "Gambia",
"GE" => "Georgia",
"DE" => "Germany",
"GH" => "Ghana",
"GI" => "Gibraltar",
"GR" => "Greece",
"GL" => "Greenland",
"GD" => "Grenada",
"GP" => "Guadeloupe",
"GU" => "Guam",
"GT" => "Guatemala",
"GG" => "Guernsey",
"GN" => "Guinea",
"GW" => "Guinea-Bissau",
"GY" => "Guyana",
"HT" => "Haiti",
"HM" => "Heard Island & Mcdonald Islands",
"VA" => "Holy See (Vatican City State)",
"HN" => "Honduras",
"HK" => "Hong Kong",
"HU" => "Hungary",
"IS" => "Iceland",
"IN" => "India",
"ID" => "Indonesia",
"IR" => "Iran, Islamic Republic Of",
"IQ" => "Iraq",
"IE" => "Ireland",
"IM" => "Isle Of Man",
"IL" => "Israel",
"IT" => "Italy",
"JM" => "Jamaica",
"JP" => "Japan",
"JE" => "Jersey",
"JO" => "Jordan",
"KZ" => "Kazakhstan",
"KE" => "Kenya",
"KI" => "Kiribati",
"KR" => "Korea",
"KW" => "Kuwait",
"KG" => "Kyrgyzstan",
"LA" => "Lao People's Democratic Republic",
"LV" => "Latvia",
"LB" => "Lebanon",
"LS" => "Lesotho",
"LR" => "Liberia",
"LY" => "Libyan Arab Jamahiriya",
"LI" => "Liechtenstein",
"LT" => "Lithuania",
"LU" => "Luxembourg",
"MO" => "Macao",
"MK" => "Macedonia",
"MG" => "Madagascar",
"MW" => "Malawi",
"MY" => "Malaysia",
"MV" => "Maldives",
"ML" => "Mali",
"MT" => "Malta",
"MH" => "Marshall Islands",
"MQ" => "Martinique",
"MR" => "Mauritania",
"MU" => "Mauritius",
"YT" => "Mayotte",
"MX" => "Mexico",
"FM" => "Micronesia, Federated States Of",
"MD" => "Moldova",
"MC" => "Monaco",
"MN" => "Mongolia",
"ME" => "Montenegro",
"MS" => "Montserrat",
"MA" => "Morocco",
"MZ" => "Mozambique",
"MM" => "Myanmar",
"NA" => "Namibia",
"NR" => "Nauru",
"NP" => "Nepal",
"NL" => "Netherlands",
"NC" => "New Caledonia",
"NZ" => "New Zealand",
"NI" => "Nicaragua",
"NE" => "Niger",
"NG" => "Nigeria",
"NU" => "Niue",
"NF" => "Norfolk Island",
"MP" => "Northern Mariana Islands",
"NO" => "Norway",
"OM" => "Oman",
"PK" => "Pakistan",
"PW" => "Palau",
"PS" => "Palestinian Territory, Occupied",
"PA" => "Panama",
"PG" => "Papua New Guinea",
"PY" => "Paraguay",
"PE" => "Peru",
"PH" => "Philippines",
"PN" => "Pitcairn",
"PL" => "Poland",
"PT" => "Portugal",
"PR" => "Puerto Rico",
"QA" => "Qatar",
"RE" => "Reunion",
"RO" => "Romania",
"RU" => "Russian Federation",
"RW" => "Rwanda",
"BL" => "Saint Barthelemy",
"SH" => "Saint Helena",
"KN" => "Saint Kitts And Nevis",
"LC" => "Saint Lucia",
"MF" => "Saint Martin",
"PM" => "Saint Pierre And Miquelon",
"VC" => "Saint Vincent And Grenadines",
"WS" => "Samoa",
"SM" => "San Marino",
"ST" => "Sao Tome And Principe",
"SA" => "Saudi Arabia",
"SN" => "Senegal",
"RS" => "Serbia",
"SC" => "Seychelles",
"SL" => "Sierra Leone",
"SG" => "Singapore",
"SK" => "Slovakia",
"SI" => "Slovenia",
"SB" => "Solomon Islands",
"SO" => "Somalia",
"ZA" => "South Africa",
"GS" => "South Georgia And Sandwich Isl.",
"ES" => "Spain",
"LK" => "Sri Lanka",
"SD" => "Sudan",
"SR" => "Suriname",
"SJ" => "Svalbard And Jan Mayen",
"SZ" => "Swaziland",
"SE" => "Sweden",
"CH" => "Switzerland",
"SY" => "Syrian Arab Republic",
"TW" => "Taiwan",
"TJ" => "Tajikistan",
"TZ" => "Tanzania",
"TH" => "Thailand",
"TL" => "Timor-Leste",
"TG" => "Togo",
"TK" => "Tokelau",
"TO" => "Tonga",
"TT" => "Trinidad And Tobago",
"TN" => "Tunisia",
"TR" => "Turkey",
"TM" => "Turkmenistan",
"TC" => "Turks And Caicos Islands",
"TV" => "Tuvalu",
"UG" => "Uganda",
"UA" => "Ukraine",
"AE" => "United Arab Emirates",
"GB" => "United Kingdom",
"US" => "United States",
"UM" => "United States Outlying Islands",
"UY" => "Uruguay",
"UZ" => "Uzbekistan",
"VU" => "Vanuatu",
"VE" => "Venezuela",
"VN" => "Viet Nam",
"VG" => "Virgin Islands, British",
"VI" => "Virgin Islands, U.S.",
"WF" => "Wallis And Futuna",
"EH" => "Western Sahara",
"YE" => "Yemen",
"ZM" => "Zambia",
"ZW" => "Zimbabwe"
}
@alpha3_codes %{
"SL" => "SLE",
"VA" => "VAT",
"SM" => "SMR",
"TV" => "TUV",
"KZ" => "KAZ",
"PE" => "PER",
"CD" => "COD",
"SN" => "SEN",
"BA" => "BIH",
"VC" => "VCT",
"TW" => "TWN",
"NP" => "NPL",
"UZ" => "UZB",
"MY" => "MYS",
"GM" => "GMB",
"GA" => "GAB",
"PW" => "PLW",
"TH" => "THA",
"NR" => "NRU",
"KW" => "KWT",
"MU" => "MUS",
"GI" => "GIB",
"BJ" => "BEN",
"AR" => "ARG",
"SA" => "SAU",
"GW" => "GNB",
"AT" => "AUT",
"AW" => "ABW",
"MZ" => "MOZ",
"IT" => "ITA",
"BI" => "BDI",
"GR" => "GRC",
"FO" => "FRO",
"SD" => "SDN",
"PS" => "PSE",
"SB" => "SLB",
"KR" => "KOR",
"TR" => "TUR",
"ST" => "STP",
"JP" => "JPN",
"TG" => "TGO",
"GQ" => "GNQ",
"PH" => "PHL",
"LS" => "LSO",
"LT" => "LTU",
"JM" => "JAM",
"GF" => "GUF",
"AZ" => "AZE",
"LA" => "LAO",
"TM" => "TKM",
"MH" => "MHL",
"TJ" => "TJK",
"FR" => "FRA",
"GG" => "GGY",
"WF" => "WLF",
"BZ" => "BLZ",
"VN" => "VNM",
"SC" => "SYC",
"PN" => "PCN",
"GL" => "GRL",
"RS" => "SRB",
"CV" => "CPV",
"BM" => "BMU",
"DJ" => "DJI",
"BV" => "BVT",
"CK" => "COK",
"NA" => "NAM",
"BS" => "BHS",
"PL" => "POL",
"HN" => "HND",
"BF" => "BFA",
"PF" => "PYF",
"BR" => "BRA",
"ER" => "ERI",
"UG" => "UGA",
"NE" => "NER",
"NL" => "NLD",
"NG" => "NGA",
"IQ" => "IRQ",
"BB" => "BRB",
"PY" => "PRY",
"IR" => "IRN",
"CH" => "CHE",
"UM" => "UMI",
"TK" => "TKL",
"TL" => "TLS",
"YT" => "MYT",
"CI" => "CIV",
"SK" => "SVK",
"WS" => "WSM",
"AQ" => "ATA",
"MG" => "MDG",
"TT" => "TTO",
"PM" => "SPM",
"ZW" => "ZWE",
"DM" => "DMA",
"RE" => "REU",
"CM" => "CMR",
"CC" => "CCK",
"SZ" => "SWZ",
"MR" => "MRT",
"HM" => "HMD",
"RW" => "RWA",
"FM" => "FSM",
"MP" => "MNP",
"PK" => "PAK",
"MW" => "MWI",
"VE" => "VEN",
"KY" => "CYM",
"CN" => "CHN",
"DZ" => "DZA",
"KM" => "COM",
"NU" => "NIU",
"PT" => "PRT",
"US" => "USA",
"MS" => "MSR",
"SO" => "SOM",
"ES" => "ESP",
"AM" => "ARM",
"ML" => "MLI",
"TC" => "TCA",
"MM" => "MMR",
"JE" => "JEY",
"AO" => "AGO",
"LV" => "LVA",
"FJ" => "FJI",
"UA" => "UKR",
"BH" => "BHR",
"EG" => "EGY",
"ME" => "MNE",
"AX" => "ALA",
"MQ" => "MTQ",
"MX" => "MEX",
"CX" => "CXR",
"KH" => "KHM",
"SV" => "SLV",
"AS" => "ASM",
"IN" => "IND",
"GB" => "GBR",
"LU" => "LUX",
"HT" => "HTI",
"DE" => "DEU",
"VI" => "VIR",
"NI" => "NIC",
"CG" => "COG",
"ET" => "ETH",
"CZ" => "CZE",
"HR" => "HRV",
"CY" => "CYP",
"KG" => "KGZ",
"BO" => "BOL",
"PG" => "PNG",
"ZA" => "ZAF",
"MO" => "MAC",
"FI" => "FIN",
"PR" => "PRI",
"SR" => "SUR",
"GN" => "GIN",
"TD" => "TCD",
"MF" => "MAF",
"VG" => "VGB",
"NF" => "NFK",
"IL" => "ISR",
"GH" => "GHA",
"TZ" => "TZA",
"SH" => "SHN",
"JO" => "JOR",
"MV" => "MDV",
"BE" => "BEL",
"NC" => "NCL",
"DK" => "DNK",
"GS" => "SGS",
"LB" => "LBN",
"AI" => "AIA",
"LI" => "LIE",
"OM" => "OMN",
"UY" => "URY",
"KN" => "KNA",
"HU" => "HUN",
"BT" => "BTN",
"EC" => "ECU",
"TO" => "TON",
"NO" => "NOR",
"RU" => "RUS",
"MD" => "MDA",
"SY" => "SYR",
"DO" => "DOM",
"MA" => "MAR",
"HK" => "HKG",
"SJ" => "SJM",
"BW" => "BWA",
"IO" => "IOT",
"LY" => "LBY",
"CA" => "CAN",
"LK" => "LKA",
"LC" => "LCA",
"AG" => "ATG",
"SI" => "SVN",
"GP" => "GLP",
"BN" => "BRN",
"IM" => "IMN",
"SE" => "SWE",
"BD" => "BGD",
"VU" => "VUT",
"KI" => "KIR",
"AL" => "ALB",
"MT" => "MLT",
"KE" => "KEN",
"BG" => "BGR",
"GE" => "GEO",
"IE" => "IRL",
"PA" => "PAN",
"ID" => "IDN",
"AE" => "ARE",
"RO" => "ROU",
"NZ" => "NZL",
"MN" => "MNG",
"GY" => "GUY",
"BL" => "BLM",
"EE" => "EST",
"LR" => "LBR",
"CO" => "COL",
"CF" => "CAF",
"MC" => "MCO",
"CR" => "CRI",
"AU" => "AUS",
"ZM" => "ZMB",
"GT" => "GTM",
"AF" => "AFG",
"MK" => "MKD",
"GD" => "GRD",
"FK" => "FLK",
"AD" => "AND",
"SG" => "SGP",
"QA" => "QAT",
"CL" => "CHL",
"GU" => "GUM",
"TN" => "TUN",
"YE" => "YEM",
"BY" => "BLR",
"IS" => "ISL",
"CU" => "CUB",
"TF" => "ATF",
"EH" => "ESH"
}
@alpha2_codes @alpha3_codes
|> Enum.map(fn {k, v} -> {v, k} end)
|> Enum.into(%{})
def to_alpha3(code) do
Map.get(@alpha3_codes, code, code)
end
def to_alpha2(code) do
Map.get(@alpha2_codes, code, code)
end
def search_alpha2(name_search_query) do
Enum.reduce(@country_codes_to_names, [], fn {code, name}, acc ->
matches =
name
|> String.downcase()
|> String.contains?(String.downcase(name_search_query))
if matches, do: [code | acc], else: acc
end)
end
def from_iso3166(code) do
Map.get(@country_codes_to_names, code, code)
end
def from_iso3166_2(code) do
Map.get(@subdivision_names, code, code)
end
def search_iso3166_2(name_search_query) do
Enum.reduce(@subdivision_names, [], fn {code, name}, acc ->
matches =
name
|> String.downcase()
|> String.contains?(String.downcase(name_search_query))
if matches, do: [code | acc], else: acc
end)
end
def search_geoname(name_search_query) do
Enum.reduce(@city_names, [], fn {code, name}, acc ->
matches =
name
|> String.downcase()
|> String.contains?(String.downcase(name_search_query))
if matches, do: [code | acc], else: acc
end)
end
def from_geoname_id(geoname_id, default) do
Map.get(@city_names, geoname_id, default)
end
def to_geoname_id(city_name) do
Map.get(@city_codes, city_name)
end
end

@ -2,10 +2,9 @@ defmodule Plausible.Stats.FilterSuggestions do
use Plausible.Repo
use Plausible.ClickhouseRepo
import Plausible.Stats.Base
alias Plausible.Stats.CountryName
def filter_suggestions(site, query, "country", filter_search) do
matches = Plausible.Stats.CountryName.search_alpha2(filter_search)
matches = Location.search_country(filter_search)
q =
from(
@ -16,12 +15,13 @@ defmodule Plausible.Stats.FilterSuggestions do
)
ClickhouseRepo.all(q)
|> Enum.filter(fn c -> Enum.find(matches, false, fn x -> x == c end) end)
|> Enum.map(fn c -> Enum.find(matches, fn x -> x.alpha_2 == c end) end)
|> Enum.filter(& &1)
|> Enum.slice(0..24)
|> Enum.map(fn c ->
|> Enum.map(fn match ->
%{
code: CountryName.to_alpha3(c),
name: CountryName.from_iso3166(c)
code: match.alpha_2,
name: match.name
}
end)
end
@ -37,15 +37,17 @@ defmodule Plausible.Stats.FilterSuggestions do
)
|> ClickhouseRepo.all()
|> Enum.map(fn c ->
subdiv = Location.get_subdivision(c)
%{
code: c,
name: CountryName.from_iso3166_2(c)
name: subdiv.name
}
end)
end
def filter_suggestions(site, query, "region", filter_search) do
matches = Plausible.Stats.CountryName.search_iso3166_2(filter_search)
matches = Location.search_subdivision(filter_search)
q =
from(
@ -56,12 +58,13 @@ defmodule Plausible.Stats.FilterSuggestions do
)
ClickhouseRepo.all(q)
|> Enum.filter(fn c -> Enum.find(matches, false, fn x -> x == c end) end)
|> Enum.map(fn c -> Enum.find(matches, fn x -> x.code == c end) end)
|> Enum.filter(& &1)
|> Enum.slice(0..24)
|> Enum.map(fn c ->
|> Enum.map(fn subdiv ->
%{
code: c,
name: CountryName.from_iso3166_2(c)
code: subdiv.code,
name: subdiv.name
}
end)
end
@ -77,15 +80,17 @@ defmodule Plausible.Stats.FilterSuggestions do
)
|> ClickhouseRepo.all()
|> Enum.map(fn c ->
city = Location.get_city(c)
%{
code: Integer.to_string(c),
name: CountryName.from_geoname_id(c, "N/A")
name: (city && city.name) || "N/A"
}
end)
end
def filter_suggestions(site, query, "city", filter_search) do
matches = Plausible.Stats.CountryName.search_geoname(filter_search)
filter_search = String.downcase(filter_search)
q =
from(
@ -93,16 +98,20 @@ defmodule Plausible.Stats.FilterSuggestions do
group_by: e.city_geoname_id,
order_by: [desc: fragment("count(*)")],
select: e.city_geoname_id,
where: e.city_geoname_id != 0
where: e.city_geoname_id != 0,
limit: 5000
)
ClickhouseRepo.all(q)
|> Enum.filter(fn c -> Enum.find(matches, false, fn x -> x == c end) end)
|> Enum.map(fn c -> Location.get_city(c) end)
|> Enum.filter(fn city ->
city && String.contains?(String.downcase(city.name), filter_search)
end)
|> Enum.slice(0..24)
|> Enum.map(fn c ->
%{
code: Integer.to_string(c),
name: CountryName.from_geoname_id(c, "N/A")
code: Integer.to_string(c.id),
name: c.name
}
end)
end

@ -31,11 +31,6 @@ defmodule Plausible.Stats.Filters do
new_filters =
Enum.reduce(query.filters, %{}, fn {name, val}, new_filters ->
cond do
name == "country" ->
{filter_type, filter_val} = filter_value(name, val)
new_val = Plausible.Stats.CountryName.to_alpha2(filter_val)
Map.put(new_filters, "visit:country", {filter_type, new_val})
name == "goal" ->
filter =
case val do

@ -513,7 +513,8 @@ defmodule PlausibleWeb.Api.StatsController do
Map.merge(row, %{
"name" => country.name,
"flag" => country.flag,
"code" => country.alpha_3
"alpha_3" => country.alpha_3,
"code" => country.alpha_2
})
end)

@ -54,7 +54,7 @@
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
"kaffy": {:hex, :kaffy, "0.9.0", "bef34c9729f6a3af4d0dea8eede8bcb9e11371a83ac9a8b393991bce81839517", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "d18ff57b8e68feb433aed11e71510cd357abc7034e75358af5deff7d0d4c6ed3"},
"location": {:git, "https://github.com/plausible/location.git", "6c7a372eed1065020f3d379723d2417784b1c41b", []},
"location": {:git, "https://github.com/plausible/location.git", "5d715e3f56c5ebc1d50b5754a20a8d914c0f0621", []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -22,14 +22,16 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
assert json_response(conn, 200) == [
%{
"code" => "EST",
"code" => "EE",
"alpha_3" => "EST",
"name" => "Estonia",
"flag" => "🇪🇪",
"visitors" => 2,
"percentage" => 67
},
%{
"code" => "GBR",
"code" => "GB",
"alpha_3" => "GBR",
"name" => "United Kingdom",
"flag" => "🇬🇧",
"visitors" => 1,
@ -62,7 +64,8 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
assert json_response(conn, 200) == [
%{
"code" => "GBR",
"code" => "GB",
"alpha_3" => "GBR",
"name" => "United Kingdom",
"flag" => "🇬🇧",
"total_visitors" => 1,
@ -70,7 +73,8 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
"conversion_rate" => 100.0
},
%{
"code" => "EST",
"code" => "EE",
"alpha_3" => "EST",
"name" => "Estonia",
"flag" => "🇪🇪",
"total_visitors" => 2,

@ -301,14 +301,14 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
describe "GET /api/stats/main-graph - top stats - filters" do
setup [:create_user, :log_in, :create_new_site]
test "returns only visitors from a country based on alpha3 code", %{conn: conn, site: site} do
test "returns only visitors from a country based on alpha2 code", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, country_code: "US"),
build(:pageview, country_code: "US"),
build(:pageview, country_code: "EE")
])
filters = Jason.encode!(%{country: "USA"})
filters = Jason.encode!(%{country: "US"})
conn =
get(

@ -48,7 +48,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
"/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=Unit"
)
assert json_response(conn, 200) == [%{"code" => "USA", "name" => "United States"}]
assert json_response(conn, 200) == [%{"code" => "US", "name" => "United States"}]
end
test "returns suggestions for regions", %{conn: conn, user: user} do

Loading…
Cancel
Save