Re-write CH queries to use query params.

This commit is contained in:
Brian Cao 2023-01-12 12:06:25 -08:00
parent 9a7385e4d5
commit befc5cf6c0
12 changed files with 114 additions and 125 deletions

View file

@ -68,15 +68,19 @@ async function clickhouseQuery(
const { startDate, endDate, column, filters = {} } = data;
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
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);
return rawQuery(
`select ${column} x, count(*) y
from event
where website_id = $1
and rev_id = $2
and event_type = $3
where website_id = {websiteId:UUID}
and rev_id = {revId:UInt32}
and event_type = {eventType:UInt32}
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
group by x

View file

@ -78,7 +78,7 @@ async function clickhouseQuery(
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const params = [websiteId, website?.revId || 0];
const params = { websiteId, revId: website?.revId || 0 };
const { filterQuery } = parseFilters(filters, params);
return rawQuery(
@ -90,8 +90,8 @@ async function clickhouseQuery(
${getDateQuery('created_at', unit, timezone)} t,
count(${count !== '*' ? 'distinct session_id' : count}) y
from event
where website_id = $1
and rev_id = $2
where website_id = {websiteId:UUID}
and rev_id = {revId:UInt32}
and event_type = ${EVENT_TYPE.pageView}
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}

View file

@ -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)}`,
);
}

View file

@ -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,
},
);
}