Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Brian Cao 2023-04-07 11:40:30 -07:00
commit f4f657b666
6 changed files with 23 additions and 71 deletions

View file

@ -3,17 +3,18 @@ import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE } from 'lib/constants';
export function getEvents(...args: [websiteId: string, startAt: Date]) {
export function getEvents(...args: [websiteId: string, startAt: Date, eventType: number]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
function relationalQuery(websiteId: string, startAt: Date) {
function relationalQuery(websiteId: string, startAt: Date, eventType: number) {
return prisma.client.websiteEvent.findMany({
where: {
websiteId,
eventType,
createdAt: {
gte: startAt,
},
@ -21,7 +22,7 @@ function relationalQuery(websiteId: string, startAt: Date) {
});
}
function clickhouseQuery(websiteId: string, startAt: Date) {
function clickhouseQuery(websiteId: string, startAt: Date, eventType: number) {
const { rawQuery } = clickhouse;
return rawQuery(
@ -34,12 +35,13 @@ function clickhouseQuery(websiteId: string, startAt: Date) {
url_path,
event_name as eventName
from website_event
where event_type = ${EVENT_TYPE.customEvent}
where event_type = {eventType:Uint32}
and website_id = {websiteId:UUID}
and created_at >= {startAt:DateTime('UTC')}`,
{
websiteId,
startAt,
eventType,
},
);
}

View file

@ -1,43 +0,0 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE } from 'lib/constants';
export async function getPageviews(...args: [websiteId: string, startAt: Date]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websiteId: string, startAt: Date) {
return prisma.client.websiteEvent.findMany({
where: {
websiteId,
createdAt: {
gte: startAt,
},
},
});
}
async function clickhouseQuery(websiteId: string, startAt: Date) {
const { rawQuery } = clickhouse;
return rawQuery(
`select
website_id as websiteId,
session_id as sessionId,
created_at as createdAt,
toUnixTimestamp(created_at) as timestamp,
url_path
from website_event
where event_type = ${EVENT_TYPE.pageView}
and website_id = {websiteId:UUID}
and created_at >= {startAt:DateTime('UTC')}`,
{
websiteId,
startAt,
},
);
}

View file

@ -1,13 +1,13 @@
import { md5 } from 'lib/crypto';
import { getPageviews } from '../pageview/getPageviews';
import { getSessions } from '../session/getSessions';
import { getEvents } from '../event/getEvents';
import { EVENT_TYPE } from 'lib/constants';
export async function getRealtimeData(websiteId, time) {
const [pageviews, sessions, events] = await Promise.all([
getPageviews(websiteId, time),
getEvents(websiteId, time, EVENT_TYPE.pageView),
getSessions(websiteId, time),
getEvents(websiteId, time),
getEvents(websiteId, time, EVENT_TYPE.customEvent),
]);
const decorate = (id, data) => {

View file

@ -7,7 +7,6 @@ export * from './analytics/event/getEvents';
export * from './analytics/eventData/getEventData';
export * from './analytics/event/saveEvent';
export * from './analytics/pageview/getPageviewMetrics';
export * from './analytics/pageview/getPageviews';
export * from './analytics/pageview/getPageviewStats';
export * from './analytics/session/createSession';
export * from './analytics/session/getSession';