Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix mysql retention query

Francis Cao495b7f1f2011
diff --git a/lib/prisma.ts b/lib/prisma.ts
index f392252e9..5a21a6ec6 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 12ef1d529..3abc0e7ae 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(