Commit 9c62b992 authored by Mikko Ahlroth's avatar Mikko Ahlroth

Fix crash when bots request, also fix one crash when no url is given

Fixes #1
parent 094bec17
......@@ -45,6 +45,9 @@ defmodule Tilastokeskus.Archive.Schemas.PageView do
# GeoIP calculated information
field(:loc_city, :string)
field(:loc_country, :string)
# Was the hit from a bot
field(:is_bot, :boolean)
end
@doc """
......@@ -73,7 +76,8 @@ defmodule Tilastokeskus.Archive.Schemas.PageView do
:screen_h,
:tz_offset,
:loc_city,
:loc_country
:loc_country,
:is_bot
])
|> put_change(:type, event_type())
end
......
......@@ -16,7 +16,7 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
at = DateTime.utc_now()
addr = get_addr(req)
ua = parse_ua(req)
{ua, ua_name, ua_version, os_name, os_version, device_type, is_bot} = parse_ua(req)
{referrer, referrer_noq, referrer_domain} = parse_referrer(body)
screen_w = Map.get(body, "screen_width")
......@@ -43,17 +43,18 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
referrer: referrer,
referrer_noq: referrer_noq,
referrer_domain: referrer_domain,
ua: unknown_2_str(ua.user_agent),
ua_name: unknown_2_str(ua.client.name),
ua_version: unknown_2_str(ua.client.version),
os_name: unknown_2_str(ua.os.name),
os_version: unknown_2_str(ua.os.version),
device_type: unknown_2_str(ua.device.type),
ua: ua,
ua_name: ua_name,
ua_version: ua_version,
os_name: os_name,
os_version: os_version,
device_type: device_type,
screen_w: screen_w,
screen_h: screen_h,
tz_offset: tz_offset,
loc_city: city,
loc_country: country
loc_country: country,
is_bot: is_bot
}
)
......@@ -143,6 +144,29 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
defp parse_ua(req) do
Raxx.get_header(req, "user-agent", nil)
|> UAInspector.parse()
|> case do
%UAInspector.Result{} = ua ->
{
ua.user_agent,
unknown_2_str(ua.client.name),
unknown_2_str(ua.client.version),
unknown_2_str(ua.os.name),
unknown_2_str(ua.os.version),
unknown_2_str(ua.device.type),
false
}
%UAInspector.Result.Bot{} = ua ->
{
ua.user_agent,
unknown_2_str(ua.name),
"n/a",
"n/a",
"n/a",
"n/a",
true
}
end
end
defp parse_referrer(body) do
......@@ -172,7 +196,7 @@ defmodule Tilastokeskus.Reception.Routes.PageView do
defp parse_url(%{} = body) do
case Map.get(body, "url") do
nil ->
{nil, nil, nil}
{nil, nil, nil, nil}
url ->
parsed = URI.parse(url)
......
defmodule Tilastokeskus.Archive.Repo.Migrations.IsBot do
use Ecto.Migration
def change do
alter table(:events) do
add(:is_bot, :boolean, default: false)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment