Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Handle s3 timeout on settings page (#4036)
ruslandoga7af8273702df
diff --git a/lib/plausible/exports.ex b/lib/plausible/exports.exindex eed07ca9bd..184a70707b 100644--- a/lib/plausible/exports.ex+++ b/lib/plausible/exports.ex@@ -139,17 +139,14 @@ defmodule Plausible.Exports do])end- @doc "Gets S3 export for a site"- @spec get_s3_export(pos_integer) :: export | nil- def get_s3_export(site_id) do+ @doc "Gets S3 export for a site. Raises if object storage is unavailable."+ @spec get_s3_export!(pos_integer, non_neg_integer) :: export | nil+ def get_s3_export!(site_id, retries \\ 0) dopath = s3_export_key(site_id)bucket = Plausible.S3.exports_bucket()head_object_op = ExAws.S3.head_object(bucket, path)- case ExAws.request(head_object_op) do- {:error, {:http_error, 404, _response}} ->- nil-+ case ExAws.request(head_object_op, retries: retries) do{:ok, %{status_code: 200, headers: headers}} ->"attachment; filename=" <> filename = :proplists.get_value("content-disposition", headers)name = String.trim(filename, "\"")@@ -169,13 +166,19 @@ defmodule Plausible.Exports doexpires_at: expires_at,size: String.to_integer(size)}++ {:error, {:http_error, 404, _response}} ->+ nil++ {:error, %Mint.TransportError{} = e} ->+ raise eendend- @doc "Deletes S3 export for a site"- @spec delete_s3_export(pos_integer) :: :ok- def delete_s3_export(site_id) do- if export = get_s3_export(site_id) do+ @doc "Deletes S3 export for a site. Raises if object storage is unavailable."+ @spec delete_s3_export!(pos_integer) :: :ok+ def delete_s3_export!(site_id) do+ if export = get_s3_export!(site_id) doexports_bucket = Plausible.S3.exports_bucket()delete_op = ExAws.S3.delete_object(exports_bucket, export.path)ExAws.request!(delete_op)diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex 1d04e940eb..738ef40c3b 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -683,7 +683,7 @@ defmodule PlausibleWeb.SiteController dodef download_export(conn, _params) do%{id: site_id, domain: domain} = conn.assigns.site- if s3_export = Plausible.Exports.get_s3_export(site_id) do+ if s3_export = Plausible.Exports.get_s3_export!(site_id) dos3_bucket = Plausible.S3.exports_bucket()download_url = Plausible.S3.download_url(s3_bucket, s3_export.path)redirect(conn, external: download_url)diff --git a/lib/plausible_web/live/csv_export.ex b/lib/plausible_web/live/csv_export.exindex bd584d0f6d..2682cdf768 100644--- a/lib/plausible_web/live/csv_export.ex+++ b/lib/plausible_web/live/csv_export.ex@@ -38,7 +38,7 @@ defmodule PlausibleWeb.Live.CSVExport doget_export =case storage do"s3" ->- &Exports.get_s3_export/1+ &Exports.get_s3_export!/1"local" ->%{domain: domain, timezone: timezone} = socket.assigns.site@@ -71,7 +71,7 @@ defmodule PlausibleWeb.Live.CSVExport doend"discarded" ->- assign(socket, status: "failed")+ assign(socket, status: "export_failed")"cancelled" -># credo:disable-for-next-line Credo.Check.Refactor.Nesting@@ -88,6 +88,9 @@ defmodule PlausibleWeb.Live.CSVExport doassign(socket, status: "can_schedule")endend+ rescue+ _e ->+ assign(socket, status: "fetch_error", export: nil)end@impl true@@ -98,8 +101,10 @@ defmodule PlausibleWeb.Live.CSVExport do<.prepare_download /><% "in_progress" -> %><.in_progress />- <% "failed" -> %>- <.failed />+ <% "export_failed" -> %>+ <.export_failed />+ <% "fetch_error" -> %>+ <.fetch_export_failed /><% "ready" -> %><.downloadstorage={@storage}@@ -140,7 +145,18 @@ defmodule PlausibleWeb.Live.CSVExport do"""end- defp failed(assigns) do+ defp fetch_export_failed(assigns) do+ ~H"""+ <div class="flex items-center">+ <Heroicons.exclamation_circle class="w-4 h-4 text-red-500" />+ <p class="ml-2 text-sm text-gray-500">+ Something went wrong when fetching exports. Please try again later.+ </p>+ </div>+ """+ end++ defp export_failed(assigns) do~H"""<div class="flex items-center"><Heroicons.exclamation_circle class="w-4 h-4 text-red-500" />@@ -227,7 +243,7 @@ defmodule PlausibleWeb.Live.CSVExport do%{storage: storage, site_id: site_id} = socket.assignscase storage do- "s3" -> Exports.delete_s3_export(site_id)+ "s3" -> Exports.delete_s3_export!(site_id)"local" -> Exports.delete_local_export(site_id)enddiff --git a/lib/workers/notify_exported_analytics.ex b/lib/workers/notify_exported_analytics.exindex cdad6891d8..20e4249920 100644--- a/lib/workers/notify_exported_analytics.ex+++ b/lib/workers/notify_exported_analytics.ex@@ -22,7 +22,7 @@ defmodule Plausible.Workers.NotifyExportedAnalytics do"success" ->case storage do"s3" ->- %{expires_at: expires_at} = Plausible.Exports.get_s3_export(site_id)+ %{expires_at: expires_at} = Plausible.Exports.get_s3_export!(site_id, _retries = 10)PlausibleWeb.Email.export_success(user, site, expires_at)"local" ->diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex f525d32506..b3629816dd 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -744,6 +744,32 @@ defmodule PlausibleWeb.SiteControllerTest doassert response =~ "The import process might be taking longer due to the amount of data"assert response =~ "and rate limiting enforced by Google Analytics"end++ test "displays CSV export button", %{conn: conn, site: site} do+ assert conn |> get("/#{site.domain}/settings/imports-exports") |> html_response(200) =~+ "Prepare download"+ end+ end++ describe "GET /:website/settings/imports-exports when object storage is unreachable" do+ setup [:create_user, :log_in, :create_site]++ setup tags do+ if tags[:async] do+ raise "this test modifies application envinronment and can't be run asynchronously"+ end++ prev_env = Application.get_env(:ex_aws, :s3)+ new_env = Keyword.update!(prev_env, :port, fn prev_port -> prev_port + 1 end)+ Application.put_env(:ex_aws, :s3, new_env)+ on_exit(fn -> Application.put_env(:ex_aws, :s3, prev_env) end)+ end++ @tag capture_log: true, ee_only: true+ test "displays error message", %{conn: conn, site: site} do+ assert conn |> get("/#{site.domain}/settings/imports-exports") |> html_response(200) =~+ "Something went wrong when fetching exports. Please try again later."+ endenddescribe "GET /:website/settings/integrations for self-hosting" do