Added query options.

This commit is contained in:
Mike Cao 2023-08-04 14:23:03 -07:00
parent 2559263e91
commit 688705dbb3
6 changed files with 42 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
import { FILTER_COLUMNS, IGNORED_FILTERS, SESSION_COLUMNS } from './constants';
import { loadWebsite } from './load';
import { maxDate } from './date';
import { QueryFilters } from './types';
import { QueryFilters, QueryOptions } from './types';
const MYSQL_DATE_FORMATS = {
minute: '%Y-%m-%d %H:%i:00',
@ -88,13 +88,18 @@ function getFilterQuery(filters = {}): string {
return query.join('\n');
}
async function parseFilters(websiteId, filters: QueryFilters & { [key: string]: any } = {}) {
async function parseFilters(
websiteId,
filters: QueryFilters & { [key: string]: any } = {},
options: QueryOptions = {},
) {
const website = await loadWebsite(websiteId);
return {
joinSession: Object.keys(filters).find(key => SESSION_COLUMNS[key])
? `inner join session on website_event.session_id = session.session_id`
: '',
joinSession:
options?.joinSession || Object.keys(filters).find(key => SESSION_COLUMNS.includes(key))
? `inner join session on website_event.session_id = session.session_id`
: '',
filterQuery: getFilterQuery(filters),
params: {
...filters,

View File

@ -137,11 +137,11 @@ export interface QueryFilters {
unit?: string;
domain?: string;
eventType?: number;
eventName?: string;
url?: string;
referrer?: string;
title?: string;
query?: string;
event?: string;
os?: string;
browser?: string;
device?: string;
@ -150,3 +150,7 @@ export interface QueryFilters {
city?: string;
language?: string;
}
export interface QueryOptions {
joinSession?: boolean;
}

View File

@ -43,10 +43,8 @@ export default async (
endDate,
timezone,
unit,
filters: {
url,
eventName,
},
url,
eventName,
});
return ok(res, events);

View File

@ -5,7 +5,7 @@ import { WebsiteEventMetric, QueryFilters } from 'lib/types';
import { EVENT_TYPE } from 'lib/constants';
export async function getEventMetrics(
...args: [websiteId: string, criteria: QueryFilters]
...args: [websiteId: string, filters: QueryFilters]
): Promise<WebsiteEventMetric[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -16,7 +16,7 @@ export async function getEventMetrics(
async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { timezone = 'utc', unit = 'day' } = filters;
const { rawQuery, getDateQuery, parseFilters } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, {
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
...filters,
eventType: EVENT_TYPE.customEvent,
});
@ -28,6 +28,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
${getDateQuery('created_at', unit, timezone)} t,
count(*) y
from website_event
${joinSession}
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and event_type = {{eventType}}

View File

@ -1,7 +1,7 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE } from 'lib/constants';
import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants';
import { QueryFilters } from 'lib/types';
export async function getPageviewMetrics(
@ -15,16 +15,20 @@ export async function getPageviewMetrics(
async function relationalQuery(websiteId: string, column: string, filters: QueryFilters) {
const { rawQuery, parseFilters } = prisma;
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
...filters,
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
});
const { filterQuery, joinSession, params } = await parseFilters(
websiteId,
{
...filters,
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
},
{ joinSession: SESSION_COLUMNS.includes(column) },
);
return rawQuery(
`
select ${column} x, count(*) y
from website_event
${joinSession}
${joinSession}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and event_type = {{eventType}}

View File

@ -1,7 +1,7 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE } from 'lib/constants';
import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants';
import { QueryFilters } from 'lib/types';
export async function getSessionMetrics(
@ -15,17 +15,22 @@ export async function getSessionMetrics(
async function relationalQuery(websiteId: string, column: string, filters: QueryFilters) {
const { parseFilters, rawQuery } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, {
...filters,
eventType: EVENT_TYPE.pageView,
});
const { filterQuery, joinSession, params } = await parseFilters(
websiteId,
{
...filters,
eventType: EVENT_TYPE.pageView,
},
{
joinSession: SESSION_COLUMNS.includes(column),
},
);
return rawQuery(
`
select ${column} x, count(*) y
from website_event
inner join session
on session.session_id = website_event.session_id
${joinSession}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type = {{eventType}}