umami/src/lib/clickhouse.ts

174 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-09-29 20:00:06 +02:00
import { ClickHouseClient, createClient } from '@clickhouse/client';
2022-08-26 07:04:32 +02:00
import dateFormat from 'dateformat';
2022-08-28 06:38:35 +02:00
import debug from 'debug';
import { CLICKHOUSE } from 'lib/db';
import { QueryFilters, QueryOptions } from './types';
2023-08-11 18:05:56 +02:00
import { FILTER_COLUMNS, OPERATORS } from './constants';
2023-08-04 22:18:30 +02:00
import { loadWebsite } from './load';
import { maxDate } from './date';
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
2023-09-29 20:00:06 +02:00
let clickhouse: ClickHouseClient;
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,
protocol,
2022-08-28 06:38:35 +02:00
username = 'default',
password,
} = new URL(process.env.CLICKHOUSE_URL);
2023-09-29 20:00:06 +02:00
const client = createClient({
host: `${protocol}//${hostname}:${port}`,
2023-09-29 20:00:06 +02:00
database: pathname.replace('/', ''),
username: username,
password,
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
2024-01-14 11:21:39 +01:00
function getDateStringQuery(data: any, unit: string | number) {
2022-08-26 07:04:32 +02:00
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
}
2024-01-14 11:21:39 +01:00
function getDateQuery(field: string, unit: string, timezone?: string) {
2022-08-26 07:04:32 +02:00
if (timezone) {
return `date_trunc('${unit}', ${field}, '${timezone}')`;
}
return `date_trunc('${unit}', ${field})`;
}
2024-01-14 11:21:39 +01:00
function getDateFormat(date: Date) {
2022-08-26 07:04:32 +02:00
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
}
2024-03-27 01:31:16 +01:00
function mapFilter(column: string, operator: string, name: string, type: string = 'String') {
switch (operator) {
2023-08-11 18:05:56 +02:00
case OPERATORS.equals:
return `${column} = {${name}:${type}}`;
2023-08-11 18:05:56 +02:00
case OPERATORS.notEquals:
2023-08-16 19:50:28 +02:00
return `${column} != {${name}:${type}}`;
2024-03-05 09:45:55 +01:00
case OPERATORS.contains:
return `positionCaseInsensitive(${column}, {${name}:${type}}) > 0`;
case OPERATORS.doesNotContain:
return `positionCaseInsensitive(${column}, {${name}:${type}}) = 0`;
2023-08-11 18:05:56 +02:00
default:
return '';
}
}
function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {}) {
2023-08-11 18:05:56 +02:00
const query = Object.keys(filters).reduce((arr, name) => {
const value = filters[name];
2024-03-05 09:45:55 +01:00
const filter = value?.filter ?? OPERATORS.equals;
const column = value?.column ?? FILTER_COLUMNS[name] ?? options?.columns?.[name];
2022-08-26 07:04:32 +02:00
2024-03-05 09:45:55 +01:00
if (value !== undefined && column !== undefined) {
arr.push(`and ${mapFilter(column, filter, name)}`);
2022-08-26 07:04:32 +02:00
2023-08-11 18:05:56 +02:00
if (name === 'referrer') {
arr.push('and referrer_domain != {websiteDomain:String}');
}
2023-08-04 09:51:52 +02:00
}
2022-08-26 07:04:32 +02:00
return arr;
}, []);
return query.join('\n');
}
2023-08-16 22:56:41 +02:00
function normalizeFilters(filters = {}) {
return Object.keys(filters).reduce((obj, key) => {
const value = filters[key];
obj[key] = value?.value ?? value;
return obj;
}, {});
}
2023-08-11 18:05:56 +02:00
async function parseFilters(websiteId: string, filters: QueryFilters = {}, options?: QueryOptions) {
2023-08-04 22:18:30 +02:00
const website = await loadWebsite(websiteId);
2022-08-26 07:04:32 +02:00
return {
filterQuery: getFilterQuery(filters, options),
2023-08-04 22:18:30 +02:00
params: {
2023-08-16 22:56:41 +02:00
...normalizeFilters(filters),
2023-08-04 22:18:30 +02:00
websiteId,
startDate: maxDate(filters.startDate, new Date(website?.resetAt)),
2023-08-04 22:18:30 +02:00
websiteDomain: website.domain,
},
2022-08-26 07:04:32 +02:00
};
}
2023-09-29 20:00:06 +02:00
async function rawQuery(query: string, params: Record<string, unknown> = {}): Promise<unknown> {
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-09-29 20:00:06 +02:00
const resultSet = await clickhouse.query({
query: query,
query_params: params,
format: 'JSONEachRow',
});
2024-01-14 11:21:39 +01:00
return resultSet.json();
2022-08-26 07:04:32 +02:00
}
2024-01-14 11:21:39 +01:00
async function findUnique(data: any[]) {
2022-08-26 07:04:32 +02:00
if (data.length > 1) {
throw `${data.length} records found when expecting 1.`;
}
return findFirst(data);
2022-08-26 07:04:32 +02:00
}
2022-08-26 07:20:30 +02:00
2024-01-14 11:21:39 +01:00
async function findFirst(data: any[]) {
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,
getFilterQuery,
parseFilters,
findUnique,
findFirst,
2022-08-29 05:20:54 +02:00
rawQuery,
2022-08-26 07:43:22 +02:00
};