umami/lib/prisma.ts

217 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-12-27 06:51:16 +01:00
import prisma from '@umami/prisma-client';
2022-08-26 07:04:32 +02:00
import moment from 'moment-timezone';
2022-12-27 06:51:16 +01:00
import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
2022-08-28 06:38:35 +02:00
import { FILTER_IGNORED } from 'lib/constants';
const MYSQL_DATE_FORMATS = {
minute: '%Y-%m-%d %H:%i:00',
hour: '%Y-%m-%d %H:00:00',
day: '%Y-%m-%d',
month: '%Y-%m-01',
year: '%Y-01-01',
};
const POSTGRESQL_DATE_FORMATS = {
minute: 'YYYY-MM-DD HH24:MI:00',
hour: 'YYYY-MM-DD HH24:00:00',
day: 'YYYY-MM-DD',
month: 'YYYY-MM-01',
year: 'YYYY-01-01',
};
2022-08-27 07:43:31 +02:00
function toUuid(): string {
const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
return '::uuid';
}
if (db === MYSQL) {
return '';
}
}
2022-12-27 06:51:16 +01:00
function getDateQuery(field: string, unit: string, timezone?: string): string {
2022-08-27 07:43:31 +02:00
const db = getDatabaseType(process.env.DATABASE_URL);
2022-08-26 07:04:32 +02:00
if (db === POSTGRESQL) {
if (timezone) {
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
}
return `to_char(date_trunc('${unit}', ${field}), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
}
if (db === MYSQL) {
if (timezone) {
const tz = moment.tz(timezone).format('Z');
return `date_format(convert_tz(${field},'+00:00','${tz}'), '${MYSQL_DATE_FORMATS[unit]}')`;
}
return `date_format(${field}, '${MYSQL_DATE_FORMATS[unit]}')`;
}
}
2022-12-27 06:51:16 +01:00
function getTimestampInterval(field: string): string {
2022-08-27 07:43:31 +02:00
const db = getDatabaseType(process.env.DATABASE_URL);
2022-08-27 06:29:26 +02:00
if (db === POSTGRESQL) {
return `floor(extract(epoch from max(${field}) - min(${field})))`;
}
if (db === MYSQL) {
return `floor(unix_timestamp(max(${field})) - unix_timestamp(min(${field})))`;
}
}
function getJsonField(column: string, property: string, isNumber: boolean): string {
const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
let accessor = `${column} ->> '${property}'`;
if (isNumber) {
accessor = `CAST(${accessor} AS DECIMAL)`;
}
return accessor;
}
if (db === MYSQL) {
return `${column} ->> "$.${property}"`;
}
}
2022-11-15 22:21:14 +01:00
function getEventDataColumnsQuery(column, columns): string {
const query = Object.keys(columns).reduce((arr, key) => {
const filter = columns[key];
if (filter === undefined) {
return arr;
}
const isNumber = ['sum', 'avg', 'min', 'max'].some(a => a === filter);
arr.push(`${filter}(${getJsonField(column, key, isNumber)}) as "${filter}(${key})"`);
return arr;
}, []);
return query.join(',\n');
}
2022-11-15 22:21:14 +01:00
function getEventDataFilterQuery(column, filters): string {
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
if (filter === undefined) {
return arr;
}
const isNumber = filter && typeof filter === 'number';
arr.push(
`${getJsonField(column, key, isNumber)} = ${
typeof filter === 'string' ? `'${filter}'` : filter
}`,
);
return arr;
}, []);
return query.join('\nand ');
}
2022-11-15 22:21:14 +01:00
function getFilterQuery(filters = {}, params = []): string {
2022-08-26 07:04:32 +02:00
const query = Object.keys(filters).reduce((arr, key) => {
const filter = filters[key];
if (filter === undefined || filter === FILTER_IGNORED) {
return arr;
}
switch (key) {
case 'url':
case 'os':
case 'browser':
case 'device':
case 'country':
2022-11-15 22:21:14 +01:00
arr.push(`and ${key}=$${params.length + 1}`);
params.push(decodeURIComponent(filter));
2022-08-26 07:04:32 +02:00
break;
2022-12-27 08:21:54 +01:00
case 'eventName':
arr.push(`and event_name=$${params.length + 1}`);
params.push(decodeURIComponent(filter));
break;
2022-08-26 07:04:32 +02:00
case 'referrer':
2022-11-15 22:21:14 +01:00
arr.push(`and referrer like $${params.length + 1}`);
params.push(`%${decodeURIComponent(filter)}%`);
2022-08-26 07:04:32 +02:00
break;
2022-12-27 08:21:54 +01:00
2022-08-26 07:04:32 +02:00
case 'domain':
2022-11-15 22:21:14 +01:00
arr.push(`and referrer not like $${params.length + 1}`);
arr.push(`and referrer not like '/%'`);
params.push(`%://${filter}/%`);
2022-08-26 07:04:32 +02:00
break;
2022-12-27 08:21:54 +01:00
2022-08-26 07:04:32 +02:00
case 'query':
2022-11-15 22:21:14 +01:00
arr.push(`and url like '%?%'`);
2022-08-26 07:04:32 +02:00
}
return arr;
}, []);
return query.join('\n');
}
2022-11-15 22:21:14 +01:00
function parseFilters(
filters: { [key: string]: any } = {},
params = [],
sessionKey = 'session_id',
) {
2022-12-27 08:21:54 +01:00
const { domain, url, eventUrl, referrer, os, browser, device, country, eventName, query } =
2022-08-26 07:04:32 +02:00
filters;
const pageviewFilters = { domain, url, referrer, query };
const sessionFilters = { os, browser, device, country };
2022-12-27 08:21:54 +01:00
const eventFilters = { url: eventUrl, eventName };
2022-08-26 07:04:32 +02:00
return {
pageviewFilters,
sessionFilters,
eventFilters,
2022-12-27 02:36:48 +01:00
event: { eventName },
2022-08-26 07:04:32 +02:00
joinSession:
os || browser || device || country
? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
2022-08-26 07:04:32 +02:00
: '',
2022-11-15 22:21:14 +01:00
filterQuery: getFilterQuery(filters, params),
2022-08-26 07:04:32 +02:00
};
}
2022-12-27 06:51:16 +01:00
async function rawQuery(query: string, params: never[] = []): Promise<any> {
2022-08-27 07:43:31 +02:00
const db = getDatabaseType(process.env.DATABASE_URL);
2022-08-26 07:04:32 +02:00
if (db !== POSTGRESQL && db !== MYSQL) {
return Promise.reject(new Error('Unknown database.'));
}
const sql = db === MYSQL ? query.replace(/\$[0-9]+/g, '?') : query;
2022-12-27 06:51:16 +01:00
return prisma.rawQuery(sql, params);
2022-08-26 07:04:32 +02:00
}
2022-08-27 07:43:31 +02:00
export default {
2022-12-27 06:51:16 +01:00
...prisma,
2022-08-27 07:43:31 +02:00
getDateQuery,
getTimestampInterval,
getFilterQuery,
getEventDataColumnsQuery,
getEventDataFilterQuery,
toUuid,
2022-08-27 07:43:31 +02:00
parseFilters,
rawQuery,
};