diff --git a/src/server/model/workspace.ts b/src/server/model/workspace.ts index fb9a4ef..47e1156 100644 --- a/src/server/model/workspace.ts +++ b/src/server/model/workspace.ts @@ -121,9 +121,11 @@ export async function getWorkspaceWebsitePageviewStats( from "WebsiteEvent" ${joinSession ? Prisma.sql([joinSession]) : Prisma.empty} where "WebsiteEvent"."websiteId" = ${params.websiteId}::uuid - and "WebsiteEvent"."createdAt" - between ${params.startDate}::timestamptz and ${params.endDate}::timestamptz + and "WebsiteEvent"."createdAt" between ${ + params.startDate + }::timestamptz and ${params.endDate}::timestamptz and "WebsiteEvent"."eventType" = ${EVENT_TYPE.pageView} + ${filterQuery} group by 1 `; } diff --git a/src/server/utils/const.ts b/src/server/utils/const.ts index a947285..8b87d8f 100644 --- a/src/server/utils/const.ts +++ b/src/server/utils/const.ts @@ -96,10 +96,10 @@ export const OPERATORS = { } as const; export const FILTER_COLUMNS = { - url: 'url_path', - referrer: 'referrer_domain', - title: 'page_title', - query: 'url_query', + url: 'urlPath', + referrer: 'referrerDomain', + title: 'pageTitle', + query: 'urlQuery', os: 'os', browser: 'browser', device: 'device', @@ -107,7 +107,7 @@ export const FILTER_COLUMNS = { region: 'subdivision1', city: 'city', language: 'language', - event: 'event_name', + event: 'eventName', }; export const DEFAULT_RESET_DATE = '2000-01-01'; diff --git a/src/server/utils/prisma.ts b/src/server/utils/prisma.ts index 8c22c52..c95605e 100644 --- a/src/server/utils/prisma.ts +++ b/src/server/utils/prisma.ts @@ -49,6 +49,8 @@ export async function parseFilters( throw new Error('Not found website'); } + const websiteDomain = website.domain; + return { joinSession: options?.joinSession || @@ -58,7 +60,7 @@ export async function parseFilters( ) ? `inner join "WebsiteSession" on "WebsiteEvent"."sessionId" = "WebsiteSession"."id"` : '', - filterQuery: getFilterQuery(filters, options), + filterQuery: getFilterQuery(filters, options, websiteDomain), params: { ...normalizeFilters(filters), websiteId, @@ -68,7 +70,7 @@ export async function parseFilters( endDate: filters.endDate ? dayjs(filters.endDate).toISOString() : undefined, - websiteDomain: website.domain, + websiteDomain, }, }; } @@ -85,8 +87,9 @@ function normalizeFilters(filters: Record = {}) { export function getFilterQuery( filters: QueryFilters = {}, - options: QueryOptions = {} -): string { + options: QueryOptions = {}, + websiteDomain: string | null = null +) { const query = Object.keys(filters).reduce((arr, name) => { const value: any = filters[name as keyof QueryFilters]; const operator = value?.filter ?? OPERATORS.equals; @@ -95,11 +98,11 @@ export function getFilterQuery( // TODO if (value !== undefined && column) { - arr.push(`and ${mapFilter(column, operator, name)}`); + arr.push(`AND ${mapFilter(column, operator, name)}`); if (name === 'referrer') { arr.push( - 'and (website_event.referrer_domain != {{websiteDomain}} or website_event.referrer_domain is null)' + `AND ("WebsiteEvent"."referrerDomain" != ${websiteDomain} or "WebsiteEvent"."referrerDomain" is null)` ); } } @@ -107,7 +110,7 @@ export function getFilterQuery( return arr; }, []); - return query.join('\n'); + return Prisma.sql([query.join('\n')]); } function mapFilter( @@ -118,9 +121,9 @@ function mapFilter( ) { switch (operator) { case OPERATORS.equals: - return `${column} = {{${name}::${type}}}`; + return `"${column}" = '${name}'::${type}`; case OPERATORS.notEquals: - return `${column} != {{${name}::${type}}}`; + return `"${column}" != '${name}'::${type}`; default: return ''; }