mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
Database refactoring.
This commit is contained in:
parent
bb184dc2cc
commit
467c7f289f
37 changed files with 566 additions and 591 deletions
|
|
@ -1,21 +1,20 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, KAFKA, RELATIONAL } from 'lib/constants';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import redis from 'lib/redis';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
|
||||
export async function createSession(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, data) {
|
||||
return runQuery(
|
||||
prisma.session.create({
|
||||
return prisma.client.session
|
||||
.create({
|
||||
data: {
|
||||
website_id,
|
||||
...data,
|
||||
|
|
@ -23,14 +22,14 @@ async function relationalQuery(website_id, data) {
|
|||
select: {
|
||||
session_id: true,
|
||||
},
|
||||
}),
|
||||
).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`session:${res.session_uuid}`, '');
|
||||
}
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`session:${res.session_uuid}`, '');
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
@ -48,10 +47,11 @@ async function clickhouseQuery(
|
|||
language,
|
||||
country ? country : null,
|
||||
];
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
|
||||
await clickhouse.rawQuery(
|
||||
await rawQuery(
|
||||
`insert into umami.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
|
||||
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
||||
values (${getDateFormat(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -60,10 +60,11 @@ async function kafkaQuery(
|
|||
website_id,
|
||||
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
session_uuid: session_uuid,
|
||||
website_id: website_id,
|
||||
created_at: kafka.getDateFormat(new Date()),
|
||||
created_at: getDateFormat(new Date()),
|
||||
hostname: hostname,
|
||||
browser: browser,
|
||||
os: os,
|
||||
|
|
@ -73,7 +74,7 @@ async function kafkaQuery(
|
|||
country: country ? country : null,
|
||||
};
|
||||
|
||||
await kafka.sendMessage(params, 'session');
|
||||
await sendMessage(params, 'session');
|
||||
|
||||
await redis.set(`session:${session_uuid}`, '');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,32 +1,28 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getSessionByUuid(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(session_uuid) {
|
||||
return runQuery(
|
||||
prisma.session.findUnique({
|
||||
where: {
|
||||
session_uuid,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.session.findUnique({
|
||||
where: {
|
||||
session_uuid,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(session_uuid) {
|
||||
const { rawQuery, findFirst } = clickhouse;
|
||||
const params = [session_uuid];
|
||||
|
||||
return clickhouse
|
||||
.rawQuery(
|
||||
`
|
||||
select
|
||||
return rawQuery(
|
||||
`select
|
||||
session_uuid,
|
||||
website_id,
|
||||
created_at,
|
||||
|
|
@ -35,12 +31,10 @@ async function clickhouseQuery(session_uuid) {
|
|||
os,
|
||||
device,
|
||||
screen,
|
||||
"language",
|
||||
language,
|
||||
country
|
||||
from session
|
||||
where session_uuid = $1
|
||||
`,
|
||||
params,
|
||||
)
|
||||
.then(result => clickhouse.findFirst(result));
|
||||
where session_uuid = $1`,
|
||||
params,
|
||||
).then(result => findFirst(result));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { parseFilters, rawQuery } from 'lib/relational';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getSessionMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
|
|
@ -20,8 +20,7 @@ async function relationalQuery(website_id, start_at, end_at, field, filters = {}
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
`select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_id in (
|
||||
select pageview.session_id
|
||||
|
|
@ -33,15 +32,15 @@ async function relationalQuery(website_id, start_at, end_at, field, filters = {}
|
|||
${sessionQuery}
|
||||
)
|
||||
group by 1
|
||||
order by 2 desc
|
||||
`,
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = clickhouse.parseFilters(
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
null,
|
||||
filters,
|
||||
|
|
@ -49,22 +48,20 @@ async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}
|
|||
'session_uuid',
|
||||
);
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_uuid in (
|
||||
select pageview.session_uuid
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and ${clickhouse.getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
and ${getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
)
|
||||
group by x
|
||||
order by y desc
|
||||
`,
|
||||
order by y desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
||||
|
||||
export async function getSessions(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.session.findMany({
|
||||
prisma.client.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
? {
|
||||
|
|
@ -32,9 +31,10 @@ async function relationalQuery(websites, start_at) {
|
|||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
session_uuid,
|
||||
website_id,
|
||||
created_at,
|
||||
|
|
@ -43,11 +43,10 @@ async function clickhouseQuery(websites, start_at) {
|
|||
os,
|
||||
device,
|
||||
screen,
|
||||
"language",
|
||||
language,
|
||||
country
|
||||
from session
|
||||
where ${websites && websites.length > 0 ? `(website_id in (${websites.join[',']})` : '0 = 0'}
|
||||
and created_at >= ${clickhouse.getDateFormat(start_at)}
|
||||
`,
|
||||
and created_at >= ${getDateFormat(start_at)}`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue