mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
create kafka save queries
This commit is contained in:
parent
b0a36f18b4
commit
1ab1e58cb1
4 changed files with 70 additions and 19 deletions
|
|
@ -1,16 +1,18 @@
|
|||
import { CLICKHOUSE, RELATIONAL, URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
|
||||
import {
|
||||
getDateFormatClickhouse,
|
||||
prisma,
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
runQuery,
|
||||
kafkaProducer,
|
||||
} from 'lib/db';
|
||||
|
||||
export async function saveEvent(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -44,3 +46,15 @@ async function clickhouseQuery(website_id, { session_uuid, url, event_type, even
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { session_uuid, url, event_type, event_value }) {
|
||||
const params = {
|
||||
website_id: website_id,
|
||||
session_uuid: session_uuid,
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
event_type: event_type?.substr(0, 50),
|
||||
event_value: event_value?.substr(0, 50),
|
||||
};
|
||||
|
||||
await kafkaProducer(params, 'event');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,18 @@
|
|||
import { CLICKHOUSE, RELATIONAL, URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
|
||||
import {
|
||||
getDateFormatClickhouse,
|
||||
prisma,
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
runQuery,
|
||||
kafkaProducer,
|
||||
} from 'lib/db';
|
||||
|
||||
export async function savePageView(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -42,3 +44,14 @@ async function clickhouseQuery(website_id, { session_uuid, url, referrer }) {
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { session_uuid, url, referrer }) {
|
||||
const params = {
|
||||
website_id: website_id,
|
||||
session_uuid: session_uuid,
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
referrer: referrer?.substr(0, URL_LENGTH),
|
||||
};
|
||||
|
||||
await kafkaProducer(params, 'pageview');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { CLICKHOUSE, RELATIONAL, KAFKA } from 'lib/constants';
|
||||
import {
|
||||
getDateFormatClickhouse,
|
||||
prisma,
|
||||
|
|
@ -13,6 +13,7 @@ export async function createSession(...args) {
|
|||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -33,6 +34,31 @@ async function relationalQuery(website_id, data) {
|
|||
async function clickhouseQuery(
|
||||
website_id,
|
||||
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
const params = [
|
||||
session_uuid,
|
||||
website_id,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country ? country : null,
|
||||
];
|
||||
|
||||
await rawQueryClickhouse(
|
||||
`insert into umami_dev.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
|
||||
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
||||
params,
|
||||
);
|
||||
|
||||
return getSessionByUuid(session_uuid);
|
||||
}
|
||||
|
||||
async function kafkaQuery(
|
||||
website_id,
|
||||
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
const params = {
|
||||
session_uuid: session_uuid,
|
||||
|
|
@ -46,17 +72,7 @@ async function clickhouseQuery(
|
|||
country: country ? country : null,
|
||||
};
|
||||
|
||||
if (process.env.KAFKA_URL) {
|
||||
await kafkaProducer(params, 'session');
|
||||
} else {
|
||||
const paramsValue = Object.keys(params);
|
||||
|
||||
await rawQueryClickhouse(
|
||||
`insert into umami_dev.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
|
||||
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
||||
paramsValue,
|
||||
);
|
||||
}
|
||||
await kafkaProducer(params, 'session');
|
||||
|
||||
return getSessionByUuid(session_uuid);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue