Skip to content

Commit 18538db

Browse files
authored
Merge pull request #30 from Recruitee/reports-move-to-locus-CU-86c0p0yb9
Reports move to locus cu 86c0p0yb9
2 parents c8cea1a + 0e6f9d1 commit 18538db

3 files changed

Lines changed: 43 additions & 28 deletions

File tree

lib/plausible_web/controllers/api/external_controller.ex

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -450,43 +450,56 @@ defmodule PlausibleWeb.Api.ExternalController do
450450

451451
@decorate trace("ingest.geolocation")
452452
defp visitor_location_details(conn) do
453-
result =
454-
PlausibleWeb.RemoteIp.get(conn)
455-
|> Geolix.lookup()
453+
ip = PlausibleWeb.RemoteIp.get(conn)
456454

457-
country_code =
458-
get_in(result, [:geolocation, :country, :iso_code])
459-
|> ignore_unknown_country
455+
case :locus.lookup(:geolocation, ip) do
456+
{:ok, result} -> get_location_details(result)
457+
_ -> get_location_details(%{})
458+
end
459+
end
460460

461-
city_geoname_id = get_in(result, [:geolocation, :city, :geoname_id])
461+
defp get_location_details(geo_data) do
462+
%{
463+
country_code: get_country_code(geo_data),
464+
subdivision1_code: get_subdivision_code(geo_data, 0),
465+
subdivision2_code: get_subdivision_code(geo_data, 1),
466+
city_geoname_id: get_city_geoname_id(geo_data)
467+
}
468+
end
462469

463-
subdivision1_code =
464-
case result do
465-
%{geolocation: %{subdivisions: [%{iso_code: iso_code} | _rest]}} ->
466-
country_code <> "-" <> iso_code
470+
defp get_country_code(%{}), do: ""
467471

468-
_ ->
469-
""
470-
end
472+
defp get_country_code(geo_data) do
473+
geo_data
474+
|> Map.get("country", %{})
475+
|> Map.get("iso_code")
476+
|> ignore_unknown_country()
477+
end
471478

472-
subdivision2_code =
473-
case result do
474-
%{geolocation: %{subdivisions: [_first, %{iso_code: iso_code} | _rest]}} ->
475-
country_code <> "-" <> iso_code
479+
defp get_city_geoname_id(%{}), do: ""
476480

477-
_ ->
478-
""
479-
end
481+
defp get_city_geoname_id(geo_data) do
482+
city =
483+
geo_data
484+
|> Map.get("city", %{})
485+
|> Map.get("geoname_id", "")
480486

481-
%{
482-
country_code: country_code,
483-
subdivision1_code: subdivision1_code,
484-
subdivision2_code: subdivision2_code,
485-
city_geoname_id: Map.get(@city_overrides, city_geoname_id, city_geoname_id)
486-
}
487+
Map.get(@city_overrides, city, city)
488+
end
489+
490+
defp get_subdivision_code(%{}, _), do: ""
491+
492+
defp get_subdivision_code(geo_data, n) do
493+
subdivisions = Map.get(geo_data, "subdivision", [])
494+
country_code = get_country_code(geo_data)
495+
496+
case Enum.at(subdivisions, n) do
497+
%{"iso_code" => iso_code} -> country_code <> "-" <> iso_code
498+
_ -> ""
499+
end
487500
end
488501

489-
defp ignore_unknown_country("ZZ"), do: nil
502+
defp ignore_unknown_country("ZZ"), do: ""
490503
defp ignore_unknown_country(country), do: country
491504

492505
@decorate trace("ingest.parse_referrer")

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ defmodule Plausible.MixProject do
8686
{:siphash, "~> 3.2"},
8787
{:oban, "~> 2.12.0"},
8888
{:geolix, "~> 1.0"},
89+
{:locus, "~> 2.3.10"},
8990
{:clickhouse_ecto, git: "https://github.com/Recruitee/clickhouse_ecto.git", ref: "ed45cd0"},
9091
{:location, git: "https://github.com/plausible/location.git"},
9192
{:geolix_adapter_mmdb2, "~> 0.5.0"},

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
5858
"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"},
5959
"location": {:git, "https://github.com/plausible/location.git", "ca2e4e69e1032d7100b190bbcbabf51a5349847a", []},
60+
"locus": {:hex, :locus, "2.3.10", "e5baa7f9fcea23d293e1d7fae46ac4e6b1ffceb10a5552ae8afa1ef0815761da", [:rebar3], [{:tls_certificate_check, "~> 1.9", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "5b6de8c70ca3070956286e6412446f4593103c19f88944b38e3c9f66d2c8fcd2"},
6061
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
6162
"mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"},
6263
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},

0 commit comments

Comments
 (0)