2020-05-07 14:28:41 +03:00
|
|
|
defmodule Plausible.Test.ClickhouseSetup do
|
|
|
|
def run() do
|
|
|
|
create_events()
|
|
|
|
create_sessions()
|
2020-05-18 12:44:52 +03:00
|
|
|
load_fixtures()
|
2020-05-07 14:28:41 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_events() do
|
2020-05-18 12:44:52 +03:00
|
|
|
drop = "DROP TABLE events"
|
|
|
|
create = """
|
|
|
|
CREATE TABLE events (
|
2020-05-07 14:28:41 +03:00
|
|
|
timestamp DateTime,
|
|
|
|
name String,
|
|
|
|
domain String,
|
2020-05-22 12:33:17 +03:00
|
|
|
user_id UInt64,
|
|
|
|
session_id UInt64,
|
2020-05-07 14:28:41 +03:00
|
|
|
hostname String,
|
|
|
|
pathname String,
|
2020-05-22 12:33:17 +03:00
|
|
|
referrer String,
|
|
|
|
referrer_source String,
|
|
|
|
initial_referrer String,
|
|
|
|
initial_referrer_source String,
|
|
|
|
country_code LowCardinality(FixedString(2)),
|
|
|
|
screen_size LowCardinality(String),
|
|
|
|
operating_system LowCardinality(String),
|
|
|
|
browser LowCardinality(String)
|
2020-05-07 14:28:41 +03:00
|
|
|
) ENGINE = MergeTree()
|
|
|
|
PARTITION BY toYYYYMM(timestamp)
|
2020-05-22 12:33:17 +03:00
|
|
|
ORDER BY (name, domain, user_id, timestamp)
|
2020-05-07 14:28:41 +03:00
|
|
|
SETTINGS index_granularity = 8192
|
|
|
|
"""
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
Clickhousex.query(:clickhouse, drop, [],log: {Plausible.Clickhouse, :log, []})
|
|
|
|
Clickhousex.query(:clickhouse, create, [],log: {Plausible.Clickhouse, :log, []})
|
2020-05-07 14:28:41 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_sessions() do
|
2020-05-18 12:44:52 +03:00
|
|
|
drop = "DROP TABLE sessions"
|
|
|
|
create = """
|
|
|
|
CREATE TABLE sessions (
|
2020-05-22 12:33:17 +03:00
|
|
|
session_id UInt64,
|
2020-05-18 12:44:52 +03:00
|
|
|
sign Int8,
|
2020-05-07 14:28:41 +03:00
|
|
|
domain String,
|
2020-05-22 12:33:17 +03:00
|
|
|
user_id UInt64,
|
2020-05-07 14:28:41 +03:00
|
|
|
hostname String,
|
2020-05-18 12:44:52 +03:00
|
|
|
timestamp DateTime,
|
2020-05-07 14:28:41 +03:00
|
|
|
start DateTime,
|
|
|
|
is_bounce UInt8,
|
2020-05-22 12:33:17 +03:00
|
|
|
entry_page String,
|
|
|
|
exit_page String,
|
2020-05-18 12:44:52 +03:00
|
|
|
pageviews Int32,
|
|
|
|
events Int32,
|
|
|
|
duration UInt32,
|
2020-05-22 12:33:17 +03:00
|
|
|
referrer String,
|
|
|
|
referrer_source String,
|
|
|
|
country_code LowCardinality(FixedString(2)),
|
|
|
|
screen_size LowCardinality(String),
|
|
|
|
operating_system LowCardinality(String),
|
|
|
|
browser LowCardinality(String)
|
2020-05-18 12:44:52 +03:00
|
|
|
) ENGINE = CollapsingMergeTree(sign)
|
2020-05-07 14:28:41 +03:00
|
|
|
PARTITION BY toYYYYMM(start)
|
2020-05-22 12:33:17 +03:00
|
|
|
ORDER BY (domain, user_id, session_id, start)
|
2020-05-07 14:28:41 +03:00
|
|
|
SETTINGS index_granularity = 8192
|
|
|
|
"""
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
Clickhousex.query(:clickhouse, drop, [],log: {Plausible.Clickhouse, :log, []})
|
|
|
|
Clickhousex.query(:clickhouse, create, [],log: {Plausible.Clickhouse, :log, []})
|
|
|
|
end
|
|
|
|
|
2020-05-25 14:52:20 +03:00
|
|
|
@conversion_1_session_id 123
|
|
|
|
@conversion_2_session_id 234
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
def load_fixtures() do
|
|
|
|
Plausible.TestUtils.create_events([
|
|
|
|
%{name: "pageview", domain: "test-site.com", pathname: "/", country_code: "EE", browser: "Chrome", operating_system: "Mac", screen_size: "Desktop", referrer_source: "10words", referrer: "10words.com/page1", timestamp: ~N[2019-01-01 00:00:00]},
|
|
|
|
%{name: "pageview", domain: "test-site.com", pathname: "/", country_code: "EE", browser: "Chrome", operating_system: "Mac", screen_size: "Desktop", referrer_source: "10words", referrer: "10words.com/page2", timestamp: ~N[2019-01-01 00:00:00]},
|
|
|
|
%{name: "pageview", domain: "test-site.com", pathname: "/contact", country_code: "GB", browser: "Firefox", operating_system: "Android", screen_size: "Mobile", referrer_source: "Bing", timestamp: ~N[2019-01-01 00:00:00]},
|
|
|
|
|
|
|
|
%{name: "pageview", domain: "test-site.com", timestamp: ~N[2019-01-31 00:00:00]},
|
|
|
|
|
2020-05-25 14:52:20 +03:00
|
|
|
%{name: "Signup", domain: "test-site.com", session_id: @conversion_1_session_id, timestamp: ~N[2019-01-01 01:00:00]},
|
|
|
|
%{name: "Signup", domain: "test-site.com", session_id: @conversion_1_session_id, timestamp: ~N[2019-01-01 02:00:00]},
|
|
|
|
%{name: "Signup", domain: "test-site.com", session_id: @conversion_2_session_id, timestamp: ~N[2019-01-01 02:00:00]},
|
2020-05-18 12:44:52 +03:00
|
|
|
|
2020-05-25 14:52:20 +03:00
|
|
|
%{name: "pageview", pathname: "/register", domain: "test-site.com", session_id: @conversion_1_session_id, timestamp: ~N[2019-01-01 23:00:00]},
|
|
|
|
%{name: "pageview", pathname: "/register", domain: "test-site.com", session_id: @conversion_2_session_id, timestamp: ~N[2019-01-01 23:00:00]},
|
|
|
|
%{name: "pageview", pathname: "/irrelevant", domain: "test-site.com", session_id: @conversion_1_session_id, timestamp: ~N[2019-01-01 23:00:00]},
|
2020-05-18 12:44:52 +03:00
|
|
|
|
|
|
|
%{name: "pageview", domain: "test-site.com", referrer_source: "Google", timestamp: ~N[2019-02-01 01:00:00]},
|
|
|
|
%{name: "pageview", domain: "test-site.com", referrer_source: "Google", timestamp: ~N[2019-02-01 02:00:00]},
|
|
|
|
|
|
|
|
%{name: "pageview", domain: "test-site.com", referrer: "t.co/some-link", referrer_source: "Twitter", timestamp: ~N[2019-03-01 01:00:00]},
|
|
|
|
%{name: "pageview", domain: "test-site.com", referrer: "t.co/some-link", referrer_source: "Twitter", timestamp: ~N[2019-03-01 01:00:00]},
|
|
|
|
%{name: "pageview", domain: "test-site.com", referrer: "t.co/nonexistent-link", referrer_source: "Twitter", timestamp: ~N[2019-03-01 02:00:00]},
|
|
|
|
|
|
|
|
%{name: "pageview", domain: "test-site.com"},
|
|
|
|
%{name: "pageview", domain: "test-site.com", timestamp: Timex.now() |> Timex.shift(minutes: -3)},
|
|
|
|
%{name: "pageview", domain: "test-site.com", timestamp: Timex.now() |> Timex.shift(minutes: -6)},
|
|
|
|
|
|
|
|
%{name: "pageview", domain: "tz-test.com", timestamp: ~N[2019-01-01 00:00:00]},
|
2020-06-05 16:14:17 +03:00
|
|
|
%{name: "pageview", domain: "public-site.io"},
|
|
|
|
%{name: "pageview", domain: "fetch-tweets-test.com", referrer: "t.co/a-link", referrer_source: "Twitter"},
|
|
|
|
%{name: "pageview", domain: "fetch-tweets-test.com", referrer: "t.co/b-link", referrer_source: "Twitter", timestamp: Timex.now() |> Timex.shift(days: -5)}
|
2020-05-18 12:44:52 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
Plausible.TestUtils.create_sessions([
|
2020-05-25 14:52:20 +03:00
|
|
|
%{domain: "test-site.com", entry_page: "/", exit_page: "/", referrer_source: "10words", referrer: "10words.com/page1", session_id: @conversion_1_session_id, is_bounce: true, start: ~N[2019-01-01 02:00:00]},
|
|
|
|
%{domain: "test-site.com", entry_page: "/", exit_page: "/", referrer_source: "10words", referrer: "10words.com/page1", session_id: @conversion_2_session_id, is_bounce: false, start: ~N[2019-01-01 02:00:00]}
|
2020-05-18 12:44:52 +03:00
|
|
|
])
|
2020-05-07 14:28:41 +03:00
|
|
|
end
|
|
|
|
end
|