mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
Refactored redis usage. Added lib/cache.
This commit is contained in:
parent
3485b6268b
commit
f118bc95c1
22 changed files with 236 additions and 221 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import redis, { DELETED } from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function deleteUser(userId) {
|
||||
const { client } = prisma;
|
||||
|
|
@ -8,12 +8,6 @@ export async function deleteUser(userId) {
|
|||
where: { userId },
|
||||
});
|
||||
|
||||
let websiteIds = [];
|
||||
|
||||
if (websites.length > 0) {
|
||||
websiteIds = websites.map(a => a.id);
|
||||
}
|
||||
|
||||
return client
|
||||
.$transaction([
|
||||
client.pageview.deleteMany({
|
||||
|
|
@ -37,13 +31,15 @@ export async function deleteUser(userId) {
|
|||
},
|
||||
}),
|
||||
])
|
||||
.then(async res => {
|
||||
if (redis.enabled) {
|
||||
for (let i = 0; i < websiteIds.length; i++) {
|
||||
await redis.set(`website:${websiteIds[i]}`, DELETED);
|
||||
.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 res;
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function createWebsite(userId, data) {
|
||||
return prisma.client.website
|
||||
|
|
@ -13,11 +13,11 @@ export async function createWebsite(userId, data) {
|
|||
...data,
|
||||
},
|
||||
})
|
||||
.then(async res => {
|
||||
if (redis.enabled && res) {
|
||||
await redis.set(`website:${res.id}`, res);
|
||||
.then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.storeWebsite(data);
|
||||
}
|
||||
|
||||
return res;
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import redis, { DELETED } from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function deleteWebsite(id) {
|
||||
const { client, transaction } = prisma;
|
||||
|
|
@ -20,11 +20,11 @@ export async function deleteWebsite(id) {
|
|||
client.website.delete({
|
||||
where: { id },
|
||||
}),
|
||||
]).then(async res => {
|
||||
if (redis.enabled) {
|
||||
await redis.set(`website:${id}`, DELETED);
|
||||
]).then(async data => {
|
||||
if (cache.enabled) {
|
||||
await cache.deleteWebsite(id);
|
||||
}
|
||||
|
||||
return res;
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function getWebsite(where) {
|
||||
return prisma.client.website
|
||||
.findUnique({
|
||||
where,
|
||||
})
|
||||
.then(async data => {
|
||||
if (redis.enabled && data) {
|
||||
await redis.set(`website:${data.id}`, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
return prisma.client.website.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getEventData(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -41,7 +41,7 @@ async function relationalQuery(websiteId, { startDate, endDate, event_name, colu
|
|||
async function clickhouseQuery(websiteId, { startDate, endDate, event_name, columns, filters }) {
|
||||
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
|
||||
clickhouse;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
|
||||
return rawQuery(
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getEventMetrics(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -47,7 +47,7 @@ async function clickhouseQuery(
|
|||
filters = {},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
|
||||
return rawQuery(
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function saveEvent(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -12,52 +12,49 @@ export async function saveEvent(...args) {
|
|||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId,
|
||||
{ eventId, session: { id: sessionId }, eventUuid, url, eventName, eventData },
|
||||
) {
|
||||
const data = {
|
||||
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),
|
||||
eventUuid,
|
||||
};
|
||||
|
||||
if (eventData) {
|
||||
data.eventData = {
|
||||
params.eventData = {
|
||||
create: {
|
||||
id: eventId,
|
||||
eventData: eventData,
|
||||
id: uuid(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return prisma.client.event.create({
|
||||
data,
|
||||
data: params,
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
{ session: { country, sessionUuid, ...sessionArgs }, eventUuid, url, eventName, eventData },
|
||||
) {
|
||||
async function clickhouseQuery(data) {
|
||||
const { websiteId, sessionId, url, eventName, eventData } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
||||
const params = {
|
||||
session_id: sessionUuid,
|
||||
event_id: eventUuid,
|
||||
website_id: websiteId,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
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,
|
||||
...sessionArgs,
|
||||
country: country ? country : null,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
};
|
||||
|
||||
await sendMessage(params, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getPageviewMetrics(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -38,7 +38,7 @@ async function relationalQuery(websiteId, { startDate, endDate, column, table, f
|
|||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, column, filters = {} }) {
|
||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery, eventQuery } = parseFilters(column, filters, params);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getPageviewStats(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -52,7 +52,7 @@ async function clickhouseQuery(
|
|||
{ start_at, end_at, timezone = 'UTC', unit = 'day', count = '*', filters = {} },
|
||||
) {
|
||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ import { URL_LENGTH } from 'lib/constants';
|
|||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
import { uuid } from 'lib/crypto';
|
||||
|
||||
export async function savePageView(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -11,13 +12,11 @@ export async function savePageView(...args) {
|
|||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId,
|
||||
{ pageViewId, session: { id: sessionId }, url, referrer },
|
||||
) {
|
||||
async function relationalQuery(data) {
|
||||
const { websiteId, sessionId, url, referrer } = data;
|
||||
return prisma.client.pageview.create({
|
||||
data: {
|
||||
id: pageViewId,
|
||||
id: uuid(),
|
||||
websiteId,
|
||||
sessionId,
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
|
|
@ -26,22 +25,21 @@ async function relationalQuery(
|
|||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
{ session: { country, id: sessionId, ...sessionArgs }, url, referrer },
|
||||
) {
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
async function clickhouseQuery(data) {
|
||||
const { websiteId, sessionId, url, referrer } = data;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
|
||||
const msg = {
|
||||
session_id: sessionId,
|
||||
website_id: websiteId,
|
||||
created_at: getDateFormat(new Date()),
|
||||
url: url?.substring(0, URL_LENGTH),
|
||||
referrer: referrer?.substring(0, URL_LENGTH),
|
||||
rev_id: website?.revId || 0,
|
||||
...sessionArgs,
|
||||
country: country ? country : null,
|
||||
created_at: getDateFormat(new Date()),
|
||||
};
|
||||
|
||||
await sendMessage(params, 'event');
|
||||
await sendMessage(msg, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,62 +1,45 @@
|
|||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function createSession(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
}).then(async data => {
|
||||
if (redis.enabled && data) {
|
||||
await redis.set(`session:${data.id}`, data);
|
||||
if (cache.enabled) {
|
||||
await cache.storeSession(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, data) {
|
||||
return prisma.client.session.create({
|
||||
data: {
|
||||
websiteId,
|
||||
...data,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
hostname: true,
|
||||
browser: true,
|
||||
os: true,
|
||||
screen: true,
|
||||
language: true,
|
||||
country: true,
|
||||
device: true,
|
||||
},
|
||||
});
|
||||
async function relationalQuery(data) {
|
||||
return prisma.client.session.create({ data });
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
{ sessionId, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
async function clickhouseQuery(data) {
|
||||
const { id, websiteId, hostname, browser, os, device, screen, language, country } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
||||
const data = {
|
||||
sessionId,
|
||||
const msg = {
|
||||
session_id: id,
|
||||
website_id: websiteId,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country: country ? country : null,
|
||||
country,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
};
|
||||
|
||||
await sendMessage(data, 'event');
|
||||
await sendMessage(msg, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,11 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function getSession(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
}).then(async data => {
|
||||
if (redis.enabled && data) {
|
||||
await redis.set(`session:${data.id}`, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -27,7 +20,7 @@ async function clickhouseQuery(sessionId) {
|
|||
const params = [sessionId];
|
||||
|
||||
return rawQuery(
|
||||
`select distinct
|
||||
`select
|
||||
session_id,
|
||||
website_id,
|
||||
created_at,
|
||||
|
|
@ -39,7 +32,8 @@ async function clickhouseQuery(sessionId) {
|
|||
language,
|
||||
country
|
||||
from event
|
||||
where session_id = $1`,
|
||||
where session_id = $1
|
||||
limit 1`,
|
||||
params,
|
||||
).then(result => findFirst(result));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function getSessionMetrics(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -37,7 +37,7 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
|
|||
|
||||
async function clickhouseQuery(websiteId, { startDate, endDate, field, filters = {} }) {
|
||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const { pageviewQuery, sessionQuery } = parseFilters(null, filters, params);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue