Skip to content

📈 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!

added clickhouse database initialisation queries

Blue Mouse717bf751b120
diff --git a/migrations/clickhouse/initialise_database.js b/migrations/clickhouse/initialise_database.js
new file mode 100644
index 000000000..fae204a4c
--- /dev/null
+++ b/migrations/clickhouse/initialise_database.js
@@ -0,0 +1,80 @@
+// This script initialises the Swetrix cloud edition database and tables if they're absent
+const { queriesRunner, dbName, databaselessQueriesRunner } = require('./setup')
+
+const CLICKHOUSE_DB_INIT_QUERIES = [
+ `CREATE DATABASE IF NOT EXISTS ${dbName}`,
+]
+
+const CLICKHOUSE_INIT_QUERIES = [
+ // The traffic data table
+ `CREATE TABLE IF NOT EXISTS analytics.analytics
+ (
+ sid Nullable(String),
+ pid FixedString(12),
+ 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),
+ cc Nullable(FixedString(2)),
+ sdur Nullable(UInt32),
+ unique UInt8,
+ created DateTime
+ )
+ ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(created)
+ ORDER BY (pid, created);`,
+
+ // Custom events table
+ `CREATE TABLE IF NOT EXISTS analytics.customEV
+ (
+ 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),
+ cc Nullable(FixedString(2)),
+ created DateTime
+ )
+ ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(created)
+ ORDER BY (pid, created);`,
+
+ // The performance data table
+ `CREATE TABLE IF NOT EXISTS analytics.performance
+ (
+ pid FixedString(12),
+ pg Nullable(String),
+ dv Nullable(String),
+ br Nullable(String),
+ cc Nullable(FixedString(2)),
+ dns Nullable(UInt32),
+ tls Nullable(UInt32),
+ conn Nullable(UInt32),
+ response Nullable(UInt32),
+ render Nullable(UInt32),
+ domLoad Nullable(UInt32),
+ pageLoad Nullable(UInt32),
+ ttfb Nullable(UInt32),
+ created DateTime
+ )
+ ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(created)
+ ORDER BY (pid, created);`,
+]
+
+databaselessQueriesRunner(CLICKHOUSE_DB_INIT_QUERIES)
+ .then(() => queriesRunner(CLICKHOUSE_INIT_QUERIES))
+ .catch((error) => {
+ console.error(`[ERROR] Error occured whilst initialising the database: ${error}`)
+ })
diff --git a/migrations/clickhouse/setup.js b/migrations/clickhouse/setup.js
index 7730eb4ae..9c64dfa6b 100644
--- a/migrations/clickhouse/setup.js
+++ b/migrations/clickhouse/setup.js
@@ -22,6 +22,44 @@ const clickhouse = new ClickHouse({
},
})
+const clickhouseNoDatabase = new ClickHouse({
+ url: process.env.CLICKHOUSE_HOST,
+ port: _.toNumber(process.env.CLICKHOUSE_PORT),
+ debug: false,
+ basicAuth: {
+ username: process.env.CLICKHOUSE_USER,
+ password: process.env.CLICKHOUSE_PASSWORD,
+ },
+ isUseGzip: false,
+ format: 'json',
+ raw: false,
+ config: {
+ session_timeout: 60,
+ output_format_json_quote_64bit_integers: 0,
+ enable_http_compression: 0,
+ },
+})
+
+const databaselessQueriesRunner = async (queries) => {
+ let failed = false
+ for (const query of queries) {
+ if (failed) {
+ return
+ }
+
+ if (query) {
+ try {
+ await clickhouseNoDatabase.query(query).toPromise()
+ console.log(chalk.green('Query OK: '), query)
+ } catch (error) {
+ console.error(chalk.red('Query ERROR: '), query)
+ console.error(error)
+ failed = true
+ }
+ }
+ }
+}
+
const queriesRunner = async (queries) => {
let failed = false
for (const query of queries) {
@@ -47,5 +85,6 @@ const dbName = process.env.CLICKHOUSE_DATABASE
module.exports = {
clickhouse,
queriesRunner,
+ databaselessQueriesRunner,
dbName,
}
\ No newline at end of file