remove dropoff calc from db query

This commit is contained in:
Francis Cao 2023-07-24 11:57:46 -07:00
parent 858f465566
commit 711ad2bbcf
3 changed files with 15 additions and 34 deletions

View file

@ -78,7 +78,7 @@ const AnimatedRow = ({
showPercentage = true, showPercentage = true,
}) => { }) => {
const props = useSpring({ const props = useSpring({
width: Number(percent), width: percent,
y: value, y: value,
from: { width: 0, y: 0 }, from: { width: 0, y: 0 },
config: animate ? config.default : { duration: 0 }, config: animate ? config.default : { duration: 0 },

View file

@ -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 { function getDateQuery(field: string, unit: string, timezone?: string): string {
const db = getDatabaseType(process.env.DATABASE_URL); const db = getDatabaseType(process.env.DATABASE_URL);
@ -217,15 +205,12 @@ async function rawQuery(query: string, params: never[] = []): Promise<any> {
const sql = db === MYSQL ? query.replace(/\$[0-9]+/g, '?') : query; const sql = db === MYSQL ? query.replace(/\$[0-9]+/g, '?') : query;
// console.log(sql);
// console.log(params);
return prisma.rawQuery(sql, params); return prisma.rawQuery(sql, params);
} }
export default { export default {
...prisma, ...prisma,
getAddMinutesQuery, getAddMinutesQuery,
getDropoffQuery,
getDateQuery, getDateQuery,
getTimestampInterval, getTimestampInterval,
getFilterQuery, getFilterQuery,

View file

@ -35,7 +35,7 @@ async function relationalQuery(
}[] }[]
> { > {
const { windowMinutes, startDate, endDate, urls } = criteria; 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 { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes);
const params: any = [websiteId, startDate, endDate, ...urls]; const params: any = [websiteId, startDate, endDate, ...urls];
@ -47,25 +47,19 @@ async function relationalQuery(
where url_path in (${urlFilterQuery}) where url_path in (${urlFilterQuery})
and website_id = $1${toUuid()} and website_id = $1${toUuid()}
and created_at between $2 and $3 and created_at between $2 and $3
),level1 AS ( ),level1 AS (
select distinct session_id, created_at select distinct session_id, created_at
from level0 from level0
where url_path = $4 where url_path = $4
)${levelQuery}, levelCount as ( )${levelQuery}
${sumQuery} ${sumQuery}
order by level) ORDER BY level;`,
select
level,
count,
${getDropoffQuery()} as drop_off
from levelCount;
`,
params, params,
).then(results => { ).then(results => {
return urls.map((a, i) => ({ return urls.map((a, i) => ({
x: a, x: a,
y: results[i]?.count || 0, 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 }[]>( return rawQuery<{ level: number; count: number }[]>(
` `
select level, WITH funnel as (select level,
count(*) AS count count(*) AS count
from ( from (
select session_id, select session_id,
@ -111,13 +105,15 @@ async function clickhouseQuery(
group by 1 group by 1
) )
group by level group by level
order by level asc; order by level asc)
select * from funnel where level > 0;
`, `,
params, params,
).then(results => { ).then(results => {
return urls.map((a, i) => ({ return urls.map((a, i) => ({
x: a, 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
})); }));
}); });
} }