From f89c08b0078487b76349c37d767c8b347eeb0615 Mon Sep 17 00:00:00 2001 From: eagleon Date: Thu, 19 Sep 2024 10:30:58 +0800 Subject: [PATCH] func(session): add ip for clickhouse --- db/clickhouse/migrations/04_add_ip.sql | 91 ++++++++++++++++++++++++++ db/clickhouse/schema.sql | 4 ++ 2 files changed, 95 insertions(+) create mode 100644 db/clickhouse/migrations/04_add_ip.sql diff --git a/db/clickhouse/migrations/04_add_ip.sql b/db/clickhouse/migrations/04_add_ip.sql new file mode 100644 index 000000000..dd8adc0d7 --- /dev/null +++ b/db/clickhouse/migrations/04_add_ip.sql @@ -0,0 +1,91 @@ +CREATE TABLE umami.website_event_join +( + session_id UUID, + ip String, + created_at DateTime('UTC') +) + engine = MergeTree + ORDER BY (session_id, created_at) + SETTINGS index_granularity = 8192; + +INSERT INTO umami.website_event_join +SELECT DISTINCT + s.session_id, + "", + s.created_at +FROM (SELECT DISTINCT session_id, + date_trunc('hour', created_at) created_at + FROM website_event) s; + +-- create new table +CREATE TABLE umami.website_event_new +( + website_id UUID, + session_id UUID, + visit_id UUID, + event_id UUID, + hostname LowCardinality(String), + browser LowCardinality(String), + os LowCardinality(String), + device LowCardinality(String), + screen LowCardinality(String), + language LowCardinality(String), + country LowCardinality(String), + subdivision1 LowCardinality(String), + subdivision2 LowCardinality(String), + city String, + ip String, + url_path String, + url_query String, + referrer_path String, + referrer_query String, + referrer_domain String, + page_title String, + event_type UInt32, + event_name String, + created_at DateTime('UTC'), + job_id UUID +) + engine = MergeTree + ORDER BY (website_id, session_id, created_at) + SETTINGS index_granularity = 8192; + +INSERT INTO umami.website_event_new +SELECT we.website_id, + we.session_id, + we.event_id, + we.hostname, + we.browser, + we.os, + we.device, + we.screen, + we.language, + we.country, + we.subdivision1, + we.subdivision2, + we.city, + j.ip, + we.url_path, + we.url_query, + we.referrer_path, + we.referrer_query, + we.referrer_domain, + we.page_title, + we.event_type, + we.event_name, + we.created_at, + we.job_id +FROM umami.website_event we +JOIN umami.website_event_join j + ON we.session_id = j.session_id + and date_trunc('hour', we.created_at) = j.created_at + +RENAME TABLE umami.website_event TO umami.website_event_old; +RENAME TABLE umami.website_event_new TO umami.website_event; + +/* + + DROP TABLE umami.website_event_old + DROP TABLE umami.website_event_join + + */ \ No newline at end of file diff --git a/db/clickhouse/schema.sql b/db/clickhouse/schema.sql index 551591cb2..a0b483a41 100644 --- a/db/clickhouse/schema.sql +++ b/db/clickhouse/schema.sql @@ -86,6 +86,7 @@ CREATE TABLE umami.website_event_stats_hourly country LowCardinality(String), subdivision1 LowCardinality(String), city String, + ip String, entry_url AggregateFunction(argMin, String, DateTime('UTC')), exit_url AggregateFunction(argMax, String, DateTime('UTC')), url_path SimpleAggregateFunction(groupArrayArray, Array(String)), @@ -126,6 +127,7 @@ SELECT country, subdivision1, city, + ip, entry_url, exit_url, url_paths as url_path, @@ -151,6 +153,7 @@ FROM (SELECT country, subdivision1, city, + ip, argMinState(url_path, created_at) entry_url, argMaxState(url_path, created_at) exit_url, arrayFilter(x -> x != '', groupArray(url_path)) as url_paths, @@ -176,6 +179,7 @@ GROUP BY website_id, country, subdivision1, city, + ip, event_type, timestamp);