add event-data GET route
This commit is contained in:
@@ -1,63 +1,87 @@
|
||||
import type { EventData } from '@/generated/prisma/client';
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const FUNCTION_NAME = 'getEventData';
|
||||
|
||||
export async function getEventData(
|
||||
...args: [websiteId: string, eventId: string]
|
||||
): Promise<EventData[]> {
|
||||
export async function getEventData(...args: [websiteId: string, filters: QueryFilters]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, eventId: string) {
|
||||
const { rawQuery } = prisma;
|
||||
async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select event_data.website_id as "websiteId",
|
||||
event_data.website_event_id as "eventId",
|
||||
website_event.event_name as "eventName",
|
||||
event_data.data_key as "dataKey",
|
||||
event_data.string_value as "stringValue",
|
||||
event_data.number_value as "numberValue",
|
||||
event_data.date_value as "dateValue",
|
||||
event_data.data_type as "dataType",
|
||||
event_data.created_at as "createdAt"
|
||||
select
|
||||
event_data.website_id as "websiteId",
|
||||
event_data.website_event_id as "eventId",
|
||||
website_event.event_name as "eventName",
|
||||
event_data.data_key as "dataKey",
|
||||
event_data.string_value as "stringValue",
|
||||
event_data.number_value as "numberValue",
|
||||
event_data.date_value as "dateValue",
|
||||
event_data.data_type as "dataType",
|
||||
event_data.created_at as "createdAt"
|
||||
from event_data
|
||||
join website_event on website_event.event_id = event_data.website_event_id
|
||||
and website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where event_data.website_id = {{websiteId::uuid}}
|
||||
and event_data.website_event_id = {{eventId::uuid}}
|
||||
and event_data.created_at between {{startDate}} and {{endDate}}
|
||||
${filterQuery}
|
||||
order by event_data.created_at desc
|
||||
limit 1000
|
||||
`,
|
||||
{ websiteId, eventId },
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string, eventId: string): Promise<EventData[]> {
|
||||
const { rawQuery } = clickhouse;
|
||||
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select website_id as websiteId,
|
||||
event_id as eventId,
|
||||
event_name as eventName,
|
||||
data_key as dataKey,
|
||||
string_value as stringValue,
|
||||
number_value as numberValue,
|
||||
date_value as dateValue,
|
||||
data_type as dataType,
|
||||
created_at as createdAt
|
||||
from event_data
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_id = {eventId:UUID}
|
||||
select
|
||||
website_id as websiteId,
|
||||
event_id as eventId,
|
||||
event_name as eventName,
|
||||
data_key as dataKey,
|
||||
string_value as stringValue,
|
||||
number_value as numberValue,
|
||||
date_value as dateValue,
|
||||
data_type as dataType,
|
||||
created_at as createdAt
|
||||
from event_data
|
||||
any left join (
|
||||
select *
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = 2) website_event
|
||||
on website_event.event_id = event_data.event_id
|
||||
and website_event.session_id = event_data.session_id
|
||||
and website_event.website_id = event_data.website_id
|
||||
${cohortQuery}
|
||||
where event_data.website_id = {websiteId:UUID}
|
||||
and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
${filterQuery}
|
||||
order by event_data.created_at desc
|
||||
limit 1000
|
||||
`,
|
||||
{ websiteId, eventId },
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { EventData } from '@/generated/prisma/client';
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
const FUNCTION_NAME = 'getEventDataById';
|
||||
|
||||
export async function getEventDataById(
|
||||
...args: [websiteId: string, eventId: string]
|
||||
): Promise<EventData[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, eventId: string) {
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select event_data.website_id as "websiteId",
|
||||
event_data.website_event_id as "eventId",
|
||||
website_event.event_name as "eventName",
|
||||
event_data.data_key as "dataKey",
|
||||
event_data.string_value as "stringValue",
|
||||
event_data.number_value as "numberValue",
|
||||
event_data.date_value as "dateValue",
|
||||
event_data.data_type as "dataType",
|
||||
event_data.created_at as "createdAt"
|
||||
from event_data
|
||||
join website_event on website_event.event_id = event_data.website_event_id
|
||||
and website_event.website_id = {{websiteId::uuid}}
|
||||
where event_data.website_id = {{websiteId::uuid}}
|
||||
and event_data.website_event_id = {{eventId::uuid}}
|
||||
`,
|
||||
{ websiteId, eventId },
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string, eventId: string): Promise<EventData[]> {
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select website_id as websiteId,
|
||||
event_id as eventId,
|
||||
event_name as eventName,
|
||||
data_key as dataKey,
|
||||
string_value as stringValue,
|
||||
number_value as numberValue,
|
||||
date_value as dateValue,
|
||||
data_type as dataType,
|
||||
created_at as createdAt
|
||||
from event_data
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_id = {eventId:UUID}
|
||||
`,
|
||||
{ websiteId, eventId },
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './events/getEventDataById';
|
||||
export * from './events/getEventDataEvents';
|
||||
export * from './events/getEventDataFields';
|
||||
export * from './events/getEventDataProperties';
|
||||
|
||||
Reference in New Issue
Block a user