* Initial Typescript models.

* Re-add realtime data

* get distinct sessions for session metrics

* Add queries for new schema.

* Fix Typo.

* Add some api/team endpoints.

* Fix destructure error.

* Fix getWebsites call.

* Ignore typescript build errors.

* Fix enum issue.

* add clickhouse route to deleteWebsite

* Fix Website auth.

* Updated lint-staged config.

* Add permission checks.

* Add user role api.

* Fix error when updating website.

* Fix isAdmin check.  Fix Schema.

* Initial conversion to react-basics.

* Remove user/team transfer from website update.

* delete website in relational query

* Fix login secure token creation.

* Add event type to event.

* Allow user to be added to team with role.

* Updated login form.

* Add Role to TeamUser.

* Add database migration.

* Refactored permissions check. Updated redis lib.

* Feat/um 114 roles and permissions (#1683)

* Auth checkpoint.

* Merge branch 'dev' into feat/um-114-roles-and-permissions

* Add 02 migration.

* Added lib/types.

* Updated schema.

* Updated roles and permissions logic.

* Implement react-basics styles. Fix queries.

* Update website details layout.

* Add 01 migration.

* Fix admin create.

* Update react-basics.

Co-authored-by: Francis Cao <franciscao@gmail.com>
Co-authored-by: Mike Cao <mike@mikecao.com>
Co-authored-by: Mike Cao <moocao@gmail.com>
This commit is contained in:
Brian Cao 2022-12-12 19:45:38 -08:00 committed by GitHub
parent 94848cc41b
commit 8732d056dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
165 changed files with 3370 additions and 6268 deletions

72
queries/admin/team.ts Normal file
View file

@ -0,0 +1,72 @@
import { Prisma, Team } from '@prisma/client';
import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
import { ROLES } from 'lib/constants';
import { Website } from 'lib/types';
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
return prisma.client.team.findFirst({
where,
});
}
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
return prisma.client.team.findMany({
where,
});
}
export async function getTeamWebsites(teamId: string): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
teamId,
},
orderBy: [
{
name: 'asc',
},
],
});
}
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
const { id, userId } = data;
return prisma.transaction([
prisma.client.team.create({
data,
}),
prisma.client.teamUser.create({
data: {
id: uuid(),
teamId: id,
userId,
role: ROLES.teamOwner,
},
}),
]);
}
export async function updateTeam(
data: Prisma.TeamUpdateInput,
where: Prisma.TeamWhereUniqueInput,
): Promise<Team> {
return prisma.client.team.update({
data: {
...data,
updatedAt: new Date(),
},
where,
});
}
export async function deleteTeam(teamId: string): Promise<Team> {
return prisma.client.team.update({
data: {
deletedAt: new Date(),
},
where: {
id: teamId,
},
});
}

56
queries/admin/teamUser.ts Normal file
View file

@ -0,0 +1,56 @@
import { Prisma, TeamUser } from '@prisma/client';
import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma';
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
return prisma.client.teamUser.findFirst({
where: {
teamId,
userId,
},
});
}
export async function getTeamUsers(teamId: string): Promise<TeamUser[]> {
return prisma.client.teamUser.findMany({
where: {
teamId,
},
});
}
export async function createTeamUser(
userId: string,
teamId: string,
role: string,
): Promise<TeamUser> {
return prisma.client.teamUser.create({
data: {
id: uuid(),
userId,
teamId,
role,
},
});
}
export async function updateTeamUser(
data: Prisma.TeamUserUpdateInput,
where: Prisma.TeamUserWhereUniqueInput,
): Promise<TeamUser> {
return prisma.client.teamUser.update({
data,
where,
});
}
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
return prisma.client.teamUser.update({
data: {
deletedAt: new Date(),
},
where: {
id: teamUserId,
},
});
}

159
queries/admin/user.ts Normal file
View file

@ -0,0 +1,159 @@
import { Prisma, Team } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
import { Website } from 'lib/types';
export interface User {
id: string;
username: string;
password?: string;
createdAt?: Date;
}
export async function getUser(
where: Prisma.UserWhereUniqueInput,
options: { includePassword?: boolean } = {},
): Promise<User> {
const { includePassword = false } = options;
return prisma.client.user.findUnique({
where,
select: {
id: true,
username: true,
password: includePassword,
role: true,
},
});
}
export async function getUsers(): Promise<User[]> {
return prisma.client.user.findMany({
orderBy: [
{
username: 'asc',
},
],
select: {
id: true,
username: true,
role: true,
createdAt: true,
},
});
}
export async function getUserTeams(userId: string): Promise<Team[]> {
return prisma.client.teamUser
.findMany({
where: {
userId,
},
include: {
team: true,
},
})
.then(data => {
return data.map(a => a.team);
});
}
export async function getUserWebsites(userId: string): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
userId,
},
orderBy: [
{
name: 'asc',
},
],
});
}
export async function createUser(data: {
id: string;
username: string;
password: string;
role: string;
}): Promise<{
id: string;
username: string;
role: string;
}> {
return prisma.client.user.create({
data,
select: {
id: true,
username: true,
role: true,
},
});
}
export async function updateUser(
data: Prisma.UserUpdateInput,
where: Prisma.UserWhereUniqueInput,
): Promise<User> {
return prisma.client.user.update({
where,
data,
select: {
id: true,
username: true,
role: true,
createdAt: true,
},
});
}
export async function deleteUser(
userId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
const { client } = prisma;
const websites = await client.website.findMany({
where: { userId },
});
let websiteIds = [];
if (websites.length > 0) {
websiteIds = websites.map(a => a.id);
}
return prisma
.transaction([
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.website.updateMany({
data: {
deletedAt: new Date(),
},
where: { id: { in: websiteIds } },
}),
client.user.update({
data: {
deletedAt: new Date(),
},
where: {
id: userId,
},
}),
])
.then(async data => {
if (cache.enabled) {
const ids = websites.map(a => a.id);
for (let i = 0; i < ids.length; i++) {
await cache.deleteWebsite(`website:${ids[i]}`);
}
}
return data;
});
}

View file

@ -1,7 +0,0 @@
import prisma from 'lib/prisma';
export async function createUser(data) {
return prisma.client.user.create({
data,
});
}

View file

@ -1,45 +0,0 @@
import prisma from 'lib/prisma';
import cache from 'lib/cache';
export async function deleteUser(userId) {
const { client } = prisma;
const websites = await client.website.findMany({
where: { userId },
});
let websiteIds = [];
if (websites.length > 0) {
websiteIds = websites.map(a => a.id);
}
return client
.$transaction([
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.website.deleteMany({
where: { userId },
}),
client.user.delete({
where: {
id: userId,
},
}),
])
.then(async data => {
if (cache.enabled) {
const ids = websites.map(a => a.id);
for (let i = 0; i < ids.length; i++) {
await cache.deleteWebsite(`website:${ids[i]}`);
}
}
return data;
});
}

View file

@ -1,7 +0,0 @@
import prisma from 'lib/prisma';
export async function getUser(where) {
return prisma.client.user.findUnique({
where,
});
}

View file

@ -1,18 +0,0 @@
import prisma from 'lib/prisma';
export async function getUsers() {
return prisma.client.user.findMany({
orderBy: [
{ isAdmin: 'desc' },
{
username: 'asc',
},
],
select: {
id: true,
username: true,
isAdmin: true,
createdAt: true,
},
});
}

View file

@ -1,8 +0,0 @@
import prisma from 'lib/prisma';
export async function updateUser(data, where) {
return prisma.client.user.update({
where,
data,
});
}

110
queries/admin/website.ts Normal file
View file

@ -0,0 +1,110 @@
import { Prisma, Website } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
return prisma.client.website.findUnique({
where,
});
}
export async function getWebsites(): Promise<Website[]> {
return prisma.client.website.findMany({
orderBy: {
name: 'asc',
},
});
}
export async function createWebsite(
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
): Promise<Website> {
return prisma.client.website
.create({
data,
})
.then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data);
}
return data;
});
}
export async function updateWebsite(
websiteId,
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
): Promise<Website> {
return prisma.client.website.update({
where: {
id: websiteId,
},
data,
});
}
export async function resetWebsite(
websiteId,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
const { client, transaction } = prisma;
const { revId } = await getWebsite({ id: websiteId });
return transaction([
client.websiteEvent.deleteMany({
where: { websiteId },
}),
client.session.deleteMany({
where: { websiteId },
}),
client.website.update({ where: { id: websiteId }, data: { revId: revId + 1 } }),
]).then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data[2]);
}
return data;
});
}
export async function deleteWebsite(websiteId: string) {
return runQuery({
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
[CLICKHOUSE]: () => deleteWebsiteClickhouseQuery(websiteId),
});
}
async function deleteWebsiteRelationalQuery(
websiteId,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
const { client, transaction } = prisma;
return transaction([
client.websiteEvent.deleteMany({
where: { websiteId },
}),
client.session.deleteMany({
where: { websiteId },
}),
client.website.delete({
where: { id: websiteId },
}),
]).then(async data => {
if (cache.enabled) {
await cache.deleteWebsite(websiteId);
}
return data;
});
}
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
return prisma.client.website.update({
data: {
deletedAt: new Date(),
},
where: { id: websiteId },
});
}

View file

@ -1,23 +0,0 @@
import prisma from 'lib/prisma';
import cache from 'lib/cache';
export async function createWebsite(userId, data) {
return prisma.client.website
.create({
data: {
user: {
connect: {
id: userId,
},
},
...data,
},
})
.then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data);
}
return data;
});
}

View file

@ -1,24 +0,0 @@
import prisma from 'lib/prisma';
import cache from 'lib/cache';
export async function deleteWebsite(id) {
const { client, transaction } = prisma;
return transaction([
client.websiteEvent.deleteMany({
where: { websiteId: id },
}),
client.session.deleteMany({
where: { websiteId: id },
}),
client.website.delete({
where: { id },
}),
]).then(async data => {
if (cache.enabled) {
await cache.deleteWebsite(id);
}
return data;
});
}

View file

@ -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 }));
}

View file

@ -1,12 +0,0 @@
import prisma from 'lib/prisma';
export async function getUserWebsites(userId) {
return prisma.client.website.findMany({
where: {
userId,
},
orderBy: {
name: 'asc',
},
});
}

View file

@ -1,7 +0,0 @@
import prisma from 'lib/prisma';
export async function getWebsite(where) {
return prisma.client.website.findUnique({
where,
});
}

View file

@ -1,25 +0,0 @@
import prisma from 'lib/prisma';
import { getWebsite } from 'queries';
import cache from 'lib/cache';
export async function resetWebsite(id) {
const { client, transaction } = prisma;
const { revId } = await getWebsite({ id });
return transaction([
client.websiteEvent.deleteMany({
where: { websiteId: id },
}),
client.session.deleteMany({
where: { websiteId: id },
}),
client.website.update({ where: { id }, data: { revId: revId + 1 } }),
]).then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data[2]);
}
return data;
});
}

View file

@ -1,10 +0,0 @@
import prisma from 'lib/prisma';
export async function updateWebsite(id, data) {
return prisma.client.website.update({
where: {
id,
},
data,
});
}

View file

@ -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 'lib/types';
import { EVENT_TYPE } from 'lib/constants';
export async function getEventData(...args) {
export async function getEventData(
...args: [
websiteId: string,
data: {
startDate: Date;
endDate: Date;
eventName: 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;
eventName: string;
columns: any;
filters: object;
},
) {
const { startDate, endDate, eventName, 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
${event_name ? `and event_name = ${event_name}` : ''}
${getEventDataColumnsQuery('event_data', columns)}
from website_event
where website_id ='${websiteId}'
and created_at between $1 and $2
and event_type = ${EVENT_TYPE.customEvent}
${eventName ? `and eventName = ${eventName}` : ''}
${
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;
eventName: string;
columns: any;
filters: object;
},
) {
const { startDate, endDate, eventName, columns, filters } = data;
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
clickhouse;
const website = await cache.fetchWebsite(websiteId);
@ -50,7 +80,8 @@ async function clickhouseQuery(websiteId, { startDate, endDate, event_name, colu
from event
where website_id = $1
and rev_id = $2
${event_name ? `and event_name = ${event_name}` : ''}
and event_type = ${EVENT_TYPE.customEvent}
${eventName ? `and eventName = ${eventName}` : ''}
and ${getBetweenDates('created_at', startDate, endDate)}
${
Object.keys(filters).length > 0

View file

@ -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,
);
}

View 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 'lib/types';
import { EVENT_TYPE } from 'lib/constants';
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 = ${EVENT_TYPE.customEvent}
${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 = ${EVENT_TYPE.customEvent}
and ${getBetweenDates('created_at', startDate, endDate)}
${getFilterQuery(filters, params)}
group by x, t
order by t`,
params,
);
}

View file

@ -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;
}

View file

@ -0,0 +1,91 @@
import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } 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: {
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: EVENT_TYPE.customEvent,
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 message = {
website_id: websiteId,
session_id: sessionId,
event_id: uuid(),
url: url?.substring(0, URL_LENGTH),
event_type: EVENT_TYPE.customEvent,
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(message, 'event');
return data;
}

View file

@ -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,
);
}

View file

@ -0,0 +1,81 @@
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 { EVENT_TYPE } from 'lib/constants';
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 = ${EVENT_TYPE.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, EVENT_TYPE.pageView];
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 = $3
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
group by x
order by y desc`,
params,
);
}

View file

@ -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.'));
}

View file

@ -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,
);
}

View 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 { EVENT_TYPE } from 'lib/constants';
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 = ${EVENT_TYPE.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 = ${EVENT_TYPE.pageView}
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
group by t) g
order by t`,
params,
);
}

View file

@ -1,26 +1,45 @@
import { URL_LENGTH } from 'lib/constants';
import { URL_LENGTH, EVENT_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import cache from 'lib/cache';
import { uuid } from 'lib/crypto';
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: EVENT_TYPE.pageView,
},
});
}
@ -30,7 +49,7 @@ async function clickhouseQuery(data) {
const website = await cache.fetchWebsite(websiteId);
const { getDateFormat, sendMessage } = kafka;
const msg = {
const message = {
session_id: sessionId,
website_id: websiteId,
url: url?.substring(0, URL_LENGTH),
@ -38,10 +57,11 @@ async function clickhouseQuery(data) {
rev_id: website?.revId || 0,
created_at: getDateFormat(new Date()),
country: country ? country : null,
event_type: EVENT_TYPE.pageView,
...args,
};
await sendMessage(msg, 'event');
await sendMessage(message, 'event');
return data;
}

View file

@ -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);

View file

@ -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];

View file

@ -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,21 +43,24 @@ 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
`select ${field} x, count(distinct session_id) y
from event as x
where website_id = $1
and rev_id = $2
and event_name = ''
and ${getBetweenDates('created_at', startDate, endDate)}
${pageviewQuery}
${sessionQuery}
${filterQuery}
group by x
order by y desc`,
params,

View file

@ -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];

View file

@ -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,

View file

@ -1,21 +1,12 @@
export * from './admin/user/createUser';
export * from './admin/user/deleteUser';
export * from './admin/user/getUser';
export * from './admin/user/getUsers';
export * from './admin/user/updateUser';
export * from './admin/website/createWebsite';
export * from './admin/website/deleteWebsite';
export * from './admin/website/getAllWebsites';
export * from './admin/website/getUserWebsites';
export * from './admin/website/getWebsite';
export * from './admin/website/resetWebsite';
export * from './admin/website/updateWebsite';
export * from './admin/team';
export * from './admin/teamUser';
export * from './admin/user';
export * from './admin/website';
export * from './analytics/event/getEventMetrics';
export * from './analytics/event/getEvents';
export * from './analytics/event/getEventData';
export * from './analytics/event/saveEvent';
export * from './analytics/pageview/getPageviewMetrics';
export * from './analytics/pageview/getPageviewParams';
export * from './analytics/pageview/getPageviews';
export * from './analytics/pageview/getPageviewStats';
export * from './analytics/pageview/savePageView';