forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
303 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import moment from 'moment-timezone'; | ||
|
||
const POSTGRESQL = 'postgresql'; | ||
const MYSQL = 'mysql'; | ||
|
||
export function getDatabase() { | ||
return process.env.DATABASE_URL.split(':')[0]; | ||
} | ||
|
||
export function getMetricsQuery(prisma, { website_id, start_at, end_at }) { | ||
const db = getDatabase(); | ||
|
||
if (db === POSTGRESQL) { | ||
return prisma.$queryRaw( | ||
` | ||
select sum(t.c) as "pageviews", | ||
count(distinct t.session_id) as "uniques", | ||
sum(case when t.c = 1 then t.c else 0 end) as "bounces", | ||
sum(t.time) as "totaltime" | ||
from ( | ||
select session_id, | ||
date_trunc('hour', created_at), | ||
count(*) c, | ||
floor(extract(epoch from max(created_at) - min(created_at))) as "time" | ||
from pageview | ||
where website_id=$1 | ||
and created_at between $2 and $3 | ||
group by 1, 2 | ||
) t | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
if (db === MYSQL) { | ||
return prisma.$queryRaw( | ||
` | ||
select sum(t.c) as "pageviews", | ||
count(distinct t.session_id) as "uniques", | ||
sum(case when t.c = 1 then t.c else 0 end) as "bounces", | ||
sum(t.time) as "totaltime" | ||
from ( | ||
select session_id, | ||
date_trunc('hour', created_at), | ||
count(*) c, | ||
floor(unix_timestamp(max(created_at)) - unix_timestamp(min(created_at))) as "time" | ||
from pageview | ||
where website_id=? | ||
and created_at between ? and ? | ||
group by 1, 2 | ||
) t | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
return Promise.resolve({}); | ||
} | ||
|
||
export function getPageviewsQuery(prisma, { website_id, start_at, end_at, unit, timezone, count }) { | ||
const db = getDatabase(); | ||
|
||
if (db === POSTGRESQL) { | ||
return prisma.$queryRaw( | ||
` | ||
select date_trunc('${unit}', created_at at time zone '${timezone}') t, | ||
count(${count}) y | ||
from pageview | ||
where website_id=$1 | ||
and created_at between $2 and $3 | ||
group by 1 | ||
order by 1 | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
if (db === MYSQL) { | ||
const tz = moment.tz(timezone).format('Z'); | ||
return prisma.$queryRaw( | ||
` | ||
select date_trunc('${unit}', convert_tz(created_at,'+00:00','${tz}')) t, | ||
count(${count}) y | ||
from pageview | ||
where website_id=? | ||
and created_at between ? and ? | ||
group by 1 | ||
order by 1 | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
return Promise.resolve([]); | ||
} | ||
|
||
export function getRankingsQuery(prisma, { website_id, start_at, end_at, type, table }) { | ||
const db = getDatabase(); | ||
|
||
if (db === POSTGRESQL) { | ||
return prisma.$queryRaw( | ||
` | ||
select distinct ${type} x, count(*) y | ||
from ${table} | ||
where website_id=$1 | ||
and created_at between $2 and $3 | ||
group by 1 | ||
order by 2 desc | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
if (db === MYSQL) { | ||
return prisma.$queryRaw( | ||
` | ||
select distinct ${type} x, count(*) y | ||
from ${table} | ||
where website_id=? | ||
and created_at between ? and ? | ||
group by 1 | ||
order by 2 desc | ||
`, | ||
website_id, | ||
start_at, | ||
end_at, | ||
); | ||
} | ||
|
||
return Promise.resolve([]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.