Add support for MySQL.

This commit is contained in:
Mike Cao 2020-08-11 20:05:40 -07:00
parent e309376150
commit a248f35db2
9 changed files with 302 additions and 102 deletions

View file

@ -1,5 +1,6 @@
import { PrismaClient } from '@prisma/client';
import chalk from 'chalk';
import { getMetricsQuery, getPageviewsQuery, getRankingsQuery } from 'lib/queries';
const options = {
log: [
@ -236,21 +237,7 @@ export async function getPageviews(website_id, start_at, end_at) {
}
export async function getRankings(website_id, start_at, end_at, type, table) {
return runQuery(
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,
),
);
return getRankingsQuery(prisma, { website_id, start_at, end_at, type, table });
}
export async function getPageviewData(
@ -262,42 +249,10 @@ export async function getPageviewData(
count = '*',
) {
return runQuery(
prisma.queryRaw(
`
select date_trunc('${unit}', created_at at time zone '${timezone}') 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,
),
getPageviewsQuery(prisma, { website_id, start_at, end_at, timezone, unit, count }),
);
}
export async function getMetrics(website_id, start_at, end_at) {
return runQuery(
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=${website_id}
and created_at between '${start_at}' and '${end_at}'
group by 1, 2
) t;
`,
),
);
return getMetricsQuery(prisma, { website_id, start_at, end_at });
}