<commit_before>defmodule Armsday.RoomChannel do
  use Phoenix.Channel

  def join("armsday:redemptions", _message, socket) do
    {:ok, socket}
  end

  def handle_in("redemptions_start", _, socket) do
    socket = handle_redemptions(socket)
    {:noreply, socket}
  end

  def handle_in("privileged_bungie_response", %{"url" => url, "response" => response}, socket) do
    send socket.assigns[:replyto], {url, response}
    {:noreply, socket}
  end

  defp handle_redemptions(socket) do
    child_pid = spawn_link(fn ->
      username = psn_id(socket)
      membership_id = Armsday.Destiny.membership_id(username)
      characters = Armsday.Destiny.character_ids(membership_id)
      IO.puts("#{username} is membership_id #{membership_id} and their characters are")
      IO.inspect(characters)
      data = Enum.map(characters, fn x -> redemptions(membership_id, x, socket) end) |> List.flatten
      push socket, "redemptions", %{redemptions: data}
    end)
    socket = assign(socket, :replyto, child_pid)
    socket
  end

  defp psn_id(socket) do
    privileged_bungie("https://www.bungie.net/Platform/User/GetBungieNetUser/", socket)["Response"]["psnId"]
  end

  defp redemptions(membership_id, character_id, socket) do
    redemptions = privileged_bungie("https://www.bungie.net/Platform/Destiny/2/Account/#{URI.encode(membership_id)}/Character/#{URI.encode(character_id)}/Advisors/", socket) |> Armsday.Destiny.reformat_redemptions
  end

  defp privileged_bungie(url, socket) do
    push socket, "privileged_bungie", %{url: url}
    receive do
      {^url, response} -> response
    end
  end
end<commit_msg>Privileged bungie requests for character advisors are now done in parallel<commit_after>defmodule Armsday.RoomChannel do
  use Phoenix.Channel

  def join("armsday:redemptions", _message, socket) do
    {:ok, socket}
  end

  def handle_in("redemptions_start", _, socket) do
    socket = handle_redemptions(socket)
    {:noreply, socket}
  end

  def handle_in("privileged_bungie_response", %{"url" => url, "response" => response}, socket) do
    case SocketRequestAgent.get_pid_for_request(url) do
      nil -> send socket.assigns[:replyto], {url, response}
      pid -> send pid, {url,response}
    end
    {:noreply, socket}
  end

  defp handle_redemptions(socket) do
    child_pid = spawn_link(fn ->
      username = psn_id(socket)
      membership_id = Armsday.Destiny.membership_id(username)
      characters = Armsday.Destiny.character_ids(membership_id)
      IO.puts("#{username} is membership_id #{membership_id} and their characters are")
      IO.inspect(characters)
      data = pmap(characters, fn x -> 
       redemptions(membership_id, x, socket)
      end) |> List.flatten
    push socket, "redemptions", %{redemptions: data}
    end)
  socket = assign(socket, :replyto, child_pid)
  socket
  end

  defp psn_id(socket) do
    privileged_bungie("https://www.bungie.net/Platform/User/GetBungieNetUser/", socket)["Response"]["psnId"]
  end

  defp redemptions(membership_id, character_id, socket) do
    url = "https://www.bungie.net/Platform/Destiny/2/Account/#{URI.encode(membership_id)}/Character/#{URI.encode(character_id)}/Advisors/"
    SocketRequestAgent.new_request(url, self)
    redemptions = privileged_bungie(url, socket) |> Armsday.Destiny.reformat_redemptions
  end

  defp privileged_bungie(url, socket) do
    push socket, "privileged_bungie", %{url: url}
    receive do
      {^url, response} -> response
    end
  end

  def pmap( collection, fun) do
    me = self
    collection
    |> Enum.map(fn(elem) ->
      spawn_link fn -> (send me, {self, fun.(elem)}) end
    end)
    |> Enum.map(fn (pid) ->
      receive do {^pid, result} -> result end
    end)
  end

end
{'fraction_non_alphanumeric': 0.12311827956989248, 'fraction_numerical': 0.0005376344086021505, 'mean_word_length': 3.9154557463672393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7364340', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1263, 'n_words': 271}