Add fingerprint sessions (#36)
parent
fadb55c9c5
commit
aaa9c67e47
@ -0,0 +1,74 @@
|
||||
defmodule Plausible.Ingest.FingerprintSession do
|
||||
use GenServer
|
||||
use Plausible.Repo
|
||||
|
||||
@session_timeout Application.get_env(:plausible, :session_timeout)
|
||||
|
||||
def on_event(event) do
|
||||
user_session = :global.whereis_name(event.fingerprint)
|
||||
|
||||
if is_pid(user_session) do
|
||||
GenServer.cast(user_session, {:on_event, event})
|
||||
else
|
||||
GenServer.start_link(__MODULE__, event, name: {:global, event.fingerprint})
|
||||
end
|
||||
end
|
||||
|
||||
def on_unload(fingerprint, timestamp) do
|
||||
user_session = :global.whereis_name(fingerprint)
|
||||
|
||||
if is_pid(user_session) do
|
||||
GenServer.cast(user_session, {:on_unload, timestamp})
|
||||
end
|
||||
end
|
||||
|
||||
def init(event) do
|
||||
timer = Process.send_after(self(), :finalize, @session_timeout)
|
||||
{:ok, %{first_event: event, last_event: event, timer: timer, is_bounce: true, last_unload: nil}}
|
||||
end
|
||||
|
||||
def handle_cast({:on_event, event}, state) do
|
||||
Process.cancel_timer(state[:timer])
|
||||
new_timer = Process.send_after(self(), :finalize, @session_timeout)
|
||||
{:noreply, %{state | timer: new_timer, last_event: event, is_bounce: false, last_unload: nil}}
|
||||
end
|
||||
|
||||
def handle_cast({:on_unload, timestamp}, state) do
|
||||
{:noreply, %{state | last_unload: timestamp}}
|
||||
end
|
||||
|
||||
def handle_info(:finalize, state) do
|
||||
first_event = state[:first_event]
|
||||
last_event = state[:last_event]
|
||||
|
||||
if !is_potential_leftover?(first_event) do
|
||||
length = if state[:last_unload] do
|
||||
Timex.diff(state[:last_unload], first_event.timestamp, :seconds)
|
||||
end
|
||||
|
||||
Plausible.FingerprintSession.changeset(%Plausible.FingerprintSession{}, %{
|
||||
hostname: first_event.hostname,
|
||||
domain: first_event.domain,
|
||||
fingerprint: first_event.fingerprint,
|
||||
entry_page: first_event.pathname,
|
||||
exit_page: last_event.pathname,
|
||||
is_bounce: state[:is_bounce],
|
||||
length: length,
|
||||
referrer: first_event.referrer,
|
||||
referrer_source: first_event.referrer_source,
|
||||
country_code: first_event.country_code,
|
||||
operating_system: first_event.operating_system,
|
||||
browser: first_event.browser,
|
||||
start: first_event.timestamp
|
||||
}) |> Repo.insert!
|
||||
end
|
||||
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
defp is_potential_leftover?(%{timestamp: timestamp}) do
|
||||
server_start_time = Application.get_env(:plausible, :server_start)
|
||||
Timex.diff(timestamp, server_start_time, :milliseconds) < @session_timeout
|
||||
end
|
||||
|
||||
end
|
||||
@ -0,0 +1,31 @@
|
||||
defmodule Plausible.FingerprintSession do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "fingerprint_sessions" do
|
||||
field :hostname, :string
|
||||
field :domain, :string
|
||||
field :fingerprint, :string
|
||||
|
||||
field :start, :naive_datetime, null: false
|
||||
field :length, :integer
|
||||
field :is_bounce, :boolean
|
||||
field :entry_page, :string
|
||||
field :exit_page, :string
|
||||
|
||||
field :referrer, :string
|
||||
field :referrer_source, :string
|
||||
field :country_code, :string
|
||||
field :screen_size, :string
|
||||
field :operating_system, :string
|
||||
field :browser, :string
|
||||
|
||||
timestamps(inserted_at: :timestamp, updated_at: false)
|
||||
end
|
||||
|
||||
def changeset(session, attrs) do
|
||||
session
|
||||
|> cast(attrs, [:hostname, :domain, :entry_page, :exit_page, :referrer, :fingerprint, :start, :length, :is_bounce, :operating_system, :browser, :referrer_source, :country_code, :screen_size])
|
||||
|> validate_required([:hostname, :domain, :fingerprint, :is_bounce, :start])
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,26 @@
|
||||
defmodule Plausible.Repo.Migrations.AddFingerprintSesssions do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:fingerprint_sessions) do
|
||||
add :hostname, :text, null: false
|
||||
add :domain, :text, null: false
|
||||
add :fingerprint, :string, size: 64, null: false
|
||||
|
||||
add :is_bounce, :boolean, null: false
|
||||
add :length, :integer
|
||||
|
||||
add :referrer, :string
|
||||
add :referrer_source, :string
|
||||
add :country_code, :string
|
||||
add :screen_size, :string
|
||||
add :operating_system, :string
|
||||
add :browser, :string
|
||||
add :exit_page, :text
|
||||
add :entry_page, :text, null: false
|
||||
add :start, :naive_datetime, null: false
|
||||
|
||||
timestamps(inserted_at: :timestamp, updated_at: false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue