From b6cd9f305b95b1da385be106cc205de70d632eae Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Mon, 29 Aug 2022 13:04:58 -0700 Subject: [PATCH] add db call as fallback --- .gitignore | 3 + lib/redis.js | 1 + lib/session.js | 22 ++++--- queries/admin/account/deleteAccount.js | 60 ++++++++++++------- queries/admin/website/createWebsite.js | 2 +- queries/admin/website/deleteWebsite.js | 11 +++- queries/admin/website/getWebsiteByUuid.js | 2 +- queries/admin/website/resetWebsite.js | 2 +- queries/analytics/session/createSession.js | 4 +- queries/analytics/session/getSessionByUuid.js | 4 +- 10 files changed, 71 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 6414cc5f..33f1a523 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ yarn-error.log* .env.development.local .env.test.local .env.production.local + +*.development.yml + diff --git a/lib/redis.js b/lib/redis.js index 06fd09eb..eedac517 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -6,6 +6,7 @@ import { REDIS } from 'lib/db'; const log = debug('umami:redis'); const INITIALIZED = 'redis:initialized'; +export const DELETED = 'deleted'; function getClient() { const redis = new Redis(process.env.REDIS_URL); diff --git a/lib/session.js b/lib/session.js index 0ab38f0d..3df666e2 100644 --- a/lib/session.js +++ b/lib/session.js @@ -1,7 +1,7 @@ import { parseToken } from 'next-basics'; import { validate } from 'uuid'; import { uuid } from 'lib/crypto'; -import redis from 'lib/redis'; +import redis, { DELETED } from 'lib/redis'; import { getClientInfo, getJsonBody } from 'lib/request'; import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries'; @@ -30,17 +30,18 @@ export async function getSession(req) { let websiteId = null; - //console.log(await redis.stageData()); - // Check if website exists - if (process.env.REDIS_URL) { - websiteId = await redis.client.get(`website:${website_uuid}`); - } else { + if (redis.client) { + websiteId = BigInt(await redis.client.get(`website:${website_uuid}`)); + } + + // Check database if redis does not have + if (!websiteId) { const { website_id } = await getWebsiteByUuid(website_uuid); websiteId = website_id; } - if (!websiteId) { + if (!websiteId || websiteId === DELETED) { throw new Error(`Website not found: ${website_uuid}`); } @@ -53,9 +54,12 @@ export async function getSession(req) { let session = null; // Check if session exists - if (process.env.REDIS_URL) { + if (redis.client) { sessionCreated = !!(await redis.client.get(`session:${session_uuid}`)); - } else { + } + + // Check database if redis does not have + if (!sessionCreated) { session = await getSessionByUuid(session_uuid); sessionCreated = !!session; sessionId = session ? session.session_id : null; diff --git a/queries/admin/account/deleteAccount.js b/queries/admin/account/deleteAccount.js index 209d73a9..994cccd5 100644 --- a/queries/admin/account/deleteAccount.js +++ b/queries/admin/account/deleteAccount.js @@ -1,28 +1,46 @@ import prisma from 'lib/prisma'; +import redis, { DELETED } from 'lib/redis'; export async function deleteAccount(user_id) { const { client } = prisma; - return client.$transaction([ - client.pageview.deleteMany({ - where: { session: { website: { user_id } } }, - }), - client.event_data.deleteMany({ - where: { event: { session: { website: { user_id } } } }, - }), - client.event.deleteMany({ - where: { session: { website: { user_id } } }, - }), - client.session.deleteMany({ - where: { website: { user_id } }, - }), - client.website.deleteMany({ + const websiteUuids = await client.website + .findMany({ where: { user_id }, - }), - client.account.delete({ - where: { - user_id, - }, - }), - ]); + select: { website_uuid: true }, + }) + .map(a => a.website_uuid); + + return client + .$transaction([ + client.pageview.deleteMany({ + where: { session: { website: { user_id } } }, + }), + client.event_data.deleteMany({ + where: { event: { session: { website: { user_id } } } }, + }), + client.event.deleteMany({ + where: { session: { website: { user_id } } }, + }), + client.session.deleteMany({ + where: { website: { user_id } }, + }), + client.website.deleteMany({ + where: { user_id }, + }), + client.account.delete({ + where: { + user_id, + }, + }), + ]) + .then(async res => { + if (redis.client) { + for (let i = 0; i < websiteUuids.length; i++) { + await redis.client.set(`website:${websiteUuids[i]}`, DELETED); + } + } + + return res; + }); } diff --git a/queries/admin/website/createWebsite.js b/queries/admin/website/createWebsite.js index 513e7479..9573ceee 100644 --- a/queries/admin/website/createWebsite.js +++ b/queries/admin/website/createWebsite.js @@ -14,7 +14,7 @@ export async function createWebsite(user_id, data) { }, }) .then(async res => { - if (process.env.REDIS_URL && res) { + if (redis.client && res) { await redis.set(`website:${res.website_uuid}`, Number(res.website_id)); } diff --git a/queries/admin/website/deleteWebsite.js b/queries/admin/website/deleteWebsite.js index 15667f70..4c8518dc 100644 --- a/queries/admin/website/deleteWebsite.js +++ b/queries/admin/website/deleteWebsite.js @@ -1,9 +1,12 @@ import prisma from 'lib/prisma'; -import redis from 'lib/redis'; +import redis, { DELETED } from 'lib/redis'; +import { getWebsiteById } from 'queries'; export async function deleteWebsite(website_id) { const { client, transaction } = prisma; + const { website_uuid } = await getWebsiteById(website_id); + return transaction([ client.pageview.deleteMany({ where: { session: { website: { website_id } } }, @@ -21,8 +24,10 @@ export async function deleteWebsite(website_id) { where: { website_id }, }), ]).then(async res => { - if (process.env.REDIS_URL) { - await redis.client.del(`website:${res.website_uuid}`); + if (redis.client) { + await redis.client.set(`website:${website_uuid}`, DELETED); } + + return res; }); } diff --git a/queries/admin/website/getWebsiteByUuid.js b/queries/admin/website/getWebsiteByUuid.js index 36db53b9..900e4539 100644 --- a/queries/admin/website/getWebsiteByUuid.js +++ b/queries/admin/website/getWebsiteByUuid.js @@ -9,7 +9,7 @@ export async function getWebsiteByUuid(website_uuid) { }, }) .then(async res => { - if (process.env.REDIS_URL && res) { + if (redis.client && res) { await redis.client.set(`website:${res.website_uuid}`, 1); } diff --git a/queries/admin/website/resetWebsite.js b/queries/admin/website/resetWebsite.js index 4e7adc7a..977a35c3 100644 --- a/queries/admin/website/resetWebsite.js +++ b/queries/admin/website/resetWebsite.js @@ -18,7 +18,7 @@ export async function resetWebsite(website_id) { where: { website: { website_id } }, }), ]).then(async res => { - if (process.env.REDIS_URL) { + if (redis.client) { await redis.del(`website:${res.website_uuid}`); } }); diff --git a/queries/analytics/session/createSession.js b/queries/analytics/session/createSession.js index 82753a59..8153b8f4 100644 --- a/queries/analytics/session/createSession.js +++ b/queries/analytics/session/createSession.js @@ -22,7 +22,7 @@ async function relationalQuery(website_id, data) { }, }) .then(async res => { - if (process.env.REDIS_URL && res) { + if (redis.client && res) { await redis.client.set(`session:${res.session_uuid}`, 1); } @@ -50,7 +50,7 @@ async function clickhouseQuery( await sendMessage(params, 'session'); - if (process.env.REDIS_URL) { + if (redis.client) { await redis.client.set(`session:${session_uuid}`, 1); } } diff --git a/queries/analytics/session/getSessionByUuid.js b/queries/analytics/session/getSessionByUuid.js index db564d46..6412ca8c 100644 --- a/queries/analytics/session/getSessionByUuid.js +++ b/queries/analytics/session/getSessionByUuid.js @@ -18,7 +18,7 @@ async function relationalQuery(session_uuid) { }, }) .then(async res => { - if (process.env.REDIS_URL && res) { + if (redis.client && res) { await redis.client.set(`session:${res.session_uuid}`, 1); } @@ -48,7 +48,7 @@ async function clickhouseQuery(session_uuid) { ) .then(result => findFirst(result)) .then(async res => { - if (process.env.REDIS_URL && res) { + if (redis.client && res) { await redis.client.set(`session:${res.session_uuid}`, 1); }