Merge pull request #1873 from umami-software/bug/um-250-fix-realtime-data

fix some components in realtime data
This commit is contained in:
Francis Cao 2023-04-07 10:56:42 -07:00 committed by GitHub
commit f38f0c6420
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 getIcon = ({ __type }) => icons[__type];
const getDetail = log => { 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) { if (__type === TYPE_EVENT) {
return ( return (

View File

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

View File

@ -3,17 +3,18 @@ import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { EVENT_TYPE } from 'lib/constants'; 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({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args), [CLICKHOUSE]: () => clickhouseQuery(...args),
}); });
} }
function relationalQuery(websiteId: string, startAt: Date) { function relationalQuery(websiteId: string, startAt: Date, eventType: number) {
return prisma.client.websiteEvent.findMany({ return prisma.client.websiteEvent.findMany({
where: { where: {
websiteId, websiteId,
eventType,
createdAt: { createdAt: {
gte: startAt, 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; const { rawQuery } = clickhouse;
return rawQuery( return rawQuery(
@ -34,12 +35,13 @@ function clickhouseQuery(websiteId: string, startAt: Date) {
url_path, url_path,
event_name as eventName event_name as eventName
from website_event from website_event
where event_type = ${EVENT_TYPE.customEvent} where event_type = {eventType:Uint32}
and website_id = {websiteId:UUID} and website_id = {websiteId:UUID}
and created_at >= {startAt:DateTime('UTC')}`, and created_at >= {startAt:DateTime('UTC')}`,
{ {
websiteId, websiteId,
startAt, 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 { md5 } from 'lib/crypto';
import { getPageviews } from '../pageview/getPageviews';
import { getSessions } from '../session/getSessions'; import { getSessions } from '../session/getSessions';
import { getEvents } from '../event/getEvents'; import { getEvents } from '../event/getEvents';
import { EVENT_TYPE } from 'lib/constants';
export async function getRealtimeData(websiteId, time) { export async function getRealtimeData(websiteId, time) {
const [pageviews, sessions, events] = await Promise.all([ const [pageviews, sessions, events] = await Promise.all([
getPageviews(websiteId, time), getEvents(websiteId, time, EVENT_TYPE.pageView),
getSessions(websiteId, time), getSessions(websiteId, time),
getEvents(websiteId, time), getEvents(websiteId, time, EVENT_TYPE.customEvent),
]); ]);
const decorate = (id, data) => { const decorate = (id, data) => {

View File

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