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

@ -57,7 +57,7 @@ export default function RealtimeLog({ data, websiteDomain }) {
const getIcon = ({ __type }) => icons[__type];
const getDetail = log => {
const { __type, eventName, url, browser, os, country, device } = log;
const { __type, eventName, urlPath: url, browser, os, country, device } = log;
if (__type === TYPE_EVENT) {
return (

View File

@ -35,18 +35,14 @@ export default function RealtimeUrls({ websiteDomain, data = {} }) {
if (pageviews) {
const referrers = percentFilter(
pageviews
.reduce((arr, { referrer }) => {
if (referrer?.startsWith('http')) {
const hostname = new URL(referrer).hostname.replace(/^www\./, '');
.reduce((arr, { referrerDomain }) => {
if (referrerDomain) {
const row = arr.find(({ x }) => x === referrerDomain);
if (hostname) {
const row = arr.find(({ x }) => x === hostname);
if (!row) {
arr.push({ x: hostname, y: 1 });
} else {
row.y += 1;
}
if (!row) {
arr.push({ x: referrerDomain, y: 1 });
} else {
row.y += 1;
}
}
return arr;
@ -56,15 +52,13 @@ export default function RealtimeUrls({ websiteDomain, data = {} }) {
const pages = percentFilter(
pageviews
.reduce((arr, { url }) => {
if (url?.startsWith('/')) {
const row = arr.find(({ x }) => x === url);
.reduce((arr, { urlPath }) => {
const row = arr.find(({ x }) => x === urlPath);
if (!row) {
arr.push({ x: url, y: 1 });
} else {
row.y += 1;
}
if (!row) {
arr.push({ x: urlPath, y: 1 });
} else {
row.y += 1;
}
return arr;
}, [])

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';