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.

add daydiff function query

Francis Cao979ea33d7457
diff --git a/lib/prisma.ts b/lib/prisma.ts
index efce3f4eb..94fcf0a08 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<any>): [
export default {
...prisma,
getAddMinutesQuery,
+ getDayDiffQuery,
getDateQuery,
getTimestampIntervalQuery,
getFilterQuery,
diff --git a/queries/analytics/reports/getRetention.ts b/queries/analytics/reports/getRetention.ts
index ee7e4619b..12ef1d529 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