From 0ab8d653be86c1e3da94b9bd702cb77efe058d89 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 30 Mar 2022 13:47:41 +0300 Subject: [PATCH] Add retry mechanism to HTTPoison --- lib/plausible/google/api.ex | 8 ++-- .../site/import_from_google_confirm.html.eex | 10 +++-- lib/plausible_web/views/form_helpers.ex | 37 +++++++++++++++---- mix.exs | 1 + mix.lock | 1 + 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.ex index 1eff5d9f5..5539e9490 100644 --- a/lib/plausible/google/api.ex +++ b/lib/plausible/google/api.ex @@ -2,6 +2,7 @@ defmodule Plausible.Google.Api do alias Plausible.Imported use Timex require Logger + require HTTPoison.Retry @scope URI.encode_www_form( "https://www.googleapis.com/auth/webmasters.readonly email https://www.googleapis.com/auth/analytics.readonly" @@ -191,8 +192,8 @@ defmodule Plausible.Google.Api do "https://analyticsreporting.googleapis.com/v4/reports:batchGet", Jason.encode!(%{reportRequests: [report]}), [Authorization: "Bearer #{token}"], - timeout: 30_000, - recv_timeout: 30_000 + timeout: 15_000, + recv_timeout: 15_000 ) case res.status_code do @@ -346,7 +347,7 @@ defmodule Plausible.Google.Api do sortOrder: "DESCENDING" } ], - pageSize: 10_000, + pageSize: 5_000, pageToken: page_token } @@ -360,6 +361,7 @@ defmodule Plausible.Google.Api do timeout: 30_000, recv_timeout: 30_000 ) + |> HTTPoison.Retry.autoretry(max_attempts: 3, wait: 5_000) if res.status_code == 200 do report = List.first(Jason.decode!(res.body)["reports"]) diff --git a/lib/plausible_web/templates/site/import_from_google_confirm.html.eex b/lib/plausible_web/templates/site/import_from_google_confirm.html.eex index e47949b13..7b243ede3 100644 --- a/lib/plausible_web/templates/site/import_from_google_confirm.html.eex +++ b/lib/plausible_web/templates/site/import_from_google_confirm.html.eex @@ -7,7 +7,7 @@ <% {:ok, start_date} -> %>
- Choose the view in your Google Analytics account that will be imported to the <%= @site.domain %> dashboard + Confirm the view and dates in your Google Analytics account to be imported to the Plausible dashboard
@@ -16,17 +16,19 @@ <%= hidden_input f, :view_id, readonly: "true", value: @selected_view_id %>
- Import historical data from your first Google Analytics visitor to your first Plausible visitor + Historical data will be imported from your first Google Analytics visitor to your first Plausible visitor
<%= styled_label f, :start_date, "From" %> - <%= styled_date_input f, :start_date, value: start_date, readonly: "true" %> + <%= styled_text_input f, :start_date, value: PlausibleWeb.EmailView.date_format(start_date), readonly: "true" %> + <%= hidden_input f, :start_date, value: start_date, readonly: "true" %>
→
<%= styled_label f, :end_date, "To" %> - <%= styled_date_input f, :end_date, value: @end_date, readonly: "true" %> + <%= styled_text_input f, :end_date, value: PlausibleWeb.EmailView.date_format(@end_date), readonly: "true" %> + <%= hidden_input f, :end_date, value: @end_date, readonly: "true" %>
<% {:error, error} -> %> diff --git a/lib/plausible_web/views/form_helpers.ex b/lib/plausible_web/views/form_helpers.ex index c96375ee0..97cb24ccc 100644 --- a/lib/plausible_web/views/form_helpers.ex +++ b/lib/plausible_web/views/form_helpers.ex @@ -5,22 +5,43 @@ defmodule PlausibleWeb.FormHelpers do Phoenix.HTML.Form.label(form, field, text, opts) end - @date_input_opts [ + @text_input_opts [ class: - "mt-1 block w-full px-3 py-2 text-base dark:bg-gray-900 dark:text-gray-300 dark:border-gray-500 border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" + "mt-1 block w-full px-3 py-2 text-base dark:text-gray-100 dark:border-gray-500 border-gray-300 focus:outline-none sm:text-sm rounded-md" ] - def styled_date_input(form, field, opts \\ []) do - opts = Keyword.merge(@date_input_opts, opts) - Phoenix.HTML.Form.date_input(form, field, opts) + @active_text_input_class "dark:bg-gray-900 focus:ring-indigo-500 focus:border-indigo-500" + @disabled_text_input_class "bg-gray-100 dark:bg-gray-800 select-none" + def styled_text_input(form, field, opts \\ []) do + opts = merge_opts(@text_input_opts, opts) + + extra_class = + if opts[:readonly] == "true", do: @disabled_text_input_class, else: @active_text_input_class + + opts = merge_opts(opts, class: extra_class) + + Phoenix.HTML.Form.text_input(form, field, opts) end @select_opts [ class: - "mt-1 block w-full pl-3 pr-10 py-2 text-base dark:bg-gray-900 dark:text-gray-100 dark:border-gray-500 border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" + "mt-1 block w-full pl-3 pr-10 py-2 text-base dark:text-gray-100 dark:border-gray-500 border-gray-300 focus:outline-none sm:text-sm rounded-md" ] - + @active_select_class "dark:bg-gray-900 focus:ring-indigo-500 focus:border-indigo-500" + @disabled_select_class "bg-gray-100 dark:bg-gray-800 select-none" def styled_select(form, field, options, opts \\ []) do - opts = Keyword.merge(@select_opts, opts) + opts = merge_opts(@select_opts, opts) + + extra_class = + if opts[:disabled] == "true", do: @disabled_select_class, else: @active_select_class + + opts = merge_opts(opts, class: extra_class) Phoenix.HTML.Form.select(form, field, options, opts) end + + defp merge_opts(opts1, opts2) do + Keyword.merge(opts1, opts2, fn + :class, v1, v2 -> v1 <> " " <> v2 + _k, _v1, v2 -> v2 + end) + end end diff --git a/mix.exs b/mix.exs index 0b7e66752..b2314cda8 100644 --- a/mix.exs +++ b/mix.exs @@ -73,6 +73,7 @@ defmodule Plausible.MixProject do {:bamboo_smtp, "~> 4.1"}, {:sentry, "~> 8.0"}, {:httpoison, "~> 1.4"}, + {:httpoison_retry, "~> 1.0.0"}, {:ex_machina, "~> 2.3", only: :test}, {:excoveralls, "~> 0.10", only: :test}, {:double, "~> 0.8.0", only: :test}, diff --git a/mix.lock b/mix.lock index 2ce2bd6c0..4a6ee9239 100644 --- a/mix.lock +++ b/mix.lock @@ -49,6 +49,7 @@ "hpack": {:hex, :hpack_erl, "0.2.3", "17670f83ff984ae6cd74b1c456edde906d27ff013740ee4d9efaa4f1bf999633", [:rebar3], [], "hexpm", "06f580167c4b8b8a6429040df36cc93bba6d571faeaec1b28816523379cbb23a"}, "html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"}, "httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"}, + "httpoison_retry": {:hex, :httpoison_retry, "1.0.1", "1dd7d8f39079ae43a74bc787e3b86cc925e97b2e32fb9279420060b4a25cec30", [:mix], [{:httpoison, "~> 0.13 or ~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}], "hexpm", "507f825b0a1b59fa4d5e72c9f9f250e2f587fcb81d7908d77a1d5f4037d98814"}, "hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},