From 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b Mon Sep 17 00:00:00 2001 From: Fuwn <50817549+Fuwn@users.noreply.github.com> Date: Sat, 24 Jan 2026 13:09:50 +0000 Subject: Initial commit Created from https://vercel.com/new --- src/queries/sql/getValues.ts | 129 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/queries/sql/getValues.ts (limited to 'src/queries/sql/getValues.ts') diff --git a/src/queries/sql/getValues.ts b/src/queries/sql/getValues.ts new file mode 100644 index 0000000..cc6bb7d --- /dev/null +++ b/src/queries/sql/getValues.ts @@ -0,0 +1,129 @@ +import clickhouse from '@/lib/clickhouse'; +import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; +import prisma from '@/lib/prisma'; +import type { QueryFilters } from '@/lib/types'; + +const FUNCTION_NAME = 'getValues'; + +export async function getValues( + ...args: [websiteId: string, column: string, filters: QueryFilters] +) { + return runQuery({ + [PRISMA]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), + }); +} + +async function relationalQuery(websiteId: string, column: string, filters: QueryFilters) { + const { rawQuery, getSearchSQL } = prisma; + const params = {}; + const { startDate, endDate, search } = filters; + + let searchQuery = ''; + let excludeDomain = ''; + + if (column === 'referrer_domain') { + excludeDomain = `and website_event.referrer_domain != website_event.hostname + and website_event.referrer_domain != ''`; + } + + if (search) { + if (decodeURIComponent(search).includes(',')) { + searchQuery = `AND (${decodeURIComponent(search) + .split(',') + .slice(0, 5) + .map((value: string, index: number) => { + const key = `search${index}`; + + params[key] = value; + + return getSearchSQL(column, key).replace('and ', ''); + }) + .join(' OR ')})`; + } else { + searchQuery = getSearchSQL(column); + } + } + + return rawQuery( + ` + select ${column} as "value", count(*) as "count" + from website_event + inner join session + on session.session_id = website_event.session_id + and session.website_id = website_event.website_id + where website_event.website_id = {{websiteId::uuid}} + and website_event.created_at between {{startDate}} and {{endDate}} + ${searchQuery} + ${excludeDomain} + group by 1 + order by 2 desc + limit 10 + `, + { + websiteId, + startDate, + endDate, + search: `%${search}%`, + ...params, + }, + FUNCTION_NAME, + ); +} + +async function clickhouseQuery(websiteId: string, column: string, filters: QueryFilters) { + const { rawQuery, getSearchSQL } = clickhouse; + const params = {}; + const { startDate, endDate, search } = filters; + + let searchQuery = ''; + let excludeDomain = ''; + + if (column === 'referrer_domain') { + excludeDomain = `and referrer_domain != hostname and referrer_domain != ''`; + } + + if (search) { + searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`; + } + + if (search) { + if (decodeURIComponent(search).includes(',')) { + searchQuery = `AND (${decodeURIComponent(search) + .split(',') + .slice(0, 5) + .map((value: string, index: number) => { + const key = `search${index}`; + + params[key] = value; + + return getSearchSQL(column, key).replace('and ', ''); + }) + .join(' OR ')})`; + } else { + searchQuery = getSearchSQL(column); + } + } + + return rawQuery( + ` + select ${column} as "value", count(*) as "count" + from website_event + where website_id = {websiteId:UUID} + and created_at between {startDate:DateTime64} and {endDate:DateTime64} + ${searchQuery} + ${excludeDomain} + group by 1 + order by 2 desc + limit 10 + `, + { + websiteId, + startDate, + endDate, + search, + ...params, + }, + FUNCTION_NAME, + ); +} -- cgit v1.2.3