PR comments
parent
1c26581cd1
commit
f569055771
14
lib/db.js
14
lib/db.js
|
@ -29,7 +29,7 @@ function logQuery(e) {
|
||||||
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializePrisma(options) {
|
function getPrismaClient(options) {
|
||||||
const prisma = new PrismaClient(options);
|
const prisma = new PrismaClient(options);
|
||||||
|
|
||||||
if (process.env.LOG_QUERY) {
|
if (process.env.LOG_QUERY) {
|
||||||
|
@ -39,7 +39,7 @@ function initializePrisma(options) {
|
||||||
return prisma;
|
return prisma;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeClickhouse() {
|
function getClickhouseClient() {
|
||||||
if (!process.env.ANALYTICS_URL) {
|
if (!process.env.ANALYTICS_URL) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -63,8 +63,8 @@ function initializeClickhouse() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const prisma = global.prisma || initializePrisma(options);
|
const prisma = global.prisma || getPrismaClient(options);
|
||||||
const clickhouse = global.clickhouse || initializeClickhouse();
|
const clickhouse = global.clickhouse || getClickhouseClient();
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
global.prisma = prisma;
|
global.prisma = prisma;
|
||||||
|
@ -142,15 +142,15 @@ export function getBetweenDatesClickhouse(field, start_at, end_at) {
|
||||||
and ${getDateFormatClickhouse(end_at)}`;
|
and ${getDateFormatClickhouse(end_at)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTimestampInterval(maxColumn, minColumn) {
|
export function getTimestampInterval(field) {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
if (db === POSTGRESQL) {
|
if (db === POSTGRESQL) {
|
||||||
return `floor(extract(epoch from (${maxColumn}) - (${minColumn})))`;
|
return `floor(extract(epoch from max(${field}) - min(${field})))`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db === MYSQL) {
|
if (db === MYSQL) {
|
||||||
return `floor(unix_timestamp(${maxColumn}) - unix_timestamp(${minColumn}))`;
|
return `floor(unix_timestamp(max(${field})) - unix_timestamp(min(${field})))`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ async function relationalQuery(
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
||||||
count(${count != '*' ? `${count}${sessionKey}` : count}) y
|
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||||
from pageview
|
from pageview
|
||||||
${joinSession}
|
${joinSession}
|
||||||
where pageview.website_id=$1
|
where pageview.website_id=$1
|
||||||
|
@ -78,7 +78,7 @@ async function clickhouseQuery(
|
||||||
from
|
from
|
||||||
(select
|
(select
|
||||||
${getDateQueryClickhouse('created_at', unit, timezone)} t,
|
${getDateQueryClickhouse('created_at', unit, timezone)} t,
|
||||||
count(${count != '*' ? `${count}${sessionKey}` : count}) y
|
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||||
from pageview
|
from pageview
|
||||||
${joinSession}
|
${joinSession}
|
||||||
where pageview.website_id= $1
|
where pageview.website_id= $1
|
||||||
|
|
|
@ -31,24 +31,19 @@ async function relationalQuery(website_id, start_at, end_at, filters = {}) {
|
||||||
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",
|
||||||
sum(case when m2 < m1 + interval '1 hour' then ${getTimestampInterval(
|
sum(t.time) as "totaltime"
|
||||||
'm2',
|
|
||||||
'm1',
|
|
||||||
)} else 0 end) as "totaltime"
|
|
||||||
from (
|
from (
|
||||||
select
|
select pageview.session_id,
|
||||||
pageview.session_id,
|
${getDateQuery('pageview.created_at', 'hour')},
|
||||||
${getDateQuery('pageview.created_at', 'hour')},
|
count(*) c,
|
||||||
count(*) c,
|
${getTimestampInterval('pageview.created_at')} as "time"
|
||||||
min(created_at) m1,
|
from pageview
|
||||||
max(created_at) m2
|
${joinSession}
|
||||||
from pageview
|
where pageview.website_id=$1
|
||||||
${joinSession}
|
and pageview.created_at between $2 and $3
|
||||||
where pageview.website_id=$1
|
${pageviewQuery}
|
||||||
and pageview.created_at between $2 and $3
|
${sessionQuery}
|
||||||
${pageviewQuery}
|
group by 1, 2
|
||||||
${sessionQuery}
|
|
||||||
group by 1, 2
|
|
||||||
) t
|
) t
|
||||||
`,
|
`,
|
||||||
params,
|
params,
|
||||||
|
|
Loading…
Reference in New Issue