Override stats_start_date in CRM (#2310)

* Refactor Plausible.Sites.stats_start_date/1 function

This commit adds documentation, typespecs and better pattern matching to
the Plausible.Sites.stats_start_date/1 function.

* Use memoized stats_start_date instead of querying CH

* Prevent domain from being updated from CRM

* Add stats_start_date to CRM site form

This commit adds stats_start_date to CRM site form, and creates a
separate Ecto changeset for CRM changes.

* Alias Plausible.Site calls

Co-authored-by: Adam Rutkowski <hq@mtod.org>

* List all timezones in CRM form

* Require sites.public in CRM changeset

Co-authored-by: Adam Rutkowski <hq@mtod.org>
pull/2316/head
Vinicius Brasil 4 years ago committed by GitHub
parent 523b114682
commit 4a2e0af337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,9 +19,10 @@ defmodule Plausible.SiteAdmin do
def form_fields(_) do
[
domain: nil,
timezone: nil,
public: nil
domain: %{update: :readonly},
timezone: %{choices: Plausible.Timezones.options()},
public: nil,
stats_start_date: nil
]
end
@ -130,4 +131,7 @@ defmodule Plausible.SiteAdmin do
end)
|> stringify_fields()
end
def create_changeset(schema, attrs), do: Plausible.Site.crm_changeset(schema, attrs)
def update_changeset(schema, attrs), do: Plausible.Site.crm_changeset(schema, attrs)
end

@ -54,6 +54,12 @@ defmodule Plausible.Site do
)
end
def crm_changeset(site, attrs) do
site
|> cast(attrs, [:timezone, :public, :stats_start_date])
|> validate_required([:timezone, :public])
end
def make_public(site) do
change(site, public: true)
end

@ -39,18 +39,28 @@ defmodule Plausible.Sites do
end
end
def stats_start_date(site) do
if site.stats_start_date do
site.stats_start_date
else
start_date = Plausible.Stats.Clickhouse.pageview_start_date_local(site)
@spec stats_start_date(Plausible.Site.t()) :: Date.t() | nil
@doc """
Returns the date of the first event of the given site, or `nil` if the site
does not have stats yet.
If this is the first time the function is called for the site, it queries
Clickhouse and saves the date in the sites table.
"""
def stats_start_date(site)
def stats_start_date(%Site{stats_start_date: %Date{} = date}) do
date
end
if start_date do
Site.set_stats_start_date(site, start_date)
|> Repo.update()
def stats_start_date(%Site{} = site) do
if start_date = Plausible.Stats.Clickhouse.pageview_start_date_local(site) do
updated_site =
site
|> Site.set_stats_start_date(start_date)
|> Repo.update!()
start_date
end
updated_site.stats_start_date
end
end

@ -5,6 +5,7 @@ defmodule Plausible.Stats.Clickhouse do
use Plausible.Stats.Fragments
@no_ref "Direct / None"
@spec pageview_start_date_local(Plausible.Site.t()) :: Date.t() | nil
def pageview_start_date_local(site) do
datetime =
ClickhouseRepo.one(

@ -736,9 +736,7 @@ defmodule PlausibleWeb.SiteController do
site = conn.assigns[:site]
start_date = Plausible.Google.HTTP.get_analytics_start_date(view_id, access_token)
end_date =
Plausible.Stats.Clickhouse.pageview_start_date_local(site) || Timex.today(site.timezone)
end_date = Plausible.Sites.stats_start_date(site) || Timex.today(site.timezone)
{:ok, {view_name, view_id}} = Plausible.Google.Api.get_view(access_token, view_id)

Loading…
Cancel
Save