feat: add filter
This commit is contained in:
parent
122c1d967b
commit
90166346e8
@ -121,9 +121,11 @@ export async function getWorkspaceWebsitePageviewStats(
|
|||||||
from "WebsiteEvent"
|
from "WebsiteEvent"
|
||||||
${joinSession ? Prisma.sql([joinSession]) : Prisma.empty}
|
${joinSession ? Prisma.sql([joinSession]) : Prisma.empty}
|
||||||
where "WebsiteEvent"."websiteId" = ${params.websiteId}::uuid
|
where "WebsiteEvent"."websiteId" = ${params.websiteId}::uuid
|
||||||
and "WebsiteEvent"."createdAt"
|
and "WebsiteEvent"."createdAt" between ${
|
||||||
between ${params.startDate}::timestamptz and ${params.endDate}::timestamptz
|
params.startDate
|
||||||
|
}::timestamptz and ${params.endDate}::timestamptz
|
||||||
and "WebsiteEvent"."eventType" = ${EVENT_TYPE.pageView}
|
and "WebsiteEvent"."eventType" = ${EVENT_TYPE.pageView}
|
||||||
|
${filterQuery}
|
||||||
group by 1
|
group by 1
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -96,10 +96,10 @@ export const OPERATORS = {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const FILTER_COLUMNS = {
|
export const FILTER_COLUMNS = {
|
||||||
url: 'url_path',
|
url: 'urlPath',
|
||||||
referrer: 'referrer_domain',
|
referrer: 'referrerDomain',
|
||||||
title: 'page_title',
|
title: 'pageTitle',
|
||||||
query: 'url_query',
|
query: 'urlQuery',
|
||||||
os: 'os',
|
os: 'os',
|
||||||
browser: 'browser',
|
browser: 'browser',
|
||||||
device: 'device',
|
device: 'device',
|
||||||
@ -107,7 +107,7 @@ export const FILTER_COLUMNS = {
|
|||||||
region: 'subdivision1',
|
region: 'subdivision1',
|
||||||
city: 'city',
|
city: 'city',
|
||||||
language: 'language',
|
language: 'language',
|
||||||
event: 'event_name',
|
event: 'eventName',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_RESET_DATE = '2000-01-01';
|
export const DEFAULT_RESET_DATE = '2000-01-01';
|
||||||
|
@ -49,6 +49,8 @@ export async function parseFilters(
|
|||||||
throw new Error('Not found website');
|
throw new Error('Not found website');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const websiteDomain = website.domain;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
joinSession:
|
joinSession:
|
||||||
options?.joinSession ||
|
options?.joinSession ||
|
||||||
@ -58,7 +60,7 @@ export async function parseFilters(
|
|||||||
)
|
)
|
||||||
? `inner join "WebsiteSession" on "WebsiteEvent"."sessionId" = "WebsiteSession"."id"`
|
? `inner join "WebsiteSession" on "WebsiteEvent"."sessionId" = "WebsiteSession"."id"`
|
||||||
: '',
|
: '',
|
||||||
filterQuery: getFilterQuery(filters, options),
|
filterQuery: getFilterQuery(filters, options, websiteDomain),
|
||||||
params: {
|
params: {
|
||||||
...normalizeFilters(filters),
|
...normalizeFilters(filters),
|
||||||
websiteId,
|
websiteId,
|
||||||
@ -68,7 +70,7 @@ export async function parseFilters(
|
|||||||
endDate: filters.endDate
|
endDate: filters.endDate
|
||||||
? dayjs(filters.endDate).toISOString()
|
? dayjs(filters.endDate).toISOString()
|
||||||
: undefined,
|
: undefined,
|
||||||
websiteDomain: website.domain,
|
websiteDomain,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -85,8 +87,9 @@ function normalizeFilters(filters: Record<string, any> = {}) {
|
|||||||
|
|
||||||
export function getFilterQuery(
|
export function getFilterQuery(
|
||||||
filters: QueryFilters = {},
|
filters: QueryFilters = {},
|
||||||
options: QueryOptions = {}
|
options: QueryOptions = {},
|
||||||
): string {
|
websiteDomain: string | null = null
|
||||||
|
) {
|
||||||
const query = Object.keys(filters).reduce<string[]>((arr, name) => {
|
const query = Object.keys(filters).reduce<string[]>((arr, name) => {
|
||||||
const value: any = filters[name as keyof QueryFilters];
|
const value: any = filters[name as keyof QueryFilters];
|
||||||
const operator = value?.filter ?? OPERATORS.equals;
|
const operator = value?.filter ?? OPERATORS.equals;
|
||||||
@ -95,11 +98,11 @@ export function getFilterQuery(
|
|||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
if (value !== undefined && column) {
|
if (value !== undefined && column) {
|
||||||
arr.push(`and ${mapFilter(column, operator, name)}`);
|
arr.push(`AND ${mapFilter(column, operator, name)}`);
|
||||||
|
|
||||||
if (name === 'referrer') {
|
if (name === 'referrer') {
|
||||||
arr.push(
|
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 arr;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return query.join('\n');
|
return Prisma.sql([query.join('\n')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapFilter(
|
function mapFilter(
|
||||||
@ -118,9 +121,9 @@ function mapFilter(
|
|||||||
) {
|
) {
|
||||||
switch (operator) {
|
switch (operator) {
|
||||||
case OPERATORS.equals:
|
case OPERATORS.equals:
|
||||||
return `${column} = {{${name}::${type}}}`;
|
return `"${column}" = '${name}'::${type}`;
|
||||||
case OPERATORS.notEquals:
|
case OPERATORS.notEquals:
|
||||||
return `${column} != {{${name}::${type}}}`;
|
return `"${column}" != '${name}'::${type}`;
|
||||||
default:
|
default:
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user