Skip to content

Commit

Permalink
Merge pull request #2380 from Maxime-J/stats
Browse files Browse the repository at this point in the history
Match cloud stats in self hosted
  • Loading branch information
mikecao authored Dec 1, 2023
2 parents cde85d7 + 76a5ac8 commit 5c8941b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const POSTGRESQL_DATE_FORMATS = {
year: 'YYYY-01-01',
};

function getAddMinutesQuery(field: string, minutes: number): string {
function getAddIntervalQuery(field: string, interval: string): string {
const db = getDatabaseType(process.env.DATABASE_URL);

if (db === POSTGRESQL) {
return `${field} + interval '${minutes} minute'`;
return `${field} + interval '${interval}'`;
}

if (db === MYSQL) {
return `DATE_ADD(${field}, interval ${minutes} minute)`;
return `DATE_ADD(${field}, interval ${interval})`;
}
}

Expand Down Expand Up @@ -80,15 +80,15 @@ function getDateQuery(field: string, unit: string, timezone?: string): string {
}
}

function getTimestampIntervalQuery(field: string): string {
function getTimestampDiffQuery(field1: string, field2: string): string {
const db = getDatabaseType();

if (db === POSTGRESQL) {
return `floor(extract(epoch from max(${field}) - min(${field})))`;
return `floor(extract(epoch from (${field2} - ${field1})))`;
}

if (db === MYSQL) {
return `floor(unix_timestamp(max(${field})) - unix_timestamp(min(${field})))`;
return `timestampdiff(second, ${field1}, ${field2})`;
}
}

Expand Down Expand Up @@ -216,11 +216,11 @@ function getSearchMode(): { mode?: Prisma.QueryMode } {

export default {
...prisma,
getAddMinutesQuery,
getAddIntervalQuery,
getDayDiffQuery,
getCastColumnQuery,
getDateQuery,
getTimestampIntervalQuery,
getTimestampDiffQuery,
getFilterQuery,
parseFilters,
getPageFilters,
Expand Down
14 changes: 9 additions & 5 deletions src/queries/analytics/getWebsiteStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export async function getWebsiteStats(...args: [websiteId: string, filters: Quer
}

async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { getDateQuery, getTimestampIntervalQuery, parseFilters, rawQuery } = prisma;
const { getDateQuery, getAddIntervalQuery, getTimestampDiffQuery, parseFilters, rawQuery } =
prisma;
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
...filters,
eventType: EVENT_TYPE.pageView,
Expand All @@ -24,13 +25,16 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
sum(t.c) as "pageviews",
count(distinct t.session_id) as "uniques",
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
sum(t.time) as "totaltime"
sum(case when t.max_time < ${getAddIntervalQuery('t.min_time', '1 hour')}
then ${getTimestampDiffQuery('t.min_time', 't.max_time')}
else 0 end) as "totaltime"
from (
select
website_event.session_id,
${getDateQuery('website_event.created_at', 'hour')},
count(*) as c,
${getTimestampIntervalQuery('website_event.created_at')} as "time"
${getDateQuery('website_event.created_at', 'day')},
count(*) as "c",
min(website_event.created_at) as "min_time",
max(website_event.created_at) as "max_time"
from website_event
join website
on website_event.website_id = website.website_id
Expand Down
6 changes: 3 additions & 3 deletions src/queries/analytics/reports/getFunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function relationalQuery(
}[]
> {
const { windowMinutes, startDate, endDate, urls } = criteria;
const { rawQuery, getAddMinutesQuery } = prisma;
const { rawQuery, getAddIntervalQuery } = prisma;
const { levelQuery, sumQuery } = getFunnelQuery(urls, windowMinutes);

function getFunnelQuery(
Expand All @@ -58,9 +58,9 @@ async function relationalQuery(
join website_event we
on l.session_id = we.session_id
where we.website_id = {{websiteId::uuid}}
and we.created_at between l.created_at and ${getAddMinutesQuery(
and we.created_at between l.created_at and ${getAddIntervalQuery(
`l.created_at `,
windowMinutes,
`${windowMinutes} minute`,
)}
and we.referrer_path = {{${i - 1}}}
and we.url_path = {{${i}}}
Expand Down

0 comments on commit 5c8941b

Please sign in to comment.