Add geolocation features
This commit is contained in:
parent
5ff8bb3171
commit
675302d013
8 changed files with 65 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -31,3 +31,6 @@ node_modules
|
|||
# Compiled static files
|
||||
/priv/static/*
|
||||
!/priv/static/.gitkeep
|
||||
|
||||
# Maxmind databases
|
||||
/priv/maxmind
|
||||
|
|
|
@ -43,3 +43,10 @@ config :ua_inspector,
|
|||
database_path: "priv/ua",
|
||||
# This is for runtime configuration
|
||||
init: {Tilastokeskus.Reception.UADetector, :init}
|
||||
|
||||
config :geolix,
|
||||
init: {Tilastokeskus.Reception.Geolix, :init}
|
||||
|
||||
config :geolite2data,
|
||||
geolix_updater: true,
|
||||
logger: Mix.env() != :prod
|
||||
|
|
|
@ -43,8 +43,6 @@ defmodule Tilastokeskus.Archive.Schemas.PageView do
|
|||
field(:tz_offset, :integer)
|
||||
|
||||
# GeoIP calculated information
|
||||
field(:loc_lat, :float)
|
||||
field(:loc_lon, :float)
|
||||
field(:loc_city, :string)
|
||||
field(:loc_country, :string)
|
||||
end
|
||||
|
@ -74,8 +72,6 @@ defmodule Tilastokeskus.Archive.Schemas.PageView do
|
|||
:screen_w,
|
||||
:screen_h,
|
||||
:tz_offset,
|
||||
:loc_lat,
|
||||
:loc_lon,
|
||||
:loc_city,
|
||||
:loc_country
|
||||
])
|
||||
|
|
25
lib/reception/geolix.ex
Normal file
25
lib/reception/geolix.ex
Normal file
|
@ -0,0 +1,25 @@
|
|||
defmodule Tilastokeskus.Reception.Geolix do
|
||||
@moduledoc """
|
||||
Geolix initialisation and utilities.
|
||||
"""
|
||||
|
||||
@spec init() :: :ok
|
||||
def init() do
|
||||
db_dir = Application.app_dir(:tilastokeskus, "priv") |> Path.join("maxmind")
|
||||
|
||||
databases = [
|
||||
%{
|
||||
id: :city,
|
||||
adapter: Geolix.Adapter.MMDB2,
|
||||
source: Path.join([db_dir, "GeoLite2-City.mmdb.gz"])
|
||||
},
|
||||
%{
|
||||
id: :country,
|
||||
adapter: Geolix.Adapter.MMDB2,
|
||||
source: Path.join([db_dir, "GeoLite2-Country.mmdb.gz"])
|
||||
}
|
||||
]
|
||||
|
||||
Application.put_env(:geolix, :databases, databases)
|
||||
end
|
||||
end
|
|
@ -17,6 +17,8 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
|
|||
screen_h = Map.get(body, "screen_height")
|
||||
tz_offset = Map.get(body, "tz_offset")
|
||||
|
||||
{city, country} = get_geoip(addr)
|
||||
|
||||
# Run in one transaction to avoid multiple DB checkouts
|
||||
{:ok, response} =
|
||||
Tilastokeskus.Archive.Repo.transaction(fn ->
|
||||
|
@ -43,7 +45,9 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
|
|||
device_type: unknown_2_str(ua.device.type),
|
||||
screen_w: screen_w,
|
||||
screen_h: screen_h,
|
||||
tz_offset: tz_offset
|
||||
tz_offset: tz_offset,
|
||||
loc_city: city,
|
||||
loc_country: country
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -167,6 +171,15 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
|
|||
end
|
||||
end
|
||||
|
||||
defp get_geoip(addr) do
|
||||
%{city: city, country: country} = res = Geolix.lookup(addr, as: :raw)
|
||||
|
||||
{
|
||||
get_in(city, [:city, :names, :en]),
|
||||
get_in(country, [:country, :names, :en])
|
||||
}
|
||||
end
|
||||
|
||||
defp unknown_2_str(:unknown), do: "n/a"
|
||||
defp unknown_2_str(val), do: val
|
||||
end
|
||||
|
|
4
mix.exs
4
mix.exs
|
@ -28,7 +28,9 @@ defmodule Tilastokeskus.MixProject do
|
|||
{:postgrex, ">= 0.0.0"},
|
||||
{:ecto, "~> 2.2"},
|
||||
{:jason, "~> 1.0"},
|
||||
{:ua_inspector, "~> 0.17"}
|
||||
{:ua_inspector, "~> 0.17"},
|
||||
{:geolix, "~> 0.16.0"},
|
||||
{:geolite2data, "~> 0.0.3"}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
3
mix.lock
3
mix.lock
|
@ -6,6 +6,8 @@
|
|||
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
|
||||
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
|
||||
"ecto": {:hex, :ecto, "2.2.10", "e7366dc82f48f8dd78fcbf3ab50985ceeb11cb3dc93435147c6e13f2cda0992e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
|
||||
"geolite2data": {:hex, :geolite2data, "0.0.3", "829acdaa56cc8a23909bd557c55e0bd158d03413e11d912ac44e980da70107f3", [:rebar3], [], "hexpm"},
|
||||
"geolix": {:hex, :geolix, "0.16.0", "0095f5e828c0b46596216efaf6a4d46aba3f1a4da3bdfec735b2e49c8cd908fb", [:mix], [{:mmdb2_decoder, "~> 0.2.0", [hex: :mmdb2_decoder, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.0", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm"},
|
||||
"hackney": {:hex, :hackney, "1.12.1", "8bf2d0e11e722e533903fe126e14d6e7e94d9b7983ced595b75f532e04b7fdc7", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
|
||||
"hpack": {:hex, :hpack_erl, "0.2.3", "17670f83ff984ae6cd74b1c456edde906d27ff013740ee4d9efaa4f1bf999633", [:rebar3], [], "hexpm"},
|
||||
"idna": {:hex, :idna, "5.1.1", "cbc3b2fa1645113267cc59c760bafa64b2ea0334635ef06dbac8801e42f7279c", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
|
||||
|
@ -13,6 +15,7 @@
|
|||
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
|
||||
"mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"},
|
||||
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
|
||||
"mmdb2_decoder": {:hex, :mmdb2_decoder, "0.2.0", "34346c43bb1860d38d5505e5edf3d5e31a0d9fce4ee48f0c8c762875bcd05bb2", [:mix], [], "hexpm"},
|
||||
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
|
||||
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
|
||||
"postgrex": {:hex, :postgrex, "0.13.5", "3d931aba29363e1443da167a4b12f06dcd171103c424de15e5f3fc2ba3e6d9c5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
|
||||
|
|
10
priv/repo/migrations/20180627194404_remove_lat_lon.exs
Normal file
10
priv/repo/migrations/20180627194404_remove_lat_lon.exs
Normal file
|
@ -0,0 +1,10 @@
|
|||
defmodule Tilastokeskus.Archive.Repo.Migrations.RemoveLatLon do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:events) do
|
||||
remove(:loc_lat)
|
||||
remove(:loc_lon)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue