feat: add filter

This commit is contained in:
moonrailgun 2023-09-16 15:50:36 +08:00
parent 122c1d967b
commit 90166346e8
3 changed files with 21 additions and 16 deletions

View File

@ -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
`;
}

View File

@ -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';

View File

@ -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<string, any> = {}) {
export function getFilterQuery(
filters: QueryFilters = {},
options: QueryOptions = {}
): string {
options: QueryOptions = {},
websiteDomain: string | null = null
) {
const query = Object.keys(filters).reduce<string[]>((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 '';
}