From 979ea33d7457291de341ca902d40208477cffad3 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Tue, 15 Aug 2023 10:58:55 -0700 Subject: [PATCH 1/2] add daydiff function query --- lib/prisma.ts | 13 +++++++++++++ queries/analytics/reports/getRetention.ts | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index efce3f4ebe..94fcf0a080 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -34,6 +34,18 @@ function getAddMinutesQuery(field: string, minutes: number): string { } } +function getDayDiffQuery(field1: string, field2: string): string { + const db = getDatabaseType(process.env.DATABASE_URL); + + if (db === POSTGRESQL) { + return `${field1}::date - ${field2}::date`; + } + + if (db === MYSQL) { + return `DATEDIFF(${field1}, ${field2});`; + } +} + function getDateQuery(field: string, unit: string, timezone?: string): string { const db = getDatabaseType(); @@ -180,6 +192,7 @@ function getPageFilters(filters: SearchFilter): [ export default { ...prisma, getAddMinutesQuery, + getDayDiffQuery, getDateQuery, getTimestampIntervalQuery, getFilterQuery, diff --git a/queries/analytics/reports/getRetention.ts b/queries/analytics/reports/getRetention.ts index ee7e4619b7..12ef1d5290 100644 --- a/queries/analytics/reports/getRetention.ts +++ b/queries/analytics/reports/getRetention.ts @@ -33,7 +33,7 @@ async function relationalQuery( }[] > { const { startDate, endDate } = dateRange; - const { getDateQuery, rawQuery } = prisma; + const { getDateQuery, getDayDiffQuery, rawQuery } = prisma; const timezone = 'utc'; const unit = 'day'; @@ -49,7 +49,10 @@ async function relationalQuery( user_activities AS ( select distinct w.session_id, - (${getDateQuery('created_at', unit, timezone)}::date - c.cohort_date::date) as day_number + (${getDayDiffQuery( + getDateQuery('created_at', unit, timezone), + 'c.cohort_date', + )}) as day_number from website_event w join cohort_items c on w.session_id = c.session_id From 495b7f1f20115c4261b2c3d50809c664c2a98861 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Tue, 15 Aug 2023 12:15:27 -0700 Subject: [PATCH 2/2] fix mysql retention query --- lib/prisma.ts | 15 ++++++++++++++- queries/analytics/reports/getRetention.ts | 12 +++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index f392252e9d..5a21a6ec60 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -43,7 +43,19 @@ function getDayDiffQuery(field1: string, field2: string): string { } if (db === MYSQL) { - return `DATEDIFF(${field1}, ${field2});`; + return `DATEDIFF(${field1}, ${field2})`; + } +} + +function getCastColumnQuery(field: string, type: string): string { + const db = getDatabaseType(process.env.DATABASE_URL); + + if (db === POSTGRESQL) { + return `${field}::${type}`; + } + + if (db === MYSQL) { + return `${field}`; } } @@ -206,6 +218,7 @@ export default { ...prisma, getAddMinutesQuery, getDayDiffQuery, + getCastColumnQuery, getDateQuery, getTimestampIntervalQuery, getFilterQuery, diff --git a/queries/analytics/reports/getRetention.ts b/queries/analytics/reports/getRetention.ts index 12ef1d5290..3abc0e7ae5 100644 --- a/queries/analytics/reports/getRetention.ts +++ b/queries/analytics/reports/getRetention.ts @@ -33,7 +33,7 @@ async function relationalQuery( }[] > { const { startDate, endDate } = dateRange; - const { getDateQuery, getDayDiffQuery, rawQuery } = prisma; + const { getDateQuery, getDayDiffQuery, getCastColumnQuery, rawQuery } = prisma; const timezone = 'utc'; const unit = 'day'; @@ -49,10 +49,10 @@ async function relationalQuery( user_activities AS ( select distinct w.session_id, - (${getDayDiffQuery( + ${getDayDiffQuery( getDateQuery('created_at', unit, timezone), 'c.cohort_date', - )}) as day_number + )} as day_number from website_event w join cohort_items c on w.session_id = c.session_id @@ -81,7 +81,7 @@ async function relationalQuery( c.day_number as day, s.visitors, c.visitors as "returnVisitors", - c.visitors::float * 100 / s.visitors as percentage + ${getCastColumnQuery('c.visitors', 'float')} * 100 / s.visitors as percentage from cohort_date c join cohort_size s on c.cohort_date = s.cohort_date @@ -92,7 +92,9 @@ async function relationalQuery( startDate, endDate, }, - ); + ).then(results => { + return results.map(i => ({ ...i, percentage: Number(i.percentage) || 0 })); + }); } async function clickhouseQuery(