Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/umami-software/umami into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Oct 12, 2022
2 parents 8627782 + 71badb8 commit c33729e
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion db/clickhouse/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CREATE TABLE event_queue (
created_at DateTime('UTC')
)
ENGINE = Kafka
SETTINGS kafka_broker_list = 'dev-01.umami.dev:9092,dev-01.umami.dev:9093,dev-01.umami.dev:9094', -- input broker list
SETTINGS kafka_broker_list = 'domain:9092,domain:9093,domain:9094', -- input broker list
kafka_topic_list = 'event',
kafka_group_name = 'event_consumer_group',
kafka_format = 'JSONEachRow',
Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function getDateFormat(date) {
}

function getCommaSeparatedStringFormat(data) {
return data.map(a => `'${a}'`).join(',');
return data.map(a => `'${a}'`).join(',') || '';
}

function getBetweenDates(field, start_at, end_at) {
Expand Down
11 changes: 7 additions & 4 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ export async function getSession(req) {
// Check database if does not exists in Redis
if (!websiteId) {
const website = await getWebsiteByUuid(websiteUuid);
websiteId = website ? website.websiteId : null;
websiteId = website ? website.id : null;
}

if (!websiteId || websiteId === DELETED) {
throw new Error(`Website not found: ${websiteUuid}`);
}

const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const sessionUuid = uuid(websiteId, hostname, ip, userAgent);
const sessionUuid = uuid(websiteUuid, hostname, ip, userAgent);

let sessionId = null;
let session = null;
Expand All @@ -61,7 +61,7 @@ export async function getSession(req) {
// Check database if does not exists in Redis
if (!sessionId) {
session = await getSessionByUuid(sessionUuid);
sessionId = session ? session.sessionId : null;
sessionId = session ? session.id : null;
}

if (!sessionId) {
Expand Down Expand Up @@ -97,7 +97,10 @@ export async function getSession(req) {
}

return {
websiteId: websiteId,
website: {
websiteId,
websiteUuid,
},
session,
};
}
12 changes: 8 additions & 4 deletions pages/api/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default async (req, res) => {
await useSession(req, res);

const {
session: { websiteId, session },
session: { website, session },
} = req;

const { type, payload } = getJsonBody(req);
Expand All @@ -73,9 +73,9 @@ export default async (req, res) => {
const eventUuid = uuid();

if (type === 'pageview') {
await savePageView(websiteId, { session, url, referrer });
await savePageView(website, { session, url, referrer });
} else if (type === 'event') {
await saveEvent(websiteId, {
await saveEvent(website, {
session,
eventUuid,
url,
Expand All @@ -87,7 +87,11 @@ export default async (req, res) => {
}

const token = createToken(
{ websiteId, sessionId: session.sessionId, sessionUuid: session.sessionUuid },
{
websiteId: website.websiteUuid,
sessionId: session.sessionId,
sessionUuid: session.sessionUuid,
},
secret(),
);

Expand Down
2 changes: 1 addition & 1 deletion pages/api/realtime/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async (req, res) => {
const { userId } = req.auth;

const websites = await getUserWebsites(userId);
const ids = websites.map(({ id }) => id);
const ids = websites.map(({ websiteUuid }) => websiteUuid);
const token = createToken({ websites: ids }, secret());
const data = await getRealtimeData(ids, subMinutes(new Date(), 30));

Expand Down
2 changes: 1 addition & 1 deletion queries/analytics/event/getEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function relationalQuery(websites, start_at) {
return prisma.client.event.findMany({
where: {
website: {
id: {
websiteUuid: {
in: websites,
},
},
Expand Down
7 changes: 5 additions & 2 deletions queries/analytics/event/saveEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export async function saveEvent(...args) {
});
}

async function relationalQuery(websiteId, { sessionId, url, eventName, eventData }) {
async function relationalQuery(
{ websiteId },
{ session: { id: sessionId }, url, eventName, eventData },
) {
const data = {
websiteId,
sessionId,
Expand All @@ -32,7 +35,7 @@ async function relationalQuery(websiteId, { sessionId, url, eventName, eventData
}

async function clickhouseQuery(
websiteId,
{ websiteUuid: websiteId },
{ session: { country, sessionUuid, ...sessionArgs }, eventUuid, url, eventName, eventData },
) {
const { getDateFormat, sendMessage } = kafka;
Expand Down
6 changes: 3 additions & 3 deletions queries/analytics/pageview/getPageviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function relationalQuery(websites, start_at) {
return prisma.client.pageview.findMany({
where: {
website: {
id: {
websiteUuid: {
in: websites,
},
},
Expand All @@ -25,9 +25,9 @@ async function relationalQuery(websites, start_at) {
}

async function clickhouseQuery(websites, start_at) {
const { getCommaSeparatedStringFormat } = clickhouse;
const { rawQuery, getCommaSeparatedStringFormat } = clickhouse;

return clickhouse.rawQuery(
return rawQuery(
`select
website_id,
session_id,
Expand Down
4 changes: 2 additions & 2 deletions queries/analytics/pageview/savePageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function savePageView(...args) {
});
}

async function relationalQuery(websiteId, { session: { sessionId }, url, referrer }) {
async function relationalQuery({ websiteId }, { session: { id: sessionId }, url, referrer }) {
return prisma.client.pageview.create({
data: {
websiteId,
Expand All @@ -22,7 +22,7 @@ async function relationalQuery(websiteId, { session: { sessionId }, url, referre
}

async function clickhouseQuery(
websiteId,
{ websiteUuid: websiteId },
{ session: { country, sessionUuid, ...sessionArgs }, url, referrer },
) {
const { getDateFormat, sendMessage } = kafka;
Expand Down
4 changes: 2 additions & 2 deletions queries/analytics/session/createSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function relationalQuery(websiteId, data) {
...data,
},
select: {
sessionId: true,
id: true,
sessionUuid: true,
hostname: true,
browser: true,
Expand All @@ -31,7 +31,7 @@ async function relationalQuery(websiteId, data) {
})
.then(async res => {
if (redis.client && res) {
await redis.client.set(`session:${res.sessionUuid}`, res.id);
await redis.client.set(`session:${res.sessionUuid}`, 1);
}

return res;
Expand Down
6 changes: 3 additions & 3 deletions queries/analytics/session/getSessionByUuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function relationalQuery(sessionUuid) {
})
.then(async res => {
if (redis.client && res) {
await redis.client.set(`session:${res.sessionUuid}`, res.sessionId);
await redis.client.set(`session:${res.sessionUuid}`, 1);
}

return res;
Expand All @@ -32,7 +32,7 @@ async function clickhouseQuery(sessionUuid) {

return rawQuery(
`select distinct
session_uuid,
session_id,
website_id,
created_at,
hostname,
Expand All @@ -43,7 +43,7 @@ async function clickhouseQuery(sessionUuid) {
language,
country
from event
where session_uuid = $1`,
where session_id = $1`,
params,
)
.then(result => findFirst(result))
Expand Down
12 changes: 8 additions & 4 deletions queries/analytics/session/getSessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function relationalQuery(websites, start_at) {
...(websites && websites.length > 0
? {
website: {
id: {
websiteUuid: {
in: websites,
},
},
Expand All @@ -29,11 +29,11 @@ async function relationalQuery(websites, start_at) {
}

async function clickhouseQuery(websites, start_at) {
const { rawQuery, getDateFormat } = clickhouse;
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;

return rawQuery(
`select distinct
session_uuid,
session_id,
website_id,
created_at,
hostname,
Expand All @@ -44,7 +44,11 @@ async function clickhouseQuery(websites, start_at) {
language,
country
from event
where ${websites && websites.length > 0 ? `website_id in (${websites.join(',')})` : '0 = 0'}
where ${
websites && websites.length > 0
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
: '0 = 0'
}
and created_at >= ${getDateFormat(start_at)}`,
);
}

0 comments on commit c33729e

Please sign in to comment.