2020-07-17 10:03:38 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2022-07-16 01:47:38 +02:00
|
|
|
import { ClickHouse } from 'clickhouse';
|
2022-08-05 02:43:47 +02:00
|
|
|
import { Kafka } from 'kafkajs';
|
2020-07-29 04:04:45 +02:00
|
|
|
import chalk from 'chalk';
|
2022-07-16 01:47:38 +02:00
|
|
|
import {
|
|
|
|
MYSQL,
|
|
|
|
MYSQL_DATE_FORMATS,
|
|
|
|
POSTGRESQL,
|
|
|
|
POSTGRESQL_DATE_FORMATS,
|
|
|
|
CLICKHOUSE,
|
2022-07-21 06:31:26 +02:00
|
|
|
RELATIONAL,
|
2022-07-23 08:56:29 +02:00
|
|
|
FILTER_IGNORED,
|
2022-08-05 02:43:47 +02:00
|
|
|
KAFKA,
|
2022-07-16 01:47:38 +02:00
|
|
|
} from 'lib/constants';
|
|
|
|
import moment from 'moment-timezone';
|
2022-07-21 06:31:26 +02:00
|
|
|
import { CLICKHOUSE_DATE_FORMATS } from './constants';
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2022-07-07 14:55:43 +02:00
|
|
|
BigInt.prototype.toJSON = function () {
|
2022-07-16 07:21:37 +02:00
|
|
|
return Number(this);
|
2022-07-07 14:55:43 +02:00
|
|
|
};
|
|
|
|
|
2020-07-28 10:17:45 +02:00
|
|
|
const options = {
|
2020-07-25 02:00:56 +02:00
|
|
|
log: [
|
|
|
|
{
|
|
|
|
emit: 'event',
|
|
|
|
level: 'query',
|
|
|
|
},
|
|
|
|
],
|
2020-07-28 10:17:45 +02:00
|
|
|
};
|
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
function logQuery(e) {
|
2022-06-19 09:07:01 +02:00
|
|
|
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
2020-07-29 04:04:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 07:42:01 +02:00
|
|
|
function getPrismaClient(options) {
|
2022-07-16 08:53:31 +02:00
|
|
|
const prisma = new PrismaClient(options);
|
2022-07-16 01:47:38 +02:00
|
|
|
|
|
|
|
if (process.env.LOG_QUERY) {
|
|
|
|
prisma.$on('query', logQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
return prisma;
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:42:01 +02:00
|
|
|
function getClickhouseClient() {
|
2022-07-21 06:31:26 +02:00
|
|
|
if (!process.env.ANALYTICS_URL) {
|
2022-07-16 01:47:38 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
const url = new URL(process.env.ANALYTICS_URL);
|
|
|
|
const database = url.pathname.replace('/', '');
|
|
|
|
|
2022-07-16 01:47:38 +02:00
|
|
|
return new ClickHouse({
|
2022-07-21 06:31:26 +02:00
|
|
|
url: url.hostname,
|
|
|
|
port: Number(url.port),
|
|
|
|
basicAuth: url.password
|
|
|
|
? {
|
|
|
|
username: url.username || 'default',
|
|
|
|
password: url.password,
|
|
|
|
}
|
|
|
|
: null,
|
2022-07-16 01:47:38 +02:00
|
|
|
format: 'json',
|
2022-07-21 06:31:26 +02:00
|
|
|
config: {
|
|
|
|
database,
|
|
|
|
},
|
2022-07-16 01:47:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-01 09:28:38 +02:00
|
|
|
function getKafkaClient() {
|
|
|
|
if (!process.env.KAFKA_URL) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(process.env.KAFKA_URL);
|
|
|
|
const brokers = process.env.KAFKA_BROKER.split(',');
|
|
|
|
|
|
|
|
if (url.username.length === 0 && url.password.length === 0) {
|
|
|
|
return new Kafka({
|
|
|
|
clientId: 'umami',
|
|
|
|
brokers: brokers,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new Kafka({
|
|
|
|
clientId: 'umami',
|
|
|
|
brokers: brokers,
|
|
|
|
ssl: true,
|
|
|
|
sasl: {
|
|
|
|
mechanism: 'plain',
|
|
|
|
username: url.username,
|
|
|
|
password: url.password,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:42:01 +02:00
|
|
|
const prisma = global.prisma || getPrismaClient(options);
|
|
|
|
const clickhouse = global.clickhouse || getClickhouseClient();
|
2022-08-01 09:28:38 +02:00
|
|
|
const kafka = global.kafka || getKafkaClient();
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-07-16 08:53:31 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
global.prisma = prisma;
|
2022-07-23 02:03:42 +02:00
|
|
|
global.clickhouse = clickhouse;
|
2022-08-01 09:28:38 +02:00
|
|
|
global.kafka = kafka;
|
2022-07-23 02:03:42 +02:00
|
|
|
}
|
2022-07-21 06:31:26 +02:00
|
|
|
|
2022-08-01 09:28:38 +02:00
|
|
|
export { prisma, clickhouse, kafka };
|
2022-07-16 01:47:38 +02:00
|
|
|
|
|
|
|
export function getDatabase() {
|
|
|
|
const type =
|
|
|
|
process.env.DATABASE_TYPE ||
|
|
|
|
(process.env.DATABASE_URL && process.env.DATABASE_URL.split(':')[0]);
|
|
|
|
|
|
|
|
if (type === 'postgres') {
|
|
|
|
return POSTGRESQL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAnalyticsDatabase() {
|
2022-08-05 02:43:47 +02:00
|
|
|
const type = process.env.ANALYTICS_URL && process.env.ANALYTICS_URL.split(':')[0];
|
2022-07-16 01:47:38 +02:00
|
|
|
|
|
|
|
if (type === 'postgres') {
|
|
|
|
return POSTGRESQL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!type) {
|
|
|
|
return getDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-08-05 02:43:47 +02:00
|
|
|
export function getKafkaService() {
|
|
|
|
const type = process.env.KAFKA_URL && process.env.KAFKA_URL.split(':')[0];
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export function getDateStringQueryClickhouse(data, unit) {
|
|
|
|
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:47:38 +02:00
|
|
|
export function getDateQuery(field, unit, timezone) {
|
|
|
|
const db = getDatabase();
|
|
|
|
|
|
|
|
if (db === POSTGRESQL) {
|
|
|
|
if (timezone) {
|
2022-07-23 02:03:42 +02:00
|
|
|
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
2022-07-23 02:03:42 +02:00
|
|
|
return `to_char(date_trunc('${unit}', ${field}), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (db === MYSQL) {
|
|
|
|
if (timezone) {
|
|
|
|
const tz = moment.tz(timezone).format('Z');
|
|
|
|
|
2022-07-23 02:03:42 +02:00
|
|
|
return `date_format(convert_tz(${field},'+00:00','${tz}'), '${MYSQL_DATE_FORMATS[unit]}')`;
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 02:03:42 +02:00
|
|
|
return `date_format(${field}, '${MYSQL_DATE_FORMATS[unit]}')`;
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export function getDateQueryClickhouse(field, unit, timezone) {
|
|
|
|
if (timezone) {
|
|
|
|
return `date_trunc('${unit}', ${field},'${timezone}')`;
|
|
|
|
}
|
|
|
|
return `date_trunc('${unit}', ${field})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDateFormatClickhouse(date) {
|
|
|
|
return `parseDateTimeBestEffort('${date.toUTCString()}')`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getBetweenDatesClickhouse(field, start_at, end_at) {
|
|
|
|
return `${field} between ${getDateFormatClickhouse(start_at)}
|
|
|
|
and ${getDateFormatClickhouse(end_at)}`;
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:42:01 +02:00
|
|
|
export function getTimestampInterval(field) {
|
2022-07-16 01:47:38 +02:00
|
|
|
const db = getDatabase();
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2022-07-16 01:47:38 +02:00
|
|
|
if (db === POSTGRESQL) {
|
2022-07-23 07:42:01 +02:00
|
|
|
return `floor(extract(epoch from max(${field}) - min(${field})))`;
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (db === MYSQL) {
|
2022-07-23 07:42:01 +02:00
|
|
|
return `floor(unix_timestamp(max(${field})) - unix_timestamp(min(${field})))`;
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 02:03:42 +02:00
|
|
|
export function getFilterQuery(table, column, filters = {}, params = []) {
|
2022-07-16 01:47:38 +02:00
|
|
|
const query = Object.keys(filters).reduce((arr, key) => {
|
2022-07-23 08:56:29 +02:00
|
|
|
const filter = filters[key];
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-07-23 08:56:29 +02:00
|
|
|
if (filter === undefined || filter === FILTER_IGNORED) {
|
2022-07-16 01:47:38 +02:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'url':
|
|
|
|
if (table === 'pageview' || table === 'event') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
2022-07-23 08:56:29 +02:00
|
|
|
params.push(decodeURIComponent(filter));
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'os':
|
|
|
|
case 'browser':
|
|
|
|
case 'device':
|
|
|
|
case 'country':
|
|
|
|
if (table === 'session') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
2022-07-23 08:56:29 +02:00
|
|
|
params.push(decodeURIComponent(filter));
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-07-30 07:30:09 +02:00
|
|
|
case 'event_name':
|
2022-07-16 01:47:38 +02:00
|
|
|
if (table === 'event') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
2022-07-23 08:56:29 +02:00
|
|
|
params.push(decodeURIComponent(filter));
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'referrer':
|
2022-07-23 08:56:29 +02:00
|
|
|
if (table === 'pageview' || table === 'event') {
|
2022-07-16 01:47:38 +02:00
|
|
|
arr.push(`and ${table}.referrer like $${params.length + 1}`);
|
2022-07-23 08:56:29 +02:00
|
|
|
params.push(`%${decodeURIComponent(filter)}%`);
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'domain':
|
|
|
|
if (table === 'pageview') {
|
|
|
|
arr.push(`and ${table}.referrer not like $${params.length + 1}`);
|
|
|
|
arr.push(`and ${table}.referrer not like '/%'`);
|
2022-07-23 08:56:29 +02:00
|
|
|
params.push(`%://${filter}/%`);
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-08-08 10:26:20 +02:00
|
|
|
|
|
|
|
case 'query':
|
|
|
|
if (table === 'pageview') {
|
|
|
|
arr.push(`and ${table}.url like '%?%'`);
|
|
|
|
}
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return query.join('\n');
|
2022-06-19 09:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 02:03:42 +02:00
|
|
|
export function parseFilters(table, column, filters = {}, params = [], sessionKey = 'session_id') {
|
2022-08-08 10:26:20 +02:00
|
|
|
const { domain, url, event_url, referrer, os, browser, device, country, event_name, query } =
|
|
|
|
filters;
|
|
|
|
|
|
|
|
console.log({ table, column, filters, params });
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-08-08 10:26:20 +02:00
|
|
|
const pageviewFilters = { domain, url, referrer, query };
|
2022-07-16 01:47:38 +02:00
|
|
|
const sessionFilters = { os, browser, device, country };
|
2022-07-30 07:30:09 +02:00
|
|
|
const eventFilters = { url: event_url, event_name };
|
2022-07-16 01:47:38 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
pageviewFilters,
|
|
|
|
sessionFilters,
|
|
|
|
eventFilters,
|
2022-07-30 07:30:09 +02:00
|
|
|
event: { event_name },
|
2022-07-16 01:47:38 +02:00
|
|
|
joinSession:
|
|
|
|
os || browser || device || country
|
2022-07-22 23:43:19 +02:00
|
|
|
? `inner join session on ${table}.${sessionKey} = session.${sessionKey}`
|
2022-07-16 01:47:38 +02:00
|
|
|
: '',
|
2022-07-23 02:03:42 +02:00
|
|
|
pageviewQuery: getFilterQuery('pageview', column, pageviewFilters, params),
|
|
|
|
sessionQuery: getFilterQuery('session', column, sessionFilters, params),
|
|
|
|
eventQuery: getFilterQuery('event', column, eventFilters, params),
|
2022-07-16 01:47:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export function replaceQueryClickhouse(string, params = []) {
|
|
|
|
let formattedString = string;
|
|
|
|
|
|
|
|
params.forEach((a, i) => {
|
|
|
|
let replace = a;
|
|
|
|
|
|
|
|
if (typeof a === 'string' || a instanceof String) {
|
|
|
|
replace = `'${replace}'`;
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedString = formattedString.replace(`$${i + 1}`, replace);
|
|
|
|
});
|
|
|
|
|
|
|
|
return formattedString;
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:47:38 +02:00
|
|
|
export async function runQuery(query) {
|
|
|
|
return query.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function rawQuery(query, params = []) {
|
|
|
|
const db = getDatabase();
|
|
|
|
|
|
|
|
if (db !== POSTGRESQL && db !== MYSQL) {
|
|
|
|
return Promise.reject(new Error('Unknown database.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const sql = db === MYSQL ? query.replace(/\$[0-9]+/g, '?') : query;
|
|
|
|
|
|
|
|
return runQuery(prisma.$queryRawUnsafe.apply(prisma, [sql, ...params]));
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export async function rawQueryClickhouse(query, params = [], debug = false) {
|
|
|
|
let formattedQuery = replaceQueryClickhouse(query, params);
|
|
|
|
|
|
|
|
if (debug || process.env.LOG_QUERY) {
|
|
|
|
console.log(formattedQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clickhouse.query(formattedQuery).toPromise();
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:47:11 +02:00
|
|
|
export async function findUnique(data) {
|
|
|
|
if (data.length > 1) {
|
|
|
|
throw `${data.length} records found when expecting 1.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data[0] ?? null;
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export async function runAnalyticsQuery(queries) {
|
2022-07-16 01:47:38 +02:00
|
|
|
const db = getAnalyticsDatabase();
|
|
|
|
|
|
|
|
if (db === POSTGRESQL || db === MYSQL) {
|
2022-07-25 18:47:11 +02:00
|
|
|
return queries[RELATIONAL]();
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (db === CLICKHOUSE) {
|
2022-08-05 02:43:47 +02:00
|
|
|
const kafka = getKafkaService();
|
|
|
|
if (kafka === KAFKA && queries[KAFKA]) {
|
|
|
|
return queries[KAFKA]();
|
|
|
|
}
|
2022-07-25 18:47:11 +02:00
|
|
|
return queries[CLICKHOUSE]();
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-01 09:28:38 +02:00
|
|
|
|
|
|
|
export async function kafkaProducer(params, topic) {
|
|
|
|
const producer = kafka.producer();
|
|
|
|
await producer.connect();
|
|
|
|
|
|
|
|
await producer.send({
|
|
|
|
topic,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: 'key',
|
|
|
|
value: JSON.stringify(params),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|