create kafka save queries

This commit is contained in:
Francis Cao 2022-08-04 17:43:47 -07:00
parent b0a36f18b4
commit 1ab1e58cb1
4 changed files with 70 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { ClickHouse } from 'clickhouse'; import { ClickHouse } from 'clickhouse';
import { Kafka } from 'kafkajs';
import chalk from 'chalk'; import chalk from 'chalk';
import { import {
MYSQL, MYSQL,
@ -9,6 +10,7 @@ import {
CLICKHOUSE, CLICKHOUSE,
RELATIONAL, RELATIONAL,
FILTER_IGNORED, FILTER_IGNORED,
KAFKA,
} from 'lib/constants'; } from 'lib/constants';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { CLICKHOUSE_DATE_FORMATS } from './constants'; import { CLICKHOUSE_DATE_FORMATS } from './constants';
@ -69,7 +71,6 @@ function getKafkaClient() {
return null; return null;
} }
const { Kafka } = require('kafkajs');
const url = new URL(process.env.KAFKA_URL); const url = new URL(process.env.KAFKA_URL);
const brokers = process.env.KAFKA_BROKER.split(','); const brokers = process.env.KAFKA_BROKER.split(',');
@ -117,9 +118,7 @@ export function getDatabase() {
} }
export function getAnalyticsDatabase() { export function getAnalyticsDatabase() {
const type = const type = process.env.ANALYTICS_URL && process.env.ANALYTICS_URL.split(':')[0];
process.env.ANALYTICS_TYPE ||
(process.env.ANALYTICS_URL && process.env.ANALYTICS_URL.split(':')[0]);
if (type === 'postgres') { if (type === 'postgres') {
return POSTGRESQL; return POSTGRESQL;
@ -132,6 +131,12 @@ export function getAnalyticsDatabase() {
return type; return type;
} }
export function getKafkaService() {
const type = process.env.KAFKA_URL && process.env.KAFKA_URL.split(':')[0];
return type;
}
export function getDateStringQueryClickhouse(data, unit) { export function getDateStringQueryClickhouse(data, unit) {
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`; return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
} }
@ -321,9 +326,12 @@ export async function runAnalyticsQuery(queries) {
if (db === POSTGRESQL || db === MYSQL) { if (db === POSTGRESQL || db === MYSQL) {
return queries[RELATIONAL](); return queries[RELATIONAL]();
} }
59;
if (db === CLICKHOUSE) { if (db === CLICKHOUSE) {
const kafka = getKafkaService();
if (kafka === KAFKA && queries[KAFKA]) {
return queries[KAFKA]();
}
return queries[CLICKHOUSE](); return queries[CLICKHOUSE]();
} }
} }

View File

@ -1,16 +1,18 @@
import { CLICKHOUSE, RELATIONAL, URL_LENGTH } from 'lib/constants'; import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
import { import {
getDateFormatClickhouse, getDateFormatClickhouse,
prisma, prisma,
rawQueryClickhouse, rawQueryClickhouse,
runAnalyticsQuery, runAnalyticsQuery,
runQuery, runQuery,
kafkaProducer,
} from 'lib/db'; } from 'lib/db';
export async function saveEvent(...args) { export async function saveEvent(...args) {
return runAnalyticsQuery({ return runAnalyticsQuery({
[RELATIONAL]: () => relationalQuery(...args), [RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args), [CLICKHOUSE]: () => clickhouseQuery(...args),
[KAFKA]: () => kafkaQuery(...args),
}); });
} }
@ -44,3 +46,15 @@ async function clickhouseQuery(website_id, { session_uuid, url, event_type, even
params, 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');
}

View File

@ -1,16 +1,18 @@
import { CLICKHOUSE, RELATIONAL, URL_LENGTH } from 'lib/constants'; import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
import { import {
getDateFormatClickhouse, getDateFormatClickhouse,
prisma, prisma,
rawQueryClickhouse, rawQueryClickhouse,
runAnalyticsQuery, runAnalyticsQuery,
runQuery, runQuery,
kafkaProducer,
} from 'lib/db'; } from 'lib/db';
export async function savePageView(...args) { export async function savePageView(...args) {
return runAnalyticsQuery({ return runAnalyticsQuery({
[RELATIONAL]: () => relationalQuery(...args), [RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args), [CLICKHOUSE]: () => clickhouseQuery(...args),
[KAFKA]: () => kafkaQuery(...args),
}); });
} }
@ -42,3 +44,14 @@ async function clickhouseQuery(website_id, { session_uuid, url, referrer }) {
params, 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');
}

View File

@ -1,4 +1,4 @@
import { CLICKHOUSE, RELATIONAL } from 'lib/constants'; import { CLICKHOUSE, RELATIONAL, KAFKA } from 'lib/constants';
import { import {
getDateFormatClickhouse, getDateFormatClickhouse,
prisma, prisma,
@ -13,6 +13,7 @@ export async function createSession(...args) {
return runAnalyticsQuery({ return runAnalyticsQuery({
[RELATIONAL]: () => relationalQuery(...args), [RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args), [CLICKHOUSE]: () => clickhouseQuery(...args),
[KAFKA]: () => kafkaQuery(...args),
}); });
} }
@ -33,6 +34,31 @@ async function relationalQuery(website_id, data) {
async function clickhouseQuery( async function clickhouseQuery(
website_id, website_id,
{ session_uuid, hostname, browser, os, screen, language, country, device }, { 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 = { const params = {
session_uuid: session_uuid, session_uuid: session_uuid,
@ -46,17 +72,7 @@ async function clickhouseQuery(
country: country ? country : null, country: country ? country : null,
}; };
if (process.env.KAFKA_URL) { await kafkaProducer(params, 'session');
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,
);
}
return getSessionByUuid(session_uuid); return getSessionByUuid(session_uuid);
} }