feat: separate mongoQuery & add mongo filter
parent
4c57ab1388
commit
b5b689b156
|
@ -26,9 +26,12 @@ export function getDatabaseType(url = process.env.DATABASE_URL) {
|
||||||
export async function runQuery(queries) {
|
export async function runQuery(queries) {
|
||||||
const db = getDatabaseType(process.env.CLICKHOUSE_URL || process.env.DATABASE_URL);
|
const db = getDatabaseType(process.env.CLICKHOUSE_URL || process.env.DATABASE_URL);
|
||||||
|
|
||||||
if (db === POSTGRESQL || db === MYSQL || db === MONGODB) {
|
if (db === POSTGRESQL || db === MYSQL) {
|
||||||
return queries[PRISMA]();
|
return queries[PRISMA]();
|
||||||
}
|
}
|
||||||
|
if (db === MONGODB) {
|
||||||
|
return queries[MONGODB]();
|
||||||
|
}
|
||||||
|
|
||||||
if (db === CLICKHOUSE) {
|
if (db === CLICKHOUSE) {
|
||||||
if (queries[KAFKA]) {
|
if (queries[KAFKA]) {
|
||||||
|
|
|
@ -142,6 +142,25 @@ function parseFilters(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseMongoFilter(filters: { [key: string]: any } = {}) {
|
||||||
|
const query = {};
|
||||||
|
|
||||||
|
for (let k in filters) {
|
||||||
|
const v = filters[k];
|
||||||
|
if (v !== undefined) {
|
||||||
|
const tempK = FILTER_COLUMNS[k];
|
||||||
|
if (tempK !== undefined) {
|
||||||
|
k = tempK;
|
||||||
|
}
|
||||||
|
if (k === 'browser' || k === 'os' || k === 'device' || k === 'language') {
|
||||||
|
k = 'session.' + k;
|
||||||
|
}
|
||||||
|
query[k] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { $match: query };
|
||||||
|
}
|
||||||
|
|
||||||
async function rawQuery(query: string, params: never[] = []): Promise<any> {
|
async function rawQuery(query: string, params: never[] = []): Promise<any> {
|
||||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||||
|
|
||||||
|
@ -163,5 +182,6 @@ export default {
|
||||||
getEventDataFilterQuery,
|
getEventDataFilterQuery,
|
||||||
toUuid,
|
toUuid,
|
||||||
parseFilters,
|
parseFilters,
|
||||||
|
parseMongoFilter,
|
||||||
rawQuery,
|
rawQuery,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
import { WebsiteEventMetric } from 'lib/types';
|
import { WebsiteEventMetric } from 'lib/types';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
@ -23,6 +23,7 @@ export async function getEventMetrics(
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,16 +46,96 @@ async function relationalQuery(
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const { getDatabaseType, toUuid, rawQuery, getDateQuery, getFilterQuery, client } = prisma;
|
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||||
const filterQuery = getFilterQuery(filters, params);
|
const filterQuery = getFilterQuery(filters, params);
|
||||||
const db = getDatabaseType();
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
event_name x,
|
||||||
|
${getDateQuery('created_at', unit, timezone)} t,
|
||||||
|
count(*) y
|
||||||
|
from website_event
|
||||||
|
where website_id = $1${toUuid()}
|
||||||
|
and created_at >= $2
|
||||||
|
and created_at between $3 and $4
|
||||||
|
and event_type = ${EVENT_TYPE.customEvent}
|
||||||
|
${filterQuery}
|
||||||
|
group by 1, 2
|
||||||
|
order by 2`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
{
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
timezone = 'utc',
|
||||||
|
unit = 'day',
|
||||||
|
filters,
|
||||||
|
}: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timezone: string;
|
||||||
|
unit: string;
|
||||||
|
filters: {
|
||||||
|
url: string;
|
||||||
|
eventName: string;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { rawQuery, getDateQuery, getDateFormat, getBetweenDates, getFilterQuery } = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = { websiteId };
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
event_name x,
|
||||||
|
${getDateQuery('created_at', unit, timezone)} t,
|
||||||
|
count(*) y
|
||||||
|
from website_event
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and event_type = ${EVENT_TYPE.customEvent}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${getFilterQuery(filters, params)}
|
||||||
|
group by x, t
|
||||||
|
order by t`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(
|
||||||
|
websiteId: string,
|
||||||
|
{
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
timezone = 'utc',
|
||||||
|
unit = 'day',
|
||||||
|
filters,
|
||||||
|
}: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timezone: string;
|
||||||
|
unit: string;
|
||||||
|
filters: {
|
||||||
|
url: string;
|
||||||
|
eventName: string;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { getDateQuery, parseMongoFilter, client } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -143,62 +224,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
event_name x,
|
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
|
||||||
count(*) y
|
|
||||||
from website_event
|
|
||||||
where website_id = $1${toUuid()}
|
|
||||||
and created_at >= $2
|
|
||||||
and created_at between $3 and $4
|
|
||||||
and event_type = ${EVENT_TYPE.customEvent}
|
|
||||||
${filterQuery}
|
|
||||||
group by 1, 2
|
|
||||||
order by 2`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
{
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
timezone = 'utc',
|
|
||||||
unit = 'day',
|
|
||||||
filters,
|
|
||||||
}: {
|
|
||||||
startDate: Date;
|
|
||||||
endDate: Date;
|
|
||||||
timezone: string;
|
|
||||||
unit: string;
|
|
||||||
filters: {
|
|
||||||
url: string;
|
|
||||||
eventName: string;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
const { rawQuery, getDateQuery, getDateFormat, getBetweenDates, getFilterQuery } = clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = { websiteId };
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
event_name x,
|
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
|
||||||
count(*) y
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and event_type = ${EVENT_TYPE.customEvent}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${getFilterQuery(filters, params)}
|
|
||||||
group by x, t
|
|
||||||
order by t`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
|
||||||
|
|
||||||
export function getEvents(...args: [websiteId: string, startAt: Date, eventType: number]) {
|
export function getEvents(...args: [websiteId: string, startAt: Date, eventType: number]) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => relationalQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
|
import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import kafka from 'lib/kafka';
|
import kafka from 'lib/kafka';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { uuid } from 'lib/crypto';
|
import { uuid } from 'lib/crypto';
|
||||||
|
@ -29,6 +29,7 @@ export async function saveEvent(args: {
|
||||||
}) {
|
}) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(args),
|
[PRISMA]: () => relationalQuery(args),
|
||||||
|
[MONGODB]: () => relationalQuery(args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { WebsiteEventDataMetric } from 'lib/types';
|
import { WebsiteEventDataMetric } from 'lib/types';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
@ -23,6 +23,7 @@ export async function getEventData(
|
||||||
): Promise<WebsiteEventDataMetric[]> {
|
): Promise<WebsiteEventDataMetric[]> {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongoQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -47,14 +48,105 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
||||||
const { getDatabaseType, toUuid, rawQuery, getEventDataFilterQuery, getDateQuery, client } =
|
const { toUuid, rawQuery, getEventDataFilterQuery, getDateQuery } = prisma;
|
||||||
prisma;
|
|
||||||
const db = getDatabaseType();
|
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const params: any = [websiteId, resetDate, startDate, endDate, eventName || ''];
|
const params: any = [websiteId, resetDate, startDate, endDate, eventName || ''];
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
count(*) x
|
||||||
|
${eventName ? `,event_name eventName` : ''}
|
||||||
|
${urlPath ? `,url_path urlPath` : ''}
|
||||||
|
${
|
||||||
|
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
||||||
|
}
|
||||||
|
from event_data
|
||||||
|
${
|
||||||
|
eventName || urlPath
|
||||||
|
? 'join website_event on event_data.id = website_event.website_event_id'
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
where website_id = $1${toUuid()}
|
||||||
|
and created_at >= $2
|
||||||
|
and created_at between $3 and $4
|
||||||
|
${eventName ? `and eventName = $5` : ''}
|
||||||
|
${getEventDataFilterQuery(filters, params)}
|
||||||
|
${timeSeries ? 'group by t' : ''}`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
data: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timeSeries?: {
|
||||||
|
unit: string;
|
||||||
|
timezone: string;
|
||||||
|
};
|
||||||
|
eventName?: string;
|
||||||
|
urlPath?: string;
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
eventKey?: string;
|
||||||
|
eventValue?: string | number | boolean | Date;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
||||||
|
const { rawQuery, getDateFormat, getBetweenDates, getDateQuery, getEventDataFilterQuery } =
|
||||||
|
clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = { websiteId };
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
count(*) x
|
||||||
|
${eventName ? `,event_name eventName` : ''}
|
||||||
|
${urlPath ? `,url_path urlPath` : ''}
|
||||||
|
${
|
||||||
|
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
||||||
|
}
|
||||||
|
from event_data
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
${eventName ? `and eventName = ${eventName}` : ''}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${getEventDataFilterQuery(filters, params)}
|
||||||
|
${timeSeries ? 'group by t' : ''}`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongoQuery(
|
||||||
|
websiteId: string,
|
||||||
|
data: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timeSeries?: {
|
||||||
|
unit: string;
|
||||||
|
timezone: string;
|
||||||
|
};
|
||||||
|
eventName: string;
|
||||||
|
urlPath?: string;
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
eventKey?: string;
|
||||||
|
eventValue?: string | number | boolean | Date;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
||||||
|
const { client, parseMongoFilter } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
|
|
||||||
let joinAggregation: any = { match: {} };
|
let joinAggregation: any = { match: {} };
|
||||||
let matchAggregation: any = { match: {} };
|
let matchAggregation: any = { match: {} };
|
||||||
let eventTypeProjectProperty = '';
|
let eventTypeProjectProperty = '';
|
||||||
|
@ -86,6 +178,7 @@ async function relationalQuery(
|
||||||
}
|
}
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -161,73 +254,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
count(*) x
|
|
||||||
${eventName ? `,event_name eventName` : ''}
|
|
||||||
${urlPath ? `,url_path urlPath` : ''}
|
|
||||||
${
|
|
||||||
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
|
||||||
}
|
|
||||||
from event_data
|
|
||||||
${
|
|
||||||
eventName || urlPath
|
|
||||||
? 'join website_event on event_data.id = website_event.website_event_id'
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
where website_id = $1${toUuid()}
|
|
||||||
and created_at >= $2
|
|
||||||
and created_at between $3 and $4
|
|
||||||
${eventName ? `and eventName = $5` : ''}
|
|
||||||
${getEventDataFilterQuery(filters, params)}
|
|
||||||
${timeSeries ? 'group by t' : ''}`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
data: {
|
|
||||||
startDate: Date;
|
|
||||||
endDate: Date;
|
|
||||||
timeSeries?: {
|
|
||||||
unit: string;
|
|
||||||
timezone: string;
|
|
||||||
};
|
|
||||||
eventName?: string;
|
|
||||||
urlPath?: string;
|
|
||||||
filters: [
|
|
||||||
{
|
|
||||||
eventKey?: string;
|
|
||||||
eventValue?: string | number | boolean | Date;
|
|
||||||
},
|
|
||||||
];
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
|
||||||
const { rawQuery, getDateFormat, getBetweenDates, getDateQuery, getEventDataFilterQuery } =
|
|
||||||
clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = { websiteId };
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
count(*) x
|
|
||||||
${eventName ? `,event_name eventName` : ''}
|
|
||||||
${urlPath ? `,url_path urlPath` : ''}
|
|
||||||
${
|
|
||||||
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
|
||||||
}
|
|
||||||
from event_data
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
${eventName ? `and eventName = ${eventName}` : ''}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${getEventDataFilterQuery(filters, params)}
|
|
||||||
${timeSeries ? 'group by t' : ''}`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Prisma } from '@prisma/client';
|
import { Prisma } from '@prisma/client';
|
||||||
import { EVENT_DATA_TYPE } from 'lib/constants';
|
import { EVENT_DATA_TYPE } from 'lib/constants';
|
||||||
import { uuid } from 'lib/crypto';
|
import { uuid } from 'lib/crypto';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import { flattenJSON } from 'lib/eventData';
|
import { flattenJSON } from 'lib/eventData';
|
||||||
import kafka from 'lib/kafka';
|
import kafka from 'lib/kafka';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
|
@ -18,6 +18,7 @@ export async function saveEventData(args: {
|
||||||
}) {
|
}) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(args),
|
[PRISMA]: () => relationalQuery(args),
|
||||||
|
[MONGODB]: () => relationalQuery(args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ export async function getPageviewMetrics(
|
||||||
) {
|
) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -31,8 +32,7 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const { startDate, endDate, filters = {}, column } = criteria;
|
const { startDate, endDate, filters = {}, column } = criteria;
|
||||||
const { getDatabaseType, rawQuery, parseFilters, toUuid, client } = prisma;
|
const { rawQuery, parseFilters, toUuid } = prisma;
|
||||||
const db = getDatabaseType();
|
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const params: any = [
|
const params: any = [
|
||||||
|
@ -44,21 +44,107 @@ async function relationalQuery(
|
||||||
];
|
];
|
||||||
|
|
||||||
let excludeDomain = '';
|
let excludeDomain = '';
|
||||||
let excludeDomainMongo = {};
|
|
||||||
|
|
||||||
if (column === 'referrer_domain') {
|
if (column === 'referrer_domain') {
|
||||||
excludeDomain = 'and website_event.referrer_domain != $6';
|
excludeDomain = 'and website_event.referrer_domain != $6';
|
||||||
|
params.push(website.domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||||
|
return rawQuery(
|
||||||
|
`select ${column} x, count(*) y
|
||||||
|
from website_event
|
||||||
|
${joinSession}
|
||||||
|
where website_event.website_id = $1${toUuid()}
|
||||||
|
and website_event.created_at >= $2
|
||||||
|
and website_event.created_at between $3 and $4
|
||||||
|
and event_type = $5
|
||||||
|
${excludeDomain}
|
||||||
|
${filterQuery}
|
||||||
|
group by 1
|
||||||
|
order by 2 desc
|
||||||
|
limit 100`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
column: string;
|
||||||
|
filters: object;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, filters = {}, column } = criteria;
|
||||||
|
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = {
|
||||||
|
websiteId,
|
||||||
|
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||||
|
domain: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
let excludeDomain = '';
|
||||||
|
|
||||||
|
if (column === 'referrer_domain') {
|
||||||
|
excludeDomain = 'and referrer_domain != {domain:String}';
|
||||||
|
params.domain = website.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select ${column} x, count(*) y
|
||||||
|
from website_event
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and event_type = {eventType:UInt32}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${excludeDomain}
|
||||||
|
${filterQuery}
|
||||||
|
group by x
|
||||||
|
order by y desc
|
||||||
|
limit 100`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
column: string;
|
||||||
|
filters: object;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, filters = {}, column } = criteria;
|
||||||
|
const { parseMongoFilter, client } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params: any = [
|
||||||
|
websiteId,
|
||||||
|
resetDate,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||||
|
];
|
||||||
|
|
||||||
|
let excludeDomainMongo: any = '';
|
||||||
|
|
||||||
|
if (column === 'referrer_domain') {
|
||||||
excludeDomainMongo = {
|
excludeDomainMongo = {
|
||||||
$ne: ['$referrer_domain', website.domain],
|
$ne: ['$referrer_domain', website.domain],
|
||||||
};
|
};
|
||||||
params.push(website.domain);
|
params.push(website.domain);
|
||||||
}
|
}
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -134,65 +220,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select ${column} x, count(*) y
|
|
||||||
from website_event
|
|
||||||
${joinSession}
|
|
||||||
where website_event.website_id = $1${toUuid()}
|
|
||||||
and website_event.created_at >= $2
|
|
||||||
and website_event.created_at between $3 and $4
|
|
||||||
and event_type = $5
|
|
||||||
${excludeDomain}
|
|
||||||
${filterQuery}
|
|
||||||
group by 1
|
|
||||||
order by 2 desc
|
|
||||||
limit 100`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
criteria: {
|
|
||||||
startDate: Date;
|
|
||||||
endDate: Date;
|
|
||||||
column: string;
|
|
||||||
filters: object;
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
const { startDate, endDate, filters = {}, column } = criteria;
|
|
||||||
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = {
|
|
||||||
websiteId,
|
|
||||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
||||||
domain: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
let excludeDomain = '';
|
|
||||||
|
|
||||||
if (column === 'referrer_domain') {
|
|
||||||
excludeDomain = 'and referrer_domain != {domain:String}';
|
|
||||||
params.domain = website.domain;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select ${column} x, count(*) y
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and event_type = {eventType:UInt32}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${excludeDomain}
|
|
||||||
${filterQuery}
|
|
||||||
group by x
|
|
||||||
order by y desc
|
|
||||||
limit 100`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
@ -20,6 +20,7 @@ export async function getPageviewStats(
|
||||||
) {
|
) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -45,16 +46,116 @@ async function relationalQuery(
|
||||||
filters = {},
|
filters = {},
|
||||||
sessionKey = 'session_id',
|
sessionKey = 'session_id',
|
||||||
} = criteria;
|
} = criteria;
|
||||||
const { getDatabaseType, toUuid, getDateQuery, parseFilters, rawQuery, client } = prisma;
|
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
|
||||||
const db = getDatabaseType();
|
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select ${getDateQuery('website_event.created_at', unit, timezone)} x,
|
||||||
|
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||||
|
from website_event
|
||||||
|
${joinSession}
|
||||||
|
where website_event.website_id = $1${toUuid()}
|
||||||
|
and website_event.created_at >= $2
|
||||||
|
and website_event.created_at between $3 and $4
|
||||||
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
|
${filterQuery}
|
||||||
|
group by 1`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timezone?: string;
|
||||||
|
unit?: string;
|
||||||
|
count?: string;
|
||||||
|
filters: object;
|
||||||
|
sessionKey?: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const {
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
timezone = 'UTC',
|
||||||
|
unit = 'day',
|
||||||
|
count = '*',
|
||||||
|
filters = {},
|
||||||
|
} = criteria;
|
||||||
|
const {
|
||||||
|
parseFilters,
|
||||||
|
getDateFormat,
|
||||||
|
rawQuery,
|
||||||
|
getDateStringQuery,
|
||||||
|
getDateQuery,
|
||||||
|
getBetweenDates,
|
||||||
|
} = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = { websiteId };
|
||||||
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
${getDateStringQuery('g.t', unit)} as x,
|
||||||
|
g.y as y
|
||||||
|
from
|
||||||
|
(select
|
||||||
|
${getDateQuery('created_at', unit, timezone)} t,
|
||||||
|
count(${count !== '*' ? 'distinct session_id' : count}) y
|
||||||
|
from website_event
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${filterQuery}
|
||||||
|
group by t) g
|
||||||
|
order by t`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timezone?: string;
|
||||||
|
unit?: string;
|
||||||
|
count?: string;
|
||||||
|
filters: object;
|
||||||
|
sessionKey?: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const {
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
timezone = 'utc',
|
||||||
|
unit = 'day',
|
||||||
|
count = '*',
|
||||||
|
filters = {},
|
||||||
|
} = criteria;
|
||||||
|
const { getDateQuery, client, parseMongoFilter } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
let sessionInclude = '';
|
let sessionInclude = '';
|
||||||
let sessionGroupAggregation: any = { $match: {} };
|
let sessionGroupAggregation: any = { $match: {} };
|
||||||
|
const sessionLookUpAggregation: any = {
|
||||||
|
$lookup: {
|
||||||
|
from: 'session',
|
||||||
|
foreignField: '_id',
|
||||||
|
localField: 'session_id',
|
||||||
|
as: 'session',
|
||||||
|
},
|
||||||
|
};
|
||||||
let sessionProjectAggregation: any = { $match: {} };
|
let sessionProjectAggregation: any = { $match: {} };
|
||||||
|
|
||||||
if (count !== '*') {
|
if (count !== '*') {
|
||||||
sessionInclude = 'session_id : 1';
|
sessionInclude = 'session_id : 1';
|
||||||
sessionGroupAggregation = {
|
sessionGroupAggregation = {
|
||||||
|
@ -71,9 +172,10 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (db === 'mongodb') {
|
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
sessionLookUpAggregation,
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -162,72 +264,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select ${getDateQuery('website_event.created_at', unit, timezone)} x,
|
|
||||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
|
||||||
from website_event
|
|
||||||
${joinSession}
|
|
||||||
where website_event.website_id = $1${toUuid()}
|
|
||||||
and website_event.created_at >= $2
|
|
||||||
and website_event.created_at between $3 and $4
|
|
||||||
and event_type = ${EVENT_TYPE.pageView}
|
|
||||||
${filterQuery}
|
|
||||||
group by 1`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
criteria: {
|
|
||||||
startDate: Date;
|
|
||||||
endDate: Date;
|
|
||||||
timezone?: string;
|
|
||||||
unit?: string;
|
|
||||||
count?: string;
|
|
||||||
filters: object;
|
|
||||||
sessionKey?: string;
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
const {
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
timezone = 'UTC',
|
|
||||||
unit = 'day',
|
|
||||||
count = '*',
|
|
||||||
filters = {},
|
|
||||||
} = criteria;
|
|
||||||
const {
|
|
||||||
parseFilters,
|
|
||||||
getDateFormat,
|
|
||||||
rawQuery,
|
|
||||||
getDateStringQuery,
|
|
||||||
getDateQuery,
|
|
||||||
getBetweenDates,
|
|
||||||
} = clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = { websiteId };
|
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
${getDateStringQuery('g.t', unit)} as x,
|
|
||||||
g.y as y
|
|
||||||
from
|
|
||||||
(select
|
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
|
||||||
count(${count !== '*' ? 'distinct session_id' : count}) y
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and event_type = ${EVENT_TYPE.pageView}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${filterQuery}
|
|
||||||
group by t) g
|
|
||||||
order by t`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import kafka from 'lib/kafka';
|
import kafka from 'lib/kafka';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import cache from 'lib/cache';
|
import cache from 'lib/cache';
|
||||||
|
@ -7,6 +7,7 @@ import { Prisma } from '@prisma/client';
|
||||||
export async function createSession(args: Prisma.SessionCreateInput) {
|
export async function createSession(args: Prisma.SessionCreateInput) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(args),
|
[PRISMA]: () => relationalQuery(args),
|
||||||
|
[MONGODB]: () => relationalQuery(args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||||
}).then(async data => {
|
}).then(async data => {
|
||||||
if (cache.enabled) {
|
if (cache.enabled) {
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import { Prisma } from '@prisma/client';
|
import { Prisma } from '@prisma/client';
|
||||||
|
|
||||||
export async function getSession(args: { id: string }) {
|
export async function getSession(args: { id: string }) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(args),
|
[PRISMA]: () => relationalQuery(args),
|
||||||
|
[MONGODB]: () => relationalQuery(args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ export async function getSessionMetrics(
|
||||||
) {
|
) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -23,14 +24,68 @@ async function relationalQuery(
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const { startDate, endDate, column, filters = {} } = criteria;
|
const { startDate, endDate, column, filters = {} } = criteria;
|
||||||
const { getDatabaseType, toUuid, parseFilters, rawQuery, client } = prisma;
|
const { toUuid, parseFilters, rawQuery } = prisma;
|
||||||
const db = getDatabaseType();
|
|
||||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
return rawQuery(
|
||||||
|
`select ${column} x, count(*) y
|
||||||
|
from session as x
|
||||||
|
where x.session_id in (
|
||||||
|
select website_event.session_id
|
||||||
|
from website_event
|
||||||
|
join website
|
||||||
|
on website_event.website_id = website.website_id
|
||||||
|
${joinSession}
|
||||||
|
where website.website_id = $1${toUuid()}
|
||||||
|
and website_event.created_at >= $2
|
||||||
|
and website_event.created_at between $3 and $4
|
||||||
|
${filterQuery}
|
||||||
|
)
|
||||||
|
group by 1
|
||||||
|
order by 2 desc
|
||||||
|
limit 100`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
data: { startDate: Date; endDate: Date; column: string; filters: object },
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, column, filters = {} } = data;
|
||||||
|
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = { websiteId };
|
||||||
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select ${column} x, count(distinct session_id) y
|
||||||
|
from website_event as x
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${filterQuery}
|
||||||
|
group by x
|
||||||
|
order by y desc
|
||||||
|
limit 100`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: { startDate: Date; endDate: Date; column: string; filters: object },
|
||||||
|
) {
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const { startDate, endDate, column, filters = {} } = criteria;
|
||||||
|
const { parseMongoFilter, client } = prisma;
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -117,50 +172,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select ${column} x, count(*) y
|
|
||||||
from session as x
|
|
||||||
where x.session_id in (
|
|
||||||
select website_event.session_id
|
|
||||||
from website_event
|
|
||||||
join website
|
|
||||||
on website_event.website_id = website.website_id
|
|
||||||
${joinSession}
|
|
||||||
where website.website_id = $1${toUuid()}
|
|
||||||
and website_event.created_at >= $2
|
|
||||||
and website_event.created_at between $3 and $4
|
|
||||||
${filterQuery}
|
|
||||||
)
|
|
||||||
group by 1
|
|
||||||
order by 2 desc
|
|
||||||
limit 100`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
data: { startDate: Date; endDate: Date; column: string; filters: object },
|
|
||||||
) {
|
|
||||||
const { startDate, endDate, column, filters = {} } = data;
|
|
||||||
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = { websiteId };
|
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select ${column} x, count(distinct session_id) y
|
|
||||||
from website_event as x
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and event_type = ${EVENT_TYPE.pageView}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${filterQuery}
|
|
||||||
group by x
|
|
||||||
order by y desc
|
|
||||||
limit 100`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
import { runQuery, PRISMA, CLICKHOUSE, MONGODB } from 'lib/db';
|
||||||
|
|
||||||
export async function getSessions(...args: [websiteId: string, startAt: Date]) {
|
export async function getSessions(...args: [websiteId: string, startAt: Date]) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => relationalQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,51 @@
|
||||||
import { subMinutes } from 'date-fns';
|
import { subMinutes } from 'date-fns';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
|
|
||||||
export async function getActiveVisitors(...args: [websiteId: string]) {
|
export async function getActiveVisitors(...args: [websiteId: string]) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function relationalQuery(websiteId: string) {
|
async function relationalQuery(websiteId: string) {
|
||||||
const { getDatabaseType, toUuid, rawQuery, client } = prisma;
|
const { toUuid, rawQuery } = prisma;
|
||||||
const db = getDatabaseType();
|
|
||||||
|
|
||||||
const date = subMinutes(new Date(), 5);
|
const date = subMinutes(new Date(), 5);
|
||||||
const params: any = [websiteId, date];
|
const params: any = [websiteId, date];
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
return rawQuery(
|
||||||
|
`select count(distinct session_id) x
|
||||||
|
from website_event
|
||||||
|
join website
|
||||||
|
on website_event.website_id = website.website_id
|
||||||
|
where website.website_id = $1${toUuid()}
|
||||||
|
and website_event.created_at >= $2`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(websiteId: string) {
|
||||||
|
const { rawQuery } = clickhouse;
|
||||||
|
const params = { websiteId, startAt: subMinutes(new Date(), 5) };
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select count(distinct session_id) x
|
||||||
|
from website_event
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at >= {startAt:DateTime('UTC')}`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(websiteId: string) {
|
||||||
|
const { client } = prisma;
|
||||||
|
|
||||||
|
const date = subMinutes(new Date(), 5);
|
||||||
|
|
||||||
const result: any = await client.websiteEvent.aggregateRaw({
|
const result: any = await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
{
|
{
|
||||||
|
@ -56,28 +84,4 @@ async function relationalQuery(websiteId: string) {
|
||||||
} else {
|
} else {
|
||||||
return { x: 0 };
|
return { x: 0 };
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select count(distinct session_id) x
|
|
||||||
from website_event
|
|
||||||
join website
|
|
||||||
on website_event.website_id = website.website_id
|
|
||||||
where website.website_id = $1${toUuid()}
|
|
||||||
and website_event.created_at >= $2`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(websiteId: string) {
|
|
||||||
const { rawQuery } = clickhouse;
|
|
||||||
const params = { websiteId, startAt: subMinutes(new Date(), 5) };
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select count(distinct session_id) x
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and created_at >= {startAt:DateTime('UTC')}`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { loadWebsite } from 'lib/query';
|
import { loadWebsite } from 'lib/query';
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ export async function getWebsiteStats(
|
||||||
) {
|
) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[MONGODB]: () => mongodbQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -21,24 +22,84 @@ async function relationalQuery(
|
||||||
criteria: { startDate: Date; endDate: Date; filters: object },
|
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||||
) {
|
) {
|
||||||
const { startDate, endDate, filters = {} } = criteria;
|
const { startDate, endDate, filters = {} } = criteria;
|
||||||
const {
|
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||||
getDatabaseType,
|
|
||||||
toUuid,
|
|
||||||
getDateQuery,
|
|
||||||
getTimestampInterval,
|
|
||||||
parseFilters,
|
|
||||||
rawQuery,
|
|
||||||
client,
|
|
||||||
} = prisma;
|
|
||||||
const db = getDatabaseType();
|
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||||
|
return rawQuery(
|
||||||
|
`select sum(t.c) as "pageviews",
|
||||||
|
count(distinct t.session_id) as "uniques",
|
||||||
|
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||||
|
sum(t.time) as "totaltime"
|
||||||
|
from (
|
||||||
|
select website_event.session_id,
|
||||||
|
${getDateQuery('website_event.created_at', 'hour')},
|
||||||
|
count(*) c,
|
||||||
|
${getTimestampInterval('website_event.created_at')} as "time"
|
||||||
|
from website_event
|
||||||
|
join website
|
||||||
|
on website_event.website_id = website.website_id
|
||||||
|
${joinSession}
|
||||||
|
where event_type = ${EVENT_TYPE.pageView}
|
||||||
|
and website.website_id = $1${toUuid()}
|
||||||
|
and website_event.created_at >= $2
|
||||||
|
and website_event.created_at between $3 and $4
|
||||||
|
${filterQuery}
|
||||||
|
group by 1, 2
|
||||||
|
) t`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, filters = {} } = criteria;
|
||||||
|
const { rawQuery, getDateFormat, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const params = { websiteId };
|
||||||
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
sum(t.c) as "pageviews",
|
||||||
|
count(distinct t.session_id) as "uniques",
|
||||||
|
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||||
|
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
||||||
|
from (
|
||||||
|
select session_id,
|
||||||
|
${getDateQuery('created_at', 'day')} time_series,
|
||||||
|
count(*) c,
|
||||||
|
min(created_at) min_time,
|
||||||
|
max(created_at) max_time
|
||||||
|
from website_event
|
||||||
|
where event_type = ${EVENT_TYPE.pageView}
|
||||||
|
and website_id = {websiteId:UUID}
|
||||||
|
and created_at >= ${getDateFormat(resetDate)}
|
||||||
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
${filterQuery}
|
||||||
|
group by session_id, time_series
|
||||||
|
) t;`,
|
||||||
|
params,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mongodbQuery(
|
||||||
|
websiteId: string,
|
||||||
|
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||||
|
) {
|
||||||
|
const { startDate, endDate, filters = {} } = criteria;
|
||||||
|
const { parseMongoFilter, client } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||||
|
const mongoFilter = parseMongoFilter(filters);
|
||||||
|
|
||||||
if (db === 'mongodb') {
|
|
||||||
return await client.websiteEvent.aggregateRaw({
|
return await client.websiteEvent.aggregateRaw({
|
||||||
pipeline: [
|
pipeline: [
|
||||||
|
mongoFilter,
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
|
@ -145,64 +206,4 @@ async function relationalQuery(
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return rawQuery(
|
|
||||||
`select sum(t.c) as "pageviews",
|
|
||||||
count(distinct t.session_id) as "uniques",
|
|
||||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
|
||||||
sum(t.time) as "totaltime"
|
|
||||||
from (
|
|
||||||
select website_event.session_id,
|
|
||||||
${getDateQuery('website_event.created_at', 'hour')},
|
|
||||||
count(*) c,
|
|
||||||
${getTimestampInterval('website_event.created_at')} as "time"
|
|
||||||
from website_event
|
|
||||||
join website
|
|
||||||
on website_event.website_id = website.website_id
|
|
||||||
${joinSession}
|
|
||||||
where event_type = ${EVENT_TYPE.pageView}
|
|
||||||
and website.website_id = $1${toUuid()}
|
|
||||||
and website_event.created_at >= $2
|
|
||||||
and website_event.created_at between $3 and $4
|
|
||||||
${filterQuery}
|
|
||||||
group by 1, 2
|
|
||||||
) t`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(
|
|
||||||
websiteId: string,
|
|
||||||
criteria: { startDate: Date; endDate: Date; filters: object },
|
|
||||||
) {
|
|
||||||
const { startDate, endDate, filters = {} } = criteria;
|
|
||||||
const { rawQuery, getDateFormat, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
|
||||||
const website = await loadWebsite(websiteId);
|
|
||||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
|
||||||
const params = { websiteId };
|
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
sum(t.c) as "pageviews",
|
|
||||||
count(distinct t.session_id) as "uniques",
|
|
||||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
|
||||||
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
|
||||||
from (
|
|
||||||
select session_id,
|
|
||||||
${getDateQuery('created_at', 'day')} time_series,
|
|
||||||
count(*) c,
|
|
||||||
min(created_at) min_time,
|
|
||||||
max(created_at) max_time
|
|
||||||
from website_event
|
|
||||||
where event_type = ${EVENT_TYPE.pageView}
|
|
||||||
and website_id = {websiteId:UUID}
|
|
||||||
and created_at >= ${getDateFormat(resetDate)}
|
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
||||||
${filterQuery}
|
|
||||||
group by session_id, time_series
|
|
||||||
) t;`,
|
|
||||||
params,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue