This commit is contained in:
Brian Cao 2022-10-31 23:42:37 -07:00
parent 246e4e5f4f
commit 17041efaae
73 changed files with 491 additions and 874 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -1,18 +1,17 @@
import prisma from 'lib/prisma';
import redis, { DELETED } from 'lib/redis';
export async function deleteAccount(userId) {
export async function deleteUser(userId) {
const { client } = prisma;
const websites = await client.website.findMany({
where: { userId },
select: { websiteUuid: true },
});
let websiteUuids = [];
let websiteIds = [];
if (websites.length > 0) {
websiteUuids = websites.map(a => a.websiteUuid);
websiteIds = websites.map(a => a.id);
}
return client
@ -32,7 +31,7 @@ export async function deleteAccount(userId) {
client.website.deleteMany({
where: { userId },
}),
client.account.delete({
client.user.delete({
where: {
id: userId,
},
@ -40,8 +39,8 @@ export async function deleteAccount(userId) {
])
.then(async res => {
if (redis.enabled) {
for (let i = 0; i < websiteUuids.length; i++) {
await redis.set(`website:${websiteUuids[i]}`, DELETED);
for (let i = 0; i < websiteIds.length; i++) {
await redis.set(`website:${websiteIds[i]}`, DELETED);
}
}

View file

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

View file

@ -1,7 +1,7 @@
import prisma from 'lib/prisma';
export async function getAccounts() {
return prisma.client.account.findMany({
export async function getUsers() {
return prisma.client.user.findMany({
orderBy: [
{ isAdmin: 'desc' },
{
@ -13,8 +13,6 @@ export async function getAccounts() {
username: true,
isAdmin: true,
createdAt: true,
updatedAt: true,
accountUuid: true,
},
});
}

View file

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

View file

@ -5,7 +5,7 @@ export async function createWebsite(userId, data) {
return prisma.client.website
.create({
data: {
account: {
user: {
connect: {
id: userId,
},
@ -15,7 +15,7 @@ export async function createWebsite(userId, data) {
})
.then(async res => {
if (redis.enabled && res) {
await redis.set(`website:${res.websiteUuid}`, res.id);
await redis.set(`website:${res.id}`, 1);
}
return res;

View file

@ -1,28 +1,28 @@
import prisma from 'lib/prisma';
import redis, { DELETED } from 'lib/redis';
export async function deleteWebsite(websiteUuid) {
export async function deleteWebsite(id) {
const { client, transaction } = prisma;
return transaction([
client.pageview.deleteMany({
where: { session: { website: { websiteUuid } } },
where: { session: { website: { id } } },
}),
client.eventData.deleteMany({
where: { event: { session: { website: { websiteUuid } } } },
where: { event: { session: { website: { id } } } },
}),
client.event.deleteMany({
where: { session: { website: { websiteUuid } } },
where: { session: { website: { id } } },
}),
client.session.deleteMany({
where: { website: { websiteUuid } },
where: { website: { id } },
}),
client.website.delete({
where: { websiteUuid },
where: { id },
}),
]).then(async res => {
if (redis.enabled) {
await redis.set(`website:${websiteUuid}`, DELETED);
await redis.set(`website:${id}`, DELETED);
}
return res;

View file

@ -11,7 +11,7 @@ export async function getAllWebsites() {
},
],
include: {
account: {
user: {
select: {
username: true,
},
@ -19,5 +19,5 @@ export async function getAllWebsites() {
},
});
return data.map(i => ({ ...i, account: i.account.username }));
return data.map(i => ({ ...i, user: i.user.username }));
}

View file

@ -8,7 +8,7 @@ export async function getWebsite(where) {
})
.then(async data => {
if (redis.enabled && data) {
await redis.set(`website:${data.websiteUuid}`, data.id);
await redis.set(`website:${data.id}`, 1);
}
return data;

View file

@ -1,20 +1,20 @@
import prisma from 'lib/prisma';
export async function resetWebsite(websiteId) {
export async function resetWebsite(id) {
const { client, transaction } = prisma;
return transaction([
client.pageview.deleteMany({
where: { session: { website: { websiteUuid: websiteId } } },
where: { session: { website: { id } } },
}),
client.eventData.deleteMany({
where: { event: { session: { website: { websiteUuid: websiteId } } } },
where: { event: { session: { website: { id } } } },
}),
client.event.deleteMany({
where: { session: { website: { websiteUuid: websiteId } } },
where: { session: { website: { id } } },
}),
client.session.deleteMany({
where: { website: { websiteUuid: websiteId } },
where: { website: { id } },
}),
]);
}

View file

@ -21,7 +21,7 @@ async function relationalQuery(websiteId, { startDate, endDate, event_name, colu
on event.website_id = website.website_id
join event_data
on event.event_id = event_data.event_id
where website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and event.created_at between $1 and $2
${event_name ? `and event_name = ${event_name}` : ''}
${

View file

@ -28,7 +28,7 @@ async function relationalQuery(
from event
join website
on event.website_id = website.website_id
where website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and event.created_at between $1 and $2
${getFilterQuery('event', filters, params)}
group by 1, 2

View file

@ -13,7 +13,7 @@ function relationalQuery(websites, start_at) {
return prisma.client.event.findMany({
where: {
website: {
websiteUuid: {
id: {
in: websites,
},
},

View file

@ -2,6 +2,7 @@ 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';
export async function saveEvent(...args) {
return runQuery({
@ -11,10 +12,11 @@ export async function saveEvent(...args) {
}
async function relationalQuery(
{ websiteId },
{ session: { id: sessionId }, eventUuid, url, eventName, eventData },
websiteId,
{ eventId, session: { id: sessionId }, eventUuid, url, eventName, eventData },
) {
const data = {
id: eventId,
websiteId,
sessionId,
url: url?.substring(0, URL_LENGTH),
@ -26,6 +28,7 @@ async function relationalQuery(
data.eventData = {
create: {
eventData: eventData,
id: uuid(),
},
};
}
@ -36,7 +39,7 @@ async function relationalQuery(
}
async function clickhouseQuery(
{ websiteUuid: websiteId },
websiteId,
{ session: { country, sessionUuid, ...sessionArgs }, eventUuid, url, eventName, eventData },
) {
const { getDateFormat, sendMessage } = kafka;

View file

@ -24,7 +24,7 @@ async function relationalQuery(websiteId, { startDate, endDate, column, table, f
from ${table}
${` join website on ${table}.website_id = website.website_id`}
${joinSession}
where website.website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and ${table}.created_at between $1 and $2
${pageviewQuery}
${joinSession && sessionQuery}

View file

@ -24,7 +24,7 @@ async function relationalQuery(websiteId, start_at, end_at, column, table, filte
from ${table}
${` join website on ${table}.website_id = website.website_id`}
${joinSession}
where website.website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and ${table}.created_at between $1 and $2
and ${table}.url like '%?%'
${pageviewQuery}

View file

@ -37,7 +37,7 @@ async function relationalQuery(
join website
on pageview.website_id = website.website_id
${joinSession}
where website.website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and pageview.created_at between $1 and $2
${pageviewQuery}
${sessionQuery}

View file

@ -13,7 +13,7 @@ async function relationalQuery(websites, start_at) {
return prisma.client.pageview.findMany({
where: {
website: {
websiteUuid: {
id: {
in: websites,
},
},

View file

@ -10,9 +10,13 @@ export async function savePageView(...args) {
});
}
async function relationalQuery({ websiteId }, { session: { id: sessionId }, url, referrer }) {
async function relationalQuery(
websiteId,
{ pageViewId, session: { id: sessionId }, url, referrer },
) {
return prisma.client.pageview.create({
data: {
id: pageViewId,
websiteId,
sessionId,
url: url?.substring(0, URL_LENGTH),
@ -22,12 +26,12 @@ async function relationalQuery({ websiteId }, { session: { id: sessionId }, url,
}
async function clickhouseQuery(
{ websiteUuid: websiteId },
{ session: { country, sessionUuid, ...sessionArgs }, url, referrer },
websiteId,
{ session: { country, id: sessionId, ...sessionArgs }, url, referrer },
) {
const { getDateFormat, sendMessage } = kafka;
const params = {
session_uuid: sessionUuid,
session_id: sessionId,
website_id: websiteId,
created_at: getDateFormat(new Date()),
url: url?.substring(0, URL_LENGTH),

View file

@ -19,7 +19,6 @@ async function relationalQuery(websiteId, data) {
},
select: {
id: true,
sessionUuid: true,
hostname: true,
browser: true,
os: true,
@ -31,7 +30,7 @@ async function relationalQuery(websiteId, data) {
})
.then(async res => {
if (redis.enabled && res) {
await redis.set(`session:${res.sessionUuid}`, 1);
await redis.set(`session:${res.id}`, 1);
}
return res;
@ -40,12 +39,12 @@ async function relationalQuery(websiteId, data) {
async function clickhouseQuery(
websiteId,
{ sessionUuid, hostname, browser, os, screen, language, country, device },
{ sessionId, hostname, browser, os, screen, language, country, device },
) {
const { getDateFormat, sendMessage } = kafka;
const params = {
session_uuid: sessionUuid,
sessionId,
website_id: websiteId,
created_at: getDateFormat(new Date()),
hostname,
@ -60,6 +59,6 @@ async function clickhouseQuery(
await sendMessage(params, 'event');
if (redis.enabled) {
await redis.set(`session:${sessionUuid}`, 1);
await redis.set(`session:${sessionId}`, 1);
}
}

View file

@ -3,19 +3,17 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import redis from 'lib/redis';
export async function getSessionByUuid(...args) {
export async function getSession(...args) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(sessionUuid) {
async function relationalQuery(where) {
return prisma.client.session
.findUnique({
where: {
sessionUuid,
},
where,
})
.then(async res => {
if (redis.enabled && res) {
@ -49,7 +47,7 @@ async function clickhouseQuery(sessionUuid) {
.then(result => findFirst(result))
.then(async res => {
if (redis.enabled && res) {
await redis.set(`session:${res.session_uuid}`, 1);
await redis.set(`session:${res.id}`, 1);
}
return res;

View file

@ -23,7 +23,7 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
join website
on pageview.website_id = website.website_id
${joinSession}
where website.website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and pageview.created_at between $1 and $2
${pageviewQuery}
${sessionQuery}

View file

@ -15,7 +15,7 @@ async function relationalQuery(websites, start_at) {
...(websites && websites.length > 0
? {
website: {
websiteUuid: {
id: {
in: websites,
},
},

View file

@ -19,7 +19,7 @@ async function relationalQuery(websiteId) {
from pageview
join website
on pageview.website_id = website.website_id
where website.website_uuid = '${websiteId}'
where website.website_id = '${websiteId}'
and pageview.created_at >= $1`,
params,
);

View file

@ -33,7 +33,7 @@ async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
join website
on pageview.website_id = website.website_id
${joinSession}
where website.website_uuid='${websiteId}'
where website.website_id='${websiteId}'
and pageview.created_at between $1 and $2
${pageviewQuery}
${sessionQuery}

View file

@ -1,8 +1,8 @@
export * from './admin/account/createAccount';
export * from './admin/account/deleteAccount';
export * from './admin/account/getAccount';
export * from './admin/account/getAccounts';
export * from './admin/account/updateAccount';
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';
@ -20,7 +20,7 @@ export * from './analytics/pageview/getPageviews';
export * from './analytics/pageview/getPageviewStats';
export * from './analytics/pageview/savePageView';
export * from './analytics/session/createSession';
export * from './analytics/session/getSessionByUuid';
export * from './analytics/session/getSession';
export * from './analytics/session/getSessionMetrics';
export * from './analytics/session/getSessions';
export * from './analytics/stats/getActiveVisitors';