📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
autogenerate clickhouse tables if not exists
Blue Mouse03ece5d232f9
diff --git a/README.md b/README.mdindex 08461826b..2a60b27b5 100644--- a/README.md+++ b/README.md@@ -54,39 +54,6 @@ create database analytics;The tables at `analytics` database should be generated on the first start of the API by the TypeORM module.--### Clickhouse DB setup:-```bash-create database analytics;--use analytics;--CREATE TABLE analytics.analytics-(- `id` UUID,- `pid` FixedString(12),- `ev` String,- `pg` Nullable(String),- `dv` Nullable(String),- `br` Nullable(String),- `os` Nullable(String),- `lc` Nullable(String),- `ref` Nullable(String),- `so` Nullable(String),- `me` Nullable(String),- `ca` Nullable(String),- `lt` Nullable(UInt16),- `cc` Nullable(FixedString(2)),- `unique` UInt8,- `created` DateTime-)-ENGINE = MergeTree()-PARTITION BY toYYYYMM(created)-ORDER BY (id, created, pid);-```--For Redis please run `redis-cli` to test if it's working well.-### NodeJS (v14 LTS) & NPM (and PM2) installation:```bashcurl -sL https://deb.nodesource.com/setup_14.x | sudo bash -diff --git a/src/common/constants.ts b/src/common/constants.tsindex 27cdc4251..32b9127c9 100644--- a/src/common/constants.ts+++ b/src/common/constants.ts@@ -1,5 +1,6 @@import { ClickHouse } from 'clickhouse'import Redis from 'ioredis'+import { v5 as uuidv5 } from 'uuid'import * as _toNumber from 'lodash/toNumber'import * as _size from 'lodash/size'import * as _round from 'lodash/round'@@ -36,7 +37,59 @@ const clickhouse = new ClickHouse({},})+const CLICKHOUSE_INIT_QUERIES = [+ 'CREATE DATABASE IF NOT EXISTS analytics',+ `CREATE TABLE IF NOT EXISTS analytics.analytics+ (+ id UUID,+ pid FixedString(12),+ ev String,+ pg Nullable(String),+ dv Nullable(String),+ br Nullable(String),+ os Nullable(String),+ lc Nullable(String),+ ref Nullable(String),+ so Nullable(String),+ me Nullable(String),+ ca Nullable(String),+ lt Nullable(UInt16),+ cc Nullable(FixedString(2)),+ unique UInt8,+ created DateTime+ )+ ENGINE = MergeTree()+ PARTITION BY toYYYYMM(created)+ ORDER BY (id, created, pid);`,+ `CREATE TABLE IF NOT EXISTS analytics.customEV+ (+ id UUID,+ pid FixedString(12),+ ev String,+ created DateTime+ )+ ENGINE = MergeTree()+ PARTITION BY toYYYYMM(created)+ ORDER BY (id, created, pid);`,+]++const initialiseClickhouse = async () => {+ console.log('Initialising Clickhouse')++ for (const query of CLICKHOUSE_INIT_QUERIES) {+ await clickhouse.query(query).toPromise()+ }++ console.log('Initialising Clickhouse: DONE')+}++initialiseClickhouse()+const isSelfhosted = process.env.SELFHOSTED+const SELFHOSTED_EMAIL = process.env.EMAIL+const SELFHOSTED_PASSWORD = process.env.PASSWORD+const UUIDV5_NAMESPACE = '912c64c1-73fd-42b6-859f-785f839a9f68'+const SELFHOSTED_UUID = isSelfhosted ? uuidv5(SELFHOSTED_EMAIL, UUIDV5_NAMESPACE) : ''/*** Calculates in percent, the change between 2 numbers.@@ -94,5 +147,6 @@ export {clickhouse, JWT_LIFE_TIME, HISTORY_LIFE_TIME_DAYS, redis, isValidPID, getRedisProjectKey,redisProjectCacheTimeout, getPercentageChange, UNIQUE_SESSION_LIFE_TIME, REDIS_LOG_DATA_CACHE_KEY,GDPR_EXPORT_TIMEFRAME, getRedisUserCountKey, redisProjectCountCacheTimeout, REDIS_LOG_CUSTOM_CACHE_KEY,- STRIPE_SECRET, STRIPE_WH_SECRET, REDIS_SESSION_SALT_KEY, HEARTBEAT_SID_LIFE_TIME, isSelfhosted,+ STRIPE_SECRET, STRIPE_WH_SECRET, REDIS_SESSION_SALT_KEY, HEARTBEAT_SID_LIFE_TIME, isSelfhosted, UUIDV5_NAMESPACE,+ SELFHOSTED_EMAIL, SELFHOSTED_PASSWORD, SELFHOSTED_UUID, CLICKHOUSE_INIT_QUERIES,}