defmodule PlausibleWeb.Live.ImportsExportsSettings do @moduledoc """ LiveView allowing listing and deleting imports. """ use PlausibleWeb, :live_view use Phoenix.HTML import PlausibleWeb.Components.Generic import PlausibleWeb.TextHelpers alias Plausible.Imported alias Plausible.Imported.SiteImport alias Plausible.Sites require Plausible.Imported.SiteImport def mount( _params, %{"domain" => domain, "current_user_id" => user_id}, socket ) do socket = socket |> assign_new(:site, fn -> Sites.get_for_user!(user_id, domain, [:owner, :admin, :super_admin]) end) |> assign_new(:site_imports, fn %{site: site} -> site |> Imported.list_all_imports() |> Enum.map(&%{site_import: &1, live_status: &1.status}) end) |> assign_new(:pageview_counts, fn %{site: site} -> Plausible.Stats.Clickhouse.imported_pageview_counts(site) end) |> assign_new(:current_user, fn -> Plausible.Repo.get(Plausible.Auth.User, user_id) end) |> assign_new(:max_imports, fn %{site: site} -> Imported.max_complete_imports(site) end) :ok = Imported.listen() {:ok, socket} end def render(assigns) do import_in_progress? = Enum.any?( assigns.site_imports, &(&1.live_status in [SiteImport.pending(), SiteImport.importing()]) ) at_maximum? = length(assigns.site_imports) >= assigns.max_imports csv_imports_exports_enabled? = FunWithFlags.enabled?(:csv_imports_exports, for: assigns.site) import_warning = cond do import_in_progress? -> "No new imports can be started until the import in progress is completed or cancelled." at_maximum? -> "Maximum of #{assigns.max_imports} imports is reached. " <> "Delete or cancel an existing import to start a new one." true -> nil end assigns = assign(assigns, import_in_progress?: import_in_progress?, at_maximum?: at_maximum?, import_warning: import_warning, csv_imports_exports_enabled?: csv_imports_exports_enabled? ) ~H"""
<.button_link class="w-36 h-20" theme="bright" disabled={@import_in_progress? or @at_maximum?} href={Plausible.Google.API.import_authorize_url(@site.id)} > Google Analytics import <.button_link :if={@csv_imports_exports_enabled?} class="w-36 h-20" theme="bright" disabled={@import_in_progress? or @at_maximum?} href={"/#{URI.encode_www_form(@site.domain)}/settings/import"} > New CSV import

<%= @import_warning %>

Existing Imports

A maximum of <%= @max_imports %> imports at any time is allowed.

There are no imports yet for this site.

""" end def handle_info({:notification, :analytics_imports_jobs, details}, socket) do {site_imports, updated?} = update_imports(socket.assigns.site_imports, details["import_id"], details["event"]) pageview_counts = if updated? do Plausible.Stats.Clickhouse.imported_pageview_counts(socket.assigns.site) else socket.assigns.pageview_counts end {:noreply, assign(socket, site_imports: site_imports, pageview_counts: pageview_counts)} end defp pageview_count(site_import, pageview_counts) do count = Map.get(pageview_counts, site_import.id, 0) if site_import.legacy do count + Map.get(pageview_counts, 0, 0) else count end end defp update_imports(site_imports, import_id, status_str) do Enum.map_reduce(site_imports, false, fn %{site_import: %{id: ^import_id}} = entry, _changed? -> new_status = case status_str do "complete" -> SiteImport.completed() "fail" -> SiteImport.failed() "transient_fail" -> SiteImport.importing() end {%{entry | live_status: new_status}, true} entry, changed? -> {entry, changed?} end) end end