mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
configure redis
This commit is contained in:
parent
efb52f5ff1
commit
48fe6ebcc5
13 changed files with 138 additions and 183 deletions
|
|
@ -14,7 +14,7 @@ export async function createWebsite(user_id, data) {
|
|||
},
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
if (process.env.REDIS_URL && res) {
|
||||
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ export async function deleteWebsite(website_id) {
|
|||
}),
|
||||
]).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
await redis.client.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,18 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function getWebsiteByUuid(website_uuid) {
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
});
|
||||
return prisma.client.website
|
||||
.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL && res) {
|
||||
await redis.client.set(`website:${res.website_uuid}`, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
import { URL_LENGTH, EVENT_NAME_LENGTH } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function saveEvent(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -34,23 +32,6 @@ async function relationalQuery(website_id, { session_id, url, event_name, event_
|
|||
}
|
||||
|
||||
async function clickhouseQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
const params = [
|
||||
website_id,
|
||||
event_uuid,
|
||||
session_uuid,
|
||||
url?.substring(0, URL_LENGTH),
|
||||
event_name?.substring(0, EVENT_NAME_LENGTH),
|
||||
];
|
||||
|
||||
return rawQuery(
|
||||
`insert into umami.event (created_at, website_id, session_uuid, url, event_name)
|
||||
values (${getDateFormat(new Date())}, $1, $2, $3, $4);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
event_uuid: event_uuid,
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import kafka from 'lib/kafka';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
import { URL_LENGTH } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function savePageView(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -24,21 +22,6 @@ async function relationalQuery(website_id, { session_id, url, referrer }) {
|
|||
}
|
||||
|
||||
async function clickhouseQuery(website_id, { session_uuid, url, referrer }) {
|
||||
const params = [
|
||||
website_id,
|
||||
session_uuid,
|
||||
url?.substring(0, URL_LENGTH),
|
||||
referrer?.substring(0, URL_LENGTH),
|
||||
];
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`insert into umami.pageview (created_at, website_id, session_uuid, url, referrer)
|
||||
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(website_id, { session_uuid, url, referrer }) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
website_id: website_id,
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import { runQuery, CLICKHOUSE, KAFKA, PRISMA } from 'lib/db';
|
||||
|
||||
export async function createSession(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[KAFKA]: () => kafkaQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -24,8 +22,8 @@ async function relationalQuery(website_id, data) {
|
|||
},
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`session:${res.session_uuid}`, '');
|
||||
if (process.env.REDIS_URL && res) {
|
||||
await redis.client.set(`session:${res.session_uuid}`, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
|
@ -35,30 +33,6 @@ async function relationalQuery(website_id, data) {
|
|||
async function clickhouseQuery(
|
||||
website_id,
|
||||
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
const params = [
|
||||
session_uuid,
|
||||
website_id,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country ? country : null,
|
||||
];
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
|
||||
await rawQuery(
|
||||
`insert into umami.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
|
||||
values (${getDateFormat(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function kafkaQuery(
|
||||
website_id,
|
||||
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const params = {
|
||||
|
|
@ -76,5 +50,7 @@ async function kafkaQuery(
|
|||
|
||||
await sendMessage(params, 'session');
|
||||
|
||||
await redis.set(`session:${session_uuid}`, '');
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.client.set(`session:${session_uuid}`, 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function getSessionByUuid(...args) {
|
||||
return runQuery({
|
||||
|
|
@ -10,11 +11,19 @@ export async function getSessionByUuid(...args) {
|
|||
}
|
||||
|
||||
async function relationalQuery(session_uuid) {
|
||||
return prisma.client.session.findUnique({
|
||||
where: {
|
||||
session_uuid,
|
||||
},
|
||||
});
|
||||
return prisma.client.session
|
||||
.findUnique({
|
||||
where: {
|
||||
session_uuid,
|
||||
},
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL && res) {
|
||||
await redis.client.set(`session:${res.session_uuid}`, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(session_uuid) {
|
||||
|
|
@ -36,5 +45,13 @@ async function clickhouseQuery(session_uuid) {
|
|||
from session
|
||||
where session_uuid = $1`,
|
||||
params,
|
||||
).then(result => findFirst(result));
|
||||
)
|
||||
.then(result => findFirst(result))
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL && res) {
|
||||
await redis.client.set(`session:${res.session_uuid}`, 1);
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,24 +10,22 @@ export async function getSessions(...args) {
|
|||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.client.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
? {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
return prisma.client.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
? {
|
||||
website: {
|
||||
website_id: {
|
||||
in: websites,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
created_at: {
|
||||
gte: start_at,
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue