Remove references to site.imported_data (#4006)

* Remove references to `site.imported_data`

* Count pre-existing ID 0 imports when showing pageview count summary for legacy imports

* Fix tests after rebase

* Dry `delete_imported_stats!`

* Clean up remaining imported data references and add notes
This commit is contained in:
Adrian Gruntkowski 2024-04-19 11:15:51 +02:00 committed by GitHub
parent 83643450c4
commit c10580777e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
38 changed files with 272 additions and 451 deletions

View File

@ -58,7 +58,7 @@ defmodule Plausible.DataMigration.SiteImports do
params = params =
site.imported_data site.imported_data
|> Imported.SiteImport.from_legacy() |> from_legacy()
|> Map.put(:site_id, site.id) |> Map.put(:site_id, site.id)
|> Map.take([:legacy, :start_date, :end_date, :source, :status, :site_id]) |> Map.take([:legacy, :start_date, :end_date, :source, :status, :site_id])
@ -216,4 +216,22 @@ defmodule Plausible.DataMigration.SiteImports do
select: %{max_date: fragment("max(?)", q.date)} select: %{max_date: fragment("max(?)", q.date)}
) )
end end
defp from_legacy(%Site.ImportedData{} = data) do
status =
case data.status do
"ok" -> SiteImport.completed()
"error" -> SiteImport.failed()
_ -> SiteImport.importing()
end
%SiteImport{
id: 0,
legacy: true,
start_date: data.start_date,
end_date: data.end_date,
source: :universal_analytics,
status: status
}
end
end end

View File

@ -67,20 +67,23 @@ defmodule Plausible.Imported do
Repo.get_by(SiteImport, id: import_id, site_id: site.id) Repo.get_by(SiteImport, id: import_id, site_id: site.id)
end end
@spec get_legacy_import(Site.t()) :: SiteImport.t() | nil
def get_legacy_import(site) do
from(i in SiteImport,
where: i.site_id == ^site.id and i.legacy == true,
order_by: [desc: i.updated_at],
limit: 1
)
|> Repo.one()
end
defdelegate listen(), to: Imported.Importer defdelegate listen(), to: Imported.Importer
@spec list_all_imports(Site.t(), atom()) :: [SiteImport.t()] @spec list_all_imports(Site.t(), atom()) :: [SiteImport.t()]
def list_all_imports(site, status \\ nil) do def list_all_imports(site, status \\ nil) do
imports =
from(i in SiteImport, where: i.site_id == ^site.id, order_by: [desc: i.inserted_at]) from(i in SiteImport, where: i.site_id == ^site.id, order_by: [desc: i.inserted_at])
|> maybe_filter_by_status(status) |> maybe_filter_by_status(status)
|> Repo.all() |> Repo.all()
if site.imported_data && not Enum.any?(imports, & &1.legacy) do
imports ++ [SiteImport.from_legacy(site.imported_data)]
else
imports
end
end end
@spec other_imports_in_progress?(SiteImport.t()) :: boolean() @spec other_imports_in_progress?(SiteImport.t()) :: boolean()
@ -113,7 +116,7 @@ defmodule Plausible.Imported do
ids = Enum.map(ids, &elem(&1, 1)) ids = Enum.map(ids, &elem(&1, 1))
# account for legacy imports as well # account for legacy imports as well
if has_legacy? || (site.imported_data && site.imported_data.status == "ok") do if has_legacy? do
[0 | ids] [0 | ids]
else else
ids ids
@ -132,23 +135,7 @@ defmodule Plausible.Imported do
) )
|> Repo.one() |> Repo.one()
dates = dates || %{start_date: nil, end_date: nil} dates || %{start_date: nil, end_date: nil}
if site.imported_data && site.imported_data.status == "ok" do
start_date =
[dates.start_date, site.imported_data.start_date]
|> Enum.reject(&is_nil/1)
|> Enum.min(Date, fn -> nil end)
end_date =
[dates.end_date, site.imported_data.end_date]
|> Enum.reject(&is_nil/1)
|> Enum.max(Date, fn -> nil end)
%{start_date: start_date, end_date: end_date}
else
dates
end
end end
@spec delete_imports_for_site(Site.t()) :: :ok @spec delete_imports_for_site(Site.t()) :: :ok

View File

@ -33,34 +33,11 @@ defmodule Plausible.Imported.SiteImport do
defmacro unquote(status)(), do: unquote(status) defmacro unquote(status)(), do: unquote(status)
end end
@spec label(t() | Site.ImportedData.t()) :: String.t() @spec label(t()) :: String.t()
def label(%__MODULE__{source: source, label: label}) do def label(%{source: source, label: label}) do
build_label(ImportSources.by_name(source).label(), label) build_label(ImportSources.by_name(source).label(), label)
end end
# NOTE: this is necessary for backwards compatibility
# with legacy imports
def label(%Site.ImportedData{source: source}), do: build_label(source, nil)
@spec from_legacy(Site.ImportedData.t()) :: t()
def from_legacy(%Site.ImportedData{} = data) do
status =
case data.status do
"ok" -> completed()
"error" -> failed()
_ -> importing()
end
%__MODULE__{
id: 0,
legacy: true,
start_date: data.start_date,
end_date: data.end_date,
source: :universal_analytics,
status: status
}
end
@spec create_changeset(Site.t(), User.t(), map()) :: Ecto.Changeset.t() @spec create_changeset(Site.t(), User.t(), map()) :: Ecto.Changeset.t()
def create_changeset(site, user, params) do def create_changeset(site, user, params) do
%__MODULE__{} %__MODULE__{}

View File

@ -5,8 +5,6 @@ defmodule Plausible.Imported.UniversalAnalytics do
use Plausible.Imported.Importer use Plausible.Imported.Importer
alias Plausible.Repo
@missing_values ["(none)", "(not set)", "(not provided)", "(other)"] @missing_values ["(none)", "(not set)", "(not provided)", "(other)"]
@impl true @impl true
@ -18,49 +16,6 @@ defmodule Plausible.Imported.UniversalAnalytics do
@impl true @impl true
def email_template(), do: "google_analytics_import.html" def email_template(), do: "google_analytics_import.html"
@impl true
def before_start(site_import) do
if site_import.legacy do
site = Repo.preload(site_import, :site).site
site
|> Plausible.Site.start_import(
site_import.start_date,
site_import.end_date,
label()
)
|> Repo.update!()
end
:ok
end
@impl true
def on_success(site_import, _extra_data) do
if site_import.legacy do
site = Repo.preload(site_import, :site).site
site
|> Plausible.Site.import_success()
|> Repo.update!()
end
:ok
end
@impl true
def on_failure(site_import) do
if site_import.legacy do
site = Repo.preload(site_import, :site).site
site
|> Plausible.Site.import_failure()
|> Repo.update!()
end
:ok
end
@impl true @impl true
def parse_args( def parse_args(
%{"view_id" => view_id, "start_date" => start_date, "end_date" => end_date} = args %{"view_id" => view_id, "start_date" => start_date, "end_date" => end_date} = args

View File

@ -60,6 +60,10 @@ defmodule Plausible.Purge do
site_import = Repo.preload(site_import, :site) site_import = Repo.preload(site_import, :site)
delete_imported_stats!(site_import.site, site_import.id) delete_imported_stats!(site_import.site, site_import.id)
if site_import.legacy do
delete_imported_stats!(site_import.site, 0)
end
:ok :ok
end end

View File

@ -29,6 +29,7 @@ defmodule Plausible.Site do
field :domain_changed_from, :string field :domain_changed_from, :string
field :domain_changed_at, :naive_datetime field :domain_changed_at, :naive_datetime
# NOTE: needed by `SiteImports` data migration script
embeds_one :imported_data, Plausible.Site.ImportedData, on_replace: :update embeds_one :imported_data, Plausible.Site.ImportedData, on_replace: :update
many_to_many :members, User, join_through: Plausible.Site.Membership many_to_many :members, User, join_through: Plausible.Site.Membership
@ -153,31 +154,6 @@ defmodule Plausible.Site do
change(site, native_stats_start_at: val) change(site, native_stats_start_at: val)
end end
def start_import(site, start_date, end_date, imported_source, status \\ "importing") do
change(site,
imported_data: %{
start_date: start_date,
end_date: end_date,
source: imported_source,
status: status
}
)
end
def import_success(site) do
change(site,
imported_data: %{status: "ok"}
)
end
def import_failure(site) do
change(site, imported_data: %{status: "error"})
end
def remove_imported_data(site) do
change(site, imported_data: nil)
end
defp clean_domain(changeset) do defp clean_domain(changeset) do
clean_domain = clean_domain =
(get_field(changeset, :domain) || "") (get_field(changeset, :domain) || "")

View File

@ -1,6 +1,8 @@
defmodule Plausible.Site.ImportedData do defmodule Plausible.Site.ImportedData do
@moduledoc """ @moduledoc """
Embedded schema for analytics imports Embedded schema for analytics imports
NOTE: needed by `SiteImports` data migration script
""" """
use Ecto.Schema use Ecto.Schema

View File

@ -7,6 +7,10 @@ defmodule PlausibleWeb.Components.Settings do
import PlausibleWeb.Components.Generic import PlausibleWeb.Components.Generic
alias Plausible.Imported.SiteImport
require Plausible.Imported.SiteImport
embed_templates("../templates/site/settings_search_console.html") embed_templates("../templates/site/settings_search_console.html")
embed_templates("../templates/site/settings_google_import.html") embed_templates("../templates/site/settings_google_import.html")
end end

View File

@ -237,8 +237,10 @@ defmodule PlausibleWeb.SiteController do
Plausible.Google.API.fetch_verified_properties(site.google_auth) Plausible.Google.API.fetch_verified_properties(site.google_auth)
end end
legacy_import = Plausible.Imported.get_legacy_import(site)
imported_pageviews = imported_pageviews =
if site.imported_data do if legacy_import do
Plausible.Stats.Clickhouse.imported_pageview_count(site) Plausible.Stats.Clickhouse.imported_pageview_count(site)
else else
0 0
@ -249,6 +251,7 @@ defmodule PlausibleWeb.SiteController do
conn conn
|> render("settings_integrations.html", |> render("settings_integrations.html",
site: site, site: site,
legacy_import: legacy_import,
imported_pageviews: imported_pageviews, imported_pageviews: imported_pageviews,
has_plugins_tokens?: has_plugins_tokens?, has_plugins_tokens?: has_plugins_tokens?,
search_console_domains: search_console_domains, search_console_domains: search_console_domains,
@ -646,15 +649,7 @@ defmodule PlausibleWeb.SiteController do
def forget_import(conn, %{"import_id" => import_id}) do def forget_import(conn, %{"import_id" => import_id}) do
site = conn.assigns.site site = conn.assigns.site
cond do if site_import = Plausible.Imported.get_import(site, import_id) do
import_id == "0" ->
Plausible.Purge.delete_imported_stats!(site, 0)
site
|> Plausible.Site.remove_imported_data()
|> Repo.update!()
site_import = Plausible.Imported.get_import(site, import_id) ->
Oban.cancel_all_jobs( Oban.cancel_all_jobs(
from(j in Oban.Job, from(j in Oban.Job,
where: where:
@ -666,14 +661,6 @@ defmodule PlausibleWeb.SiteController do
Plausible.Purge.delete_imported_stats!(site_import) Plausible.Purge.delete_imported_stats!(site_import)
Plausible.Repo.delete!(site_import) Plausible.Repo.delete!(site_import)
if site_import.legacy do
Plausible.Purge.delete_imported_stats!(site, 0)
site
|> Plausible.Site.remove_imported_data()
|> Repo.update!()
end
end end
conn conn
@ -701,10 +688,6 @@ defmodule PlausibleWeb.SiteController do
Plausible.Purge.delete_imported_stats!(site) Plausible.Purge.delete_imported_stats!(site)
Plausible.Imported.delete_imports_for_site(site) Plausible.Imported.delete_imports_for_site(site)
site
|> Plausible.Site.remove_imported_data()
|> Repo.update!()
end end
conn conn

View File

@ -137,7 +137,7 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do
<%= Plausible.Imported.SiteImport.label(entry.site_import) %> <%= Plausible.Imported.SiteImport.label(entry.site_import) %>
<span :if={entry.live_status == SiteImport.completed()} class="text-xs font-normal"> <span :if={entry.live_status == SiteImport.completed()} class="text-xs font-normal">
(<%= PlausibleWeb.StatsView.large_number_format( (<%= PlausibleWeb.StatsView.large_number_format(
Map.get(@pageview_counts, entry.site_import.id, 0) pageview_count(entry.site_import, @pageview_counts)
) %> page views) ) %> page views)
</span> </span>
</p> </p>
@ -183,6 +183,16 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do
{:noreply, assign(socket, site_imports: site_imports, pageview_counts: pageview_counts)} {:noreply, assign(socket, site_imports: site_imports, pageview_counts: pageview_counts)}
end 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 defp update_imports(site_imports, import_id, status_str) do
Enum.map_reduce(site_imports, false, fn Enum.map_reduce(site_imports, false, fn
%{site_import: %{id: ^import_id}} = entry, _changed? -> %{site_import: %{id: ^import_id}} = entry, _changed? ->

View File

@ -11,11 +11,11 @@
<%= if Keyword.get(Application.get_env(:plausible, :google), :client_id) do %> <%= if Keyword.get(Application.get_env(:plausible, :google), :client_id) do %>
<%= cond do %> <%= cond do %>
<% @site.imported_data && @site.imported_data.status == "importing" -> %> <% @legacy_import && @legacy_import.status in [SiteImport.pending(), SiteImport.importing()] -> %>
<li class="py-4 flex items-center justify-between space-x-4"> <li class="py-4 flex items-center justify-between space-x-4">
<div class="flex flex-col"> <div class="flex flex-col">
<p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100"> <p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">
Import from <%= @site.imported_data.source %> Import from Google Analytics
<svg <svg
class="animate-spin -mr-1 ml-1 h-4 w-4 inline text-indigo-600" class="animate-spin -mr-1 ml-1 h-4 w-4 inline text-indigo-600"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
@ -40,8 +40,8 @@
</svg> </svg>
</p> </p>
<p class="text-sm leading-5 text-gray-500 dark:text-gray-200"> <p class="text-sm leading-5 text-gray-500 dark:text-gray-200">
From <%= PlausibleWeb.EmailView.date_format(@site.imported_data.start_date) %> to <%= PlausibleWeb.EmailView.date_format( From <%= PlausibleWeb.EmailView.date_format(@legacy_import.start_date) %> to <%= PlausibleWeb.EmailView.date_format(
@site.imported_data.end_date @legacy_import.end_date
) %> ) %>
</p> </p>
</div> </div>
@ -52,11 +52,11 @@
"inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150" "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150"
) %> ) %>
</li> </li>
<% @site.imported_data && @site.imported_data.status == "ok" -> %> <% @legacy_import && @legacy_import.status == SiteImport.completed() -> %>
<li class="py-4 flex items-center justify-between space-x-4"> <li class="py-4 flex items-center justify-between space-x-4">
<div class="flex flex-col"> <div class="flex flex-col">
<p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100"> <p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">
Import from <%= @site.imported_data.source %> Import from Google Analytics
<svg <svg
class="h-4 w-4 inline ml-1 -mt-1 text-green-600" class="h-4 w-4 inline ml-1 -mt-1 text-green-600"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
@ -69,8 +69,8 @@
</svg> </svg>
</p> </p>
<p class="text-sm leading-5 text-gray-500 dark:text-gray-200"> <p class="text-sm leading-5 text-gray-500 dark:text-gray-200">
From <%= PlausibleWeb.EmailView.date_format(@site.imported_data.start_date) %> to <%= PlausibleWeb.EmailView.date_format( From <%= PlausibleWeb.EmailView.date_format(@legacy_import.start_date) %> to <%= PlausibleWeb.EmailView.date_format(
@site.imported_data.end_date @legacy_import.end_date
) %> ) %>
</p> </p>
</div> </div>
@ -85,7 +85,7 @@
) %> ) %>
</li> </li>
<% true -> %> <% true -> %>
<%= if @site.imported_data && @site.imported_data.status == "error" do %> <%= if @legacy_import && @legacy_import.status == SiteImport.failed() do %>
<div class="text-sm mt-2 text-gray-900 dark:text-gray-100"> <div class="text-sm mt-2 text-gray-900 dark:text-gray-100">
Your latest import has failed. You can try importing again by clicking the button below. If you try multiple times and the import keeps failing, please contact support. Your latest import has failed. You can try importing again by clicking the button below. If you try multiple times and the import keeps failing, please contact support.
</div> </div>

View File

@ -7,6 +7,7 @@
<PlausibleWeb.Components.Settings.settings_google_import <PlausibleWeb.Components.Settings.settings_google_import
site={@site} site={@site}
imported_pageviews={@imported_pageviews} imported_pageviews={@imported_pageviews}
legacy_import={@legacy_import}
/> />
<% end %> <% end %>

View File

@ -2,6 +2,10 @@ defmodule PlausibleWeb.SiteView do
use PlausibleWeb, :view use PlausibleWeb, :view
use Plausible use Plausible
alias Plausible.Imported.SiteImport
require Plausible.Imported.SiteImport
def plausible_url do def plausible_url do
PlausibleWeb.Endpoint.url() PlausibleWeb.Endpoint.url()
end end

View File

@ -256,51 +256,6 @@ native_stats_range
end) end)
|> Plausible.TestUtils.populate_stats() |> Plausible.TestUtils.populate_stats()
site =
site
|> Plausible.Site.start_import(
legacy_imported_stats_range.first,
legacy_imported_stats_range.last,
"Google Analytics"
)
|> Plausible.Repo.update!()
legacy_imported_stats_range
|> Enum.flat_map(fn date ->
Enum.flat_map(0..Enum.random(1..500), fn _ ->
[
Plausible.Factory.build(:imported_visitors,
date: date,
pageviews: Enum.random(1..20),
visitors: Enum.random(1..20),
bounces: Enum.random(1..20),
visits: Enum.random(1..200),
visit_duration: Enum.random(1000..10000)
),
Plausible.Factory.build(:imported_sources,
date: date,
source: Enum.random(["", "Facebook", "Twitter", "DuckDuckGo", "Google"]),
visitors: Enum.random(1..20),
visits: Enum.random(1..200),
bounces: Enum.random(1..20),
visit_duration: Enum.random(1000..10000)
),
Plausible.Factory.build(:imported_pages,
date: date,
visitors: Enum.random(1..20),
pageviews: Enum.random(1..20),
exits: Enum.random(1..20),
time_on_page: Enum.random(1000..10000)
)
]
end)
end)
|> then(&Plausible.TestUtils.populate_stats(site, &1))
site
|> Plausible.Site.import_success()
|> Plausible.Repo.update!()
site_import = site_import =
site site
|> Plausible.Imported.SiteImport.create_changeset(user, %{ |> Plausible.Imported.SiteImport.create_changeset(user, %{

View File

@ -6,7 +6,6 @@ defmodule Plausible.DataMigration.SiteImportsTest do
alias Plausible.DataMigration.SiteImports alias Plausible.DataMigration.SiteImports
alias Plausible.Imported alias Plausible.Imported
alias Plausible.Repo alias Plausible.Repo
alias Plausible.Site
describe "run/1" do describe "run/1" do
test "runs for empty dataset" do test "runs for empty dataset" do
@ -30,7 +29,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
test "adds site import entry when it's missing and adjusts end date" do test "adds site import entry when it's missing and adjusts end date" do
site = site =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
populate_stats(site, 0, [ populate_stats(site, 0, [
@ -54,7 +53,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
test "runs in dry mode without making any persistent changes" do test "runs in dry mode without making any persistent changes" do
site = site =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
populate_stats(site, 0, [ populate_stats(site, 0, [
@ -71,15 +70,14 @@ defmodule Plausible.DataMigration.SiteImportsTest do
site = Repo.reload!(site) site = Repo.reload!(site)
assert [%{id: id, legacy: true}] = Imported.list_all_imports(site) assert [] = Imported.list_all_imports(site)
assert id == 0
assert site.imported_data.end_date == ~D[2021-01-08] assert site.imported_data.end_date == ~D[2021-01-08]
end end
test "does not set end date to latter than the current one" do test "does not set end date to latter than the current one" do
site = site =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
populate_stats(site, 0, [ populate_stats(site, 0, [
@ -103,7 +101,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
test "removes site import when there are no stats" do test "removes site import when there are no stats" do
site = site =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2020-02-02], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2020-02-02], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
assert capture_io(fn -> assert capture_io(fn ->
@ -118,7 +116,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
test "leaves site and imports unchanged if everything fits" do test "leaves site and imports unchanged if everything fits" do
site = site =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
existing_import = existing_import =
@ -150,7 +148,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
test "only considers sites with completed site imports or 'ok' imported data" do test "only considers sites with completed site imports or 'ok' imported data" do
site1 = site1 =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error")
|> Repo.update!() |> Repo.update!()
existing_import1 = existing_import1 =
@ -164,7 +162,7 @@ defmodule Plausible.DataMigration.SiteImportsTest do
site2 = site2 =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "ok")
|> Repo.update!() |> Repo.update!()
existing_import2 = existing_import2 =
@ -178,12 +176,12 @@ defmodule Plausible.DataMigration.SiteImportsTest do
site3 = site3 =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error")
|> Repo.update!() |> Repo.update!()
site4 = site4 =
insert(:site) insert(:site)
|> Site.start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error") |> start_import(~D[2021-01-02], ~D[2021-01-08], "Google Analytics", "error")
|> Repo.update!() |> Repo.update!()
_existing_import3 = _existing_import3 =
@ -272,4 +270,15 @@ defmodule Plausible.DataMigration.SiteImportsTest do
end end
end end
end end
defp start_import(site, start_date, end_date, imported_source, status) do
change(site,
imported_data: %{
start_date: start_date,
end_date: end_date,
source: imported_source,
status: status
}
)
end
end end

View File

@ -70,8 +70,6 @@ defmodule Plausible.Imported.CSVImporterTest do
} }
] = Plausible.Imported.list_all_imports(site) ] = Plausible.Imported.list_all_imports(site)
assert %{imported_data: nil} = Repo.reload!(site)
assert CSVImporter.parse_args(args) == [ assert CSVImporter.parse_args(args) == [
uploads: uploads, uploads: uploads,
storage: on_full_build(do: "s3", else: "local") storage: on_full_build(do: "s3", else: "local")

View File

@ -50,15 +50,6 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
} }
] = Plausible.Imported.list_all_imports(site) ] = Plausible.Imported.list_all_imports(site)
assert %{
imported_data: %{
source: "Google Analytics",
start_date: ~D[2023-10-01],
end_date: ~D[2024-01-02],
status: "importing"
}
} = Repo.reload!(site)
assert opts = [_ | _] = UniversalAnalytics.parse_args(args) assert opts = [_ | _] = UniversalAnalytics.parse_args(args)
assert opts[:view_id] == "123" assert opts[:view_id] == "123"
@ -91,8 +82,6 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
legacy: false legacy: false
} }
] = Plausible.Imported.list_all_imports(site) ] = Plausible.Imported.list_all_imports(site)
assert %{imported_data: nil} = Repo.reload!(site)
end end
end end

View File

@ -30,34 +30,6 @@ defmodule Plausible.ImportedTest do
assert import3.id in ids assert import3.id in ids
assert import4.id in ids assert import4.id in ids
end 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
describe "get_imports_date_range/1" do describe "get_imports_date_range/1" do
@ -68,15 +40,7 @@ defmodule Plausible.ImportedTest do
end end
test "returns empty when only incomplete or failed imports are present" do test "returns empty when only incomplete or failed imports are present" do
site = site = insert(:site)
insert(:site)
|> Plausible.Site.start_import(
~D[2020-04-01],
~D[2022-06-22],
"Google Analytics",
"error"
)
|> Repo.update!()
_import1 = insert(:site_import, site: site, status: :pending) _import1 = insert(:site_import, site: site, status: :pending)
_import2 = insert(:site_import, site: site, status: :importing) _import2 = insert(:site_import, site: site, status: :importing)
@ -87,15 +51,7 @@ defmodule Plausible.ImportedTest do
end end
test "returns start and end dates considering all imports" do test "returns start and end dates considering all imports" do
site = site = insert(:site)
insert(:site)
|> Plausible.Site.start_import(
~D[2020-04-01],
~D[2022-06-22],
"Google Analytics",
"ok"
)
|> Repo.update!()
_import1 = _import1 =
insert(:site_import, insert(:site_import,
@ -114,7 +70,7 @@ defmodule Plausible.ImportedTest do
status: :completed status: :completed
) )
assert %{start_date: ~D[2020-04-01], end_date: ~D[2024-01-08]} = assert %{start_date: ~D[2020-04-02], end_date: ~D[2024-01-08]} =
Imported.get_imports_date_range(site) Imported.get_imports_date_range(site)
end end
end end

View File

@ -90,8 +90,6 @@ defmodule Plausible.SitesTest do
test "ignores imported stats" do test "ignores imported stats" do
site = insert(:site) site = insert(:site)
insert(:site_import, site: site) insert(:site_import, site: site)
{:ok, opts} = add_imported_data(%{site: site})
site = Map.new(opts).site
assert Sites.native_stats_start_date(site) == nil assert Sites.native_stats_start_date(site) == nil
end end

View File

@ -258,8 +258,6 @@ defmodule Plausible.Stats.ClickhouseTest do
describe "imported_pageview_counts/1" do describe "imported_pageview_counts/1" do
test "gets pageview counts for each of sites' imports" do test "gets pageview counts for each of sites' imports" do
site = insert(:site) site = insert(:site)
{:ok, opts} = add_imported_data(%{site: site})
site = Map.new(opts).site
import1 = insert(:site_import, site: site) import1 = insert(:site_import, site: site)
import2 = insert(:site_import, site: site) import2 = insert(:site_import, site: site)

View File

@ -203,7 +203,7 @@ defmodule Plausible.Stats.ComparisonsTest do
end end
describe "include_imported" do describe "include_imported" do
setup [:create_user, :create_new_site, :add_imported_data] setup [:create_user, :create_new_site, :create_site_import]
test "defaults to source_query.include_imported", %{site: site} do test "defaults to source_query.include_imported", %{site: site} do
query = Query.from(site, %{"period" => "day", "date" => "2023-01-01"}) query = Query.from(site, %{"period" => "day", "date" => "2023-01-01"})

View File

@ -473,10 +473,14 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
end end
describe "with imported data" do describe "with imported data" do
setup :add_imported_data setup :create_site_import
test "does not count imported stats unless specified", %{conn: conn, site: site} do test "does not count imported stats unless specified", %{
populate_stats(site, [ conn: conn,
site: site,
site_import: site_import
} do
populate_stats(site, site_import.id, [
build(:imported_visitors, date: ~D[2023-01-01]), build(:imported_visitors, date: ~D[2023-01-01]),
build(:pageview, timestamp: ~N[2023-01-01 00:00:00]) build(:pageview, timestamp: ~N[2023-01-01 00:00:00])
]) ])
@ -501,8 +505,12 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
} }
end end
test "counts imported stats when comparing with previous period", %{conn: conn, site: site} do test "counts imported stats when comparing with previous period", %{
populate_stats(site, [ conn: conn,
site: site,
site_import: site_import
} do
populate_stats(site, site_import.id, [
build(:imported_visitors, build(:imported_visitors,
visits: 2, visits: 2,
bounces: 1, bounces: 1,
@ -540,8 +548,12 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
} }
end end
test "ignores imported data when filters are applied", %{conn: conn, site: site} do test "ignores imported data when filters are applied", %{
populate_stats(site, [ conn: conn,
site: site,
site_import: site_import
} do
populate_stats(site, site_import.id, [
build(:imported_visitors, date: ~D[2023-01-01]), build(:imported_visitors, date: ~D[2023-01-01]),
build(:imported_sources, date: ~D[2023-01-01]), build(:imported_sources, date: ~D[2023-01-01]),
build(:pageview, build(:pageview,
@ -566,8 +578,12 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do
} }
end end
test "events metric with imported data is disallowed", %{conn: conn, site: site} do test "events metric with imported data is disallowed", %{
populate_stats(site, [ conn: conn,
site: site,
site_import: site_import
} do
populate_stats(site, site_import.id, [
build(:imported_visitors, date: ~D[2023-01-01]) build(:imported_visitors, date: ~D[2023-01-01])
]) ])

View File

@ -330,12 +330,15 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
end end
test "breaks down all metrics by visit:referrer with imported data", %{conn: conn, site: site} do test "breaks down all metrics by visit:referrer with imported data", %{conn: conn, site: site} do
site = site_import =
site insert(:site_import,
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") site: site,
|> Plausible.Repo.update!() start_date: ~D[2005-01-01],
end_date: Timex.today(),
source: :universal_analytics
)
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:pageview, referrer: "site.com", timestamp: ~N[2021-01-01 00:00:00]), build(:pageview, referrer: "site.com", timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, referrer: "site.com/1", timestamp: ~N[2021-01-01 00:00:00]), build(:pageview, referrer: "site.com/1", timestamp: ~N[2021-01-01 00:00:00]),
build(:imported_sources, build(:imported_sources,
@ -520,12 +523,15 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
end end
test "breaks down all metrics by visit:utm_source with imported data", %{conn: conn, site: site} do test "breaks down all metrics by visit:utm_source with imported data", %{conn: conn, site: site} do
site = site_import =
site insert(:site_import,
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") site: site,
|> Plausible.Repo.update!() start_date: ~D[2005-01-01],
end_date: Timex.today(),
source: :universal_analytics
)
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:pageview, utm_source: "SomeUTMSource", timestamp: ~N[2021-01-01 00:00:00]), build(:pageview, utm_source: "SomeUTMSource", timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, utm_source: "SomeUTMSource-1", timestamp: ~N[2021-01-01 00:00:00]), build(:pageview, utm_source: "SomeUTMSource-1", timestamp: ~N[2021-01-01 00:00:00]),
build(:imported_sources, build(:imported_sources,
@ -842,12 +848,15 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
test "pageviews breakdown by event:page - imported data having pageviews=0 and visitors=n should be bypassed", test "pageviews breakdown by event:page - imported data having pageviews=0 and visitors=n should be bypassed",
%{conn: conn, site: site} do %{conn: conn, site: site} do
site = site_import =
site insert(:site_import,
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") site: site,
|> Plausible.Repo.update!() start_date: ~D[2005-01-01],
end_date: Timex.today(),
source: :universal_analytics
)
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]), build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:25:00]), build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:25:00]),
build(:pageview, build(:pageview,
@ -2378,12 +2387,15 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
describe "metrics" do describe "metrics" do
test "returns time_on_page with imported data", %{conn: conn, site: site} do test "returns time_on_page with imported data", %{conn: conn, site: site} do
site = site_import =
site insert(:site_import,
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") site: site,
|> Plausible.Repo.update!() start_date: ~D[2005-01-01],
end_date: Timex.today(),
source: :universal_analytics
)
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:imported_pages, page: "/A", time_on_page: 40, date: ~D[2021-01-01]), build(:imported_pages, page: "/A", time_on_page: 40, date: ~D[2021-01-01]),
build(:imported_pages, page: "/A", time_on_page: 110, date: ~D[2021-01-01]), build(:imported_pages, page: "/A", time_on_page: 110, date: ~D[2021-01-01]),
build(:imported_pages, page: "/B", time_on_page: 499, date: ~D[2021-01-01]), build(:imported_pages, page: "/B", time_on_page: 499, date: ~D[2021-01-01]),

View File

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
use PlausibleWeb.ConnCase use PlausibleWeb.ConnCase
describe "GET /api/stats/:domain/browsers" do describe "GET /api/stats/:domain/browsers" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_site_import]
test "returns top browsers by unique visitors", %{conn: conn, site: site} do test "returns top browsers by unique visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -109,8 +109,12 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
] ]
end end
test "returns top browsers including imported data", %{conn: conn, site: site} do test "returns top browsers including imported data", %{
populate_stats(site, [ conn: conn,
site: site,
site_import: site_import
} do
populate_stats(site, site_import.id, [
build(:pageview, browser: "Chrome"), build(:pageview, browser: "Chrome"),
build(:imported_browsers, browser: "Chrome"), build(:imported_browsers, browser: "Chrome"),
build(:imported_browsers, browser: "Firefox"), build(:imported_browsers, browser: "Firefox"),
@ -133,9 +137,10 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
test "skips breakdown when visitors=0 (possibly due to 'Enable Users Metric' in GA)", %{ test "skips breakdown when visitors=0 (possibly due to 'Enable Users Metric' in GA)", %{
conn: conn, conn: conn,
site: site site: site,
site_import: site_import
} do } do
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:imported_browsers, browser: "Chrome", visitors: 0, visits: 14), build(:imported_browsers, browser: "Chrome", visitors: 0, visits: 14),
build(:imported_browsers, browser: "Firefox", visitors: 0, visits: 14), build(:imported_browsers, browser: "Firefox", visitors: 0, visits: 14),
build(:imported_browsers, build(:imported_browsers,
@ -169,9 +174,10 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
test "select empty imported_browsers as (not set), merging with the native (not set)", %{ test "select empty imported_browsers as (not set), merging with the native (not set)", %{
conn: conn, conn: conn,
site: site site: site,
site_import: site_import
} do } do
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:pageview, user_id: 123), build(:pageview, user_id: 123),
build(:imported_browsers, visitors: 1), build(:imported_browsers, visitors: 1),
build(:imported_visitors, visitors: 1) build(:imported_visitors, visitors: 1)
@ -186,7 +192,7 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
end end
describe "GET /api/stats/:domain/browser-versions" do describe "GET /api/stats/:domain/browser-versions" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns correct conversion_rate when browser_version clashes across browsers", %{ test "returns correct conversion_rate when browser_version clashes across browsers", %{
conn: conn, conn: conn,

View File

@ -32,7 +32,7 @@ defmodule PlausibleWeb.Api.StatsController.CitiesTest do
]) ])
end end
setup [:create_user, :log_in, :create_new_site, :add_imported_data, :seed] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import, :seed]
test "returns top cities by new visitors", %{conn: conn, site: site} do test "returns top cities by new visitors", %{conn: conn, site: site} do
conn = get(conn, "/api/stats/#{site.domain}/cities?period=day") conn = get(conn, "/api/stats/#{site.domain}/cities?period=day")

View File

@ -631,12 +631,14 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
conn: conn, conn: conn,
site: site site: site
} do } do
site = site_import =
site insert(:site_import,
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok") start_date: ~D[2005-01-01],
|> Plausible.Repo.update!() end_date: Timex.today(),
source: :universal_analytics
)
populate_stats(site, [ populate_stats(site, site_import.id, [
build(:pageview, pathname: "/"), build(:pageview, pathname: "/"),
build(:pageview, pathname: "/another"), build(:pageview, pathname: "/another"),
build(:pageview, pathname: "/blog/post-1"), build(:pageview, pathname: "/blog/post-1"),

View File

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Api.StatsController.CountriesTest do
use PlausibleWeb.ConnCase use PlausibleWeb.ConnCase
describe "GET /api/stats/:domain/countries" do describe "GET /api/stats/:domain/countries" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top countries by new visitors", %{conn: conn, site: site} do test "returns top countries by new visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [

View File

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
use PlausibleWeb.ConnCase use PlausibleWeb.ConnCase
describe "GET /api/stats/:domain/custom-prop-values/:prop_key" do describe "GET /api/stats/:domain/custom-prop-values/:prop_key" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns breakdown by a custom property", %{conn: conn, site: site} do test "returns breakdown by a custom property", %{conn: conn, site: site} do
prop_key = "parim_s6ber" prop_key = "parim_s6ber"

View File

@ -10,28 +10,18 @@ defmodule PlausibleWeb.Api.StatsController.ImportedTest do
|> then(&Plausible.Imported.Buffer.insert_all(table_name, &1)) |> then(&Plausible.Imported.Buffer.insert_all(table_name, &1))
end end
for import_type <- [:legacy, :new_and_legacy, :new] do for import_type <- [:new_and_legacy, :new] do
describe "Parse and import third party data fetched from Google Analytics as #{import_type} import" do describe "Parse and import third party data fetched from Google Analytics as #{import_type} import" do
if import_type == :new do
setup [:create_user, :log_in, :create_new_site] setup [:create_user, :log_in, :create_new_site]
else
setup [:create_user, :log_in, :create_new_site, :add_imported_data]
end
setup %{user: user, site: site} do setup %{user: user, site: site} do
if unquote(import_type) in [:new, :new_and_legacy] do
import_params = import_params =
if unquote(import_type) == :new_and_legacy do
site.imported_data
|> Map.from_struct()
|> Map.put(:source, :universal_analytics)
else
%{ %{
source: :universal_analytics, source: :universal_analytics,
start_date: ~D[2005-01-01], start_date: ~D[2005-01-01],
end_date: Timex.today() end_date: Timex.today(),
legacy: unquote(import_type) == :new_and_legacy
} }
end
site_import = site_import =
site site
@ -44,9 +34,6 @@ defmodule PlausibleWeb.Api.StatsController.ImportedTest do
|> Plausible.Repo.update!() |> Plausible.Repo.update!()
{:ok, %{import_id: site_import.id}} {:ok, %{import_id: site_import.id}}
else
{:ok, %{import_id: 0}}
end
end end
test "Visitors data imported from Google Analytics", %{ test "Visitors data imported from Google Analytics", %{

View File

@ -4,7 +4,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
@user_id 123 @user_id 123
describe "GET /api/stats/main-graph - plot" do describe "GET /api/stats/main-graph - plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "displays pageviews for the last 30 minutes in realtime graph", %{conn: conn, site: site} do test "displays pageviews for the last 30 minutes in realtime graph", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -368,7 +368,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
end end
describe "GET /api/stats/main-graph - pageviews plot" do describe "GET /api/stats/main-graph - pageviews plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "displays pageviews for a month", %{conn: conn, site: site} do test "displays pageviews for a month", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -556,7 +556,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
end end
describe "GET /api/stats/main-graph - bounce_rate plot" do describe "GET /api/stats/main-graph - bounce_rate plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "displays bounce_rate for a month", %{conn: conn, site: site} do test "displays bounce_rate for a month", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -620,7 +620,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
end end
describe "GET /api/stats/main-graph - visit_duration plot" do describe "GET /api/stats/main-graph - visit_duration plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "displays visit_duration for a month", %{conn: conn, site: site} do test "displays visit_duration for a month", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -915,7 +915,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
end end
describe "GET /api/stats/main-graph - comparisons" do describe "GET /api/stats/main-graph - comparisons" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns past month stats when period=30d and comparison=previous_period", %{ test "returns past month stats when period=30d and comparison=previous_period", %{
conn: conn, conn: conn,
@ -1007,17 +1007,6 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) build(:pageview, timestamp: ~N[2021-01-01 00:00:00])
]) ])
site
|> Ecto.Changeset.change(
imported_data: %{
start_date: ~D[2005-01-01],
end_date: ~D[2020-01-31],
source: "Google Analytics",
status: "ok"
}
)
|> Plausible.Repo.update!()
conn = conn =
get( get(
conn, conn,
@ -1064,17 +1053,6 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) build(:pageview, timestamp: ~N[2021-01-01 00:00:00])
]) ])
site
|> Ecto.Changeset.change(
imported_data: %{
start_date: ~D[2005-01-01],
end_date: ~D[2020-01-31],
source: "Google Analytics",
status: "ok"
}
)
|> Plausible.Repo.update!()
conn = conn =
get( get(
conn, conn,
@ -1125,7 +1103,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
@tag :full_build_only @tag :full_build_only
describe "GET /api/stats/main-graph - total_revenue plot" do describe "GET /api/stats/main-graph - total_revenue plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "plots total_revenue for a month", %{conn: conn, site: site} do test "plots total_revenue for a month", %{conn: conn, site: site} do
insert(:goal, site: site, event_name: "Payment", currency: "USD") insert(:goal, site: site, event_name: "Payment", currency: "USD")
@ -1205,7 +1183,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
@tag :full_build_only @tag :full_build_only
describe "GET /api/stats/main-graph - average_revenue plot" do describe "GET /api/stats/main-graph - average_revenue plot" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "plots total_revenue for a month", %{conn: conn, site: site} do test "plots total_revenue for a month", %{conn: conn, site: site} do
insert(:goal, site: site, event_name: "Payment", currency: "USD") insert(:goal, site: site, event_name: "Payment", currency: "USD")

View File

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Api.StatsController.OperatingSystemsTest do
use PlausibleWeb.ConnCase use PlausibleWeb.ConnCase
describe "GET /api/stats/:domain/operating-systems" do describe "GET /api/stats/:domain/operating-systems" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns operating systems by unique visitors", %{conn: conn, site: site} do test "returns operating systems by unique visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -211,7 +211,7 @@ defmodule PlausibleWeb.Api.StatsController.OperatingSystemsTest do
end end
describe "GET /api/stats/:domain/operating-system-versions" do describe "GET /api/stats/:domain/operating-system-versions" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top OS versions by unique visitors", %{conn: conn, site: site} do test "returns top OS versions by unique visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [

View File

@ -4,7 +4,7 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
@user_id 123 @user_id 123
describe "GET /api/stats/:domain/pages" do describe "GET /api/stats/:domain/pages" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top pages by visitors", %{conn: conn, site: site} do test "returns top pages by visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -1202,7 +1202,7 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
end end
describe "GET /api/stats/:domain/entry-pages" do describe "GET /api/stats/:domain/entry-pages" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top entry pages by visitors", %{conn: conn, site: site} do test "returns top entry pages by visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -1555,7 +1555,7 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
end end
describe "GET /api/stats/:domain/exit-pages" do describe "GET /api/stats/:domain/exit-pages" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top exit pages by visitors", %{conn: conn, site: site} do test "returns top exit pages by visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [

View File

@ -32,7 +32,7 @@ defmodule PlausibleWeb.Api.StatsController.RegionsTest do
]) ])
end end
setup [:create_user, :log_in, :create_new_site, :add_imported_data, :seed] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import, :seed]
test "returns top cities by new visitors", %{conn: conn, site: site} do test "returns top cities by new visitors", %{conn: conn, site: site} do
conn = get(conn, "/api/stats/#{site.domain}/regions?period=day") conn = get(conn, "/api/stats/#{site.domain}/regions?period=day")

View File

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Api.StatsController.ScreenSizesTest do
use PlausibleWeb.ConnCase use PlausibleWeb.ConnCase
describe "GET /api/stats/:domain/screen-sizes" do describe "GET /api/stats/:domain/screen-sizes" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns screen sizes by new visitors", %{conn: conn, site: site} do test "returns screen sizes by new visitors", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [

View File

@ -4,7 +4,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
@user_id 123 @user_id 123
describe "GET /api/stats/:domain/sources" do describe "GET /api/stats/:domain/sources" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top sources by unique user ids", %{conn: conn, site: site} do test "returns top sources by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -544,7 +544,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
end end
describe "GET /api/stats/:domain/utm_mediums" do describe "GET /api/stats/:domain/utm_mediums" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top utm_mediums by unique user ids", %{conn: conn, site: site} do test "returns top utm_mediums by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -696,7 +696,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
end end
describe "GET /api/stats/:domain/utm_campaigns" do describe "GET /api/stats/:domain/utm_campaigns" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top utm_campaigns by unique user ids", %{conn: conn, site: site} do test "returns top utm_campaigns by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -856,7 +856,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
end end
describe "GET /api/stats/:domain/utm_sources" do describe "GET /api/stats/:domain/utm_sources" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top utm_sources by unique user ids", %{conn: conn, site: site} do test "returns top utm_sources by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -904,7 +904,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
end end
describe "GET /api/stats/:domain/utm_terms" do describe "GET /api/stats/:domain/utm_terms" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top utm_terms by unique user ids", %{conn: conn, site: site} do test "returns top utm_terms by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [
@ -1064,7 +1064,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do
end end
describe "GET /api/stats/:domain/utm_contents" do describe "GET /api/stats/:domain/utm_contents" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns top utm_contents by unique user ids", %{conn: conn, site: site} do test "returns top utm_contents by unique user ids", %{conn: conn, site: site} do
populate_stats(site, [ populate_stats(site, [

View File

@ -463,7 +463,7 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
end end
describe "GET /api/stats/top-stats - with imported data" do describe "GET /api/stats/top-stats - with imported data" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "returns divisible metrics as 0 when no stats exist", %{ test "returns divisible metrics as 0 when no stats exist", %{
site: site, site: site,
@ -1267,7 +1267,7 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
end end
describe "GET /api/stats/top-stats - with comparisons" do describe "GET /api/stats/top-stats - with comparisons" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "does not return comparisons by default", %{site: site, conn: conn} do test "does not return comparisons by default", %{site: site, conn: conn} do
populate_stats(site, [ populate_stats(site, [
@ -1352,17 +1352,6 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) build(:pageview, timestamp: ~N[2021-01-01 00:00:00])
]) ])
site
|> Ecto.Changeset.change(
imported_data: %{
start_date: ~D[2005-01-01],
end_date: ~D[2020-01-31],
source: "Google Analytics",
status: "ok"
}
)
|> Plausible.Repo.update!()
conn = conn =
get( get(
conn, conn,
@ -1393,17 +1382,6 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) build(:pageview, timestamp: ~N[2021-01-01 00:00:00])
]) ])
site
|> Ecto.Changeset.change(
imported_data: %{
start_date: ~D[2005-01-01],
end_date: ~D[2020-01-31],
source: "Google Analytics",
status: "ok"
}
)
|> Plausible.Repo.update!()
conn = conn =
get( get(
conn, conn,

View File

@ -655,9 +655,6 @@ defmodule PlausibleWeb.SiteControllerTest do
end end
test "renders imports in import list", %{conn: conn, site: site} do test "renders imports in import list", %{conn: conn, site: site} do
{:ok, opts} = add_imported_data(%{site: site})
site = Map.new(opts).site
_site_import1 = insert(:site_import, site: site, status: SiteImport.pending()) _site_import1 = insert(:site_import, site: site, status: SiteImport.pending())
_site_import2 = insert(:site_import, site: site, status: SiteImport.importing()) _site_import2 = insert(:site_import, site: site, status: SiteImport.importing())
@ -675,7 +672,7 @@ defmodule PlausibleWeb.SiteControllerTest do
resp = html_response(conn, 200) resp = html_response(conn, 200)
buttons = find(resp, ~s|button[data-method="delete"]|) buttons = find(resp, ~s|button[data-method="delete"]|)
assert length(buttons) == 5 assert length(buttons) == 4
assert resp =~ "Google Analytics (123456)" assert resp =~ "Google Analytics (123456)"
assert resp =~ "(98 page views)" assert resp =~ "(98 page views)"
@ -693,6 +690,20 @@ defmodule PlausibleWeb.SiteControllerTest do
"Maximum of #{Plausible.Imported.max_complete_imports()} imports is reached." "Maximum of #{Plausible.Imported.max_complete_imports()} imports is reached."
end end
test "considers older legacy imports when showing pageview count", %{conn: conn, site: site} do
_site_import =
insert(:site_import, site: site, legacy: true, status: SiteImport.completed())
populate_stats(site, [
build(:imported_visitors, pageviews: 77),
build(:imported_visitors, pageviews: 21)
])
conn = get(conn, "/#{site.domain}/settings/imports-exports")
assert html_response(conn, 200) =~ "(98 page views)"
end
test "disables import buttons when there's import in progress", %{conn: conn, site: site} do test "disables import buttons when there's import in progress", %{conn: conn, site: site} do
_site_import1 = insert(:site_import, site: site, status: SiteImport.completed()) _site_import1 = insert(:site_import, site: site, status: SiteImport.completed())
_site_import2 = insert(:site_import, site: site, status: SiteImport.importing()) _site_import2 = insert(:site_import, site: site, status: SiteImport.importing())
@ -1312,7 +1323,7 @@ defmodule PlausibleWeb.SiteControllerTest do
end end
describe "DELETE /:website/settings/:forget_import/:import_id" do describe "DELETE /:website/settings/:forget_import/:import_id" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site, :create_legacy_site_import]
test "removes site import, associated data and cancels oban job for a particular import", %{ test "removes site import, associated data and cancels oban job for a particular import", %{
conn: conn, conn: conn,
@ -1338,7 +1349,11 @@ defmodule PlausibleWeb.SiteControllerTest do
build(:imported_visitors, pageviews: 10) build(:imported_visitors, pageviews: 10)
]) ])
assert [%{id: ^import_id}, %{id: 0}] = Plausible.Imported.list_all_imports(site) imports = Plausible.Imported.list_all_imports(site)
assert Enum.find(imports, &(&1.id == import_id))
site = Plausible.Imported.load_import_data(site)
assert eventually(fn -> assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site) count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
@ -1355,9 +1370,10 @@ defmodule PlausibleWeb.SiteControllerTest do
assert Repo.reload(job).state == "cancelled" assert Repo.reload(job).state == "cancelled"
end end
test "removes legacy site import along with associated data when instructed", %{ test "removes all legacy site import data when instructed", %{
conn: conn, conn: conn,
site: site site: site,
site_import: legacy_site_import
} do } do
other_site_import = insert(:site_import, site: site) other_site_import = insert(:site_import, site: site)
@ -1375,7 +1391,7 @@ defmodule PlausibleWeb.SiteControllerTest do
{count == 22, count} {count == 22, count}
end) end)
delete(conn, "/#{site.domain}/settings/forget-import/0") delete(conn, "/#{site.domain}/settings/forget-import/#{legacy_site_import.id}")
assert eventually(fn -> assert eventually(fn ->
count = Plausible.Stats.Clickhouse.imported_pageview_count(site) count = Plausible.Stats.Clickhouse.imported_pageview_count(site)
@ -1385,13 +1401,7 @@ defmodule PlausibleWeb.SiteControllerTest do
end end
describe "DELETE /:website/settings/forget_imported" do describe "DELETE /:website/settings/forget_imported" do
setup [:create_user, :log_in, :create_new_site, :add_imported_data] setup [:create_user, :log_in, :create_new_site]
test "removes imported_data field from site", %{conn: conn, site: site} do
delete(conn, "/#{site.domain}/settings/forget-imported")
assert Repo.reload(site).imported_data == nil
end
test "removes actual imported data from Clickhouse", %{conn: conn, user: user, site: site} do test "removes actual imported data from Clickhouse", %{conn: conn, user: user, site: site} do
Plausible.Imported.NoopImporter.new_import( Plausible.Imported.NoopImporter.new_import(

View File

@ -47,13 +47,21 @@ defmodule Plausible.TestUtils do
{:ok, site: site} {:ok, site: site}
end end
def add_imported_data(%{site: site}) do def create_legacy_site_import(%{site: site}) do
site = create_site_import(%{site: site, create_legacy_import?: true})
site end
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok")
|> Repo.update!()
{:ok, site: site} def create_site_import(%{site: site} = opts) do
site_import =
Factory.insert(:site_import,
site: site,
start_date: ~D[2005-01-01],
end_date: Timex.today(),
source: :universal_analytics,
legacy: opts[:create_legacy_import?] == true
)
{:ok, site_import: site_import}
end end
def create_new_site(%{user: user}) do def create_new_site(%{user: user}) do