umami/lib/clickhouse.ts

201 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import { ClickHouse } from 'clickhouse';
import dateFormat from 'dateformat';
2022-08-28 06:38:35 +02:00
import debug from 'debug';
import { CLICKHOUSE } from 'lib/db';
import { getDynamicDataType } from './dynamicData';
2023-04-02 00:44:30 +02:00
import { WebsiteMetricFilter } from './types';
import { FILTER_COLUMNS } from './constants';
2022-08-28 06:38:35 +02:00
export const CLICKHOUSE_DATE_FORMATS = {
minute: '%Y-%m-%d %H:%M:00',
hour: '%Y-%m-%d %H:00:00',
day: '%Y-%m-%d',
month: '%Y-%m-01',
year: '%Y-01-01',
};
2022-08-29 05:20:54 +02:00
const log = debug('umami:clickhouse');
2022-08-26 07:04:32 +02:00
let clickhouse: ClickHouse;
2022-10-07 00:00:16 +02:00
const enabled = Boolean(process.env.CLICKHOUSE_URL);
2022-08-26 07:04:32 +02:00
function getClient() {
2022-08-28 06:38:35 +02:00
const {
hostname,
port,
pathname,
username = 'default',
password,
} = new URL(process.env.CLICKHOUSE_URL);
const client = new ClickHouse({
url: hostname,
port: Number(port),
2022-08-26 07:04:32 +02:00
format: 'json',
config: {
2022-08-28 06:38:35 +02:00
database: pathname.replace('/', ''),
2022-08-26 07:04:32 +02:00
},
2022-08-28 06:38:35 +02:00
basicAuth: password ? { username, password } : null,
2022-08-26 07:04:32 +02:00
});
2022-08-28 06:38:35 +02:00
if (process.env.NODE_ENV !== 'production') {
2022-08-29 19:47:01 +02:00
global[CLICKHOUSE] = client;
2022-08-28 06:38:35 +02:00
}
2022-08-26 07:04:32 +02:00
2022-08-28 06:38:35 +02:00
log('Clickhouse initialized');
2022-08-26 07:04:32 +02:00
2022-08-28 06:38:35 +02:00
return client;
}
2022-08-26 07:04:32 +02:00
2022-08-26 07:43:22 +02:00
function getDateStringQuery(data, unit) {
2022-08-26 07:04:32 +02:00
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
}
function getDateQuery(field, unit, timezone?) {
2022-08-26 07:04:32 +02:00
if (timezone) {
return `date_trunc('${unit}', ${field}, '${timezone}')`;
}
return `date_trunc('${unit}', ${field})`;
}
2022-08-26 07:43:22 +02:00
function getDateFormat(date) {
2022-08-26 07:04:32 +02:00
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
}
2022-12-27 02:36:48 +01:00
function getBetweenDates(field, startAt, endAt) {
return `${field} between ${getDateFormat(startAt)} and ${getDateFormat(endAt)}`;
}
function getEventDataFilterQuery(
filters: {
eventKey?: string;
eventValue?: string | number | boolean | Date;
}[] = [],
params: any,
) {
const query = filters.reduce((ac, cv, i) => {
const type = getDynamicDataType(cv.eventValue);
let value = cv.eventValue;
ac.push(`and (event_key = {eventKey${i}:String}`);
switch (type) {
case 'number':
ac.push(`and event_numeric_value = {eventValue${i}:UInt64})`);
break;
case 'string':
ac.push(`and event_string_value = {eventValue${i}:String})`);
break;
case 'boolean':
ac.push(`and event_string_value = {eventValue${i}:String})`);
value = cv ? 'true' : 'false';
break;
case 'date':
ac.push(`and event_date_value = {eventValue${i}:DateTime('UTC')})`);
break;
}
params[`eventKey${i}`] = cv.eventKey;
params[`eventValue${i}`] = value;
return ac;
}, []);
return query.join('\n');
}
2023-04-02 00:44:30 +02:00
function getFilterQuery(filters = {}, params = {}) {
2022-08-26 07:04:32 +02:00
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
2023-04-02 00:44:30 +02:00
if (filter !== undefined) {
const column = FILTER_COLUMNS[key] || key;
arr.push(`and ${column} = {${key}:String}`);
params[key] = decodeURIComponent(filter);
2022-08-26 07:04:32 +02:00
}
return arr;
}, []);
return query.join('\n');
}
2023-05-09 08:46:58 +02:00
function getFunnelQuery(urls: string[]): {
columnsQuery: string;
conditionQuery: string;
urlParams: { [key: string]: string };
} {
return urls.reduce(
(pv, cv, i) => {
pv.columnsQuery += `\n,url_path = {url${i}:String}${
2023-05-12 01:42:58 +02:00
i > 0 && urls[i - 1] ? ` AND referrer_path = {url${i - 1}:String}` : ''
}`;
2023-05-09 08:46:58 +02:00
pv.conditionQuery += `${i > 0 ? ',' : ''} {url${i}:String}`;
pv.urlParams[`url${i}`] = cv;
return pv;
},
{
columnsQuery: '',
conditionQuery: '',
urlParams: {},
},
);
}
2023-04-02 00:44:30 +02:00
function parseFilters(filters: WebsiteMetricFilter = {}, params: any = {}) {
2022-08-26 07:04:32 +02:00
return {
2023-04-02 00:44:30 +02:00
filterQuery: getFilterQuery(filters, params),
2022-08-26 07:04:32 +02:00
};
}
2023-05-12 01:42:58 +02:00
async function rawQuery<T>(query, params = {}): Promise<T> {
2022-08-29 05:20:54 +02:00
if (process.env.LOG_QUERY) {
2023-03-30 18:44:04 +02:00
log('QUERY:\n', query);
log('PARAMETERS:\n', params);
2022-08-26 07:04:32 +02:00
}
2022-10-07 00:00:16 +02:00
await connect();
2023-05-12 01:42:58 +02:00
return clickhouse.query(query, { params }).toPromise() as Promise<T>;
2022-08-26 07:04:32 +02:00
}
2022-08-26 07:43:22 +02:00
async function findUnique(data) {
2022-08-26 07:04:32 +02:00
if (data.length > 1) {
throw `${data.length} records found when expecting 1.`;
}
return data[0] ?? null;
}
2022-08-26 07:20:30 +02:00
2022-08-26 07:43:22 +02:00
async function findFirst(data) {
2022-08-26 07:20:30 +02:00
return data[0] ?? null;
}
2022-08-26 07:43:22 +02:00
2022-10-07 00:00:16 +02:00
async function connect() {
2022-12-27 09:00:31 +01:00
if (enabled && !clickhouse) {
2022-10-07 00:00:16 +02:00
clickhouse = process.env.CLICKHOUSE_URL && (global[CLICKHOUSE] || getClient());
}
return clickhouse;
}
2022-08-28 06:38:35 +02:00
2022-08-26 07:43:22 +02:00
export default {
2022-10-07 00:00:16 +02:00
enabled,
2022-08-28 06:38:35 +02:00
client: clickhouse,
log,
2022-10-07 00:00:16 +02:00
connect,
2022-08-26 07:43:22 +02:00
getDateStringQuery,
getDateQuery,
getDateFormat,
getBetweenDates,
getFilterQuery,
2023-05-09 08:46:58 +02:00
getFunnelQuery,
getEventDataFilterQuery,
2022-08-26 07:43:22 +02:00
parseFilters,
findUnique,
findFirst,
2022-08-29 05:20:54 +02:00
rawQuery,
2022-08-26 07:43:22 +02:00
};