Refactored queries.

This commit is contained in:
Mike Cao 2023-07-24 23:06:16 -07:00
parent e4bd314bd6
commit 4bd3ef8e12
19 changed files with 330 additions and 408 deletions

View file

@ -34,18 +34,16 @@ async function relationalQuery(
}[]
> {
const { windowMinutes, startDate, endDate, urls } = criteria;
const { rawQuery, getFunnelQuery, toUuid } = prisma;
const { rawQuery, getFunnelQuery } = prisma;
const { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes);
const params: any = [websiteId, startDate, endDate, ...urls];
return rawQuery(
`WITH level0 AS (
select distinct session_id, url_path, referrer_path, created_at
from website_event
where url_path in (${urlFilterQuery})
and website_id = $1${toUuid()}
and created_at between $2 and $3
and website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
),level1 AS (
select distinct session_id, url_path as level_1_url, created_at as level_1_created_at
from level0
@ -55,7 +53,7 @@ async function relationalQuery(
SELECT ${sumQuery}
from level${urls.length};
`,
params,
{ websiteId, startDate, endDate, ...urls },
).then((a: { [key: string]: number }) => {
return urls.map((b, i) => ({ x: b, y: a[0][`level${i + 1}`] || 0 }));
});