Refactored filter logic.

This commit is contained in:
Mike Cao 2023-04-01 15:44:30 -07:00
parent 57c3d03cc8
commit 74192cd695
18 changed files with 205 additions and 296 deletions

View file

@ -14,6 +14,7 @@ export async function getEventMetrics(
endDate: Date;
timezone: string;
unit: string;
column: string;
filters: {
url: string;
eventName: string;
@ -40,6 +41,7 @@ async function relationalQuery(
endDate: Date;
timezone: string;
unit: string;
column: string;
filters: {
url: string;
eventName: string;
@ -50,6 +52,7 @@ async function relationalQuery(
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
const params: any = [websiteId, resetDate, startDate, endDate];
const filterQuery = getFilterQuery(filters, params);
return rawQuery(
`select
@ -61,7 +64,7 @@ async function relationalQuery(
and created_at >= $2
and created_at between $3 and $4
and event_type = ${EVENT_TYPE.customEvent}
${getFilterQuery(filters, params)}
${filterQuery}
group by 1, 2
order by 2`,
params,

View file

@ -1,20 +1,17 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache';
import { Prisma } from '@prisma/client';
import { EVENT_TYPE } from 'lib/constants';
import { getWebsite } from 'queries';
import { EVENT_TYPE, FILTER_COLUMNS } from 'lib/constants';
import { loadWebsite } from 'lib/query';
export async function getPageviewMetrics(
...args: [
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
column: string;
filters: object;
type: string;
},
]
) {
@ -26,25 +23,32 @@ export async function getPageviewMetrics(
async function relationalQuery(
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
column: string;
filters: object;
type: string;
},
) {
const { startDate, endDate, column, filters = {}, type } = data;
const { startDate, endDate, filters = {}, column } = criteria;
const { rawQuery, parseFilters, toUuid } = prisma;
const website = await getWebsite({ id: websiteId });
const website = await loadWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params: any = [
websiteId,
resetDate,
startDate,
endDate,
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
];
let domainFilter = '';
if (column === 'referrer_domain') {
domainFilter = 'and website_event.referrer_domain != $6';
params.push(website.domain);
}
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(
@ -55,6 +59,7 @@ async function relationalQuery(
and website_event.created_at >= $2
and website_event.created_at between $3 and $4
and event_type = $5
${domainFilter}
${filterQuery}
group by 1
order by 2 desc
@ -65,22 +70,30 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
column: string;
filters: object;
type: string;
},
) {
const { startDate, endDate, column, filters = {}, type } = data;
const { startDate, endDate, filters = {}, column } = criteria;
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const website = await loadWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params = {
websiteId,
eventType: type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
domain: undefined,
};
let excludeDomain = '';
if (column === 'referrer_domain') {
excludeDomain = 'and referrer_domain != {domain:String}';
params.domain = website.domain;
}
const { filterQuery } = parseFilters(filters, params);
return rawQuery(
@ -89,7 +102,8 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID}
and event_type = {eventType:UInt32}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
${excludeDomain}
${filterQuery}
group by x
order by y desc

View file

@ -8,7 +8,7 @@ import { getWebsite } from 'queries';
export async function getPageviewStats(
...args: [
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
timezone?: string;
@ -27,7 +27,7 @@ export async function getPageviewStats(
async function relationalQuery(
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
timezone?: string;
@ -45,7 +45,7 @@ async function relationalQuery(
count = '*',
filters = {},
sessionKey = 'session_id',
} = data;
} = criteria;
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
@ -69,7 +69,7 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
data: {
criteria: {
startDate: Date;
endDate: Date;
timezone?: string;
@ -79,7 +79,14 @@ async function clickhouseQuery(
sessionKey?: string;
},
) {
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
const {
startDate,
endDate,
timezone = 'UTC',
unit = 'day',
count = '*',
filters = {},
} = criteria;
const {
parseFilters,
getDateFormat,

View file

@ -2,13 +2,13 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache';
import { EVENT_TYPE } from 'lib/constants';
import { EVENT_TYPE, FILTER_COLUMNS } from 'lib/constants';
import { getWebsite } from 'queries';
export async function getSessionMetrics(
...args: [
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
criteria: { startDate: Date; endDate: Date; column: string; filters: object },
]
) {
return runQuery({
@ -19,17 +19,17 @@ export async function getSessionMetrics(
async function relationalQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
criteria: { startDate: Date; endDate: Date; column: string; filters: object },
) {
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
const { startDate, endDate, field, filters = {} } = data;
const { startDate, endDate, column, filters = {} } = criteria;
const { toUuid, parseFilters, rawQuery } = prisma;
const params: any = [websiteId, resetDate, startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(
`select ${field} x, count(*) y
`select ${column} x, count(*) y
from session as x
where x.session_id in (
select website_event.session_id
@ -51,17 +51,17 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
data: { startDate: Date; endDate: Date; column: string; filters: object },
) {
const { startDate, endDate, field, filters = {} } = data;
const { startDate, endDate, column, filters = {} } = data;
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params = { websiteId };
const { filterQuery } = parseFilters(filters, params, field);
const { filterQuery } = parseFilters(filters, params);
return rawQuery(
`select ${field} x, count(distinct session_id) y
`select ${column} x, count(distinct session_id) y
from website_event as x
where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.pageView}

View file

@ -6,7 +6,10 @@ import { EVENT_TYPE } from 'lib/constants';
import { getWebsite } from 'queries';
export async function getWebsiteStats(
...args: [websiteId: string, data: { startDate: Date; endDate: Date; filters: object }]
...args: [
websiteId: string,
data: { startDate: Date; endDate: Date; type?: string; filters: object },
]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -16,9 +19,9 @@ export async function getWebsiteStats(
async function relationalQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; filters: object },
criteria: { startDate: Date; endDate: Date; filters: object },
) {
const { startDate, endDate, filters = {} } = data;
const { startDate, endDate, filters = {} } = criteria;
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
@ -51,9 +54,9 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; filters: object },
criteria: { startDate: Date; endDate: Date; filters: object },
) {
const { startDate, endDate, filters = {} } = data;
const { startDate, endDate, filters = {} } = criteria;
const { rawQuery, getDateFormat, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;