Add ability to reset stats (#225)

* Add ability to reset stats

* Fix test
This commit is contained in:
Uku Taht 2020-07-16 13:07:45 +03:00 committed by GitHub
parent 73049c7e4f
commit 82c7572ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 4 deletions

View File

@ -10,6 +10,14 @@ defmodule Plausible.Clickhouse do
end)
end
def delete_stats!(site) do
delete_events = "ALTER TABLE events DELETE WHERE domain = ?"
delete_sessions = "ALTER TABLE sessions DELETE WHERE domain = ?"
Clickhousex.query!(:clickhouse, delete_events, [site.domain], log: {Plausible.Clickhouse, :log, []})
Clickhousex.query!(:clickhouse, delete_sessions, [site.domain], log: {Plausible.Clickhouse, :log, []})
end
def insert_events(events) do
insert =
"""

View File

@ -159,6 +159,16 @@ defmodule PlausibleWeb.SiteController do
end
end
def reset_stats(conn, %{"website" => website}) do
site =
Sites.get_for_user!(conn.assigns[:current_user].id, website)
Plausible.Clickhouse.delete_stats!(site)
conn
|> put_flash(:success, "#{site.domain} stats will be reset in a few minutes")
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
end
def delete_site(conn, %{"website" => website}) do
site =
Sites.get_for_user!(conn.assigns[:current_user].id, website)
@ -171,6 +181,7 @@ defmodule PlausibleWeb.SiteController do
end
Repo.delete!(site)
Plausible.Clickhouse.delete_stats!(site)
conn
|> put_flash(:success, "Site deleted succesfully along with all pageviews")

View File

@ -146,6 +146,7 @@ defmodule PlausibleWeb.Router do
put "/:website/settings/google", SiteController, :update_google_auth
delete "/:website/settings/google", SiteController, :delete_google_auth
delete "/:website", SiteController, :delete_site
delete "/:website/stats", SiteController, :reset_stats
get "/share/:slug", StatsController, :shared_link
post "/share/:slug/authenticate", StatsController, :authenticate_shared_link

View File

@ -234,10 +234,15 @@
<% end %>
<div class="max-w-xl mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-red-600 px-8 pt-6 pb-8 mt-16 mb-24">
<h2 class="text-xl font-black">Delete site data</h2>
<h2 class="text-xl font-black">Danger zone</h2>
<div class="my-4 border-b border-gray-300"></div>
<p class="text-lg">Deleting the site removes all stats you've collected</p>
<%= link "Delete #{@site.domain}", to: "/#{URI.encode_www_form(@site.domain)}", method: :delete, class: "button bg-red-600 mt-6", data: [confirm: "Deleting the site data cannot be reversed. Are you sure?"] %>
<p class="text-gray-700">Resetting the stats removes all pageviews but keeps the site configuration</p>
<%= link("Reset #{@site.domain} stats", to: "/#{URI.encode_www_form(@site.domain)}/stats", method: :delete, class: "inline-block mt-4 px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-red-700 bg-white hover:text-red-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", data: [confirm: "Resetting the stats cannot be reversed. Are you sure?"]) %>
<div class="mt-6">
<p class="text-gray-700">Deleting the site removes all stats along with the site configuration</p>
<%= link "Delete #{@site.domain}", to: "/#{URI.encode_www_form(@site.domain)}", method: :delete, class: "inline-block mt-4 px-4 py-2 border border-transparent font-medium rounded-md text-red-700 bg-red-100 hover:bg-red-50 focus:outline-none focus:border-red-300 focus:shadow-outline-red active:bg-red-200 transition ease-in-out duration-150 sm:text-sm sm:leading-5", data: [confirm: "Deleting the site data cannot be reversed. Are you sure?"] %>
</div>
</div>

View File

@ -137,7 +137,8 @@ defmodule PlausibleWeb.SiteControllerTest do
describe "DELETE /:website" do
setup [:create_user, :log_in, :create_site]
test "deletes the site", %{conn: conn, user: user, site: site} do
test "deletes the site", %{conn: conn, user: user} do
site = insert(:site, members: [user])
insert(:google_auth, user: user, site: site)
insert(:custom_domain, site: site)