<commit_before>defmodule ExMoney.Saltedge.LoginsRefreshWorker do
  use GenServer

  require Logger

  alias ExMoney.Repo
  alias ExMoney.Login
  alias ExMoney.Account

  def start_link(_opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, name: :logins_refresh_worker)
  end

  def init(:ok) do
    logins = Repo.all(Login)

    case logins do
      [] -> :ignore
      logins ->
        Enum.each(logins, fn(login) ->
          Process.send_after(self(), {:refresh, login}, 1000)
        end)

        {:ok, {}}
    end
  end

  def handle_info({:refresh, login}, state) do
    last_refreshed_at = last_refreshed_at(login.last_refreshed_at)

    case Timex.Date.diff(last_refreshed_at, Timex.Date.now, :secs) do
      secs when secs >= 3600 ->
        refresh_login(login)
      secs when secs < 3600 ->
        next_run = 3600 - secs

        Process.send_after(self(), {:refresh, login}, next_run * 1000)
    end

    {:noreply, state}
  end

  defp refresh_login(login) do
    body = """
      { "data": { "fetch_type": "recent" }}
    """

    result = ExMoney.Saltedge.Client.request(:put, "logins/#{login.saltedge_login_id}/refresh", body)
    |> Poison.decode!
    Logger.info("Refreshed login #{login.saltedge_login_id} with result => #{inspect(result)}")

    case result["data"]["refreshed"] do
      true ->
        login = Login.update_changeset(
          login,
          %{last_refreshed_at: :erlang.universaltime()}
        ) |> Repo.update!

        sync_transactions(login, login.stage)
      false ->
        Process.send_after(self(), {:refresh, login}, 5000)
    end
  end

  # Saltedge is still fetching date from bank, let's try in 5 secs.
  defp sync_transactions(login, stage) when stage != "finish" do
    Process.send_after(self(), {:refresh, login}, 5000)
  end

  # Yey, stage is finished, let's sync transactions then.
  defp sync_transactions(login, _stage) do
    accounts = Account.by_saltedge_login_id([login.saltedge_login_id])
    |> Repo.all
<commit_msg>Do not try to decode decoded response<commit_after>defmodule ExMoney.Saltedge.LoginsRefreshWorker do
  use GenServer

  require Logger

  alias ExMoney.Repo
  alias ExMoney.Login
  alias ExMoney.Account

  def start_link(_opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, name: :logins_refresh_worker)
  end

  def init(:ok) do
    logins = Repo.all(Login)

    case logins do
      [] -> :ignore
      logins ->
        Enum.each(logins, fn(login) ->
          Process.send_after(self(), {:refresh, login}, 1000)
        end)

        {:ok, {}}
    end
  end

  def handle_info({:refresh, login}, state) do
    last_refreshed_at = last_refreshed_at(login.last_refreshed_at)

    case Timex.Date.diff(last_refreshed_at, Timex.Date.now, :secs) do
      secs when secs >= 3600 ->
        refresh_login(login)
      secs when secs < 3600 ->
        next_run = 3600 - secs

        Process.send_after(self(), {:refresh, login}, next_run * 1000)
    end

    {:noreply, state}
  end

  defp refresh_login(login) do
    body = """
      { "data": { "fetch_type": "recent" }}
    """

    result = ExMoney.Saltedge.Client.request(:put, "logins/#{login.saltedge_login_id}/refresh", body)
    Logger.info("Refreshed login #{login.saltedge_login_id} with result => #{inspect(result)}")

    case result["data"]["refreshed"] do
      true ->
        login = Login.update_changeset(
          login,
          %{last_refreshed_at: :erlang.universaltime()}
        ) |> Repo.update!

        sync_transactions(login, login.stage)
      false ->
        Process.send_after(self(), {:refresh, login}, 5000)
    end
  end

  # Saltedge is still fetching date from bank, let's try in 5 secs.
  defp sync_transactions(login, stage) when stage != "finish" do
    Process.send_after(self(), {:refresh, login}, 5000)
  end

  # Yey, stage is finished, let's sync transactions then.
  defp sync_transactions(login, _stage) do
    accounts = Account.by_saltedge_login_id([login.saltedge_login_id])
    |> Repo.all

{'fraction_non_alphanumeric': 0.12418627941912869, 'fraction_numerical': 0.014521782674011016, 'mean_word_length': 2.947628458498024, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7478740', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1320, 'n_words': 305}