-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ignore XX and T1 countries * Add fallback if country_code=nil * Lookup city overrides directly in CityOverrides module * Changelog * Add empty moduledoc * Remove redundant comment
- Loading branch information
Showing
6 changed files
with
96 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule Plausible.Ingestion.Geolocation do | ||
@moduledoc false | ||
alias Plausible.Ingestion.CityOverrides | ||
|
||
def lookup(remote_ip) do | ||
result = Geolix.lookup(remote_ip, where: :geolocation) | ||
|
||
country_code = | ||
get_in(result, [:country, :iso_code]) | ||
|> ignore_unknown_country() | ||
|
||
city_geoname_id = country_code && get_in(result, [:city, :geoname_id]) | ||
city_geoname_id = CityOverrides.get(city_geoname_id, city_geoname_id) | ||
|
||
%{ | ||
country_code: country_code, | ||
subdivision1_code: subdivision1_code(country_code, result), | ||
subdivision2_code: subdivision2_code(country_code, result), | ||
city_geoname_id: city_geoname_id | ||
} | ||
end | ||
|
||
defp subdivision1_code(country_code, %{subdivisions: [%{iso_code: iso_code} | _rest]}) | ||
when not is_nil(country_code) do | ||
country_code <> "-" <> iso_code | ||
end | ||
|
||
defp subdivision1_code(_, _), do: nil | ||
|
||
defp subdivision2_code(country_code, %{subdivisions: [_first, %{iso_code: iso_code} | _rest]}) | ||
when not is_nil(country_code) do | ||
country_code <> "-" <> iso_code | ||
end | ||
|
||
defp subdivision2_code(_, _), do: nil | ||
|
||
@ignored_countries [ | ||
# Worldwide | ||
"ZZ", | ||
# Disputed territory | ||
"XX", | ||
# Tor exit node | ||
"T1" | ||
] | ||
defp ignore_unknown_country(code) when code in @ignored_countries, do: nil | ||
defp ignore_unknown_country(country), do: country | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters