Refactored realtime.

This commit is contained in:
Mike Cao 2024-06-19 21:47:27 -07:00
parent cda3ba345b
commit 5108b91f80
16 changed files with 205 additions and 227 deletions

View file

@ -1,31 +1,34 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import { QueryFilters } from 'lib/types';
export function getEvents(...args: [websiteId: string, startDate: Date, eventType: number]) {
export function getEvents(...args: [websiteId: string, filters: QueryFilters]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
function relationalQuery(websiteId: string, startDate: Date, eventType: number) {
function relationalQuery(websiteId: string, filters: QueryFilters) {
const { startDate } = filters;
return prisma.client.websiteEvent.findMany({
where: {
websiteId,
eventType,
createdAt: {
gte: startDate,
},
},
orderBy: {
createdAt: 'asc',
createdAt: 'desc',
},
});
}
function clickhouseQuery(websiteId: string, startDate: Date, eventType: number) {
function clickhouseQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery } = clickhouse;
const { startDate } = filters;
return rawQuery(
`
@ -41,13 +44,11 @@ function clickhouseQuery(websiteId: string, startDate: Date, eventType: number)
from website_event
where website_id = {websiteId:UUID}
and created_at >= {startDate:DateTime64}
and event_type = {eventType:UInt32}
order by created_at asc
order by created_at desc
`,
{
websiteId,
startDate,
eventType,
},
);
}

View file

@ -1,43 +1,91 @@
import { getSessions, getEvents } from 'queries/index';
import { EVENT_TYPE } from 'lib/constants';
import { getSessions, getEvents, getPageviewStats, getSessionStats } from 'queries/index';
export async function getRealtimeData(websiteId: string, startDate: Date) {
const [pageviews, sessions, events] = await Promise.all([
getEvents(websiteId, startDate, EVENT_TYPE.pageView),
getSessions(websiteId, startDate),
getEvents(websiteId, startDate, EVENT_TYPE.customEvent),
const MAX_SIZE = 50;
function increment(data: object, key: string) {
if (key) {
if (!data[key]) {
data[key] = 1;
} else {
data[key] += 1;
}
}
}
export async function getRealtimeData(
websiteId: string,
criteria: { startDate: Date; timezone: string },
) {
const { startDate, timezone } = criteria;
const filters = { startDate, endDate: new Date(), unit: 'minute', timezone };
const [events, sessions, pageviews, sessionviews] = await Promise.all([
getEvents(websiteId, { startDate }),
getSessions(websiteId, { startDate }),
getPageviewStats(websiteId, filters),
getSessionStats(websiteId, filters),
]);
const decorate = (type: string, data: any[]) => {
return data.map((values: { [key: string]: any }) => ({
...values,
__type: type,
timestamp: values.timestamp ? values.timestamp * 1000 : new Date(values.createdAt).getTime(),
}));
};
const uniques = new Set();
const set = new Set();
const uniques = (type: string, data: any[]) => {
return data.reduce((arr, values: { [key: string]: any }) => {
if (!set.has(values.id)) {
set.add(values.id);
const sessionStats = sessions.reduce(
(obj: { visitors: any; countries: any }, session: { id: any; country: any }) => {
const { countries, visitors } = obj;
const { id, country } = session;
return arr.concat({
...values,
__type: type,
timestamp: values.timestamp
? values.timestamp * 1000
: new Date(values.createdAt).getTime(),
});
if (!uniques.has(id)) {
uniques.add(id);
increment(countries, country);
if (visitors.length < MAX_SIZE) {
visitors.push(session);
}
}
return arr;
}, []);
};
return obj;
},
{
countries: {},
visitors: [],
},
);
const eventStats = events.reduce(
(
obj: { urls: any; referrers: any; events: any },
event: { urlPath: any; referrerDomain: any },
) => {
const { urls, referrers, events } = obj;
const { urlPath, referrerDomain } = event;
increment(urls, urlPath);
increment(referrers, referrerDomain);
if (events.length < MAX_SIZE) {
events.push(event);
}
return obj;
},
{
urls: {},
referrers: {},
events: [],
},
);
return {
pageviews: decorate('pageview', pageviews),
sessions: uniques('session', sessions),
events: decorate('event', events),
...sessionStats,
...eventStats,
series: {
views: pageviews,
visitors: sessionviews,
},
totals: {
views: events.filter(e => !e.eventName).length,
visitors: uniques.size,
events: events.filter(e => e.eventName).length,
countries: Object.keys(sessionStats.countries).length,
},
timestamp: Date.now(),
};
}

View file

@ -312,7 +312,7 @@ async function clickhouseQuery(
const where = getWhere(urls, events, eventData);
const urlResults = hasUrl
? await rawQuery<any>(
? await rawQuery(
`
select
${columns.url}
@ -332,7 +332,7 @@ async function clickhouseQuery(
: [];
const eventResults = hasEvent
? await rawQuery<any>(
? await rawQuery(
`
select
${columns.events}
@ -352,7 +352,7 @@ async function clickhouseQuery(
: [];
const eventDataResults = hasEventData
? await rawQuery<any>(
? await rawQuery(
`
select
${columns.eventData}

View file

@ -1,15 +1,18 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
import { QueryFilters } from 'lib/types';
export async function getSessions(...args: [websiteId: string, startAt: Date]) {
export async function getSessions(...args: [websiteId: string, filters: QueryFilters]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websiteId: string, startDate: Date) {
async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { startDate } = filters;
return prisma.client.session.findMany({
where: {
websiteId,
@ -18,13 +21,14 @@ async function relationalQuery(websiteId: string, startDate: Date) {
},
},
orderBy: {
createdAt: 'asc',
createdAt: 'desc',
},
});
}
async function clickhouseQuery(websiteId: string, startDate: Date) {
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery } = clickhouse;
const { startDate } = filters;
return rawQuery(
`
@ -46,7 +50,7 @@ async function clickhouseQuery(websiteId: string, startDate: Date) {
from website_event
where website_id = {websiteId:UUID}
and created_at >= {startDate:DateTime64}
order by created_at asc
order by created_at desc
`,
{
websiteId,