Skip to content

Commit

Permalink
💀 Modify ingestion to read team schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Nov 27, 2024
1 parent 7c4b976 commit 2910614
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/plausible/site/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ defmodule Plausible.Site.Cache do
def base_db_query() do
from s in Site,
left_join: rg in assoc(s, :revenue_goals),
inner_join: owner in assoc(s, :owner),
inner_join: team in assoc(s, :team),
select: {
s.domain,
s.domain_changed_from,
%{struct(s, ^@cached_schema_fields) | from_cache?: true}
},
preload: [revenue_goals: rg, owner: owner]
preload: [revenue_goals: rg, team: team]
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/site/gate_keeper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Plausible.Site.GateKeeper do

defp policy(domain, opts) when is_binary(domain) do
with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])),
site = %Site{owner: %{accept_traffic_until: accept_traffic_until}} <- from_cache do
site = %Site{team: %{accept_traffic_until: accept_traffic_until}} <- from_cache do
if not is_nil(accept_traffic_until) and
Date.after?(Date.utc_today(), accept_traffic_until) do
:payment_required
Expand Down
48 changes: 23 additions & 25 deletions test/plausible/ingestion/event_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule Plausible.Ingestion.EventTest do
use Plausible.DataCase, async: true
use Plausible.Teams.Test

import Phoenix.ConnTest

alias Plausible.Ingestion.Request
alias Plausible.Ingestion.Event

test "processes a request into an event" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -27,7 +28,7 @@ defmodule Plausible.Ingestion.EventTest do

for {user_agent, idx} <- Enum.with_index(@regressive_user_agents) do
test "processes user agents known to cause problems parsing in the past (case #{idx})" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -45,7 +46,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops verification agent" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand Down Expand Up @@ -76,7 +77,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when referrer is spam" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -93,7 +94,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when referrer is spam for multiple domains" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -110,7 +111,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "selectively drops an event for multiple domains" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -126,7 +127,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "selectively drops an event when rate-limited" do
site = insert(:site, ingest_rate_limit_threshold: 1)
site = new_site(ingest_rate_limit_threshold: 1)

payload = %{
name: "pageview",
Expand All @@ -143,7 +144,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when header x-plausible-ip-type is dc_ip" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -160,7 +161,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when ip is on blocklist" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -180,7 +181,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when country is on blocklist" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -201,7 +202,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when page is on blocklist" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -220,7 +221,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops a request when hostname allowlist is defined and hostname is not on the list" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -239,7 +240,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "passes a request when hostname allowlist is defined and hostname is on the list" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageview",
Expand All @@ -259,11 +260,8 @@ defmodule Plausible.Ingestion.EventTest do
test "drops events for site with accept_trafic_until in the past" do
yesterday = Date.add(Date.utc_today(), -1)

site =
insert(:site,
ingest_rate_limit_threshold: 1,
members: [build(:user, accept_traffic_until: yesterday)]
)
owner = new_user(team: [accept_traffic_until: yesterday])
site = new_site(ingest_rate_limit_threshold: 1, owner: owner)

payload = %{
name: "pageview",
Expand All @@ -280,7 +278,7 @@ defmodule Plausible.Ingestion.EventTest do

@tag :slow
test "drops events on session lock timeout" do
site = insert(:site)
site = new_site()

very_slow_buffer = fn sessions ->
Process.sleep(1000)
Expand Down Expand Up @@ -319,7 +317,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "drops pageleave event when no session found from cache" do
site = insert(:site)
site = new_site()

payload = %{
name: "pageleave",
Expand All @@ -336,7 +334,7 @@ defmodule Plausible.Ingestion.EventTest do

@tag :ee_only
test "saves revenue amount" do
site = insert(:site)
site = new_site()
_goal = insert(:goal, event_name: "checkout", currency: "USD", site: site)

payload = %{
Expand All @@ -353,7 +351,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "does not save revenue amount when there is no revenue goal" do
site = insert(:site)
site = new_site()

payload = %{
name: "checkout",
Expand All @@ -369,7 +367,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "IPv4 hostname is stored without public suffix processing" do
_site = insert(:site, domain: "192.168.0.1")
_site = new_site(domain: "192.168.0.1")

payload = %{
name: "checkout",
Expand All @@ -384,7 +382,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "Hostname is stored with public suffix processing" do
_site = insert(:site, domain: "foo.netlify.app")
_site = new_site(domain: "foo.netlify.app")

payload = %{
name: "checkout",
Expand All @@ -399,7 +397,7 @@ defmodule Plausible.Ingestion.EventTest do
end

test "hostname is (none) when no hostname can be derived from the url" do
site = insert(:site, domain: "foo.example.com")
site = new_site(domain: "foo.example.com")

payload = %{
domain: site.domain,
Expand Down
3 changes: 1 addition & 2 deletions test/plausible/site/cache_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Plausible.Site.CacheTest do
alias Plausible.Site.Cache

describe "public cache interface" do
@tag :skip
test "cache caches sites", %{test: test} do
{:ok, _} =
Supervisor.start_link([{Cache, [cache_name: test, child_id: :test_cache_caches_id]}],
Expand Down Expand Up @@ -36,7 +35,7 @@ defmodule Plausible.Site.CacheTest do
assert %Site{from_cache?: true} =
Cache.get("site2.example.com", force?: true, cache_name: test)

assert %Site{from_cache?: false, owner: %{accept_traffic_until: ~D[2021-01-01]}} =
assert %Site{from_cache?: false, team: %{accept_traffic_until: ~D[2021-01-01]}} =
Cache.get("site2.example.com", cache_name: test)

refute Cache.get("site3.example.com", cache_name: test, force?: true)
Expand Down
9 changes: 6 additions & 3 deletions test/plausible/site/gate_keeper_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Plausible.Site.GateKeeperTest do
use Plausible.DataCase, async: true
use Plausible.Teams.Test

alias Plausible.Site.Cache
alias Plausible.Site.GateKeeper
Expand All @@ -18,10 +19,12 @@ defmodule Plausible.Site.GateKeeperTest do
domain = "expired.example.com"
yesterday = Date.utc_today() |> Date.add(-1)

owner = new_user(team: [accept_traffic_until: yesterday])

%{id: _} =
add_site_and_refresh_cache(test,
domain: domain,
members: [build(:user, accept_traffic_until: yesterday)]
owner: owner
)

assert {:deny, :payment_required} = GateKeeper.check(domain, opts)
Expand Down Expand Up @@ -88,7 +91,7 @@ defmodule Plausible.Site.GateKeeperTest do
{:ok, _} = Plausible.Repo.delete(site)
# We need some dummy site, otherwise the cache won't refresh in case the DB
# is completely empty
insert(:site)
new_site()
deleted_site_id = site.id

assert {:allow, %Plausible.Site{id: ^deleted_site_id, from_cache?: true}} =
Expand All @@ -104,7 +107,7 @@ defmodule Plausible.Site.GateKeeperTest do
end

defp add_site_and_refresh_cache(cache_name, site_data) do
site = insert(:site, site_data)
site = new_site(site_data)

Cache.refresh_updated_recently(cache_name: cache_name, force?: true)
site
Expand Down

0 comments on commit 2910614

Please sign in to comment.