mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Database refactoring.
This commit is contained in:
parent
bb184dc2cc
commit
467c7f289f
37 changed files with 566 additions and 591 deletions
|
|
@ -1,9 +1,7 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createAccount(data) {
|
||||
return runQuery(
|
||||
prisma.account.create({
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,28 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function deleteAccount(user_id) {
|
||||
return runQuery(
|
||||
prisma.$transaction([
|
||||
prisma.pageview.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
prisma.event_data.deleteMany({
|
||||
where: { event: { session: { website: { user_id } } } },
|
||||
}),
|
||||
prisma.event.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
prisma.session.deleteMany({
|
||||
where: { website: { user_id } },
|
||||
}),
|
||||
prisma.website.deleteMany({
|
||||
where: { user_id },
|
||||
}),
|
||||
prisma.account.delete({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const { client } = prisma;
|
||||
|
||||
return client.$transaction([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { user_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { user_id } },
|
||||
}),
|
||||
client.website.deleteMany({
|
||||
where: { user_id },
|
||||
}),
|
||||
client.account.delete({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccountById(user_id) {
|
||||
return runQuery(
|
||||
prisma.account.findUnique({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.findUnique({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccountByUsername(username) {
|
||||
return runQuery(
|
||||
prisma.account.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccounts() {
|
||||
return runQuery(
|
||||
prisma.account.findMany({
|
||||
orderBy: [
|
||||
{ is_admin: 'desc' },
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
],
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
is_admin: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
return prisma.client.account.findMany({
|
||||
orderBy: [
|
||||
{ is_admin: 'desc' },
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
}),
|
||||
);
|
||||
],
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
is_admin: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateAccount(user_id, data) {
|
||||
return runQuery(
|
||||
prisma.account.update({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.update({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function createWebsite(user_id, data) {
|
||||
return runQuery(
|
||||
prisma.website.create({
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
account: {
|
||||
connect: {
|
||||
|
|
@ -12,12 +12,12 @@ export async function createWebsite(user_id, data) {
|
|||
},
|
||||
...data,
|
||||
},
|
||||
}),
|
||||
).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
|
||||
}
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,28 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function deleteWebsite(website_id) {
|
||||
return runQuery(
|
||||
prisma
|
||||
.$transaction([
|
||||
prisma.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
prisma.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
prisma.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
prisma.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
prisma.website.delete({
|
||||
where: { website_id },
|
||||
}),
|
||||
])
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
}),
|
||||
);
|
||||
const { client, multiQuery } = prisma;
|
||||
|
||||
return multiQuery([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
client.website.delete({
|
||||
where: { website_id },
|
||||
}),
|
||||
]).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,23 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAllWebsites() {
|
||||
let data = await runQuery(
|
||||
prisma.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
user_id: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
let data = await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
user_id: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return data.map(i => ({ ...i, account: i.account.username }));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUserWebsites(user_id) {
|
||||
return runQuery(
|
||||
prisma.website.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteById(website_id) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteByShareId(share_id) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
share_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
share_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteByUuid(website_uuid) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,25 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function resetWebsite(website_id) {
|
||||
return runQuery(prisma.$queryRaw`delete from session where website_id=${website_id}`);
|
||||
const { client, multiQuery } = prisma;
|
||||
|
||||
return multiQuery([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
]).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateWebsite(website_id, data) {
|
||||
return runQuery(
|
||||
prisma.website.update({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { getDateQuery, getFilterQuery, rawQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getEventMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
@ -18,11 +17,11 @@ async function relationalQuery(
|
|||
unit = 'day',
|
||||
filters = {},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
|
|
@ -31,8 +30,7 @@ async function relationalQuery(
|
|||
and created_at between $2 and $3
|
||||
${getFilterQuery('event', filters, params)}
|
||||
group by 1, 2
|
||||
order by 2
|
||||
`,
|
||||
order by 2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -45,21 +43,20 @@ async function clickhouseQuery(
|
|||
unit = 'day',
|
||||
filters = {},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates } = prisma;
|
||||
const params = [website_id];
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${clickhouse.getDateQuery('created_at', unit, timezone)} t,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where website_id= $1
|
||||
and ${clickhouse.getBetweenDates('created_at', start_at, end_at)}
|
||||
and ${getBetweenDates('created_at', start_at, end_at)}
|
||||
${clickhouse.getFilterQuery('event', filters, params)}
|
||||
group by x, t
|
||||
order by t
|
||||
`,
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,34 @@
|
|||
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 function getEvents(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.event.findMany({
|
||||
where: {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
},
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
return prisma.client.event.findMany({
|
||||
where: {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function clickhouseQuery(websites, start_at) {
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_id,
|
||||
website_id,
|
||||
session_id,
|
||||
|
|
@ -39,7 +37,6 @@ function clickhouseQuery(websites, start_at) {
|
|||
event_name
|
||||
from event
|
||||
where website_id in (${websites.join[',']}
|
||||
and created_at >= ${clickhouse.getDateFormat(start_at)})
|
||||
`,
|
||||
and created_at >= ${getDateFormat(start_at)})`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { CLICKHOUSE, KAFKA, RELATIONAL, URL_LENGTH } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import kafka from 'lib/kafka';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
import { URL_LENGTH, EVENT_NAME_LENGTH } from 'lib/constants';
|
||||
|
||||
export async function saveEvent(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
|
|
@ -16,8 +16,8 @@ async function relationalQuery(website_id, { session_id, url, event_name, event_
|
|||
const data = {
|
||||
website_id,
|
||||
session_id,
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
event_name: event_name?.substr(0, 50),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
|
||||
};
|
||||
|
||||
if (event_data) {
|
||||
|
|
@ -28,39 +28,38 @@ async function relationalQuery(website_id, { session_id, url, event_name, event_
|
|||
};
|
||||
}
|
||||
|
||||
return runQuery(
|
||||
prisma.event.create({
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.event.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
const params = [
|
||||
website_id,
|
||||
event_uuid,
|
||||
session_uuid,
|
||||
url?.substr(0, URL_LENGTH),
|
||||
event_name?.substr(0, 50),
|
||||
url?.substring(0, URL_LENGTH),
|
||||
event_name?.substring(0, EVENT_NAME_LENGTH),
|
||||
];
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
insert into umami.event (created_at, website_id, session_uuid, url, event_name)
|
||||
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4);`,
|
||||
return rawQuery(
|
||||
`insert into umami.event (created_at, website_id, session_uuid, url, event_name)
|
||||
values (${getDateFormat(new Date())}, $1, $2, $3, $4);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
event_uuid: event_uuid,
|
||||
website_id: website_id,
|
||||
session_uuid: session_uuid,
|
||||
created_at: kafka.getDateFormat(new Date()),
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
event_name: event_name?.substr(0, 50),
|
||||
created_at: getDateFormat(new Date()),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
|
||||
};
|
||||
|
||||
await kafka.sendMessage(params, 'event');
|
||||
await sendMessage(params, 'event');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { parseFilters, rawQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviewMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, column, table, filters = {}) {
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
|
|
@ -20,8 +20,7 @@ async function relationalQuery(website_id, start_at, end_at, column, table, filt
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${column} x, count(*) y
|
||||
`select ${column} x, count(*) y
|
||||
from ${table}
|
||||
${joinSession}
|
||||
where ${table}.website_id=$1
|
||||
|
|
@ -30,15 +29,15 @@ async function relationalQuery(website_id, start_at, end_at, column, table, filt
|
|||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
`,
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, column, table, filters = {}) {
|
||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = clickhouse.parseFilters(
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
column,
|
||||
filters,
|
||||
|
|
@ -46,19 +45,17 @@ async function clickhouseQuery(website_id, start_at, end_at, column, table, filt
|
|||
'session_uuid',
|
||||
);
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select ${column} x, count(*) y
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from ${table}
|
||||
${joinSession}
|
||||
where ${table}.website_id= $1
|
||||
and ${clickhouse.getBetweenDates(table + '.created_at', start_at, end_at)}
|
||||
and ${getBetweenDates(table + '.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by x
|
||||
order by y desc
|
||||
`,
|
||||
order by y desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
import { parseFilters, rawQuery, runAnalyticsQuery } from 'lib/relational';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviewParams(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, column, table, filters = {}) {
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
|
|
@ -18,8 +19,7 @@ async function relationalQuery(website_id, start_at, end_at, column, table, filt
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select url x,
|
||||
`select url x,
|
||||
count(*) y
|
||||
from ${table}
|
||||
${joinSession}
|
||||
|
|
@ -30,8 +30,7 @@ async function relationalQuery(website_id, start_at, end_at, column, table, filt
|
|||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
`,
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { getDateQuery, parseFilters, rawQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviewStats(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
@ -20,6 +19,7 @@ async function relationalQuery(
|
|||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
) {
|
||||
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
|
|
@ -29,8 +29,7 @@ async function relationalQuery(
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
||||
`select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
from pageview
|
||||
${joinSession}
|
||||
|
|
@ -38,8 +37,7 @@ async function relationalQuery(
|
|||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1
|
||||
`,
|
||||
group by 1`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -54,6 +52,7 @@ async function clickhouseQuery(
|
|||
filters = {},
|
||||
sessionKey = 'session_uuid',
|
||||
) {
|
||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
|
|
@ -63,24 +62,22 @@ async function clickhouseQuery(
|
|||
sessionKey,
|
||||
);
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
${clickhouse.getDateStringQuery('g.t', unit)} as t,
|
||||
return rawQuery(
|
||||
`select
|
||||
${getDateStringQuery('g.t', unit)} as t,
|
||||
g.y as y
|
||||
from
|
||||
(select
|
||||
${clickhouse.getDateQuery('created_at', unit, timezone)} t,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
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 t) g
|
||||
order by t
|
||||
`,
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,32 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviews(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.pageview.findMany({
|
||||
where: {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
},
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
return prisma.client.pageview.findMany({
|
||||
where: {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
`select
|
||||
view_id,
|
||||
website_id,
|
||||
session_id,
|
||||
|
|
@ -38,7 +34,6 @@ async function clickhouseQuery(websites, start_at) {
|
|||
url
|
||||
from pageview
|
||||
where website_id in (${websites.join[',']}
|
||||
and created_at >= ${clickhouse.getDateFormat(start_at)})
|
||||
`,
|
||||
and created_at >= ${clickhouse.getDateFormat(start_at)})`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,54 +1,52 @@
|
|||
import { CLICKHOUSE, KAFKA, RELATIONAL, URL_LENGTH } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import { prisma, runQuery } from 'lib/relational';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
import { URL_LENGTH } from 'lib/constants';
|
||||
|
||||
export async function savePageView(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, { session_id, url, referrer }) {
|
||||
return runQuery(
|
||||
prisma.pageview.create({
|
||||
data: {
|
||||
website_id,
|
||||
session_id,
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
referrer: referrer?.substr(0, URL_LENGTH),
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.pageview.create({
|
||||
data: {
|
||||
website_id,
|
||||
session_id,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, { session_uuid, url, referrer }) {
|
||||
const params = [
|
||||
website_id,
|
||||
session_uuid,
|
||||
url?.substr(0, URL_LENGTH),
|
||||
referrer?.substr(0, URL_LENGTH),
|
||||
url?.substring(0, URL_LENGTH),
|
||||
referrer?.substring(0, URL_LENGTH),
|
||||
];
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
insert into umami.pageview (created_at, website_id, session_uuid, url, referrer)
|
||||
`insert into umami.pageview (created_at, website_id, session_uuid, url, referrer)
|
||||
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { session_uuid, url, referrer }) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
website_id: website_id,
|
||||
session_uuid: session_uuid,
|
||||
created_at: kafka.getDateFormat(new Date()),
|
||||
url: url?.substr(0, URL_LENGTH),
|
||||
referrer: referrer?.substr(0, URL_LENGTH),
|
||||
created_at: getDateFormat(new Date()),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
};
|
||||
|
||||
await kafka.sendMessage(params, 'pageview');
|
||||
await sendMessage(params, 'pageview');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)}`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
import { subMinutes } from 'date-fns';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { rawQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getActiveVisitors(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
@ -15,27 +14,24 @@ async function relationalQuery(website_id) {
|
|||
const date = subMinutes(new Date(), 5);
|
||||
const params = [website_id, date];
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select count(distinct session_id) x
|
||||
return prisma.rawQuery(
|
||||
`select count(distinct session_id) x
|
||||
from pageview
|
||||
where website_id = $1
|
||||
and created_at >= $2
|
||||
`,
|
||||
and created_at >= $2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id) {
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
const params = [website_id];
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select count(distinct session_uuid) x
|
||||
return rawQuery(
|
||||
`select count(distinct session_uuid) x
|
||||
from pageview
|
||||
where website_id = $1
|
||||
and created_at >= ${clickhouse.getDateFormat(subMinutes(new Date(), 5))}
|
||||
`,
|
||||
and created_at >= ${getDateFormat(subMinutes(new Date(), 5))}`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { getDateQuery, getTimestampInterval, parseFilters, rawQuery } from 'lib/relational';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getWebsiteStats(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, filters = {}) {
|
||||
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
|
|
@ -20,32 +20,31 @@ async function relationalQuery(website_id, start_at, end_at, filters = {}) {
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select sum(t.c) as "pageviews",
|
||||
`select sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(t.time) as "totaltime"
|
||||
from (
|
||||
select pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
count(*) c,
|
||||
${getTimestampInterval('pageview.created_at')} as "time"
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1, 2
|
||||
) t
|
||||
`,
|
||||
select pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
count(*) c,
|
||||
${getTimestampInterval('pageview.created_at')} as "time"
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1, 2
|
||||
) t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, filters = {}) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = clickhouse.parseFilters(
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
null,
|
||||
filters,
|
||||
|
|
@ -53,28 +52,26 @@ async function clickhouseQuery(website_id, start_at, end_at, filters = {}) {
|
|||
'session_uuid',
|
||||
);
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select
|
||||
sum(t.c) as "pageviews",
|
||||
count(distinct t.session_uuid) as "uniques",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
||||
from (
|
||||
select pageview.session_uuid,
|
||||
${clickhouse.getDateQuery('pageview.created_at', 'day')} time_series,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id = $1
|
||||
and ${clickhouse.getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by pageview.session_uuid, time_series
|
||||
) t;
|
||||
`,
|
||||
return rawQuery(
|
||||
`select
|
||||
sum(t.c) as "pageviews",
|
||||
count(distinct t.session_uuid) as "uniques",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
||||
from (
|
||||
select pageview.session_uuid,
|
||||
${getDateQuery('pageview.created_at', 'day')} time_series,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id = $1
|
||||
and ${getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by pageview.session_uuid, time_series
|
||||
) t;`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue