umami/lib/prisma.ts

221 lines
5.6 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';
import { getEventDataType } from './eventData';
2023-04-02 00:44:30 +02:00
import { FILTER_COLUMNS } from './constants';
2022-08-28 06:38:35 +02:00
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 '';
}
}
2023-05-09 08:46:58 +02:00
function getAddMinutesQuery(field: string, minutes: number) {
const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
return `${field} + interval '${minutes} minute'`;
}
if (db === MYSQL) {
return `DATE_ADD(${field}, interval ${minutes} minute)`;
}
}
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 getEventDataFilterQuery(
filters: {
eventKey?: string;
eventValue?: string | number | boolean | Date;
}[],
params: any[],
) {
const query = filters.reduce((ac, cv) => {
const type = getEventDataType(cv.eventValue);
let value = cv.eventValue;
ac.push(`and (event_key = $${params.length + 1}`);
params.push(cv.eventKey);
switch (type) {
case 'number':
ac.push(`and event_numeric_value = $${params.length + 1})`);
params.push(value);
break;
case 'string':
ac.push(`and event_string_value = $${params.length + 1})`);
params.push(decodeURIComponent(cv.eventValue as string));
break;
case 'boolean':
ac.push(`and event_string_value = $${params.length + 1})`);
params.push(decodeURIComponent(cv.eventValue as string));
value = cv ? 'true' : 'false';
break;
case 'date':
ac.push(`and event_date_value = $${params.length + 1})`);
params.push(cv.eventValue);
break;
}
return ac;
}, []);
return query.join('\n');
}
2023-04-02 00:44:30 +02: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];
2023-04-02 00:44:30 +02:00
if (filter !== undefined) {
const column = FILTER_COLUMNS[key] || key;
arr.push(`and ${column}=$${params.length + 1}`);
params.push(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[],
windowMinutes: number,
initParamLength = 3,
): {
levelQuery: string;
sumQuery: string;
urlFilterQuery: string;
} {
return urls.reduce(
(pv, cv, i) => {
const levelNumber = i + 1;
const start = i > 0 ? ',' : '';
2023-05-12 01:42:58 +02:00
if (levelNumber >= 2) {
pv.levelQuery += `\n
2023-05-09 08:46:58 +02:00
, level${levelNumber} AS (
select cl.*,
l0.created_at level_${levelNumber}_created_at,
l0.url_path as level_${levelNumber}_url
from level${i} cl
left join level0 l0
on cl.session_id = l0.session_id
2023-05-12 01:42:58 +02:00
and l0.created_at between cl.level_${i}_created_at
and ${getAddMinutesQuery(`cl.level_${i}_created_at`, windowMinutes)}
2023-05-09 08:46:58 +02:00
and l0.referrer_path = $${i + initParamLength}
2023-05-12 01:42:58 +02:00
and l0.url_path = $${levelNumber + initParamLength}
2023-05-09 08:46:58 +02:00
)`;
2023-05-12 01:42:58 +02:00
}
2023-05-09 08:46:58 +02:00
2023-05-12 01:42:58 +02:00
pv.sumQuery += `\n${start}SUM(CASE WHEN level_${levelNumber}_url is not null THEN 1 ELSE 0 END) AS level${levelNumber}`;
2023-05-09 08:46:58 +02:00
pv.urlFilterQuery += `\n${start}$${levelNumber + initParamLength} `;
return pv;
},
{
levelQuery: '',
sumQuery: '',
urlFilterQuery: '',
},
);
}
2022-11-15 22:21:14 +01:00
function parseFilters(
filters: { [key: string]: any } = {},
params = [],
sessionKey = 'session_id',
) {
2023-04-17 09:10:51 +02:00
const { os, browser, device, country, region, city } = filters;
2022-08-26 07:04:32 +02:00
return {
joinSession:
2023-04-17 09:10:51 +02:00
os || browser || device || country || region || city
? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
2022-08-26 07:04:32 +02:00
: '',
2023-04-02 00:44:30 +02: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,
2023-05-09 08:46:58 +02:00
getAddMinutesQuery,
2022-08-27 07:43:31 +02:00
getDateQuery,
getTimestampInterval,
getFilterQuery,
2023-05-09 08:46:58 +02:00
getFunnelQuery,
getEventDataFilterQuery,
toUuid,
2022-08-27 07:43:31 +02:00
parseFilters,
rawQuery,
};