2022-08-26 07:04:32 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import moment from 'moment-timezone';
|
2022-08-27 07:43:31 +02:00
|
|
|
import debug from 'debug';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { PRISMA, 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
|
|
|
|
2022-08-29 05:20:54 +02:00
|
|
|
const log = debug('umami:prisma');
|
2022-08-26 07:04:32 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
const PRISMA_OPTIONS = {
|
2022-08-26 07:04:32 +02:00
|
|
|
log: [
|
|
|
|
{
|
|
|
|
emit: 'event',
|
|
|
|
level: 'query',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
function logQuery(e) {
|
2022-08-27 07:43:31 +02:00
|
|
|
log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
2022-08-26 07:04:32 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
function toUuid() {
|
|
|
|
const db = getDatabaseType(process.env.DATABASE_URL);
|
|
|
|
|
|
|
|
if (db === POSTGRESQL) {
|
|
|
|
return '::uuid';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db === MYSQL) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 05:21:53 +02:00
|
|
|
function getClient(options) {
|
2022-08-26 07:04:32 +02:00
|
|
|
const prisma = new PrismaClient(options);
|
|
|
|
|
|
|
|
if (process.env.LOG_QUERY) {
|
|
|
|
prisma.$on('query', logQuery);
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
global[PRISMA] = prisma;
|
|
|
|
}
|
2022-08-27 06:29:26 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
log('Prisma initialized');
|
2022-08-26 07:04:32 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
return prisma;
|
2022-08-26 07:04:32 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
function getDateQuery(field, unit, timezone) {
|
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-08-28 06:38:35 +02:00
|
|
|
function getTimestampInterval(field) {
|
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})))`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
function getSanitizedColumns(columns) {
|
|
|
|
return Object.keys(columns).reduce((acc, keyName) => {
|
2023-01-19 01:36:28 +01:00
|
|
|
const sanitizedProperty = keyName.replace(/[^\w\s_]/g, '');
|
2023-01-19 00:09:49 +01:00
|
|
|
|
|
|
|
acc[sanitizedProperty] = columns[keyName];
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJsonField(column, property, isNumber, params) {
|
2022-10-22 06:33:23 +02:00
|
|
|
const db = getDatabaseType(process.env.DATABASE_URL);
|
|
|
|
|
|
|
|
if (db === POSTGRESQL) {
|
2023-01-19 00:09:49 +01:00
|
|
|
params.push(property);
|
|
|
|
|
|
|
|
let accessor = `${column} ->> $${params.length}`;
|
2022-10-22 06:33:23 +02:00
|
|
|
|
|
|
|
if (isNumber) {
|
|
|
|
accessor = `CAST(${accessor} AS DECIMAL)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db === MYSQL) {
|
2023-01-19 00:09:49 +01:00
|
|
|
return `${column} ->> '$.${property}'`;
|
2022-10-22 06:33:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
function getEventDataColumnsQuery(column, columns, params) {
|
|
|
|
const query = Object.keys(columns).reduce((arr, key, i) => {
|
2022-10-22 06:33:23 +02:00
|
|
|
const filter = columns[key];
|
|
|
|
|
|
|
|
if (filter === undefined) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
switch (filter) {
|
|
|
|
case 'sum':
|
|
|
|
case 'avg':
|
|
|
|
case 'min':
|
|
|
|
case 'max':
|
|
|
|
arr.push(`${filter}(${getJsonField(column, key, true, params)}) as "${i}"`);
|
|
|
|
break;
|
|
|
|
case 'count':
|
|
|
|
arr.push(`${filter}(${getJsonField(column, key, false, params)}) as "${i}"`);
|
|
|
|
break;
|
|
|
|
}
|
2022-10-22 06:33:23 +02:00
|
|
|
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return query.join(',\n');
|
|
|
|
}
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
function getEventDataFilterQuery(column, filters, params) {
|
2022-10-22 06:33:23 +02:00
|
|
|
const query = Object.keys(filters).reduce((arr, key) => {
|
|
|
|
const filter = filters[key];
|
|
|
|
|
|
|
|
if (filter === undefined) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isNumber = filter && typeof filter === 'number';
|
|
|
|
|
2023-01-19 00:09:49 +01:00
|
|
|
arr.push(`${getJsonField(column, key, isNumber, params)} = $${params.length + 1}`);
|
|
|
|
|
|
|
|
params.push(filter);
|
2022-10-22 06:33:23 +02:00
|
|
|
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return query.join('\nand ');
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
function getFilterQuery(table, column, filters = {}, params = []) {
|
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':
|
|
|
|
if (table === 'pageview' || table === 'event') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
|
|
|
params.push(decodeURIComponent(filter));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'os':
|
|
|
|
case 'browser':
|
|
|
|
case 'device':
|
|
|
|
case 'country':
|
|
|
|
if (table === 'session') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
|
|
|
params.push(decodeURIComponent(filter));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'event_name':
|
|
|
|
if (table === 'event') {
|
|
|
|
arr.push(`and ${table}.${key}=$${params.length + 1}`);
|
|
|
|
params.push(decodeURIComponent(filter));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'referrer':
|
|
|
|
if (table === 'pageview' || table === 'event') {
|
|
|
|
arr.push(`and ${table}.referrer like $${params.length + 1}`);
|
|
|
|
params.push(`%${decodeURIComponent(filter)}%`);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'domain':
|
|
|
|
if (table === 'pageview') {
|
|
|
|
arr.push(`and ${table}.referrer not like $${params.length + 1}`);
|
|
|
|
arr.push(`and ${table}.referrer not like '/%'`);
|
|
|
|
params.push(`%://${filter}/%`);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'query':
|
|
|
|
if (table === 'pageview') {
|
|
|
|
arr.push(`and ${table}.url like '%?%'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return query.join('\n');
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
function parseFilters(table, column, filters = {}, params = [], sessionKey = 'session_id') {
|
2022-08-26 07:04:32 +02:00
|
|
|
const { domain, url, event_url, referrer, os, browser, device, country, event_name, query } =
|
|
|
|
filters;
|
|
|
|
|
|
|
|
const pageviewFilters = { domain, url, referrer, query };
|
|
|
|
const sessionFilters = { os, browser, device, country };
|
|
|
|
const eventFilters = { url: event_url, event_name };
|
|
|
|
|
|
|
|
return {
|
|
|
|
pageviewFilters,
|
|
|
|
sessionFilters,
|
|
|
|
eventFilters,
|
|
|
|
event: { event_name },
|
|
|
|
joinSession:
|
|
|
|
os || browser || device || country
|
|
|
|
? `inner join session on ${table}.${sessionKey} = session.${sessionKey}`
|
|
|
|
: '',
|
|
|
|
pageviewQuery: getFilterQuery('pageview', column, pageviewFilters, params),
|
|
|
|
sessionQuery: getFilterQuery('session', column, sessionFilters, params),
|
|
|
|
eventQuery: getFilterQuery('event', column, eventFilters, params),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
async function rawQuery(query, params = []) {
|
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-08-28 06:38:35 +02:00
|
|
|
return prisma.$queryRawUnsafe.apply(prisma, [sql, ...params]);
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:02:38 +02:00
|
|
|
async function transaction(queries) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return prisma.$transaction(queries);
|
2022-08-26 07:04:32 +02:00
|
|
|
}
|
2022-08-27 07:43:31 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
// Initialization
|
|
|
|
const prisma = global[PRISMA] || getClient(PRISMA_OPTIONS);
|
2022-08-27 07:43:31 +02:00
|
|
|
|
|
|
|
export default {
|
2022-08-28 06:38:35 +02:00
|
|
|
client: prisma,
|
|
|
|
log,
|
2023-01-19 00:09:49 +01:00
|
|
|
toUuid,
|
2022-08-27 07:43:31 +02:00
|
|
|
getDateQuery,
|
|
|
|
getTimestampInterval,
|
|
|
|
getFilterQuery,
|
2022-10-22 06:33:23 +02:00
|
|
|
getEventDataColumnsQuery,
|
|
|
|
getEventDataFilterQuery,
|
2023-01-19 00:09:49 +01:00
|
|
|
getSanitizedColumns,
|
2022-08-27 07:43:31 +02:00
|
|
|
parseFilters,
|
|
|
|
rawQuery,
|
2022-08-29 09:02:38 +02:00
|
|
|
transaction,
|
2022-08-27 07:43:31 +02:00
|
|
|
};
|