From 78c34536393428048fd3c2eec3b25fe34b6d403d Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Fri, 23 Sep 2022 22:43:51 -0700 Subject: [PATCH] fix clickhouse calls (#1536) --- .gitignore | 3 -- pages/api/website/[id]/pageviews.js | 8 ++--- pages/api/website/[id]/stats.js | 36 +++++++++++-------- queries/analytics/event/getEventMetrics.js | 3 +- queries/analytics/event/getEvents.js | 7 ++-- .../analytics/pageview/getPageviewMetrics.js | 2 +- .../analytics/pageview/getPageviewStats.js | 6 ++-- queries/analytics/pageview/getPageviews.js | 4 +-- queries/analytics/session/getSessions.js | 2 +- queries/analytics/stats/getWebsiteStats.js | 4 +-- 10 files changed, 41 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 0bad6264..54410324 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,3 @@ yarn-error.log* *.dev.yml -# cert -/lib/cert - diff --git a/pages/api/website/[id]/pageviews.js b/pages/api/website/[id]/pageviews.js index 4277250f..f2e11061 100644 --- a/pages/api/website/[id]/pageviews.js +++ b/pages/api/website/[id]/pageviews.js @@ -27,8 +27,8 @@ export default async (req, res) => { const [pageviews, sessions] = await Promise.all([ getPageviewStats(websiteId, { - startDate, - endDate, + start_at: startDate, + end_at: endDate, tz, unit, count: '*', @@ -42,8 +42,8 @@ export default async (req, res) => { }, }), getPageviewStats(websiteId, { - startDate, - endDate, + start_at: startDate, + end_at: endDate, tz, unit, count: 'distinct pageview.', diff --git a/pages/api/website/[id]/stats.js b/pages/api/website/[id]/stats.js index d652c048..a7741af5 100644 --- a/pages/api/website/[id]/stats.js +++ b/pages/api/website/[id]/stats.js @@ -21,21 +21,29 @@ export default async (req, res) => { const prevStartDate = new Date(+start_at - distance); const prevEndDate = new Date(+end_at - distance); - const metrics = await getWebsiteStats(websiteId, startDate, endDate, { - url, - referrer, - os, - browser, - device, - country, + const metrics = await getWebsiteStats(websiteId, { + start_at: startDate, + end_at: endDate, + filters: { + url, + referrer, + os, + browser, + device, + country, + }, }); - const prevPeriod = await getWebsiteStats(websiteId, prevStartDate, prevEndDate, { - url, - referrer, - os, - browser, - device, - country, + const prevPeriod = await getWebsiteStats(websiteId, { + start_at: prevStartDate, + end_at: prevEndDate, + filters: { + url, + referrer, + os, + browser, + device, + country, + }, }); const stats = Object.keys(metrics[0]).reduce((obj, key) => { diff --git a/queries/analytics/event/getEventMetrics.js b/queries/analytics/event/getEventMetrics.js index 4a828e15..c37f1b39 100644 --- a/queries/analytics/event/getEventMetrics.js +++ b/queries/analytics/event/getEventMetrics.js @@ -52,7 +52,8 @@ async function clickhouseQuery( ${getDateQuery('created_at', unit, timezone)} t, count(*) y from event - where website_id= $1 + where event_name != '' + and website_id= $1 and ${getBetweenDates('created_at', start_at, end_at)} ${getFilterQuery('event', filters, params)} group by x, t diff --git a/queries/analytics/event/getEvents.js b/queries/analytics/event/getEvents.js index 41f8da70..441eed64 100644 --- a/queries/analytics/event/getEvents.js +++ b/queries/analytics/event/getEvents.js @@ -31,12 +31,13 @@ function clickhouseQuery(websites, start_at) { `select event_uuid, website_id, - session_id, + session_uuid, created_at, url, event_name from event - where website_id in (${websites.join[',']} - and created_at >= ${getDateFormat(start_at)})`, + where event_name != '' + and ${websites && websites.length > 0 ? `website_id in (${websites.join(',')})` : '0 = 0'} + and created_at >= ${getDateFormat(start_at)}`, ); } diff --git a/queries/analytics/pageview/getPageviewMetrics.js b/queries/analytics/pageview/getPageviewMetrics.js index 4d0f1455..bea3502c 100644 --- a/queries/analytics/pageview/getPageviewMetrics.js +++ b/queries/analytics/pageview/getPageviewMetrics.js @@ -43,7 +43,7 @@ async function clickhouseQuery(website_id, { startDate, endDate, column, filters `select ${column} x, count(*) y from event where website_id= $1 - ${column !== 'event_name' ? `and event_name = ''` : ''} + ${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`} and ${getBetweenDates('created_at', startDate, endDate)} ${pageviewQuery} ${sessionQuery} diff --git a/queries/analytics/pageview/getPageviewStats.js b/queries/analytics/pageview/getPageviewStats.js index 1cf4aba6..4f74cf98 100644 --- a/queries/analytics/pageview/getPageviewStats.js +++ b/queries/analytics/pageview/getPageviewStats.js @@ -59,10 +59,10 @@ async function clickhouseQuery( from (select ${getDateQuery('created_at', unit, timezone)} t, - count(${count !== '*' ? 'session_uuid' : count}) y + count(${count !== '*' ? 'distinct session_uuid' : count}) y from event - where website_id= $1 - + where event_name = '' + and website_id= $1 and ${getBetweenDates('created_at', start_at, end_at)} ${pageviewQuery} ${sessionQuery} diff --git a/queries/analytics/pageview/getPageviews.js b/queries/analytics/pageview/getPageviews.js index 2bbfff69..bc909b72 100644 --- a/queries/analytics/pageview/getPageviews.js +++ b/queries/analytics/pageview/getPageviews.js @@ -33,7 +33,7 @@ async function clickhouseQuery(websites, start_at) { url from event where event_name = '' - and website_id in (${websites.join[',']} - and created_at >= ${clickhouse.getDateFormat(start_at)})`, + and ${websites && websites.length > 0 ? `website_id in (${websites.join(',')})` : '0 = 0'} + and created_at >= ${clickhouse.getDateFormat(start_at)}`, ); } diff --git a/queries/analytics/session/getSessions.js b/queries/analytics/session/getSessions.js index 16dd4047..7c47ce70 100644 --- a/queries/analytics/session/getSessions.js +++ b/queries/analytics/session/getSessions.js @@ -44,7 +44,7 @@ async function clickhouseQuery(websites, start_at) { language, country from event - where ${websites && websites.length > 0 ? `(website_id in (${websites.join[',']})` : '0 = 0'} + where ${websites && websites.length > 0 ? `website_id in (${websites.join(',')})` : '0 = 0'} and created_at >= ${getDateFormat(start_at)}`, ); } diff --git a/queries/analytics/stats/getWebsiteStats.js b/queries/analytics/stats/getWebsiteStats.js index 6e2693a1..0c3d432a 100644 --- a/queries/analytics/stats/getWebsiteStats.js +++ b/queries/analytics/stats/getWebsiteStats.js @@ -9,7 +9,7 @@ export async function getWebsiteStats(...args) { }); } -async function relationalQuery(website_id, start_at, end_at, filters = {}) { +async function relationalQuery(website_id, { start_at, end_at, filters = {} }) { const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma; const params = [website_id, start_at, end_at]; const { pageviewQuery, sessionQuery, joinSession } = parseFilters( @@ -41,7 +41,7 @@ async function relationalQuery(website_id, start_at, end_at, filters = {}) { ); } -async function clickhouseQuery(website_id, start_at, end_at, filters = {}) { +async function clickhouseQuery(website_id, { start_at, end_at, filters = {} }) { const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse; const params = [website_id]; const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);