Refactored filter parameters.

This commit is contained in:
Mike Cao 2024-03-26 17:31:16 -07:00
parent 1a839d1cae
commit cff2d00536
13 changed files with 291 additions and 123 deletions

View file

@ -3,7 +3,7 @@ import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getValues(
...args: [websiteId: string, column: string, startDate: Date, endDate: Date]
...args: [websiteId: string, column: string, startDate: Date, endDate: Date, search: string]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -11,42 +11,72 @@ export async function getValues(
});
}
async function relationalQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
async function relationalQuery(
websiteId: string,
column: string,
startDate: Date,
endDate: Date,
search: string,
) {
const { rawQuery } = prisma;
let searchQuery = '';
if (search) {
searchQuery = `and ${column} LIKE {{search}}`;
}
return rawQuery(
`
select distinct ${column} as "value"
select ${column} as "value", count(*)
from website_event
inner join session
on session.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
limit 500
${searchQuery}
group by 1
order by 2 desc
limit 10
`,
{
websiteId,
startDate,
endDate,
search: `%${search}%`,
},
);
}
async function clickhouseQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
async function clickhouseQuery(
websiteId: string,
column: string,
startDate: Date,
endDate: Date,
search: string,
) {
const { rawQuery } = clickhouse;
let searchQuery = '';
if (search) {
searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`;
}
return rawQuery(
`
select distinct ${column} as value
select ${column} as value, count(*)
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
limit 500
${searchQuery}
group by 1
order by 2 desc
limit 10
`,
{
websiteId,
startDate,
endDate,
search,
},
);
}