Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization #5

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 21 additions & 69 deletions lib/mave_metrics_web/channels/session_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,24 @@ defmodule MaveMetricsWeb.SessionChannel do
%{"identifier" => identifier, "key" => key} = params,
%{id: _session_id, assigns: %{ua: ua}} = socket
) do
with %UAInspector.Result.Bot{name: _name} <- UAInspector.parse(ua) do
{:error, %{reason: "bot"}}
else
parsed_ua ->
case Keys.valid_key?(key) do
{:ok, key} ->
source_url = params["source_url"] || socket.assigns.source_url

# we're not storing the complete user-agent string to make it impossible to make a fingerprint
session_attrs =
%{
metadata: params["session_data"]
}
|> Map.merge(parsed_ua |> session_info())

{:ok, video} = Stats.find_or_create_video(source_url, identifier, params["metadata"])

{:ok, session} = Stats.create_session(video, key, session_attrs)
{:ok, socket |> assign(:session_id, session.id) |> monitor(self())}

{:error, _reason} ->
{:error, %{reason: "invalid key"}}
end
case Keys.valid_key?(key) do
{:ok, key} ->
source_url = params["source_url"] || socket.assigns.source_url

# we're not storing the complete user-agent string to make it impossible to make a fingerprint
session_attrs =
%{
metadata: params["session_data"]
}
|> Map.merge(ua)

{:ok, video} = Stats.find_or_create_video(source_url, identifier, params["metadata"])

{:ok, session} = Stats.create_session(video, key, session_attrs)
{:ok, socket |> assign(:ua, nil) |> assign(:session_id, session.id) |> monitor(self())}

{:error, _reason} ->
{:error, %{reason: "invalid key"}}
end
end

Expand Down Expand Up @@ -160,21 +155,21 @@ defmodule MaveMetricsWeb.SessionChannel do
end

# https://github.com/phoenixframework/phoenix/issues/3844
defp monitor(socket, pid) do
defp monitor(%{assigns: %{session_id: session_id}} = socket, pid) do
Task.Supervisor.start_child(MaveMetrics.TaskSupervisor, fn ->
Process.flag(:trap_exit, true)
ref = Process.monitor(pid)

receive do
{:DOWN, ^ref, :process, _pid, _reason} ->
on_disconnect(socket)
on_disconnect(session_id)
end
end)

socket
end

defp on_disconnect(%{assigns: %{session_id: session_id}} = _socket) do
defp on_disconnect(session_id) do
with true <- Stats.duration_not_closed?(:play, session_id),
%{timestamp: timestamp, from: from} = duration <- Stats.get_last_duration(session_id) do
now = DateTime.utc_now()
Expand Down Expand Up @@ -208,47 +203,4 @@ defmodule MaveMetricsWeb.SessionChannel do
_ -> nil
end
end

defp on_disconnect(_socket), do: nil

defp session_info(%{os_family: platform, device: device, client: browser}) do
%{
browser_type: browser |> get_browser_type,
platform: platform |> get_platform,
device_type: device |> get_device
}
end

defp get_browser_type(%{name: browser})
when browser in ["Chrome Mobile", "Chrome Mobile iOS", "Chrome"],
do: :chrome

defp get_browser_type(%{name: browser})
when browser in ["Firefox Mobile", "Firefox Mobile iOS", "Firefox"],
do: :firefox

defp get_browser_type(%{name: browser})
when browser in ["Mobile Safari", "Safari Technology Preview", "Safari"],
do: :safari

defp get_browser_type(%{name: browser}) when browser in ["Opera Mobile", "Opera"], do: :opera
defp get_browser_type(%{name: browser}) when browser in ["Edge Mobile", "Edge"], do: :edge

defp get_browser_type(%{name: browser}) when browser in ["IE Mobile", "Internet Explorer"],
do: :ie

defp get_browser_type(%{name: browser}) when browser in ["Brave"], do: :brave
defp get_browser_type(_browser), do: :other

defp get_platform(platform) when platform in ["Windows", "Mac", "Linux", "iOS", "Android"],
do: platform |> String.downcase() |> String.to_atom()

defp get_platform(_platform), do: :other

defp get_device(%{type: device}) when device == "smartphone", do: :mobile

defp get_device(%{type: device}) when device in ["tablet", "desktop"],
do: device |> String.to_atom()

defp get_device(_device), do: :other
end
47 changes: 46 additions & 1 deletion lib/mave_metrics_web/channels/video_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
@impl true
def connect(%{"source_url" => source_url} = _params, socket, connect_info) do
ua = connect_info[:user_agent]
{:ok, socket |> assign(:ua, ua) |> assign(:source_url, source_url)}

{:ok,
socket
|> assign(:ua, UAInspector.parse(ua) |> session_info())
|> assign(:source_url, source_url)}
end

@impl true
Expand All @@ -16,4 +20,45 @@

@impl true
def id(_socket), do: nil

defp session_info(%{os_family: platform, device: device, client: browser}) do
%{
browser_type: browser |> get_browser_type,
platform: platform |> get_platform,
device_type: device |> get_device
}
end

defp get_browser_type(%{name: browser})
when browser in ["Chrome Mobile", "Chrome Mobile iOS", "Chrome"],
do: :chrome

defp get_browser_type(%{name: browser})
when browser in ["Firefox Mobile", "Firefox Mobile iOS", "Firefox"],
do: :firefox

defp get_browser_type(%{name: browser})
when browser in ["Mobile Safari", "Safari Technology Preview", "Safari"],
do: :safari

defp get_browser_type(%{name: browser}) when browser in ["Opera Mobile", "Opera"], do: :opera
defp get_browser_type(%{name: browser}) when browser in ["Edge Mobile", "Edge"], do: :edge

defp get_browser_type(%{name: browser}) when browser in ["IE Mobile", "Internet Explorer"],
do: :ie

defp get_browser_type(%{name: browser}) when browser in ["Brave"], do: :brave
defp get_browser_type(_browser), do: :other

defp get_platform(platform) when platform in ["Windows", "Mac", "Linux", "iOS", "Android"],
do: platform |> String.downcase() |> String.to_atom()
Dismissed Show dismissed Hide dismissed

defp get_platform(_platform), do: :other

defp get_device(%{type: device}) when device == "smartphone", do: :mobile

defp get_device(%{type: device}) when device in ["tablet", "desktop"],
do: device |> String.to_atom()
Dismissed Show dismissed Hide dismissed

defp get_device(_device), do: :other
end
Loading