From f189035a7326da638b753d2af5f2709db0d41743 Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Mon, 25 Jul 2022 09:47:11 -0700 Subject: [PATCH] fix CH return. --- lib/db.js | 12 ++++++++++-- lib/session.js | 2 -- queries/analytics/event/getEventMetrics.js | 4 ++-- queries/analytics/event/getEvents.js | 4 ++-- queries/analytics/event/saveEvent.js | 4 ++-- queries/analytics/pageview/getPageviewMetrics.js | 4 ++-- queries/analytics/pageview/getPageviewParams.js | 4 ++-- queries/analytics/pageview/getPageviewStats.js | 4 ++-- queries/analytics/pageview/getPageviews.js | 4 ++-- queries/analytics/pageview/savePageView.js | 4 ++-- queries/analytics/session/createSession.js | 4 ++-- queries/analytics/session/getSessionByUuid.js | 8 ++++---- queries/analytics/session/getSessionMetrics.js | 4 ++-- queries/analytics/session/getSessions.js | 4 ++-- queries/analytics/stats/getActiveVisitors.js | 4 ++-- queries/analytics/stats/getWebsiteStats.js | 4 ++-- 16 files changed, 40 insertions(+), 34 deletions(-) diff --git a/lib/db.js b/lib/db.js index 378ca742..cb9237bb 100644 --- a/lib/db.js +++ b/lib/db.js @@ -277,14 +277,22 @@ export async function rawQueryClickhouse(query, params = [], debug = false) { return clickhouse.query(formattedQuery).toPromise(); } +export async function findUnique(data) { + if (data.length > 1) { + throw `${data.length} records found when expecting 1.`; + } + + return data[0] ?? null; +} + export async function runAnalyticsQuery(queries) { const db = getAnalyticsDatabase(); if (db === POSTGRESQL || db === MYSQL) { - return queries[`${RELATIONAL}`](); + return queries[RELATIONAL](); } if (db === CLICKHOUSE) { - return queries[`${CLICKHOUSE}`](); + return queries[CLICKHOUSE](); } } diff --git a/lib/session.js b/lib/session.js index d8b66e54..7c4476d6 100644 --- a/lib/session.js +++ b/lib/session.js @@ -37,8 +37,6 @@ export async function getSession(req) { let session = await getSessionByUuid(session_uuid); - session = Array.isArray(session) && session[0] ? session[0] : session; - if (!session) { try { session = await createSession(website_id, { diff --git a/queries/analytics/event/getEventMetrics.js b/queries/analytics/event/getEventMetrics.js index ff3fcf01..73f7e469 100644 --- a/queries/analytics/event/getEventMetrics.js +++ b/queries/analytics/event/getEventMetrics.js @@ -11,8 +11,8 @@ import { export async function getEventMetrics(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/event/getEvents.js b/queries/analytics/event/getEvents.js index 059c8014..fc5c0aeb 100644 --- a/queries/analytics/event/getEvents.js +++ b/queries/analytics/event/getEvents.js @@ -9,8 +9,8 @@ import { export function getEvents(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/event/saveEvent.js b/queries/analytics/event/saveEvent.js index abb32cbb..41c068c3 100644 --- a/queries/analytics/event/saveEvent.js +++ b/queries/analytics/event/saveEvent.js @@ -9,8 +9,8 @@ import { export async function saveEvent(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/pageview/getPageviewMetrics.js b/queries/analytics/pageview/getPageviewMetrics.js index e7447375..9d3eabaa 100644 --- a/queries/analytics/pageview/getPageviewMetrics.js +++ b/queries/analytics/pageview/getPageviewMetrics.js @@ -9,8 +9,8 @@ import { export async function getPageviewMetrics(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/pageview/getPageviewParams.js b/queries/analytics/pageview/getPageviewParams.js index fd25c77e..2273f11a 100644 --- a/queries/analytics/pageview/getPageviewParams.js +++ b/queries/analytics/pageview/getPageviewParams.js @@ -3,8 +3,8 @@ import { MYSQL, POSTGRESQL, CLICKHOUSE, RELATIONAL } from 'lib/constants'; export async function getPageviewParams(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/pageview/getPageviewStats.js b/queries/analytics/pageview/getPageviewStats.js index 0a320074..00ff857a 100644 --- a/queries/analytics/pageview/getPageviewStats.js +++ b/queries/analytics/pageview/getPageviewStats.js @@ -12,8 +12,8 @@ import { export async function getPageviewStats(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/pageview/getPageviews.js b/queries/analytics/pageview/getPageviews.js index d7b28611..45463ec8 100644 --- a/queries/analytics/pageview/getPageviews.js +++ b/queries/analytics/pageview/getPageviews.js @@ -9,8 +9,8 @@ import { export async function getPageviews(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/pageview/savePageView.js b/queries/analytics/pageview/savePageView.js index 72ab35fb..c165b56d 100644 --- a/queries/analytics/pageview/savePageView.js +++ b/queries/analytics/pageview/savePageView.js @@ -9,8 +9,8 @@ import { export async function savePageView(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/session/createSession.js b/queries/analytics/session/createSession.js index 22b41ebd..1af95fe6 100644 --- a/queries/analytics/session/createSession.js +++ b/queries/analytics/session/createSession.js @@ -10,8 +10,8 @@ import { getSessionByUuid } from 'queries'; export async function createSession(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/session/getSessionByUuid.js b/queries/analytics/session/getSessionByUuid.js index fde70e7f..2b9acc1e 100644 --- a/queries/analytics/session/getSessionByUuid.js +++ b/queries/analytics/session/getSessionByUuid.js @@ -1,10 +1,10 @@ import { CLICKHOUSE, RELATIONAL } from 'lib/constants'; -import { rawQueryClickhouse, prisma, runAnalyticsQuery, runQuery } from 'lib/db'; +import { rawQueryClickhouse, findUnique, prisma, runAnalyticsQuery, runQuery } from 'lib/db'; export async function getSessionByUuid(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } @@ -38,5 +38,5 @@ async function clickhouseQuery(session_uuid) { where session_uuid = $1 `, params, - ); + ).then(data => findUnique(data)); } diff --git a/queries/analytics/session/getSessionMetrics.js b/queries/analytics/session/getSessionMetrics.js index b9bbc708..7da60bda 100644 --- a/queries/analytics/session/getSessionMetrics.js +++ b/queries/analytics/session/getSessionMetrics.js @@ -9,8 +9,8 @@ import { export async function getSessionMetrics(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/session/getSessions.js b/queries/analytics/session/getSessions.js index 09b55b8d..bc48db1c 100644 --- a/queries/analytics/session/getSessions.js +++ b/queries/analytics/session/getSessions.js @@ -9,8 +9,8 @@ import { export async function getSessions(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/stats/getActiveVisitors.js b/queries/analytics/stats/getActiveVisitors.js index 04884521..aac99085 100644 --- a/queries/analytics/stats/getActiveVisitors.js +++ b/queries/analytics/stats/getActiveVisitors.js @@ -4,8 +4,8 @@ import { getDateFormatClickhouse, rawQuery, rawQueryClickhouse, runAnalyticsQuer export async function getActiveVisitors(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); } diff --git a/queries/analytics/stats/getWebsiteStats.js b/queries/analytics/stats/getWebsiteStats.js index 9328b85b..dc0380e0 100644 --- a/queries/analytics/stats/getWebsiteStats.js +++ b/queries/analytics/stats/getWebsiteStats.js @@ -12,8 +12,8 @@ import { export async function getWebsiteStats(...args) { return runAnalyticsQuery({ - [`${RELATIONAL}`]: () => relationalQuery(...args), - [`${CLICKHOUSE}`]: () => clickhouseQuery(...args), + [RELATIONAL]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), }); }