Implement UI for multiple imports (#3727)
* Create a stub of site settings section for imports and exports * Use legacy site import indication to determine UA import handling * Add provisional logos for upcoming import sources * Stub basics of import page * Add very rudimentary support for multiple UA imports * Implement imports list as live view * Add support for opening LV modal from backend and closing from frontend * Introduce notion of themes to `button` and `button_link` components * Add confirmation modal on deleting import * Swap GA4 logo * Implement disabled state support for `button_link` component * Disable export and non-implemented import sources * Use native starts start date for upper boundary of import time range * Ensure integrations view uses legacy UA import flow * Remove unnecessary preload in SiteController * Remove unnecessary exception for legacy imports * Move API controller stats tests under PlausibleWeb * Test listing imports * Add test for explicit listener setup * Add tests for legacy flag state in UA importer * Add test for purging legacy import data * Add tests for `Sites.native_stats_start_date` * Test forgetting imports * Add `Stats.Clickhouse.imported_pageview_counts/1` and fix test flakiness * Show page view counts on imports list * Add tests for static imports and exports view * Adjust button look slightly * Use `case` instead of `cond` * Make feature flag customisable per site * Fix buttons and empty state styling * Add another import to seeds * Use JS confirm dialog instead of modal for deletion confirmations * Revert "Add support for opening LV modal from backend and closing from frontend" This reverts commit 260e6c753032b451542e24be9edc2118790b5a00. * Default `legacy` to false when inserting new import jobs * Drop `method` attribute from `button_link` and `unstyled_link` componentspull/3846/head r2024.02.28-379
parent
fdbe4cc0a4
commit
39aa81a16f
@ -0,0 +1,144 @@
|
||||
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)
|
||||
|
||||
:ok = Imported.listen()
|
||||
|
||||
{:ok, assign(socket, max_imports: Imported.max_complete_imports())}
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<header class="relative border-b border-gray-200 pb-4">
|
||||
<h3 class="mt-8 text-md leading-6 font-medium text-gray-900 dark:text-gray-100">
|
||||
Existing Imports
|
||||
</h3>
|
||||
<p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">
|
||||
A maximum of <%= @max_imports %> imports at any time is allowed.
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<div
|
||||
:if={Enum.empty?(@site_imports)}
|
||||
class="text-gray-800 dark:text-gray-200 text-center mt-8 mb-12"
|
||||
>
|
||||
<p>There are no imports yet for this site.</p>
|
||||
</div>
|
||||
<ul :if={not Enum.empty?(@site_imports)}>
|
||||
<li :for={entry <- @site_imports} class="py-4 flex items-center justify-between space-x-4">
|
||||
<div class="flex flex-col">
|
||||
<p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">
|
||||
Import from <%= Plausible.Imported.SiteImport.label(entry.site_import) %>
|
||||
<span :if={entry.live_status == SiteImport.completed()} class="text-xs font-normal">
|
||||
(<%= Map.get(@pageview_counts, entry.site_import.id, 0) %> page views)
|
||||
</span>
|
||||
<Heroicons.clock
|
||||
:if={entry.live_status == SiteImport.pending()}
|
||||
class="inline-block h-6 w-5 text-indigo-600 dark:text-green-600"
|
||||
/>
|
||||
<.spinner
|
||||
:if={entry.live_status == SiteImport.importing()}
|
||||
class="inline-block h-6 w-5 text-indigo-600 dark:text-green-600"
|
||||
/>
|
||||
<Heroicons.check
|
||||
:if={entry.live_status == SiteImport.completed()}
|
||||
class="inline-block h-6 w-5 text-indigo-600 dark:text-green-600"
|
||||
/>
|
||||
<Heroicons.exclamation_triangle
|
||||
:if={entry.live_status == SiteImport.failed()}
|
||||
class="inline-block h-6 w-5 text-indigo-600 dark:text-green-600"
|
||||
/>
|
||||
</p>
|
||||
<p class="text-sm leading-5 text-gray-500 dark:text-gray-200">
|
||||
From <%= format_date(entry.site_import.start_date) %> to <%= format_date(
|
||||
entry.site_import.end_date
|
||||
) %> (created on <%= format_date(
|
||||
entry.site_import.inserted_at || entry.site_import.start_date
|
||||
) %>)
|
||||
</p>
|
||||
</div>
|
||||
<.button
|
||||
data-to={"/#{URI.encode_www_form(@site.domain)}/settings/forget-import/#{entry.site_import.id}"}
|
||||
theme="danger"
|
||||
data-method="delete"
|
||||
data-csrf={Plug.CSRFProtection.get_csrf_token()}
|
||||
class="sm:ml-3 sm:w-auto w-full"
|
||||
data-confirm="Are you sure you want to delete this import?"
|
||||
>
|
||||
<span :if={entry.live_status in [SiteImport.completed(), SiteImport.failed()]}>
|
||||
Delete Import
|
||||
</span>
|
||||
<span :if={entry.live_status not in [SiteImport.completed(), SiteImport.failed()]}>
|
||||
Cancel Import
|
||||
</span>
|
||||
</.button>
|
||||
</li>
|
||||
</ul>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_info({:notification, :analytics_imports_jobs, status}, socket) do
|
||||
[{status_str, import_id}] = Enum.to_list(status)
|
||||
{site_imports, updated?} = update_imports(socket.assigns.site_imports, import_id, status_str)
|
||||
|
||||
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 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
|
||||
@ -0,0 +1,61 @@
|
||||
<div class="shadow bg-white dark:bg-gray-800 sm:rounded-md sm:overflow-hidden py-6 px-4 sm:p-6">
|
||||
<header class="relative border-b border-gray-200 pb-4">
|
||||
<h2 class="text-lg leading-6 font-medium text-gray-900 dark:text-gray-100">
|
||||
Import Data
|
||||
</h2>
|
||||
<p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">
|
||||
Import existing data from external sources. Pick one of the options below to start a new import.
|
||||
</p>
|
||||
|
||||
<PlausibleWeb.Components.Generic.docs_info slug="google-analytics-import" />
|
||||
</header>
|
||||
|
||||
<div class="mt-5 flex gap-x-4">
|
||||
<PlausibleWeb.Components.Generic.button_link
|
||||
class="w-36 h-20"
|
||||
theme="bright"
|
||||
href={Plausible.Google.Api.import_authorize_url(@site.id, "import", false)}
|
||||
>
|
||||
<img src="/images/icon/universal_analytics_logo.svg" alt="New Universal Analytics import" />
|
||||
</PlausibleWeb.Components.Generic.button_link>
|
||||
|
||||
<PlausibleWeb.Components.Generic.button_link
|
||||
class="w-36 h-20 opacity-40 cursor-not-allowed"
|
||||
theme="bright"
|
||||
href=""
|
||||
>
|
||||
<img
|
||||
src="/images/icon/google_analytics_4_logo.svg"
|
||||
width="110"
|
||||
alt="New Universal Analytics import"
|
||||
/>
|
||||
</PlausibleWeb.Components.Generic.button_link>
|
||||
|
||||
<PlausibleWeb.Components.Generic.button_link
|
||||
class="w-36 h-20 opacity-40 cursor-not-allowed"
|
||||
theme="bright"
|
||||
href=""
|
||||
>
|
||||
<img class="h-16" src="/images/icon/csv_logo.svg" alt="New CSV import" />
|
||||
</PlausibleWeb.Components.Generic.button_link>
|
||||
</div>
|
||||
|
||||
<%= live_render(@conn, PlausibleWeb.Live.ImportsExportsSettings,
|
||||
session: %{"domain" => @site.domain}
|
||||
) %>
|
||||
|
||||
<header class="relative border-b border-gray-200 pb-4">
|
||||
<h2 class="mt-8 text-lg leading-6 font-medium text-gray-900 dark:text-gray-100">
|
||||
Export Data
|
||||
</h2>
|
||||
<p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">
|
||||
Export all your data into CSV format.
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<div class="mt-4">
|
||||
<PlausibleWeb.Components.Generic.button_link disabled href="">
|
||||
Export to CSV
|
||||
</PlausibleWeb.Components.Generic.button_link>
|
||||
</div>
|
||||
</div>
|
||||
@ -0,0 +1 @@
|
||||
<svg height="64" viewBox="0 0 56 64" width="56" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="m5.106 0c-2.802 0-5.073 2.272-5.073 5.074v53.841c0 2.803 2.271 5.074 5.073 5.074h45.774c2.801 0 5.074-2.271 5.074-5.074v-38.605l-18.903-20.31h-31.945z" fill="#45b058" fill-rule="evenodd"/><path d="m20.306 43.197c.126.144.198.324.198.522 0 .378-.306.72-.703.72-.18 0-.378-.072-.504-.234-.702-.846-1.891-1.387-3.007-1.387-2.629 0-4.627 2.017-4.627 4.88 0 2.845 1.999 4.879 4.627 4.879 1.134 0 2.25-.486 3.007-1.369.125-.144.324-.233.504-.233.415 0 .703.359.703.738 0 .18-.072.36-.198.504-.937.972-2.215 1.693-4.015 1.693-3.457 0-6.176-2.521-6.176-6.212s2.719-6.212 6.176-6.212c1.8.001 3.096.721 4.015 1.711zm6.802 10.714c-1.782 0-3.187-.594-4.213-1.495-.162-.144-.234-.342-.234-.54 0-.361.27-.757.702-.757.144 0 .306.036.432.144.828.739 1.98 1.314 3.367 1.314 2.143 0 2.827-1.152 2.827-2.071 0-3.097-7.112-1.386-7.112-5.672 0-1.98 1.764-3.331 4.123-3.331 1.548 0 2.881.467 3.853 1.278.162.144.252.342.252.54 0 .36-.306.72-.703.72-.144 0-.306-.054-.432-.162-.882-.72-1.98-1.044-3.079-1.044-1.44 0-2.467.774-2.467 1.909 0 2.701 7.112 1.152 7.112 5.636.001 1.748-1.187 3.531-4.428 3.531zm16.994-11.254-4.159 10.335c-.198.486-.685.81-1.188.81h-.036c-.522 0-1.008-.324-1.207-.81l-4.142-10.335c-.036-.09-.054-.18-.054-.288 0-.36.323-.793.81-.793.306 0 .594.18.72.486l3.889 9.992 3.889-9.992c.108-.288.396-.486.72-.486.468 0 .81.378.81.793.001.09-.017.198-.052.288z" fill="#fff"/><g clip-rule="evenodd" fill-rule="evenodd"><path d="m56.001 20.357v1h-12.8s-6.312-1.26-6.128-6.707c0 0 .208 5.707 6.003 5.707z" fill="#349c42"/><path d="m37.098.006v14.561c0 1.656 1.104 5.791 6.104 5.791h12.8l-18.904-20.352z" fill="#fff" opacity=".5"/></g></svg>
|
||||
|
After Width: | Height: | Size: 1.7 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 5.0 KiB |
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="26 -29 120 60" width="120" height="60"><linearGradient id="A" gradientUnits="userSpaceOnUse" x1="173.867" y1="-72.65" x2="216.749" y2="-72.65"><stop offset="0" stop-color="#e96f0b"/><stop offset="1" stop-color="#f37901"/></linearGradient><g fill-rule="evenodd"><path d="M47.33 16H36.226c-1.364 0-2.468-1.104-2.468-2.468V8.208c0-1.364 1.104-2.468 2.468-2.468h7.013v-7.792c0-1.364 1.104-2.468 2.468-2.468H53.5v20.455h-6.17z" fill="#ffc517"/><path d="M61.3 16h-7.8v-27.532c0-1.364 1.104-2.468 2.468-2.468h5.325c1.364 0 2.468 1.104 2.468 2.468v25.065C63.693 14.896 62.6 16 61.3 16z" fill="#f57e02"/><path d="M216.708-72.65v32.646c0 5.713-4.625 10.338-10.338 10.338h-32.646v-85.968l42.984 42.984z" fill="url(#A)" transform="matrix(.238687 0 0 .238687 11.968065 23.081216)"/></g><g fill="#757575"><path d="M74.093 7.228l2.388 6.27h-4.627l2.24-6.27zm-.896-2.1l-5.224 13.73h1.94l1.343-3.73h5.82l1.343 3.73h1.94l-5.224-13.73h-1.94zm28.2 13.742h1.8V5.138h-1.8v13.73zm-18.35-8.07c.597-.896 1.8-1.642 2.985-1.642 2.388 0 3.582 1.642 3.582 4.03v5.82h-1.8v-5.522c0-1.94-1.045-2.687-2.388-2.687-1.493 0-2.537 1.493-2.537 2.836v5.224h-1.8V9.32h1.8l.15 1.493zm8.06 5.234c0-2.1 1.94-3.284 4.18-3.284 1.343 0 2.24.3 2.537.597v-.3c0-1.493-1.194-2.24-2.388-2.24-1.045 0-1.94.448-2.24 1.343l-1.642-.746c.3-.896 1.493-2.24 3.88-2.24 2.24 0 4.18 1.343 4.18 4.03v5.672h-1.642v-1.343h-.15c-.448.746-1.493 1.642-3.134 1.642-1.94 0-3.582-1.194-3.582-3.134m6.716-1.194s-.746-.597-2.24-.597c-1.8 0-2.537 1.045-2.537 1.8 0 1.045 1.045 1.493 1.94 1.493 1.343 0 2.836-1.194 2.836-2.687"/><path d="M105.735 23.05l2.1-4.925-3.73-8.657h1.8l2.836 6.567 2.836-6.567h1.8l-5.82 13.582h-1.8zm26.27-10.45c-.448-1.194-1.493-1.94-2.537-1.94-1.493 0-2.836 1.343-2.836 3.284s1.343 3.284 2.836 3.284c1.045 0 2.1-.746 2.537-1.8l1.493.896c-.746 1.642-2.24 2.687-4.03 2.687-2.537 0-4.627-2.24-4.627-5.075 0-2.985 2.1-5.075 4.627-5.075 1.8 0 3.284 1.045 4.03 2.687l-1.493 1.045z" fill-rule="evenodd"/><path d="M138.422 19.168c2.388 0 3.582-1.343 3.582-2.985 0-3.582-5.224-2.24-5.224-4.328 0-.746.597-1.194 1.642-1.194s2.1.448 2.537 1.194l1.045-1.045c-.597-.746-2.24-1.8-3.73-1.8-2.24 0-3.433 1.343-3.433 2.985 0 3.433 5.373 2.24 5.373 4.03 0 .896-.597 1.493-1.8 1.493s-1.8-.746-2.388-1.493L134.7 17.08c.896.896 2.24 2.1 3.73 2.1zm-17.015-.298h1.8V9.318h-1.8v9.552z"/><path d="M122.153 4.7c.746 0 1.194.597 1.194 1.194 0 .746-.597 1.194-1.194 1.194s-1.194-.597-1.194-1.194.597-1.194 1.194-1.194zm-2.537 12.687l.3 1.343h-1.8c-1.642 0-2.388-1.194-2.388-2.985V11.1h-1.8V9.3h1.8V6.482h1.8v2.836h2.1v1.8h-2.1v4.925c0 1.343 1.045 1.343 2.1 1.343z" fill-rule="evenodd"/><path d="M77.048-10.042v2.24h5.224c-.15 1.194-.597 2.1-1.194 2.687-.746.746-1.94 1.642-4.03 1.642-3.134 0-5.672-2.537-5.672-5.82 0-3.134 2.537-5.82 5.672-5.82 1.8 0 2.985.746 3.88 1.493l1.493-1.493c-1.343-1.194-2.985-2.24-5.373-2.24-4.328 0-8.06 3.582-8.06 7.9s3.73 7.9 8.06 7.9c2.388 0 4.18-.746 5.522-2.24 1.493-1.493 1.94-3.433 1.94-5.075 0-.448 0-1.045-.15-1.343h-7.313zm13.282-1.8c-2.836 0-5.075 2.1-5.075 5.075s2.24 5.075 5.075 5.075a5.03 5.03 0 0 0 5.075-5.075c0-2.985-2.24-5.075-5.075-5.075zm0 8.2c-1.493 0-2.836-1.343-2.836-3.134S88.837-9.9 90.33-9.9s2.837 1.2 2.837 3.13-1.343 3.134-2.836 3.134zm24.47-7.156c-.597-.597-1.493-1.194-2.836-1.194-2.537 0-4.776 2.24-4.776 5.075s2.24 5.075 4.776 5.075c1.194 0 2.24-.597 2.687-1.194h.15v.746c0 1.94-1.045 2.985-2.687 2.985-1.343 0-2.24-1.045-2.537-1.8l-1.94.746c.597 1.343 2.1 2.985 4.478 2.985 2.687 0 4.925-1.493 4.925-5.373V-12h-2.1v1.194zm-2.537 7.164c-1.493 0-2.687-1.343-2.687-3.134s1.194-3.134 2.687-3.134 2.687 1.343 2.687 3.134-1.194 3.134-2.687 3.134zm-10.897-8.2c-2.836 0-5.075 2.1-5.075 5.075s2.24 5.075 5.075 5.075a5.03 5.03 0 0 0 5.075-5.075c.15-2.985-2.24-5.075-5.075-5.075zm0 8.2c-1.493 0-2.836-1.343-2.836-3.134s1.343-3.134 2.836-3.134 2.836 1.194 2.836 3.134-1.343 3.134-2.836 3.134zM118.7-17.206h2.24v15.522h-2.24v-15.522zm8.358 13.582c-1.194 0-1.94-.597-2.537-1.493l6.866-2.836-.3-.597c-.448-1.194-1.8-3.284-4.328-3.284-2.687 0-4.776 2.1-4.776 5.075a5.03 5.03 0 0 0 5.075 5.075c2.388 0 3.73-1.493 4.328-2.24l-1.8-1.194c-.597.896-1.343 1.493-2.537 1.493zm-.15-6.27c.896 0 1.642.448 1.94 1.194l-4.627 1.94c0-2.24 1.493-3.134 2.687-3.134z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 4.2 KiB |
@ -0,0 +1,62 @@
|
||||
defmodule Plausible.ImportedTest do
|
||||
use Plausible.DataCase
|
||||
use Plausible
|
||||
|
||||
alias Plausible.Imported
|
||||
|
||||
describe "list_all_imports/1" do
|
||||
test "returns empty when there are no imports" do
|
||||
site = insert(:site)
|
||||
|
||||
assert Imported.list_all_imports(site) == []
|
||||
end
|
||||
|
||||
test "returns imports in various states" do
|
||||
site = insert(:site)
|
||||
|
||||
_rogue_import = insert(:site_import)
|
||||
|
||||
import1 = insert(:site_import, site: site, status: :pending)
|
||||
import2 = insert(:site_import, site: site, status: :importing)
|
||||
import3 = insert(:site_import, site: site, status: :completed)
|
||||
import4 = insert(:site_import, site: site, status: :failed)
|
||||
|
||||
assert [%{id: id1}, %{id: id2}, %{id: id3}, %{id: id4}] = Imported.list_all_imports(site)
|
||||
|
||||
ids = [id1, id2, id3, id4]
|
||||
|
||||
assert import1.id in ids
|
||||
assert import2.id in ids
|
||||
assert import3.id in ids
|
||||
assert import4.id in ids
|
||||
end
|
||||
|
||||
test "returns one legacy import when present with respective site import entry" do
|
||||
site = insert(:site)
|
||||
{:ok, opts} = add_imported_data(%{site: site})
|
||||
site = Map.new(opts).site
|
||||
site_import = insert(:site_import, site: site, legacy: true)
|
||||
site_import_id = site_import.id
|
||||
|
||||
assert [%{id: ^site_import_id}] = Imported.list_all_imports(site)
|
||||
end
|
||||
|
||||
test "returns legacy import without respective site import entry" do
|
||||
site = insert(:site)
|
||||
{:ok, opts} = add_imported_data(%{site: site})
|
||||
site = Map.new(opts).site
|
||||
imported_start_date = site.imported_data.start_date
|
||||
imported_end_date = site.imported_data.end_date
|
||||
|
||||
assert [
|
||||
%{
|
||||
id: 0,
|
||||
source: :universal_analytics,
|
||||
start_date: ^imported_start_date,
|
||||
end_date: ^imported_end_date,
|
||||
status: :completed
|
||||
}
|
||||
] = Imported.list_all_imports(site)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,4 +1,4 @@
|
||||
defmodule Plausible.ImportedTest do
|
||||
defmodule PlausibleWeb.Api.StatsController.ImportedTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
use Timex
|
||||
|
||||
Loading…
Reference in New Issue