From 231cf556b0ae54732a4c683ee26eed1f91565381 Mon Sep 17 00:00:00 2001 From: William Tso Date: Tue, 25 Mar 2025 13:03:01 +0800 Subject: [PATCH] table sql --- .../clickhouse/create_team_project_qrcode.sql | 146 --- .../clickhouse/load-clickhouse-testdata.sh | 29 - scripts/db/sql/clickhouse/mock_link_data.sql | 997 ------------------ .../db/sql/clickhouse/modify_device_type.sql | 122 --- scripts/db/sql/clickhouse/recreate_limq.sql | 379 ------- .../clickhouse/seed-clickhouse-analytics.sql | 828 --------------- 6 files changed, 2501 deletions(-) delete mode 100644 scripts/db/sql/clickhouse/create_team_project_qrcode.sql delete mode 100755 scripts/db/sql/clickhouse/load-clickhouse-testdata.sh delete mode 100644 scripts/db/sql/clickhouse/mock_link_data.sql delete mode 100644 scripts/db/sql/clickhouse/modify_device_type.sql delete mode 100644 scripts/db/sql/clickhouse/recreate_limq.sql delete mode 100644 scripts/db/sql/clickhouse/seed-clickhouse-analytics.sql diff --git a/scripts/db/sql/clickhouse/create_team_project_qrcode.sql b/scripts/db/sql/clickhouse/create_team_project_qrcode.sql deleted file mode 100644 index 67259ae..0000000 --- a/scripts/db/sql/clickhouse/create_team_project_qrcode.sql +++ /dev/null @@ -1,146 +0,0 @@ --- 添加team、project和qrcode表到limq数据库 -USE limq; - --- 团队表 -CREATE TABLE IF NOT EXISTS limq.teams ( - team_id String, - name String, - created_at DateTime, - created_by String, - description String DEFAULT '', - avatar_url String DEFAULT '', - is_active Boolean DEFAULT true, - plan_type Enum8( - 'free' = 1, - 'pro' = 2, - 'enterprise' = 3 - ), - members_count UInt32 DEFAULT 1, - PRIMARY KEY (team_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - team_id SETTINGS index_granularity = 8192; - --- 项目表 -CREATE TABLE IF NOT EXISTS limq.projects ( - project_id String, - team_id String, - name String, - created_at DateTime, - created_by String, - description String DEFAULT '', - is_archived Boolean DEFAULT false, - links_count UInt32 DEFAULT 0, - total_clicks UInt64 DEFAULT 0, - last_updated DateTime DEFAULT now(), - PRIMARY KEY (project_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (project_id, team_id) SETTINGS index_granularity = 8192; - --- QR码表 (扩展现有的qr_scans表) -CREATE TABLE IF NOT EXISTS limq.qrcodes ( - qr_code_id String, - link_id String, - team_id String, - project_id String DEFAULT '', - name String, - description String DEFAULT '', - created_at DateTime, - created_by String, - updated_at DateTime DEFAULT now(), - qr_type Enum8( - 'standard' = 1, - 'custom' = 2, - 'dynamic' = 3 - ) DEFAULT 'standard', - image_url String DEFAULT '', - design_config String DEFAULT '{}', - is_active Boolean DEFAULT true, - total_scans UInt64 DEFAULT 0, - unique_scanners UInt32 DEFAULT 0, - PRIMARY KEY (qr_code_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (qr_code_id, link_id) SETTINGS index_granularity = 8192; - --- 团队成员表 -CREATE TABLE IF NOT EXISTS limq.team_members ( - team_id String, - user_id String, - role Enum8( - 'owner' = 1, - 'admin' = 2, - 'editor' = 3, - 'viewer' = 4 - ), - joined_at DateTime DEFAULT now(), - invited_by String, - is_active Boolean DEFAULT true, - last_active DateTime DEFAULT now(), - PRIMARY KEY (team_id, user_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (team_id, user_id) SETTINGS index_granularity = 8192; - --- 团队每日统计视图 -CREATE MATERIALIZED VIEW limq.team_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, team_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.team_id AS team_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.team_id != '' -GROUP BY - date, - l.team_id; - --- 项目每日统计视图 -CREATE MATERIALIZED VIEW limq.project_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, project_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.project_id AS project_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.project_id != '' -GROUP BY - date, - l.project_id; - --- QR码每日统计视图 -CREATE MATERIALIZED VIEW limq.qrcode_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, qr_code_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(scan_time) AS date, - qr_code_id, - count() AS total_scans, - uniqExact(visitor_id) AS unique_scanners, - countIf(led_to_conversion) AS conversions, - countIf(device_type = 'mobile') AS mobile_scans, - countIf(device_type = 'tablet') AS tablet_scans, - countIf(device_type = 'desktop') AS desktop_scans, - uniqExact(location) AS unique_locations -FROM - limq.qr_scans -GROUP BY - date, - qr_code_id; \ No newline at end of file diff --git a/scripts/db/sql/clickhouse/load-clickhouse-testdata.sh b/scripts/db/sql/clickhouse/load-clickhouse-testdata.sh deleted file mode 100755 index 26f99c3..0000000 --- a/scripts/db/sql/clickhouse/load-clickhouse-testdata.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -# 脚本名称: load-clickhouse-testdata.sh -# 用途: 将测试数据加载到ClickHouse数据库中 - -# 设置脚本目录路径 -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -# 设置SQL文件路径 -SQL_FILE="$SCRIPT_DIR/sql/clickhouse/seed-clickhouse-analytics.sql" - -# 检查SQL文件是否存在 -if [ ! -f "$SQL_FILE" ]; then - echo "错误: SQL文件 '$SQL_FILE' 不存在" - exit 1 -fi - -# 执行CH查询脚本 -echo "开始加载测试数据到ClickHouse数据库..." -bash "$SCRIPT_DIR/sql/clickhouse/ch-query.sh" -f "$SQL_FILE" - -# 检查执行结果 -if [ $? -eq 0 ]; then - echo "测试数据已成功加载到ClickHouse数据库" -else - echo "错误: 加载测试数据失败" - exit 1 -fi - -exit 0 diff --git a/scripts/db/sql/clickhouse/mock_link_data.sql b/scripts/db/sql/clickhouse/mock_link_data.sql deleted file mode 100644 index 2fa76cb..0000000 --- a/scripts/db/sql/clickhouse/mock_link_data.sql +++ /dev/null @@ -1,997 +0,0 @@ --- 移动端点击访问事件 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 10:25:30', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-123', - 's-456', - 'click', - '103.45.67.89', - 'China', - 'Shanghai', - 'https://www.google.com', - 'google', - 'organic', - 'none', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 45, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 11:32:21', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-124', - 's-457', - 'click', - '43.78.123.45', - 'Japan', - 'Tokyo', - 'https://twitter.com', - 'twitter', - 'social', - 'spring_promo', - 'Mozilla/5.0 (Android 10)', - 'mobile', - 'Chrome', - 'Android', - 15, - true, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 14:15:45', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-125', - 's-458', - 'click', - '72.34.67.81', - 'US', - 'New York', - 'https://www.facebook.com', - 'facebook', - 'social', - 'crypto_ad', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 120, - false, - false, - 'interact', - 0 - ); - --- 桌面设备点击事件 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 08:45:12', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-126', - 's-459', - 'click', - '89.67.43.21', - 'Germany', - 'Berlin', - 'https://www.reddit.com', - 'reddit', - 'referral', - 'none', - 'Mozilla/5.0 (Windows NT 10.0)', - 'desktop', - 'Chrome', - 'Windows', - 300, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 16:20:33', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-127', - 's-460', - 'click', - '178.65.43.12', - 'UK', - 'London', - 'https://www.linkedin.com', - 'linkedin', - 'social', - 'biz_campaign', - 'Mozilla/5.0 (Macintosh)', - 'desktop', - 'Safari', - 'MacOS', - 250, - false, - false, - 'stay', - 0 - ); - --- 平板设备点击事件 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 13:10:55', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-128', - 's-461', - 'click', - '156.78.34.12', - 'Canada', - 'Toronto', - 'https://www.youtube.com', - 'youtube', - 'video', - 'tutorial', - 'Mozilla/5.0 (iPad)', - 'tablet', - 'Safari', - 'iOS', - 180, - false, - false, - 'visit', - 0 - ); - --- QR扫描访问事件 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 09:30:22', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_qr', - 'v-129', - 's-462', - 'click', - '101.56.78.90', - 'China', - 'Beijing', - 'direct', - 'qr', - 'print', - 'offline_event', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 75, - false, - true, - 'visit', - 0 - ); - --- 转化事件 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 10:27:45', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-123', - 's-456', - 'conversion', - '103.45.67.89', - 'China', - 'Shanghai', - 'https://www.google.com', - 'google', - 'organic', - 'none', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 120, - false, - false, - 'signup', - 50 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-15 08:52:18', - '2025-03-15', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-126', - 's-459', - 'conversion', - '89.67.43.21', - 'Germany', - 'Berlin', - 'https://www.reddit.com', - 'reddit', - 'referral', - 'none', - 'Mozilla/5.0 (Windows NT 10.0)', - 'desktop', - 'Chrome', - 'Windows', - 450, - false, - false, - 'purchase', - 150.75 - ); - --- 第二天的数据 (3/16) -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-16 11:15:30', - '2025-03-16', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-130', - 's-463', - 'click', - '178.91.45.67', - 'France', - 'Paris', - 'https://www.google.com', - 'google', - 'organic', - 'none', - 'Mozilla/5.0 (Android 11)', - 'mobile', - 'Chrome', - 'Android', - 60, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-16 14:22:45', - '2025-03-16', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-131', - 's-464', - 'click', - '89.123.45.78', - 'Spain', - 'Madrid', - 'https://www.instagram.com', - 'instagram', - 'social', - 'influencer', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 90, - false, - false, - 'interact', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-16 16:40:12', - '2025-03-16', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-131', - 's-464', - 'conversion', - '89.123.45.78', - 'Spain', - 'Madrid', - 'https://www.instagram.com', - 'instagram', - 'social', - 'influencer', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 200, - false, - false, - 'subscription', - 75.50 - ); - --- 第三天数据 (3/17) -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-17 09:10:22', - '2025-03-17', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-132', - 's-465', - 'click', - '45.67.89.123', - 'US', - 'Los Angeles', - 'https://www.google.com', - 'google', - 'cpc', - 'spring_sale', - 'Mozilla/5.0 (Windows NT 10.0)', - 'desktop', - 'Edge', - 'Windows', - 150, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-17 12:30:45', - '2025-03-17', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-133', - 's-466', - 'click', - '67.89.123.45', - 'Brazil', - 'Sao Paulo', - 'https://www.yahoo.com', - 'yahoo', - 'organic', - 'none', - 'Mozilla/5.0 (iPad)', - 'tablet', - 'Safari', - 'iOS', - 120, - false, - false, - 'stay', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-17 15:45:33', - '2025-03-17', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-132', - 's-465', - 'conversion', - '45.67.89.123', - 'US', - 'Los Angeles', - 'https://www.google.com', - 'google', - 'cpc', - 'spring_sale', - 'Mozilla/5.0 (Windows NT 10.0)', - 'desktop', - 'Edge', - 'Windows', - 300, - false, - false, - 'purchase', - 225.50 - ); - --- 添加一周前的数据 (对比期) -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-08 10:25:30', - '2025-03-08', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-140', - 's-470', - 'click', - '103.45.67.89', - 'China', - 'Shanghai', - 'https://www.google.com', - 'google', - 'organic', - 'none', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 30, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-08 11:32:21', - '2025-03-08', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-141', - 's-471', - 'click', - '89.67.43.21', - 'Germany', - 'Berlin', - 'https://www.reddit.com', - 'reddit', - 'referral', - 'none', - 'Mozilla/5.0 (Windows NT 10.0)', - 'desktop', - 'Chrome', - 'Windows', - 200, - false, - false, - 'visit', - 0 - ); - -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - conversion_type, - conversion_value - ) -VALUES - ( - generateUUIDv4(), - '2025-03-08 13:10:55', - '2025-03-08', - 'a71fcfe8-d293-4d6d-91c5-1528fa7f6294', - 'ch_main', - 'v-140', - 's-470', - 'conversion', - '103.45.67.89', - 'China', - 'Shanghai', - 'https://www.google.com', - 'google', - 'organic', - 'none', - 'Mozilla/5.0 (iPhone)', - 'mobile', - 'Safari', - 'iOS', - 100, - false, - false, - 'purchase', - 100.00 - ); \ No newline at end of file diff --git a/scripts/db/sql/clickhouse/modify_device_type.sql b/scripts/db/sql/clickhouse/modify_device_type.sql deleted file mode 100644 index d1651d5..0000000 --- a/scripts/db/sql/clickhouse/modify_device_type.sql +++ /dev/null @@ -1,122 +0,0 @@ --- 修改设备类型字段从枚举类型更改为字符串类型 --- 先删除依赖于link_events表的物化视图 -DROP TABLE IF EXISTS limq.platform_distribution; - -DROP TABLE IF EXISTS limq.link_hourly_patterns; - -DROP TABLE IF EXISTS limq.link_daily_stats; - -DROP TABLE IF EXISTS limq.team_daily_stats; - -DROP TABLE IF EXISTS limq.project_daily_stats; - --- 修改link_events表的device_type字段 -ALTER TABLE - limq.link_events -MODIFY - COLUMN device_type String; - --- 重新创建物化视图 --- 每日链接汇总视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.link_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, link_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - link_id, - count() AS total_clicks, - uniqExact(visitor_id) AS unique_visitors, - uniqExact(session_id) AS unique_sessions, - sum(time_spent_sec) AS total_time_spent, - avg(time_spent_sec) AS avg_time_spent, - countIf(is_bounce) AS bounce_count, - countIf(event_type = 'conversion') AS conversion_count, - uniqExact(referrer) AS unique_referrers, - countIf(device_type = 'mobile') AS mobile_count, - countIf(device_type = 'tablet') AS tablet_count, - countIf(device_type = 'desktop') AS desktop_count, - countIf(is_qr_scan) AS qr_scan_count, - sum(conversion_value) AS total_conversion_value -FROM - limq.link_events -GROUP BY - date, - link_id; - --- 每小时访问模式视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.link_hourly_patterns ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, hour, link_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - toHour(event_time) AS hour, - link_id, - count() AS visits, - uniqExact(visitor_id) AS unique_visitors -FROM - limq.link_events -GROUP BY - date, - hour, - link_id; - --- 平台分布视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.platform_distribution ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, utm_source, device_type) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - utm_source, - device_type, - count() AS visits, - uniqExact(visitor_id) AS unique_visitors -FROM - limq.link_events -WHERE - utm_source != '' -GROUP BY - date, - utm_source, - device_type; - --- 团队每日统计视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.team_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, team_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.team_id AS team_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.team_id != '' -GROUP BY - date, - l.team_id; - --- 项目每日统计视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.project_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, project_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.project_id AS project_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.project_id != '' -GROUP BY - date, - l.project_id; \ No newline at end of file diff --git a/scripts/db/sql/clickhouse/recreate_limq.sql b/scripts/db/sql/clickhouse/recreate_limq.sql deleted file mode 100644 index 91baa7b..0000000 --- a/scripts/db/sql/clickhouse/recreate_limq.sql +++ /dev/null @@ -1,379 +0,0 @@ --- 删除所有物化视图(需要先删除视图,因为它们依赖于表) -DROP TABLE IF EXISTS limq.platform_distribution; - -DROP TABLE IF EXISTS limq.link_hourly_patterns; - -DROP TABLE IF EXISTS limq.link_daily_stats; - -DROP TABLE IF EXISTS limq.team_daily_stats; - -DROP TABLE IF EXISTS limq.project_daily_stats; - -DROP TABLE IF EXISTS limq.qrcode_daily_stats; - --- 删除所有表 -DROP TABLE IF EXISTS limq.qr_scans; - -DROP TABLE IF EXISTS limq.sessions; - -DROP TABLE IF EXISTS limq.link_events; - -DROP TABLE IF EXISTS limq.links; - -DROP TABLE IF EXISTS limq.teams; - -DROP TABLE IF EXISTS limq.projects; - -DROP TABLE IF EXISTS limq.qrcodes; - -DROP TABLE IF EXISTS limq.team_members; - -DROP TABLE IF EXISTS limq.users; - --- 创建数据库(如果不存在) -CREATE DATABASE IF NOT EXISTS limq; - --- 切换到limq数据库 -USE limq; - --- 创建短链接访问事件表 -CREATE TABLE IF NOT EXISTS limq.link_events ( - event_id UUID DEFAULT generateUUIDv4(), - event_time DateTime64(3) DEFAULT now64(), - date Date DEFAULT toDate(event_time), - link_id String, - channel_id String, - visitor_id String, - session_id String, - event_type Enum8( - 'click' = 1, - 'redirect' = 2, - 'conversion' = 3, - 'error' = 4 - ), - -- 访问者信息 - ip_address String, - country String, - city String, - -- 来源信息 - referrer String, - utm_source String, - utm_medium String, - utm_campaign String, - -- 设备信息 - user_agent String, - device_type Enum8( - 'mobile' = 1, - 'tablet' = 2, - 'desktop' = 3, - 'other' = 4 - ), - browser String, - os String, - -- 交互信息 - time_spent_sec UInt32 DEFAULT 0, - is_bounce Boolean DEFAULT true, - -- QR码相关 - is_qr_scan Boolean DEFAULT false, - qr_code_id String DEFAULT '', - -- 转化数据 - conversion_type Enum8( - 'visit' = 1, - 'stay' = 2, - 'interact' = 3, - 'signup' = 4, - 'subscription' = 5, - 'purchase' = 6 - ) DEFAULT 'visit', - conversion_value Float64 DEFAULT 0, - -- 其他属性 - custom_data String DEFAULT '{}' -) ENGINE = MergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, link_id, event_time) SETTINGS index_granularity = 8192; - --- 短链接维度表 -CREATE TABLE IF NOT EXISTS limq.links ( - link_id String, - original_url String, - created_at DateTime64(3), - created_by String, - title String, - description String, - tags Array(String), - is_active Boolean DEFAULT true, - expires_at Nullable(DateTime64(3)), - team_id String DEFAULT '', - project_id String DEFAULT '', - PRIMARY KEY (link_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - link_id SETTINGS index_granularity = 8192; - --- 会话跟踪表 -CREATE TABLE IF NOT EXISTS limq.sessions ( - session_id String, - visitor_id String, - link_id String, - started_at DateTime64(3), - last_activity DateTime64(3), - ended_at Nullable(DateTime64(3)), - duration_sec UInt32 DEFAULT 0, - session_pages UInt8 DEFAULT 1, - is_completed Boolean DEFAULT false, - PRIMARY KEY (session_id) -) ENGINE = ReplacingMergeTree(last_activity) -ORDER BY - (session_id, link_id, visitor_id) SETTINGS index_granularity = 8192; - --- QR码统计表 -CREATE TABLE IF NOT EXISTS limq.qr_scans ( - scan_id UUID DEFAULT generateUUIDv4(), - qr_code_id String, - link_id String, - scan_time DateTime64(3), - visitor_id String, - location String, - device_type Enum8( - 'mobile' = 1, - 'tablet' = 2, - 'desktop' = 3, - 'other' = 4 - ), - led_to_conversion Boolean DEFAULT false, - PRIMARY KEY (scan_id) -) ENGINE = MergeTree() PARTITION BY toYYYYMM(scan_time) -ORDER BY - scan_id SETTINGS index_granularity = 8192; - --- 团队表 -CREATE TABLE IF NOT EXISTS limq.teams ( - team_id String, - name String, - created_at DateTime, - created_by String, - description String DEFAULT '', - avatar_url String DEFAULT '', - is_active Boolean DEFAULT true, - plan_type Enum8( - 'free' = 1, - 'pro' = 2, - 'enterprise' = 3 - ), - members_count UInt32 DEFAULT 1, - PRIMARY KEY (team_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - team_id SETTINGS index_granularity = 8192; - --- 项目表 -CREATE TABLE IF NOT EXISTS limq.projects ( - project_id String, - team_id String, - name String, - created_at DateTime, - created_by String, - description String DEFAULT '', - is_archived Boolean DEFAULT false, - links_count UInt32 DEFAULT 0, - total_clicks UInt64 DEFAULT 0, - last_updated DateTime DEFAULT now(), - PRIMARY KEY (project_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (project_id, team_id) SETTINGS index_granularity = 8192; - --- QR码表 -CREATE TABLE IF NOT EXISTS limq.qrcodes ( - qr_code_id String, - link_id String, - team_id String, - project_id String DEFAULT '', - name String, - description String DEFAULT '', - created_at DateTime, - created_by String, - updated_at DateTime DEFAULT now(), - qr_type Enum8( - 'standard' = 1, - 'custom' = 2, - 'dynamic' = 3 - ) DEFAULT 'standard', - image_url String DEFAULT '', - design_config String DEFAULT '{}', - is_active Boolean DEFAULT true, - total_scans UInt64 DEFAULT 0, - unique_scanners UInt32 DEFAULT 0, - PRIMARY KEY (qr_code_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (qr_code_id, link_id) SETTINGS index_granularity = 8192; - --- 团队成员表 -CREATE TABLE IF NOT EXISTS limq.team_members ( - team_id String, - user_id String, - role Enum8( - 'owner' = 1, - 'admin' = 2, - 'editor' = 3, - 'viewer' = 4 - ), - joined_at DateTime DEFAULT now(), - invited_by String, - is_active Boolean DEFAULT true, - last_active DateTime DEFAULT now(), - PRIMARY KEY (team_id, user_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - (team_id, user_id) SETTINGS index_granularity = 8192; - --- 用户表 -CREATE TABLE IF NOT EXISTS limq.users ( - user_id String, - username String, - email String, - full_name String, - avatar_url String DEFAULT '', - created_at DateTime, - last_login DateTime DEFAULT now(), - is_active Boolean DEFAULT true, - is_verified Boolean DEFAULT false, - auth_provider Enum8( - 'email' = 1, - 'google' = 2, - 'github' = 3, - 'microsoft' = 4 - ) DEFAULT 'email', - roles Array(String) DEFAULT [ 'user' ], - preferences String DEFAULT '{}', - teams_count UInt32 DEFAULT 0, - links_created UInt32 DEFAULT 0, - PRIMARY KEY (user_id) -) ENGINE = ReplacingMergeTree() -ORDER BY - user_id SETTINGS index_granularity = 8192; - --- 每日链接汇总视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.link_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, link_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - link_id, - count() AS total_clicks, - uniqExact(visitor_id) AS unique_visitors, - uniqExact(session_id) AS unique_sessions, - sum(time_spent_sec) AS total_time_spent, - avg(time_spent_sec) AS avg_time_spent, - countIf(is_bounce) AS bounce_count, - countIf(event_type = 'conversion') AS conversion_count, - uniqExact(referrer) AS unique_referrers, - countIf(device_type = 'mobile') AS mobile_count, - countIf(device_type = 'tablet') AS tablet_count, - countIf(device_type = 'desktop') AS desktop_count, - countIf(is_qr_scan) AS qr_scan_count, - sum(conversion_value) AS total_conversion_value -FROM - limq.link_events -GROUP BY - date, - link_id; - --- 每小时访问模式视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.link_hourly_patterns ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, hour, link_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - toHour(event_time) AS hour, - link_id, - count() AS visits, - uniqExact(visitor_id) AS unique_visitors -FROM - limq.link_events -GROUP BY - date, - hour, - link_id; - --- 平台分布视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.platform_distribution ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, utm_source, device_type) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - utm_source, - device_type, - count() AS visits, - uniqExact(visitor_id) AS unique_visitors -FROM - limq.link_events -WHERE - utm_source != '' -GROUP BY - date, - utm_source, - device_type; - --- 团队每日统计视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.team_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, team_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.team_id AS team_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.team_id != '' -GROUP BY - date, - l.team_id; - --- 项目每日统计视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.project_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, project_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(event_time) AS date, - l.project_id AS project_id, - count() AS total_clicks, - uniqExact(e.visitor_id) AS unique_visitors, - countIf(e.event_type = 'conversion') AS conversion_count, - uniqExact(e.link_id) AS links_used, - countIf(e.is_qr_scan) AS qr_scan_count -FROM - limq.link_events e - JOIN limq.links l ON e.link_id = l.link_id -WHERE - l.project_id != '' -GROUP BY - date, - l.project_id; - --- QR码每日统计视图 -CREATE MATERIALIZED VIEW IF NOT EXISTS limq.qrcode_daily_stats ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (date, qr_code_id) SETTINGS index_granularity = 8192 AS -SELECT - toDate(scan_time) AS date, - qr_code_id, - count() AS total_scans, - uniqExact(visitor_id) AS unique_scanners, - countIf(led_to_conversion) AS conversions, - countIf(device_type = 'mobile') AS mobile_scans, - countIf(device_type = 'tablet') AS tablet_scans, - countIf(device_type = 'desktop') AS desktop_scans, - uniqExact(location) AS unique_locations -FROM - limq.qr_scans -GROUP BY - date, - qr_code_id; \ No newline at end of file diff --git a/scripts/db/sql/clickhouse/seed-clickhouse-analytics.sql b/scripts/db/sql/clickhouse/seed-clickhouse-analytics.sql deleted file mode 100644 index d6bdcbc..0000000 --- a/scripts/db/sql/clickhouse/seed-clickhouse-analytics.sql +++ /dev/null @@ -1,828 +0,0 @@ --- 清空现有数据(可选) -TRUNCATE TABLE IF EXISTS limq.link_events; - -TRUNCATE TABLE IF EXISTS limq.link_daily_stats; - -TRUNCATE TABLE IF EXISTS limq.link_hourly_patterns; - -TRUNCATE TABLE IF EXISTS limq.links; - --- 使用固定的UUID值插入链接 -INSERT INTO - limq.links ( - link_id, - original_url, - created_at, - created_by, - title, - description, - tags, - is_active - ) -VALUES - ( - '11111111-1111-1111-1111-111111111111', - 'https://example.com/page1', - now(), - 'user-1', - '产品页面', - '我们的主要产品页面', - [ '产品', - '营销' ], - true - ); - -INSERT INTO - limq.links ( - link_id, - original_url, - created_at, - created_by, - title, - description, - tags, - is_active - ) -VALUES - ( - '22222222-2222-2222-2222-222222222222', - 'https://example.com/promo', - now(), - 'user-1', - '促销活动', - '夏季特别促销活动', - [ '促销', - '活动' ], - true - ); - -INSERT INTO - limq.links ( - link_id, - original_url, - created_at, - created_by, - title, - description, - tags, - is_active - ) -VALUES - ( - '33333333-3333-3333-3333-333333333333', - 'https://example.com/blog', - now(), - 'user-2', - '公司博客', - '公司新闻和更新', - [ '博客', - '内容' ], - true - ); - -INSERT INTO - limq.links ( - link_id, - original_url, - created_at, - created_by, - title, - description, - tags, - is_active - ) -VALUES - ( - '44444444-4444-4444-4444-444444444444', - 'https://example.com/signup', - now(), - 'user-2', - '注册页面', - '新用户注册页面', - [ '转化', - '注册' ], - true - ); - --- 为第一个链接创建500条记录 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - qr_code_id, - conversion_type, - conversion_value, - custom_data - ) -SELECT - generateUUIDv4() AS event_id, - subtractDays(now(), rand() % 30) AS event_time, - toDate(event_time) AS date, - '11111111-1111-1111-1111-111111111111' AS link_id, - 'channel-1' AS channel_id, - concat('visitor-', toString(rand() % 100 + 1)) AS visitor_id, - concat('session-', toString(number % 50 + 1)) AS session_id, - multiIf( - rand() % 100 < 70, - 'click', - rand() % 100 < 90, - 'redirect', - rand() % 100 < 98, - 'conversion', - 'error' - ) AS event_type, - concat('192.168.1.', toString(rand() % 255)) AS ip_address, - multiIf( - rand() % 100 < 60, - 'China', - rand() % 100 < 85, - 'US', - rand() % 100 < 95, - 'Japan', - 'Other' - ) AS country, - multiIf( - rand() % 100 < 60, - 'Beijing', - rand() % 100 < 85, - 'New York', - rand() % 100 < 95, - 'Tokyo', - 'Other' - ) AS city, - multiIf( - rand() % 100 < 30, - 'https://google.com', - rand() % 100 < 50, - 'https://facebook.com', - rand() % 100 < 65, - 'https://twitter.com', - rand() % 100 < 75, - 'https://instagram.com', - rand() % 100 < 85, - 'https://linkedin.com', - rand() % 100 < 90, - 'https://bing.com', - rand() % 100 < 95, - 'https://baidu.com', - 'direct' - ) AS referrer, - multiIf( - rand() % 100 < 40, - 'google', - rand() % 100 < 70, - 'facebook', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_source, - multiIf( - rand() % 100 < 40, - 'cpc', - rand() % 100 < 70, - 'social', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_medium, - multiIf( - rand() % 100 < 40, - 'summer_sale', - rand() % 100 < 70, - 'product_launch', - rand() % 100 < 90, - 'newsletter', - 'brand' - ) AS utm_campaign, - 'Mozilla/5.0' AS user_agent, - multiIf( - rand() % 100 < 60, - 'mobile', - rand() % 100 < 85, - 'desktop', - rand() % 100 < 95, - 'tablet', - 'other' - ) AS device_type, - multiIf( - rand() % 100 < 50, - 'Chrome', - rand() % 100 < 80, - 'Safari', - rand() % 100 < 95, - 'Firefox', - 'Edge' - ) AS browser, - multiIf( - rand() % 100 < 50, - 'iOS', - rand() % 100 < 90, - 'Android', - 'Windows' - ) AS os, - rand() % 300 AS time_spent_sec, - rand() % 100 < 25 AS is_bounce, - rand() % 100 < 20 AS is_qr_scan, - concat('qr-', toString(rand() % 10 + 1)) AS qr_code_id, - multiIf( - rand() % 100 < 50, - 'visit', - rand() % 100 < 70, - 'stay', - rand() % 100 < 85, - 'interact', - rand() % 100 < 93, - 'signup', - rand() % 100 < 97, - 'subscription', - 'purchase' - ) AS conversion_type, - rand() % 100 * 1.5 AS conversion_value, - '{}' AS custom_data -FROM - numbers(500); - --- 为第二个链接创建300条记录 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - qr_code_id, - conversion_type, - conversion_value, - custom_data - ) -SELECT - generateUUIDv4() AS event_id, - subtractDays(now(), rand() % 30) AS event_time, - toDate(event_time) AS date, - '22222222-2222-2222-2222-222222222222' AS link_id, - 'channel-1' AS channel_id, - concat('visitor-', toString(rand() % 100 + 1)) AS visitor_id, - concat('session-', toString(number % 40 + 1)) AS session_id, - multiIf( - rand() % 100 < 70, - 'click', - rand() % 100 < 90, - 'redirect', - rand() % 100 < 98, - 'conversion', - 'error' - ) AS event_type, - concat('192.168.1.', toString(rand() % 255)) AS ip_address, - multiIf( - rand() % 100 < 60, - 'China', - rand() % 100 < 85, - 'US', - rand() % 100 < 95, - 'Japan', - 'Other' - ) AS country, - multiIf( - rand() % 100 < 60, - 'Beijing', - rand() % 100 < 85, - 'New York', - rand() % 100 < 95, - 'Tokyo', - 'Other' - ) AS city, - multiIf( - rand() % 100 < 30, - 'https://google.com', - rand() % 100 < 50, - 'https://facebook.com', - rand() % 100 < 65, - 'https://twitter.com', - rand() % 100 < 75, - 'https://instagram.com', - rand() % 100 < 85, - 'https://linkedin.com', - rand() % 100 < 90, - 'https://bing.com', - rand() % 100 < 95, - 'https://baidu.com', - 'direct' - ) AS referrer, - multiIf( - rand() % 100 < 40, - 'google', - rand() % 100 < 70, - 'facebook', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_source, - multiIf( - rand() % 100 < 40, - 'cpc', - rand() % 100 < 70, - 'social', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_medium, - multiIf( - rand() % 100 < 40, - 'summer_sale', - rand() % 100 < 70, - 'product_launch', - rand() % 100 < 90, - 'newsletter', - 'brand' - ) AS utm_campaign, - 'Mozilla/5.0' AS user_agent, - multiIf( - rand() % 100 < 60, - 'mobile', - rand() % 100 < 85, - 'desktop', - rand() % 100 < 95, - 'tablet', - 'other' - ) AS device_type, - multiIf( - rand() % 100 < 50, - 'Chrome', - rand() % 100 < 80, - 'Safari', - rand() % 100 < 95, - 'Firefox', - 'Edge' - ) AS browser, - multiIf( - rand() % 100 < 50, - 'iOS', - rand() % 100 < 90, - 'Android', - 'Windows' - ) AS os, - rand() % 300 AS time_spent_sec, - rand() % 100 < 25 AS is_bounce, - rand() % 100 < 15 AS is_qr_scan, - concat('qr-', toString(rand() % 10 + 1)) AS qr_code_id, - multiIf( - rand() % 100 < 50, - 'visit', - rand() % 100 < 70, - 'stay', - rand() % 100 < 85, - 'interact', - rand() % 100 < 93, - 'signup', - rand() % 100 < 97, - 'subscription', - 'purchase' - ) AS conversion_type, - rand() % 100 * 2.5 AS conversion_value, - '{}' AS custom_data -FROM - numbers(300); - --- 为第三个链接创建200条记录 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - qr_code_id, - conversion_type, - conversion_value, - custom_data - ) -SELECT - generateUUIDv4() AS event_id, - subtractDays(now(), rand() % 30) AS event_time, - toDate(event_time) AS date, - '33333333-3333-3333-3333-333333333333' AS link_id, - 'channel-2' AS channel_id, - concat('visitor-', toString(rand() % 100 + 1)) AS visitor_id, - concat('session-', toString(number % 30 + 1)) AS session_id, - multiIf( - rand() % 100 < 70, - 'click', - rand() % 100 < 90, - 'redirect', - rand() % 100 < 98, - 'conversion', - 'error' - ) AS event_type, - concat('192.168.1.', toString(rand() % 255)) AS ip_address, - multiIf( - rand() % 100 < 60, - 'China', - rand() % 100 < 85, - 'US', - rand() % 100 < 95, - 'Japan', - 'Other' - ) AS country, - multiIf( - rand() % 100 < 60, - 'Beijing', - rand() % 100 < 85, - 'New York', - rand() % 100 < 95, - 'Tokyo', - 'Other' - ) AS city, - multiIf( - rand() % 100 < 30, - 'https://google.com', - rand() % 100 < 50, - 'https://facebook.com', - rand() % 100 < 65, - 'https://twitter.com', - rand() % 100 < 75, - 'https://instagram.com', - rand() % 100 < 85, - 'https://linkedin.com', - rand() % 100 < 90, - 'https://bing.com', - rand() % 100 < 95, - 'https://baidu.com', - 'direct' - ) AS referrer, - multiIf( - rand() % 100 < 40, - 'google', - rand() % 100 < 70, - 'facebook', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_source, - multiIf( - rand() % 100 < 40, - 'cpc', - rand() % 100 < 70, - 'social', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_medium, - multiIf( - rand() % 100 < 40, - 'summer_sale', - rand() % 100 < 70, - 'product_launch', - rand() % 100 < 90, - 'newsletter', - 'brand' - ) AS utm_campaign, - 'Mozilla/5.0' AS user_agent, - multiIf( - rand() % 100 < 60, - 'mobile', - rand() % 100 < 85, - 'desktop', - rand() % 100 < 95, - 'tablet', - 'other' - ) AS device_type, - multiIf( - rand() % 100 < 50, - 'Chrome', - rand() % 100 < 80, - 'Safari', - rand() % 100 < 95, - 'Firefox', - 'Edge' - ) AS browser, - multiIf( - rand() % 100 < 50, - 'iOS', - rand() % 100 < 90, - 'Android', - 'Windows' - ) AS os, - rand() % 600 AS time_spent_sec, - rand() % 100 < 15 AS is_bounce, - rand() % 100 < 10 AS is_qr_scan, - concat('qr-', toString(rand() % 10 + 1)) AS qr_code_id, - multiIf( - rand() % 100 < 50, - 'visit', - rand() % 100 < 70, - 'stay', - rand() % 100 < 85, - 'interact', - rand() % 100 < 93, - 'signup', - rand() % 100 < 97, - 'subscription', - 'purchase' - ) AS conversion_type, - rand() % 100 * 1.2 AS conversion_value, - '{}' AS custom_data -FROM - numbers(200); - --- 为第四个链接创建400条记录 -INSERT INTO - limq.link_events ( - event_id, - event_time, - date, - link_id, - channel_id, - visitor_id, - session_id, - event_type, - ip_address, - country, - city, - referrer, - utm_source, - utm_medium, - utm_campaign, - user_agent, - device_type, - browser, - os, - time_spent_sec, - is_bounce, - is_qr_scan, - qr_code_id, - conversion_type, - conversion_value, - custom_data - ) -SELECT - generateUUIDv4() AS event_id, - subtractDays(now(), rand() % 30) AS event_time, - toDate(event_time) AS date, - '44444444-4444-4444-4444-444444444444' AS link_id, - 'channel-2' AS channel_id, - concat('visitor-', toString(rand() % 100 + 1)) AS visitor_id, - concat('session-', toString(number % 60 + 1)) AS session_id, - multiIf( - rand() % 100 < 70, - 'click', - rand() % 100 < 90, - 'redirect', - rand() % 100 < 98, - 'conversion', - 'error' - ) AS event_type, - concat('192.168.1.', toString(rand() % 255)) AS ip_address, - multiIf( - rand() % 100 < 60, - 'China', - rand() % 100 < 85, - 'US', - rand() % 100 < 95, - 'Japan', - 'Other' - ) AS country, - multiIf( - rand() % 100 < 60, - 'Beijing', - rand() % 100 < 85, - 'New York', - rand() % 100 < 95, - 'Tokyo', - 'Other' - ) AS city, - multiIf( - rand() % 100 < 30, - 'https://google.com', - rand() % 100 < 50, - 'https://facebook.com', - rand() % 100 < 65, - 'https://twitter.com', - rand() % 100 < 75, - 'https://instagram.com', - rand() % 100 < 85, - 'https://linkedin.com', - rand() % 100 < 90, - 'https://bing.com', - rand() % 100 < 95, - 'https://baidu.com', - 'direct' - ) AS referrer, - multiIf( - rand() % 100 < 40, - 'google', - rand() % 100 < 70, - 'facebook', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_source, - multiIf( - rand() % 100 < 40, - 'cpc', - rand() % 100 < 70, - 'social', - rand() % 100 < 90, - 'email', - 'direct' - ) AS utm_medium, - multiIf( - rand() % 100 < 40, - 'summer_sale', - rand() % 100 < 70, - 'product_launch', - rand() % 100 < 90, - 'newsletter', - 'brand' - ) AS utm_campaign, - 'Mozilla/5.0' AS user_agent, - multiIf( - rand() % 100 < 60, - 'mobile', - rand() % 100 < 85, - 'desktop', - rand() % 100 < 95, - 'tablet', - 'other' - ) AS device_type, - multiIf( - rand() % 100 < 50, - 'Chrome', - rand() % 100 < 80, - 'Safari', - rand() % 100 < 95, - 'Firefox', - 'Edge' - ) AS browser, - multiIf( - rand() % 100 < 50, - 'iOS', - rand() % 100 < 90, - 'Android', - 'Windows' - ) AS os, - rand() % 400 AS time_spent_sec, - rand() % 100 < 20 AS is_bounce, - rand() % 100 < 25 AS is_qr_scan, - concat('qr-', toString(rand() % 10 + 1)) AS qr_code_id, - multiIf( - rand() % 100 < 50, - 'visit', - rand() % 100 < 70, - 'stay', - rand() % 100 < 85, - 'interact', - rand() % 100 < 93, - 'signup', - rand() % 100 < 97, - 'subscription', - 'purchase' - ) AS conversion_type, - rand() % 100 * 3.5 AS conversion_value, - '{}' AS custom_data -FROM - numbers(400); - --- 插入link_daily_stats表数据 -INSERT INTO - limq.link_daily_stats ( - date, - link_id, - total_clicks, - unique_visitors, - unique_sessions, - total_time_spent, - avg_time_spent, - bounce_count, - conversion_count, - unique_referrers, - mobile_count, - tablet_count, - desktop_count, - qr_scan_count, - total_conversion_value - ) -SELECT - subtractDays(today(), number) AS date, - multiIf( - number % 4 = 0, - '11111111-1111-1111-1111-111111111111', - number % 4 = 1, - '22222222-2222-2222-2222-222222222222', - number % 4 = 2, - '33333333-3333-3333-3333-333333333333', - '44444444-4444-4444-4444-444444444444' - ) AS link_id, - 50 + rand() % 100 AS total_clicks, - 30 + rand() % 50 AS unique_visitors, - 20 + rand() % 40 AS unique_sessions, - (500 + rand() % 1000) * 60 AS total_time_spent, - (rand() % 10) * 60 + rand() % 60 AS avg_time_spent, - 5 + rand() % 20 AS bounce_count, - rand() % 30 AS conversion_count, - 3 + rand() % 8 AS unique_referrers, - 20 + rand() % 40 AS mobile_count, - 5 + rand() % 15 AS tablet_count, - 15 + rand() % 30 AS desktop_count, - rand() % 10 AS qr_scan_count, - rand() % 1000 * 2.5 AS total_conversion_value -FROM - numbers(30) -WHERE - number < 30; - --- 插入link_hourly_patterns表数据 -INSERT INTO - limq.link_hourly_patterns (date, hour, link_id, visits, unique_visitors) -SELECT - subtractDays(today(), number % 7) AS date, - number % 24 AS hour, - multiIf( - intDiv(number, 24) % 4 = 0, - '11111111-1111-1111-1111-111111111111', - intDiv(number, 24) % 4 = 1, - '22222222-2222-2222-2222-222222222222', - intDiv(number, 24) % 4 = 2, - '33333333-3333-3333-3333-333333333333', - '44444444-4444-4444-4444-444444444444' - ) AS link_id, - 5 + rand() % 20 AS visits, - 3 + rand() % 10 AS unique_visitors -FROM - numbers(672) -- 7天 x 24小时 x 4个链接 -WHERE - number < 672; - --- 显示数据行数,验证插入成功 -SELECT - 'link_events 表行数:' AS metric, - count() AS value -FROM - limq.link_events -UNION -ALL -SELECT - 'link_daily_stats 表行数:', - count() -FROM - limq.link_daily_stats -UNION -ALL -SELECT - 'link_hourly_patterns 表行数:', - count() -FROM - limq.link_hourly_patterns; \ No newline at end of file