All queries should use runQuery.

This commit is contained in:
Mike Cao 2020-09-02 15:14:33 -07:00
parent a3e920d944
commit f17be19110

View File

@ -252,8 +252,9 @@ export function getMetrics(website_id, start_at, end_at) {
const db = getDatabase(); const db = getDatabase();
if (db === POSTGRESQL) { if (db === POSTGRESQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select sum(t.c) as "pageviews", select sum(t.c) as "pageviews",
count(distinct t.session_id) as "uniques", count(distinct t.session_id) as "uniques",
sum(case when t.c = 1 then 1 else 0 end) as "bounces", sum(case when t.c = 1 then 1 else 0 end) as "bounces",
@ -269,15 +270,17 @@ export function getMetrics(website_id, start_at, end_at) {
group by 1, 2 group by 1, 2
) t ) t
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
if (db === MYSQL) { if (db === MYSQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select sum(t.c) as "pageviews", select sum(t.c) as "pageviews",
count(distinct t.session_id) as "uniques", count(distinct t.session_id) as "uniques",
sum(case when t.c = 1 then 1 else 0 end) as "bounces", sum(case when t.c = 1 then 1 else 0 end) as "bounces",
@ -293,9 +296,10 @@ export function getMetrics(website_id, start_at, end_at) {
group by 1, 2 group by 1, 2
) t ) t
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
@ -313,8 +317,9 @@ export function getPageviews(
const db = getDatabase(); const db = getDatabase();
if (db === POSTGRESQL) { if (db === POSTGRESQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select ${getDateQuery(db, 'created_at', unit, timezone)} t, select ${getDateQuery(db, 'created_at', unit, timezone)} t,
count(${count}) y count(${count}) y
from pageview from pageview
@ -323,15 +328,17 @@ export function getPageviews(
group by 1 group by 1
order by 1 order by 1
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
if (db === MYSQL) { if (db === MYSQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select ${getDateQuery(db, 'created_at', unit, timezone)} t, select ${getDateQuery(db, 'created_at', unit, timezone)} t,
count(${count}) y count(${count}) y
from pageview from pageview
@ -340,9 +347,10 @@ export function getPageviews(
group by 1 group by 1
order by 1 order by 1
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
@ -355,8 +363,9 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) {
const filter = domain ? `and ${type} not like '%${domain}%'` : ''; const filter = domain ? `and ${type} not like '%${domain}%'` : '';
if (db === POSTGRESQL) { if (db === POSTGRESQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select distinct ${type} x, count(*) y select distinct ${type} x, count(*) y
from ${table} from ${table}
where website_id=$1 where website_id=$1
@ -365,15 +374,17 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) {
group by 1 group by 1
order by 2 desc order by 2 desc
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
if (db === MYSQL) { if (db === MYSQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select distinct ${type} x, count(*) y select distinct ${type} x, count(*) y
from ${table} from ${table}
where website_id=? where website_id=?
@ -382,9 +393,10 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) {
group by 1 group by 1
order by 2 desc order by 2 desc
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
@ -396,28 +408,32 @@ export function getActiveVisitors(website_id) {
const date = subMinutes(new Date(), 5); const date = subMinutes(new Date(), 5);
if (db === POSTGRESQL) { if (db === POSTGRESQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select count(distinct session_id) x select count(distinct session_id) x
from pageview from pageview
where website_id=$1 where website_id=$1
and created_at >= $2 and created_at >= $2
`, `,
website_id, website_id,
date, date,
),
); );
} }
if (db === MYSQL) { if (db === MYSQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select count(distinct session_id) x select count(distinct session_id) x
from pageview from pageview
where website_id=? where website_id=?
and created_at >= ? and created_at >= ?
`, `,
website_id, website_id,
date, date,
),
); );
} }
@ -428,8 +444,9 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit =
const db = getDatabase(); const db = getDatabase();
if (db === POSTGRESQL) { if (db === POSTGRESQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select select
event_value x, event_value x,
${getDateQuery(db, 'created_at', unit, timezone)} t, ${getDateQuery(db, 'created_at', unit, timezone)} t,
@ -440,15 +457,17 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit =
group by 1, 2 group by 1, 2
order by 2 order by 2
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }
if (db === MYSQL) { if (db === MYSQL) {
return prisma.$queryRaw( return runQuery(
` prisma.$queryRaw(
`
select select
event_value x, event_value x,
${getDateQuery(db, 'created_at', unit, timezone)} t, ${getDateQuery(db, 'created_at', unit, timezone)} t,
@ -459,9 +478,10 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit =
group by 1, 2 group by 1, 2
order by 2 order by 2
`, `,
website_id, website_id,
start_at, start_at,
end_at, end_at,
),
); );
} }