Re-write CH queries to use query params.
parent
9a7385e4d5
commit
befc5cf6c0
|
@ -14,7 +14,7 @@ export const CLICKHOUSE_DATE_FORMATS = {
|
||||||
|
|
||||||
const log = debug('umami:clickhouse');
|
const log = debug('umami:clickhouse');
|
||||||
|
|
||||||
let clickhouse;
|
let clickhouse: ClickHouse;
|
||||||
const enabled = Boolean(process.env.CLICKHOUSE_URL);
|
const enabled = Boolean(process.env.CLICKHOUSE_URL);
|
||||||
|
|
||||||
function getClient() {
|
function getClient() {
|
||||||
|
@ -49,7 +49,7 @@ function getDateStringQuery(data, unit) {
|
||||||
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
|
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDateQuery(field, unit, timezone) {
|
function getDateQuery(field, unit, timezone?) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
return `date_trunc('${unit}', ${field}, '${timezone}')`;
|
return `date_trunc('${unit}', ${field}, '${timezone}')`;
|
||||||
}
|
}
|
||||||
|
@ -60,12 +60,8 @@ function getDateFormat(date) {
|
||||||
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
|
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCommaSeparatedStringFormat(data) {
|
function getBetweenDates(field, startAt, endAt) {
|
||||||
return data.map(a => `'${a}'`).join(',') || '';
|
return `${field} between ${getDateFormat(startAt)} and ${getDateFormat(endAt)}`;
|
||||||
}
|
|
||||||
|
|
||||||
function getBetweenDates(field, start_at, end_at) {
|
|
||||||
return `${field} between ${getDateFormat(start_at)} and ${getDateFormat(end_at)}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getJsonField(column, property) {
|
function getJsonField(column, property) {
|
||||||
|
@ -106,7 +102,7 @@ function getEventDataFilterQuery(column, filters) {
|
||||||
return query.join('\nand ');
|
return query.join('\nand ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterQuery(filters = {}, params = []) {
|
function getFilterQuery(filters = {}, params = {}) {
|
||||||
const query = Object.keys(filters).reduce((arr, key) => {
|
const query = Object.keys(filters).reduce((arr, key) => {
|
||||||
const filter = filters[key];
|
const filter = filters[key];
|
||||||
|
|
||||||
|
@ -120,20 +116,24 @@ function getFilterQuery(filters = {}, params = []) {
|
||||||
case 'browser':
|
case 'browser':
|
||||||
case 'device':
|
case 'device':
|
||||||
case 'country':
|
case 'country':
|
||||||
case 'event_name':
|
arr.push(`and ${key} = {${key}:String}`);
|
||||||
arr.push(`and ${key}=$${params.length + 1}`);
|
params[key] = filter;
|
||||||
params.push(decodeURIComponent(filter));
|
break;
|
||||||
|
|
||||||
|
case 'eventName':
|
||||||
|
arr.push(`and event_name = {${key}:String}`);
|
||||||
|
params[key] = filter;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'referrer':
|
case 'referrer':
|
||||||
arr.push(`and referrer like $${params.length + 1}`);
|
arr.push(`and referrer ILIKE {${key}:String}`);
|
||||||
params.push(`%${decodeURIComponent(filter)}%`);
|
params[key] = `%${filter}`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'domain':
|
case 'domain':
|
||||||
arr.push(`and referrer not like $${params.length + 1}`);
|
arr.push(`and referrer NOT ILIKE {${key}:String}`);
|
||||||
arr.push(`and referrer not like '/%'`);
|
arr.push(`and referrer NOT ILIKE '/%'`);
|
||||||
params.push(`%://${filter}/%`);
|
params[key] = `%://${filter}/%`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'query':
|
case 'query':
|
||||||
|
@ -146,49 +146,32 @@ function getFilterQuery(filters = {}, params = []) {
|
||||||
return query.join('\n');
|
return query.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFilters(filters = {}, params = []) {
|
function parseFilters(filters: any = {}, params: any = {}) {
|
||||||
const { domain, url, event_url, referrer, os, browser, device, country, event_name, query } =
|
const { domain, url, eventUrl, referrer, os, browser, device, country, eventName, query } =
|
||||||
filters;
|
filters;
|
||||||
|
|
||||||
const pageviewFilters = { domain, url, referrer, query };
|
const pageviewFilters = { domain, url, referrer, query };
|
||||||
const sessionFilters = { os, browser, device, country };
|
const sessionFilters = { os, browser, device, country };
|
||||||
const eventFilters = { url: event_url, event_name };
|
const eventFilters = { url: eventUrl, eventName };
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pageviewFilters,
|
pageviewFilters,
|
||||||
sessionFilters,
|
sessionFilters,
|
||||||
eventFilters,
|
eventFilters,
|
||||||
event: { event_name },
|
event: { eventName },
|
||||||
filterQuery: getFilterQuery(filters, params),
|
filterQuery: getFilterQuery(filters, params),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatQuery(str, params = []) {
|
async function rawQuery(query, params = {}) {
|
||||||
let formattedString = str;
|
|
||||||
|
|
||||||
params.forEach((param, i) => {
|
|
||||||
let replace = param;
|
|
||||||
|
|
||||||
if (typeof param === 'string' || param instanceof String) {
|
|
||||||
replace = `'${replace}'`;
|
|
||||||
}
|
|
||||||
|
|
||||||
formattedString = formattedString.replace(`$${i + 1}`, replace);
|
|
||||||
});
|
|
||||||
|
|
||||||
return formattedString;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function rawQuery(query, params = []) {
|
|
||||||
let formattedQuery = formatQuery(query, params);
|
|
||||||
|
|
||||||
if (process.env.LOG_QUERY) {
|
if (process.env.LOG_QUERY) {
|
||||||
log(formattedQuery);
|
log(query);
|
||||||
|
log(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
await connect();
|
await connect();
|
||||||
|
|
||||||
return clickhouse.query(formattedQuery).toPromise();
|
return clickhouse.query(query, { params }).toPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findUnique(data) {
|
async function findUnique(data) {
|
||||||
|
@ -204,7 +187,7 @@ async function findFirst(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function connect() {
|
async function connect() {
|
||||||
if (!clickhouse) {
|
if (enabled && !clickhouse) {
|
||||||
clickhouse = process.env.CLICKHOUSE_URL && (global[CLICKHOUSE] || getClient());
|
clickhouse = process.env.CLICKHOUSE_URL && (global[CLICKHOUSE] || getClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +202,6 @@ export default {
|
||||||
getDateStringQuery,
|
getDateStringQuery,
|
||||||
getDateQuery,
|
getDateQuery,
|
||||||
getDateFormat,
|
getDateFormat,
|
||||||
getCommaSeparatedStringFormat,
|
|
||||||
getBetweenDates,
|
getBetweenDates,
|
||||||
getEventDataColumnsQuery,
|
getEventDataColumnsQuery,
|
||||||
getEventDataFilterQuery,
|
getEventDataFilterQuery,
|
|
@ -72,14 +72,14 @@ async function clickhouseQuery(
|
||||||
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
|
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
|
||||||
clickhouse;
|
clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0];
|
const params = { websiteId, revId: website?.revId || 0 };
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
${getEventDataColumnsQuery('event_data', columns)}
|
${getEventDataColumnsQuery('event_data', columns)}
|
||||||
from event
|
from event
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and event_type = ${EVENT_TYPE.customEvent}
|
and event_type = ${EVENT_TYPE.customEvent}
|
||||||
${eventName ? `and eventName = ${eventName}` : ''}
|
${eventName ? `and eventName = ${eventName}` : ''}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
|
|
|
@ -85,7 +85,7 @@ async function clickhouseQuery(
|
||||||
) {
|
) {
|
||||||
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0];
|
const params = { websiteId, revId: website?.revId || 0 };
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
|
@ -93,8 +93,8 @@ async function clickhouseQuery(
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
${getDateQuery('created_at', unit, timezone)} t,
|
||||||
count(*) y
|
count(*) y
|
||||||
from event
|
from event
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and event_type = ${EVENT_TYPE.customEvent}
|
and event_type = ${EVENT_TYPE.customEvent}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
${getFilterQuery(filters, params)}
|
${getFilterQuery(filters, params)}
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
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 } from 'lib/db';
|
||||||
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
|
|
||||||
export function getEvents(...args) {
|
export function getEvents(...args: [websites: string[], startAt: Date]) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function relationalQuery(websites, start_at) {
|
function relationalQuery(websites: string[], startAt: Date) {
|
||||||
return prisma.client.event.findMany({
|
return prisma.client.event.findMany({
|
||||||
where: {
|
where: {
|
||||||
websiteId: {
|
websiteId: {
|
||||||
in: websites,
|
in: websites,
|
||||||
},
|
},
|
||||||
createdAt: {
|
createdAt: {
|
||||||
gte: start_at,
|
gte: startAt,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function clickhouseQuery(websites, start_at) {
|
function clickhouseQuery(websites: string[], startAt: Date) {
|
||||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
const { rawQuery } = clickhouse;
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
|
@ -34,12 +35,12 @@ function clickhouseQuery(websites, start_at) {
|
||||||
url,
|
url,
|
||||||
event_name
|
event_name
|
||||||
from event
|
from event
|
||||||
where event_name != ''
|
where event_type = ${EVENT_TYPE.customEvent}
|
||||||
and ${
|
and ${websites && websites.length > 0 ? `website_id in {websites:Array(UUID)}` : '0 = 0'}
|
||||||
websites && websites.length > 0
|
and created_at >= {startAt:DateTime('UTC')}`,
|
||||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
{
|
||||||
: '0 = 0'
|
websites,
|
||||||
}
|
startAt,
|
||||||
and created_at >= ${getDateFormat(start_at)}`,
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
|
@ -68,15 +68,19 @@ async function clickhouseQuery(
|
||||||
const { startDate, endDate, column, filters = {} } = data;
|
const { startDate, endDate, column, filters = {} } = data;
|
||||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0, EVENT_TYPE.pageView];
|
const params = {
|
||||||
|
websiteId,
|
||||||
|
revId: website?.revId || 0,
|
||||||
|
eventType: EVENT_TYPE.pageView,
|
||||||
|
};
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select ${column} x, count(*) y
|
`select ${column} x, count(*) y
|
||||||
from event
|
from event
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and event_type = $3
|
and event_type = {eventType:UInt32}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
group by x
|
group by x
|
||||||
|
|
|
@ -78,7 +78,7 @@ async function clickhouseQuery(
|
||||||
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
|
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
|
||||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0];
|
const params = { websiteId, revId: website?.revId || 0 };
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
|
@ -90,8 +90,8 @@ async function clickhouseQuery(
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
${getDateQuery('created_at', unit, timezone)} t,
|
||||||
count(${count !== '*' ? 'distinct session_id' : count}) y
|
count(${count !== '*' ? 'distinct session_id' : count}) y
|
||||||
from event
|
from event
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and event_type = ${EVENT_TYPE.pageView}
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
import prisma from 'lib/prisma';
|
|
||||||
import clickhouse from 'lib/clickhouse';
|
|
||||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
|
||||||
|
|
||||||
export async function getPageviews(...args) {
|
|
||||||
return runQuery({
|
|
||||||
[PRISMA]: () => relationalQuery(...args),
|
|
||||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function relationalQuery(websites, start_at) {
|
|
||||||
return prisma.client.pageview.findMany({
|
|
||||||
where: {
|
|
||||||
websiteId: {
|
|
||||||
in: websites,
|
|
||||||
},
|
|
||||||
createdAt: {
|
|
||||||
gte: start_at,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function clickhouseQuery(websites, start_at) {
|
|
||||||
const { rawQuery, getCommaSeparatedStringFormat } = clickhouse;
|
|
||||||
|
|
||||||
return rawQuery(
|
|
||||||
`select
|
|
||||||
website_id,
|
|
||||||
session_id,
|
|
||||||
created_at,
|
|
||||||
url
|
|
||||||
from event
|
|
||||||
where event_name = ''
|
|
||||||
and ${
|
|
||||||
websites && websites.length > 0
|
|
||||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
|
||||||
: '0 = 0'
|
|
||||||
}
|
|
||||||
and created_at >= ${clickhouse.getDateFormat(start_at)}`,
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
import prisma from 'lib/prisma';
|
||||||
|
import clickhouse from 'lib/clickhouse';
|
||||||
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||||
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
|
|
||||||
|
export async function getPageviews(...args: [websites: string[], startAt: Date]) {
|
||||||
|
return runQuery({
|
||||||
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function relationalQuery(websites: string[], startAt: Date) {
|
||||||
|
return prisma.client.pageview.findMany({
|
||||||
|
where: {
|
||||||
|
websiteId: {
|
||||||
|
in: websites,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
gte: startAt,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(websites: string[], startAt: Date) {
|
||||||
|
const { rawQuery } = clickhouse;
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`select
|
||||||
|
website_id,
|
||||||
|
session_id,
|
||||||
|
created_at,
|
||||||
|
url
|
||||||
|
from event
|
||||||
|
where event_type = ${EVENT_TYPE.pageView}
|
||||||
|
and ${websites && websites.length > 0 ? `website_id in {websites:Array(UUID)}` : '0 = 0'}
|
||||||
|
and created_at >= {startAt:DateTime('UTC')}`,
|
||||||
|
{
|
||||||
|
websites,
|
||||||
|
startAt,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
|
@ -18,7 +18,7 @@ async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
|
||||||
|
|
||||||
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
||||||
const { rawQuery, findFirst } = clickhouse;
|
const { rawQuery, findFirst } = clickhouse;
|
||||||
const params = [sessionId];
|
const params = { sessionId };
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select
|
`select
|
||||||
|
@ -33,7 +33,7 @@ async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
||||||
language,
|
language,
|
||||||
country
|
country
|
||||||
from event
|
from event
|
||||||
where session_id = $1
|
where session_id = {sessionId:UUID}
|
||||||
limit 1`,
|
limit 1`,
|
||||||
params,
|
params,
|
||||||
).then(result => findFirst(result));
|
).then(result => findFirst(result));
|
||||||
|
|
|
@ -2,6 +2,7 @@ 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 } from 'lib/db';
|
||||||
import cache from 'lib/cache';
|
import cache from 'lib/cache';
|
||||||
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
|
|
||||||
export async function getSessionMetrics(
|
export async function getSessionMetrics(
|
||||||
...args: [
|
...args: [
|
||||||
|
@ -50,15 +51,15 @@ async function clickhouseQuery(
|
||||||
const { startDate, endDate, field, filters = {} } = data;
|
const { startDate, endDate, field, filters = {} } = data;
|
||||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0];
|
const params = { websiteId, revId: website?.revId || 0 };
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select ${field} x, count(distinct session_id) y
|
`select ${field} x, count(distinct session_id) y
|
||||||
from event as x
|
from event as x
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and event_name = ''
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
group by x
|
group by x
|
||||||
|
|
|
@ -28,14 +28,14 @@ async function relationalQuery(websiteId: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clickhouseQuery(websiteId: string) {
|
async function clickhouseQuery(websiteId: string) {
|
||||||
const { rawQuery, getDateFormat } = clickhouse;
|
const { rawQuery } = clickhouse;
|
||||||
const params = [websiteId];
|
const params = { websiteId, startAt: subMinutes(new Date(), 5) };
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select count(distinct session_id) x
|
`select count(distinct session_id) x
|
||||||
from event
|
from event
|
||||||
where website_id = $1
|
where website_id = {websiteId:UUID}
|
||||||
and created_at >= ${getDateFormat(subMinutes(new Date(), 5))}`,
|
and created_at >= {startAt:DateTime('UTC')}`,
|
||||||
params,
|
params,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ async function clickhouseQuery(
|
||||||
const { startDate, endDate, filters = {} } = data;
|
const { startDate, endDate, filters = {} } = data;
|
||||||
const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
||||||
const website = await cache.fetchWebsite(websiteId);
|
const website = await cache.fetchWebsite(websiteId);
|
||||||
const params = [websiteId, website?.revId || 0];
|
const params = { websiteId, revId: website?.revId || 0 };
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
const { filterQuery } = parseFilters(filters, params);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
|
@ -69,8 +69,8 @@ async function clickhouseQuery(
|
||||||
max(created_at) max_time
|
max(created_at) max_time
|
||||||
from event
|
from event
|
||||||
where event_type = ${EVENT_TYPE.pageView}
|
where event_type = ${EVENT_TYPE.pageView}
|
||||||
and website_id = $1
|
and website_id = {websiteId:UUID}
|
||||||
and rev_id = $2
|
and rev_id = {revId:UInt32}
|
||||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
group by session_id, time_series
|
group by session_id, time_series
|
||||||
|
|
Loading…
Reference in New Issue