From f17be191100d25d5c5a0d56aa392f6e08fb3adf4 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 2 Sep 2020 15:14:33 -0700 Subject: [PATCH] All queries should use runQuery. --- lib/queries.js | 116 +++++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/lib/queries.js b/lib/queries.js index 204b545b..5b30c0a9 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -252,8 +252,9 @@ export function getMetrics(website_id, start_at, end_at) { const db = getDatabase(); if (db === POSTGRESQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select 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", @@ -269,15 +270,17 @@ export function getMetrics(website_id, start_at, end_at) { group by 1, 2 ) t `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } if (db === MYSQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select 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", @@ -293,9 +296,10 @@ export function getMetrics(website_id, start_at, end_at) { group by 1, 2 ) t `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } @@ -313,8 +317,9 @@ export function getPageviews( const db = getDatabase(); if (db === POSTGRESQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select ${getDateQuery(db, 'created_at', unit, timezone)} t, count(${count}) y from pageview @@ -323,15 +328,17 @@ export function getPageviews( group by 1 order by 1 `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } if (db === MYSQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select ${getDateQuery(db, 'created_at', unit, timezone)} t, count(${count}) y from pageview @@ -340,9 +347,10 @@ export function getPageviews( group by 1 order by 1 `, - website_id, - start_at, - end_at, + website_id, + start_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}%'` : ''; if (db === POSTGRESQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select distinct ${type} x, count(*) y from ${table} where website_id=$1 @@ -365,15 +374,17 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) { group by 1 order by 2 desc `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } if (db === MYSQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select distinct ${type} x, count(*) y from ${table} where website_id=? @@ -382,9 +393,10 @@ export function getRankings(website_id, start_at, end_at, type, table, domain) { group by 1 order by 2 desc `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } @@ -396,28 +408,32 @@ export function getActiveVisitors(website_id) { const date = subMinutes(new Date(), 5); if (db === POSTGRESQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select count(distinct session_id) x from pageview where website_id=$1 and created_at >= $2 `, - website_id, - date, + website_id, + date, + ), ); } if (db === MYSQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select count(distinct session_id) x from pageview where website_id=? and created_at >= ? `, - website_id, - date, + website_id, + date, + ), ); } @@ -428,8 +444,9 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit = const db = getDatabase(); if (db === POSTGRESQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select event_value x, ${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 order by 2 `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); } if (db === MYSQL) { - return prisma.$queryRaw( - ` + return runQuery( + prisma.$queryRaw( + ` select event_value x, ${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 order by 2 `, - website_id, - start_at, - end_at, + website_id, + start_at, + end_at, + ), ); }