You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/sites.ex

181 lines
4.2 KiB
Elixir

7 years ago
defmodule Plausible.Sites do
use Plausible.Repo
alias Plausible.Site
alias Plausible.Site.SharedLink
import Ecto.Query
def get_by_domain(domain) do
Repo.get_by(Site, domain: domain)
end
def create(user, params) do
site_changeset = Site.changeset(%Site{}, params)
Ecto.Multi.new()
|> Ecto.Multi.run(:limit, fn _, _ ->
limit = Plausible.Billing.sites_limit(user)
count = owned_sites_count(user)
if count >= limit do
{:error, limit}
else
{:ok, count}
end
end)
|> Ecto.Multi.insert(:site, site_changeset)
|> Ecto.Multi.run(:site_membership, fn repo, %{site: site} ->
membership_changeset =
Site.Membership.changeset(%Site.Membership{}, %{
site_id: site.id,
user_id: user.id
})
repo.insert(membership_changeset)
end)
|> maybe_start_trial(user)
|> Repo.transaction()
end
defp maybe_start_trial(multi, user) do
case user.trial_expiry_date do
nil ->
changeset = Plausible.Auth.User.start_trial(user)
Ecto.Multi.update(multi, :user, changeset)
_ ->
multi
end
end
@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
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!()
updated_site.stats_start_date
end
end
def has_stats?(site) do
5 years ago
!!stats_start_date(site)
end
def create_shared_link(site, name, password \\ nil) do
changes =
SharedLink.changeset(
%SharedLink{
site_id: site.id,
slug: Nanoid.generate()
},
%{name: name, password: password}
)
Repo.insert(changes)
end
def shared_link_url(site, link) do
base = PlausibleWeb.Endpoint.url()
domain = "/share/#{URI.encode_www_form(site.domain)}"
base <> domain <> "?auth=" <> link.slug
end
7 years ago
def get_for_user!(user_id, domain, roles \\ [:owner, :admin, :viewer]),
do: Repo.one!(get_for_user_q(user_id, domain, roles))
6 years ago
def get_for_user(user_id, domain, roles \\ [:owner, :admin, :viewer]),
do: Repo.one(get_for_user_q(user_id, domain, roles))
defp get_for_user_q(user_id, domain, roles) do
from(s in Site,
join: sm in Site.Membership,
6 years ago
on: sm.site_id == s.id,
where: sm.user_id == ^user_id,
where: sm.role in ^roles,
where: s.domain == ^domain or s.domain_changed_from == ^domain,
6 years ago
select: s
7 years ago
)
end
def has_goals?(site) do
Repo.exists?(
from g in Plausible.Goal,
where: g.site_id == ^site.id
)
end
def is_member?(user_id, site) do
role(user_id, site) !== nil
end
def has_admin_access?(user_id, site) do
role(user_id, site) in [:admin, :owner]
end
def locked?(%Site{locked: locked}) do
locked
end
def role(user_id, site) do
Repo.one(
from sm in Site.Membership,
where: sm.user_id == ^user_id and sm.site_id == ^site.id,
select: sm.role
)
end
def owned_sites_count(user) do
user
|> owned_sites_query()
|> Repo.aggregate(:count)
end
def owned_sites_domains(user) do
user
|> owned_sites_query()
|> select([site], site.domain)
|> Repo.all()
end
Conditionally support switching between v1 and v2 clickhouse schemas (#2780) * Remove ClickhouseSetup module This has been an implicit point of contact to many tests. From now on the goal is for each test to maintain its own, isolated setup so that no accidental clashes and implicit assumptions are relied upon. * Implement v2 schema check An environment variable V2_MIGRATION_DONE acts like a feature flag, switching plausible from using old events/sessions schemas to v2 schemas introduced by NumericIDs migration. * Run both test suites sequentially While the code for v1 and v2 schemas must be kept still, we will from now on run tests against both code paths. Secondary test run will set V2_MIGRATION_DONE=1 variable, thus making all `Plausible.v2?()` checks return `true'. * Remove unused function This is a remnant from the short period when we would check for existing events before allowing creating a new site. * Update test setups/factories with v2 migration check * Make GateKeeper return site id along with :allow * Make Billing module check for v2 schema * Make ingestion aware of v2 schema * Disable site transfers for when v2 is live In a separate changeset we will implement simplified site transfer for when v2 migration is complete. The new transfer will only rename the site domain in postgres and keep track of the original site prior to the transfer so we keep an ingestion grace period until the customers redeploy their scripting. * Make Stats base queries aware of v2 schema switch * Update breakdown with v2 conditionals * Update pageview local start with v2 check * Update current visitoris with v2 check * Update stats controller with v2 checks * Update external controller with v2 checks * Update remaining tests with proper fixtures * Rewrite redundant assignment * Remove unused alias * Mute credo, this is not the right time * Add test_helper prompt * Fetch priv dir so it works with a release * Fetch distinct partitions only * Don't limit inspect output for partitions * Ensure SQL is printed to IO * Remove redundant domain fixture
4 years ago
def owned_site_ids(user) do
user
|> owned_sites_query()
|> select([site], site.id)
|> Repo.all()
end
defp owned_sites_query(user) do
from(s in Site,
join: sm in Site.Membership,
on: sm.site_id == s.id,
where: sm.role == :owner,
where: sm.user_id == ^user.id
)
end
def owner_for(site) do
Repo.one(
from u in Plausible.Auth.User,
join: sm in Site.Membership,
on: sm.user_id == u.id,
where: sm.site_id == ^site.id,
where: sm.role == :owner
7 years ago
)
end
end