From 711ad2bbcfdbac4d48230dd7f866f4ea09f245fe Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 24 Jul 2023 11:57:46 -0700 Subject: [PATCH] remove dropoff calc from db query --- components/metrics/DataTable.js | 2 +- lib/prisma.ts | 15 --------- .../analytics/pageview/getPageviewFunnel.ts | 32 ++++++++----------- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/components/metrics/DataTable.js b/components/metrics/DataTable.js index 9c5fb559..e2e9462d 100644 --- a/components/metrics/DataTable.js +++ b/components/metrics/DataTable.js @@ -78,7 +78,7 @@ const AnimatedRow = ({ showPercentage = true, }) => { const props = useSpring({ - width: Number(percent), + width: percent, y: value, from: { width: 0, y: 0 }, config: animate ? config.default : { duration: 0 }, diff --git a/lib/prisma.ts b/lib/prisma.ts index 722535c2..85dc25b8 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -44,18 +44,6 @@ function getAddMinutesQuery(field: string, minutes: number) { } } -function getDropoffQuery() { - const db = getDatabaseType(process.env.DATABASE_URL); - - if (db === POSTGRESQL) { - return `round((1.0 - count::numeric/lag(nullif(count, 0), 1) over ()),2) * 100`; - } - - if (db === MYSQL) { - return `round((1.0 - count/LAG(nullif(count, 0), 1) OVER (ORDER BY level)),2) * 100`; - } -} - function getDateQuery(field: string, unit: string, timezone?: string): string { const db = getDatabaseType(process.env.DATABASE_URL); @@ -217,15 +205,12 @@ async function rawQuery(query: string, params: never[] = []): Promise { const sql = db === MYSQL ? query.replace(/\$[0-9]+/g, '?') : query; - // console.log(sql); - // console.log(params); return prisma.rawQuery(sql, params); } export default { ...prisma, getAddMinutesQuery, - getDropoffQuery, getDateQuery, getTimestampInterval, getFilterQuery, diff --git a/queries/analytics/pageview/getPageviewFunnel.ts b/queries/analytics/pageview/getPageviewFunnel.ts index b7d918b9..422338da 100644 --- a/queries/analytics/pageview/getPageviewFunnel.ts +++ b/queries/analytics/pageview/getPageviewFunnel.ts @@ -35,7 +35,7 @@ async function relationalQuery( }[] > { const { windowMinutes, startDate, endDate, urls } = criteria; - const { rawQuery, getFunnelQuery, toUuid, getDropoffQuery } = prisma; + const { rawQuery, getFunnelQuery, toUuid } = prisma; const { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes); const params: any = [websiteId, startDate, endDate, ...urls]; @@ -47,25 +47,19 @@ async function relationalQuery( where url_path in (${urlFilterQuery}) and website_id = $1${toUuid()} and created_at between $2 and $3 - ),level1 AS ( - select distinct session_id, created_at - from level0 - where url_path = $4 - )${levelQuery}, levelCount as ( - ${sumQuery} - order by level) - select - level, - count, - ${getDropoffQuery()} as drop_off - from levelCount; - `, + ),level1 AS ( + select distinct session_id, created_at + from level0 + where url_path = $4 + )${levelQuery} + ${sumQuery} + ORDER BY level;`, params, ).then(results => { return urls.map((a, i) => ({ x: a, y: results[i]?.count || 0, - z: results[i]?.drop_off || 0, + z: (1 - (Number(results[i]?.count) * 1.0) / Number(results[i - 1]?.count)) * 100 || 0, // drop off })); }); } @@ -96,7 +90,7 @@ async function clickhouseQuery( return rawQuery<{ level: number; count: number }[]>( ` - select level, + WITH funnel as (select level, count(*) AS count from ( select session_id, @@ -111,13 +105,15 @@ async function clickhouseQuery( group by 1 ) group by level - order by level asc; + order by level asc) + select * from funnel where level > 0; `, params, ).then(results => { return urls.map((a, i) => ({ x: a, - y: results[i + 1]?.count || 0, + y: results[i]?.count || 0, + z: (1 - (Number(results[i]?.count) * 1.0) / Number(results[i - 1]?.count)) * 100 || 0, // drop off })); }); }