convert analytics queries

This commit is contained in:
Brian Cao 2022-07-20 21:31:26 -07:00
parent 4f12933f42
commit 6ea2282f82
12 changed files with 470 additions and 52 deletions

View file

@ -1,6 +1,20 @@
import { parseFilters, rawQuery } from 'lib/db';
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
import {
rawQueryClickhouse,
runAnalyticsQuery,
parseFilters,
rawQuery,
getBetweenDatesClickhouse,
} from 'lib/db';
export function getPageviewMetrics(website_id, start_at, end_at, field, table, filters = {}) {
export async function getPageviewMetrics(...args) {
return runAnalyticsQuery({
[`${RELATIONAL}`]: () => relationalQuery(...args),
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(website_id, start_at, end_at, field, table, filters = {}) {
const params = [website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table,
@ -24,3 +38,28 @@ export function getPageviewMetrics(website_id, start_at, end_at, field, table, f
params,
);
}
async function clickhouseQuery(website_id, start_at, end_at, field, table, filters = {}) {
const params = [website_id];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table,
filters,
params,
);
return rawQueryClickhouse(
`
select ${field} x, count(*) y
from ${table}
${joinSession}
where ${table}.website_id= $1
and ${getBetweenDatesClickhouse(table + '.created_at', start_at, end_at)}
${pageviewQuery}
${joinSession && sessionQuery}
${eventQuery}
group by x
order by y desc
`,
params,
);
}