mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 07:37:11 +01:00
Initial Typescript models.
This commit is contained in:
parent
04e9f06e93
commit
0aaba8cbd1
74 changed files with 1144 additions and 768 deletions
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUser(data) {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
20
queries/admin/user/createUser.ts
Normal file
20
queries/admin/user/createUser.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUser(data: {
|
||||
id: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{
|
||||
id: string;
|
||||
username: string;
|
||||
isAdmin: boolean;
|
||||
}> {
|
||||
return prisma.client.user.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
isAdmin: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma, User } from '@prisma/client';
|
||||
|
||||
export async function deleteUser(userId) {
|
||||
export async function deleteUser(
|
||||
userId: string,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
|
||||
const { client } = prisma;
|
||||
|
||||
const websites = await client.website.findMany({
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUser(where) {
|
||||
return prisma.client.user.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
25
queries/admin/user/getUser.ts
Normal file
25
queries/admin/user/getUser.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
isAdmin: boolean;
|
||||
password?: string;
|
||||
createdAt?: Date;
|
||||
}
|
||||
|
||||
export async function getUser(
|
||||
where: Prisma.UserWhereUniqueInput,
|
||||
includePassword = false,
|
||||
): Promise<User> {
|
||||
return prisma.client.user.findUnique({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
isAdmin: true,
|
||||
password: includePassword,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { User } from './getUser';
|
||||
|
||||
export async function getUsers() {
|
||||
export async function getUsers(): Promise<User[]> {
|
||||
return prisma.client.user.findMany({
|
||||
orderBy: [
|
||||
{ isAdmin: 'desc' },
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateUser(data, where) {
|
||||
return prisma.client.user.update({
|
||||
where,
|
||||
data,
|
||||
});
|
||||
}
|
||||
19
queries/admin/user/updateUser.ts
Normal file
19
queries/admin/user/updateUser.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { Prisma } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
import { User } from './getUser';
|
||||
|
||||
export async function updateUser(
|
||||
data: Prisma.UserUpdateArgs,
|
||||
where: Prisma.UserWhereUniqueInput,
|
||||
): Promise<User> {
|
||||
return prisma.client.user.update({
|
||||
where,
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
isAdmin: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +1,16 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { Website } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createWebsite(userId, data) {
|
||||
export async function createWebsite(
|
||||
userId: string,
|
||||
data: {
|
||||
id: string;
|
||||
name: string;
|
||||
domain: string;
|
||||
shareId?: string;
|
||||
},
|
||||
): Promise<Website> {
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
|
|
@ -1,22 +1,25 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma, Website } from '@prisma/client';
|
||||
|
||||
export async function deleteWebsite(id) {
|
||||
export async function deleteWebsite(
|
||||
websiteId: string,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: id },
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId: id },
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.website.delete({
|
||||
where: { id },
|
||||
where: { id: websiteId },
|
||||
}),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.deleteWebsite(id);
|
||||
await cache.deleteWebsite(websiteId);
|
||||
}
|
||||
|
||||
return data;
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAllWebsites() {
|
||||
let data = await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
userId: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return data.map(i => ({ ...i, user: i.user.username }));
|
||||
}
|
||||
24
queries/admin/website/getAllWebsites.ts
Normal file
24
queries/admin/website/getAllWebsites.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { Website } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAllWebsites(): Promise<(Website & { user: string })[]> {
|
||||
return await prisma.client.website
|
||||
.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
userId: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(data => data.map(i => ({ ...i, user: i.user.username })));
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { Website } from '@prisma/client';
|
||||
|
||||
export async function getUserWebsites(userId) {
|
||||
export async function getUserWebsites(userId): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsite(where) {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
8
queries/admin/website/getWebsite.ts
Normal file
8
queries/admin/website/getWebsite.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { Prisma, Website } from '@prisma/client';
|
||||
|
||||
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,20 +1,23 @@
|
|||
import { Prisma, Website } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import prisma from 'lib/prisma';
|
||||
import { getWebsite } from 'queries';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function resetWebsite(id) {
|
||||
export async function resetWebsite(
|
||||
websiteId,
|
||||
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
||||
const { client, transaction } = prisma;
|
||||
|
||||
const { revId } = await getWebsite({ id });
|
||||
const { revId } = await getWebsite({ id: websiteId });
|
||||
|
||||
return transaction([
|
||||
client.websiteEvent.deleteMany({
|
||||
where: { websiteId: id },
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { websiteId: id },
|
||||
where: { websiteId },
|
||||
}),
|
||||
client.website.update({ where: { id }, data: { revId: revId + 1 } }),
|
||||
client.website.update({ where: { id: websiteId }, data: { revId: revId + 1 } }),
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data[2]);
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateWebsite(id, data) {
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
11
queries/admin/website/updateWebsite.ts
Normal file
11
queries/admin/website/updateWebsite.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { Prisma, Website } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateWebsite(websiteId, data: Prisma.WebsiteUpdateInput): Promise<Website> {
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
id: websiteId,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
@ -2,8 +2,21 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { WebsiteMetric } from 'interface/api/models';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function getEventData(...args) {
|
||||
export async function getEventData(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
event_name: string;
|
||||
columns: any;
|
||||
filters: object;
|
||||
},
|
||||
]
|
||||
): Promise<WebsiteMetric[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
|
|
@ -14,31 +27,48 @@ export async function getEventData(...args) {
|
|||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, { startDate, endDate, event_name, columns, filters }) {
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
event_name: string;
|
||||
columns: any;
|
||||
filters: object;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, event_name, columns, filters } = data;
|
||||
const { rawQuery, getEventDataColumnsQuery, getEventDataFilterQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
${getEventDataColumnsQuery('event_data.event_data', columns)}
|
||||
from event
|
||||
join website
|
||||
on event.website_id = website.website_id
|
||||
join event_data
|
||||
on event.event_id = event_data.event_id
|
||||
where website.website_id ='${websiteId}'
|
||||
and event.created_at between $1 and $2
|
||||
${getEventDataColumnsQuery('event_data', columns)}
|
||||
from website_event
|
||||
where website_id ='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
and event_type = ${UmamiApi.EventType.Event}
|
||||
${event_name ? `and event_name = ${event_name}` : ''}
|
||||
${
|
||||
Object.keys(filters).length > 0
|
||||
? `and ${getEventDataFilterQuery('event_data.event_data', filters)}`
|
||||
? `and ${getEventDataFilterQuery('event_data', filters)}`
|
||||
: ''
|
||||
}`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, event_name, columns, filters }) {
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
event_name: string;
|
||||
columns: any;
|
||||
filters: object;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, event_name, columns, filters } = data;
|
||||
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
|
||||
clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
|
@ -50,6 +80,7 @@ async function clickhouseQuery(websiteId, { startDate, endDate, event_name, colu
|
|||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${UmamiApi.EventType.Event}
|
||||
${event_name ? `and event_name = ${event_name}` : ''}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getEventMetrics(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId,
|
||||
start_at,
|
||||
end_at,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
filters = {},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const params = [start_at, end_at];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('event.created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
join website
|
||||
on event.website_id = website.website_id
|
||||
where website.website_id='${websiteId}'
|
||||
and event.created_at between $1 and $2
|
||||
${getFilterQuery('event', filters, params)}
|
||||
group by 1, 2
|
||||
order by 2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
start_at,
|
||||
end_at,
|
||||
timezone = 'UTC',
|
||||
unit = 'day',
|
||||
filters = {},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where event_name != ''
|
||||
and website_id = $1
|
||||
and rev_id = $2
|
||||
and ${getBetweenDates('created_at', start_at, end_at)}
|
||||
${getFilterQuery('event', filters, params)}
|
||||
group by x, t
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
105
queries/analytics/event/getEventMetrics.ts
Normal file
105
queries/analytics/event/getEventMetrics.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { WebsiteEventMetric } from 'interface/api/models';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function getEventMetrics(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone: string;
|
||||
unit: string;
|
||||
filters: {
|
||||
url: string;
|
||||
eventName: string;
|
||||
};
|
||||
},
|
||||
]
|
||||
): Promise<WebsiteEventMetric[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
{
|
||||
startDate,
|
||||
endDate,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
filters,
|
||||
}: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone: string;
|
||||
unit: string;
|
||||
filters: {
|
||||
url: string;
|
||||
eventName: string;
|
||||
};
|
||||
},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from website_event
|
||||
where website_id='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
and event_type = ${UmamiApi.EventType.Event}
|
||||
${getFilterQuery(filters, params)}
|
||||
group by 1, 2
|
||||
order by 2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
{
|
||||
startDate,
|
||||
endDate,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
filters,
|
||||
}: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone: string;
|
||||
unit: string;
|
||||
filters: {
|
||||
url: string;
|
||||
eventName: string;
|
||||
};
|
||||
},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${UmamiApi.EventType.Event}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${getFilterQuery(filters, params)}
|
||||
group by x, t
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export function getEvents(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(websites, start_at) {
|
||||
return prisma.client.event.findMany({
|
||||
where: {
|
||||
websiteId: {
|
||||
in: websites,
|
||||
},
|
||||
createdAt: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function clickhouseQuery(websites, start_at) {
|
||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
event_id,
|
||||
website_id,
|
||||
session_id,
|
||||
created_at,
|
||||
url,
|
||||
event_name
|
||||
from event
|
||||
where event_name != ''
|
||||
and ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${getDateFormat(start_at)}`,
|
||||
);
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function saveEvent(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(data) {
|
||||
const { websiteId, sessionId, url, eventName, eventData } = data;
|
||||
const eventId = uuid();
|
||||
|
||||
const params = {
|
||||
id: eventId,
|
||||
websiteId,
|
||||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
};
|
||||
|
||||
if (eventData) {
|
||||
params.eventData = {
|
||||
create: {
|
||||
id: eventId,
|
||||
eventData: eventData,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return prisma.client.event.create({
|
||||
data: params,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(data) {
|
||||
const { websiteId, id: sessionId, url, eventName, eventData, country, ...args } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
||||
const params = {
|
||||
website_id: websiteId,
|
||||
session_id: sessionId,
|
||||
event_id: uuid(),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
event_data: eventData ? JSON.stringify(eventData) : null,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
country: country ? country : null,
|
||||
...args,
|
||||
};
|
||||
|
||||
await sendMessage(params, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
91
queries/analytics/event/saveEvent.ts
Normal file
91
queries/analytics/event/saveEvent.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import cache from 'lib/cache';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function saveEvent(args: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
url: string;
|
||||
referrer?: string;
|
||||
eventName?: string;
|
||||
eventData?: any;
|
||||
hostname?: string;
|
||||
browser?: string;
|
||||
os?: string;
|
||||
device?: string;
|
||||
screen?: string;
|
||||
language?: string;
|
||||
country?: string;
|
||||
}) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(data: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
url: string;
|
||||
referrer?: string;
|
||||
eventName?: string;
|
||||
eventData?: any;
|
||||
}) {
|
||||
const { websiteId, id: sessionId, url, eventName, eventData, referrer } = data;
|
||||
|
||||
const params = {
|
||||
id: uuid(),
|
||||
websiteId,
|
||||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
eventType: UmamiApi.EventType.Event,
|
||||
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
eventData,
|
||||
};
|
||||
|
||||
return prisma.client.websiteEvent.create({
|
||||
data: params,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(data: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
url: string;
|
||||
referrer?: string;
|
||||
eventName?: string;
|
||||
eventData?: any;
|
||||
hostname?: string;
|
||||
browser?: string;
|
||||
os?: string;
|
||||
device?: string;
|
||||
screen?: string;
|
||||
language?: string;
|
||||
country?: string;
|
||||
}) {
|
||||
const { websiteId, id: sessionId, url, eventName, eventData, country, ...args } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
||||
const params = {
|
||||
website_id: websiteId,
|
||||
session_id: sessionId,
|
||||
event_id: uuid(),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
||||
event_data: eventData ? JSON.stringify(eventData) : null,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
country: country ? country : null,
|
||||
...args,
|
||||
};
|
||||
|
||||
await sendMessage(params, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getPageviewMetrics(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, { startDate, endDate, column, table, filters = {} }) {
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
column,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from ${table}
|
||||
${` join website on ${table}.website_id = website.website_id`}
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and ${table}.created_at between $1 and $2
|
||||
${pageviewQuery}
|
||||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, column, filters = {} }) {
|
||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery, eventQuery } = parseFilters(column, filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${eventQuery}
|
||||
group by x
|
||||
order by y desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
82
queries/analytics/pageview/getPageviewMetrics.ts
Normal file
82
queries/analytics/pageview/getPageviewMetrics.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function getPageviewMetrics(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
table: string;
|
||||
filters: object;
|
||||
},
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
filters: object;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, column, filters = {} } = data;
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
where website_id='${websiteId}'
|
||||
and website_event.created_at between $1 and $2
|
||||
and event_type = ${UmamiApi.EventType.Pageview}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
filters: object;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, column, filters = {} } = data;
|
||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${UmamiApi.EventType.Pageview}
|
||||
${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviewParams(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, start_at, end_at, column, table, filters = {}) {
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
column,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
|
||||
return rawQuery(
|
||||
`select url x,
|
||||
count(*) y
|
||||
from ${table}
|
||||
${` join website on ${table}.website_id = website.website_id`}
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and ${table}.created_at between $1 and $2
|
||||
and ${table}.url like '%?%'
|
||||
${pageviewQuery}
|
||||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
function clickhouseQuery() {
|
||||
return Promise.reject(new Error('Not implemented.'));
|
||||
}
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getPageviewStats(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId,
|
||||
{
|
||||
start_at,
|
||||
end_at,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
count = '*',
|
||||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
},
|
||||
) {
|
||||
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const params = [start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
null,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
|
||||
return rawQuery(
|
||||
`select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
from pageview
|
||||
join website
|
||||
on pageview.website_id = website.website_id
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
{ start_at, end_at, timezone = 'UTC', unit = 'day', count = '*', filters = {} },
|
||||
) {
|
||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
${getDateStringQuery('g.t', unit)} as t,
|
||||
g.y as y
|
||||
from
|
||||
(select
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? 'distinct session_id' : count}) y
|
||||
from event
|
||||
where event_name = ''
|
||||
and website_id = $1
|
||||
and rev_id = $2
|
||||
and ${getBetweenDates('created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by t) g
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
102
queries/analytics/pageview/getPageviewStats.ts
Normal file
102
queries/analytics/pageview/getPageviewStats.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import cache from 'lib/cache';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function getPageviewStats(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
unit?: string;
|
||||
count?: string;
|
||||
filters: object;
|
||||
sessionKey?: string;
|
||||
},
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
unit?: string;
|
||||
count?: string;
|
||||
filters: object;
|
||||
sessionKey?: string;
|
||||
},
|
||||
) {
|
||||
const {
|
||||
startDate,
|
||||
endDate,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
count = '*',
|
||||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
} = data;
|
||||
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${getDateQuery('website_event.created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
and event_type = ${UmamiApi.EventType.Pageview}
|
||||
${filterQuery}
|
||||
group by 1`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
unit?: string;
|
||||
count?: string;
|
||||
filters: object;
|
||||
sessionKey?: string;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
|
||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
${getDateStringQuery('g.t', unit)} as t,
|
||||
g.y as y
|
||||
from
|
||||
(select
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? 'distinct session_id' : count}) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${UmamiApi.EventType.Pageview}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by t) g
|
||||
order by t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getPageviews(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return prisma.client.pageview.findMany({
|
||||
where: {
|
||||
websiteId: {
|
||||
in: websites,
|
||||
},
|
||||
createdAt: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
const { rawQuery, getCommaSeparatedStringFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
website_id,
|
||||
session_id,
|
||||
created_at,
|
||||
url
|
||||
from event
|
||||
where event_name = ''
|
||||
and ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${clickhouse.getDateFormat(start_at)}`,
|
||||
);
|
||||
}
|
||||
|
|
@ -4,23 +4,43 @@ import kafka from 'lib/kafka';
|
|||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { UmamiApi } from 'interface/enum';
|
||||
|
||||
export async function savePageView(...args) {
|
||||
export async function savePageView(args: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
url: string;
|
||||
referrer?: string;
|
||||
hostname?: string;
|
||||
browser?: string;
|
||||
os?: string;
|
||||
device?: string;
|
||||
screen?: string;
|
||||
language?: string;
|
||||
country?: string;
|
||||
}) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(data) {
|
||||
const { websiteId, sessionId, url, referrer } = data;
|
||||
return prisma.client.pageview.create({
|
||||
async function relationalQuery(data: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
url: string;
|
||||
referrer?: string;
|
||||
}) {
|
||||
const { websiteId, id: sessionId, url, referrer } = data;
|
||||
|
||||
return prisma.client.websiteEvent.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
websiteId,
|
||||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
eventType: UmamiApi.EventType.Pageview,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -2,11 +2,12 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export async function createSession(...args) {
|
||||
export async function createSession(args: Prisma.SessionCreateInput) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
}).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeSession(data);
|
||||
|
|
@ -16,11 +17,21 @@ export async function createSession(...args) {
|
|||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(data) {
|
||||
async function relationalQuery(data: Prisma.SessionCreateInput) {
|
||||
return prisma.client.session.create({ data });
|
||||
}
|
||||
|
||||
async function clickhouseQuery(data) {
|
||||
async function clickhouseQuery(data: {
|
||||
id: string;
|
||||
websiteId: string;
|
||||
hostname?: string;
|
||||
browser?: string;
|
||||
os?: string;
|
||||
device?: string;
|
||||
screen?: string;
|
||||
language?: string;
|
||||
country?: string;
|
||||
}) {
|
||||
const { id, websiteId, hostname, browser, os, device, screen, language, country } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export async function getSession(...args) {
|
||||
export async function getSession(args: { id: string }) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(where) {
|
||||
async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
|
||||
return prisma.client.session.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery({ id: sessionId }) {
|
||||
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
||||
const { rawQuery, findFirst } = clickhouse;
|
||||
const params = [sessionId];
|
||||
|
||||
|
|
@ -3,17 +3,26 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getSessionMetrics(...args) {
|
||||
export async function getSessionMetrics(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, { startDate, endDate, field, filters = {} }) {
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(null, filters, params);
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
|
|
@ -26,8 +35,7 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
|
|||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${filterQuery}
|
||||
)
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
|
|
@ -35,11 +43,15 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
|
|||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, field, filters = {} }) {
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
|
|
@ -48,8 +60,7 @@ async function clickhouseQuery(websiteId, { startDate, endDate, field, filters =
|
|||
and rev_id = $2
|
||||
and event_name = ''
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc`,
|
||||
params,
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
||||
|
||||
export async function getSessions(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return prisma.client.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
? {
|
||||
websiteId: {
|
||||
in: websites,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
createdAt: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select distinct
|
||||
session_id,
|
||||
website_id,
|
||||
created_at,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country
|
||||
from event
|
||||
where ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${getDateFormat(start_at)}`,
|
||||
);
|
||||
}
|
||||
|
|
@ -3,14 +3,14 @@ import prisma from 'lib/prisma';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getActiveVisitors(...args) {
|
||||
export async function getActiveVisitors(...args: [websiteId: string]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId) {
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params = [date];
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ async function relationalQuery(websiteId) {
|
|||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId) {
|
||||
async function clickhouseQuery(websiteId: string) {
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
const params = [websiteId];
|
||||
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
import { getPageviews } from '../pageview/getPageviews';
|
||||
import { getSessions } from '../session/getSessions';
|
||||
import { getEvents } from '../event/getEvents';
|
||||
|
||||
export async function getRealtimeData(websites, time) {
|
||||
const [pageviews, sessions, events] = await Promise.all([
|
||||
getPageviews(websites, time),
|
||||
getSessions(websites, time),
|
||||
getEvents(websites, time),
|
||||
]);
|
||||
|
||||
return {
|
||||
pageviews: pageviews.map(({ id, ...props }) => ({
|
||||
__id: `p${id}`,
|
||||
pageviewId: id,
|
||||
...props,
|
||||
})),
|
||||
sessions: sessions.map(({ id, ...props }) => ({
|
||||
__id: `s${id}`,
|
||||
sessionId: id,
|
||||
...props,
|
||||
})),
|
||||
events: events.map(({ id, ...props }) => ({
|
||||
__id: `e${id}`,
|
||||
eventId: id,
|
||||
...props,
|
||||
})),
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
}
|
||||
3
queries/analytics/stats/getRealtimeData.ts
Normal file
3
queries/analytics/stats/getRealtimeData.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export async function getRealtimeData() {
|
||||
throw new Error('Not Implemented');
|
||||
}
|
||||
|
|
@ -3,22 +3,23 @@ import clickhouse from 'lib/clickhouse';
|
|||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getWebsiteStats(...args) {
|
||||
export async function getWebsiteStats(
|
||||
...args: [websiteId: string, data: { startDate: Date; endDate: Date; filters: object }]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = data;
|
||||
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const params = [start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
null,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
const params = [startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select sum(t.c) as "pageviews",
|
||||
|
|
@ -36,19 +37,22 @@ async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
|
|||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
) t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId, { start_at, end_at, filters = {} }) {
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = data;
|
||||
const { rawQuery, getDateQuery, getBetweenDates, parseFilters } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
|
|
@ -66,9 +70,8 @@ async function clickhouseQuery(websiteId, { start_at, end_at, filters = {} }) {
|
|||
where event_name = ''
|
||||
and website_id = $1
|
||||
and rev_id = $2
|
||||
and ${getBetweenDates('created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by session_id, time_series
|
||||
) t;`,
|
||||
params,
|
||||
Loading…
Add table
Add a link
Reference in a new issue