From 4c373f30705deb005d88f4f1decc07c8b95a8278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Owczarczyk?= Date: Mon, 23 Sep 2024 22:04:26 +0200 Subject: [PATCH 1/2] #2317 Fetch pinned documents once per folder to reduce the number of queries. --- server/models/documents.js | 4 +++- server/utils/files/index.js | 42 ++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/server/models/documents.js b/server/models/documents.js index 43ec5f9f453..7d768578c70 100644 --- a/server/models/documents.js +++ b/server/models/documents.js @@ -76,7 +76,8 @@ const Document = { clause = {}, limit = null, orderBy = null, - include = null + include = null, + select = null ) { try { const results = await prisma.workspace_documents.findMany({ @@ -84,6 +85,7 @@ const Document = { ...(limit !== null ? { take: limit } : {}), ...(orderBy !== null ? { orderBy } : {}), ...(include !== null ? { include } : {}), + ...(select !== null ? { select: { ...select } } : {}), }); return results; } catch (error) { diff --git a/server/utils/files/index.js b/server/utils/files/index.js index 58bdf807a4e..f68375f3b8b 100644 --- a/server/utils/files/index.js +++ b/server/utils/files/index.js @@ -44,17 +44,17 @@ async function viewLocalFiles() { items: [], }; const subfiles = fs.readdirSync(folderPath); + const filenames = {}; for (const subfile of subfiles) { if (path.extname(subfile) !== ".json") continue; const filePath = path.join(folderPath, subfile); const rawData = fs.readFileSync(filePath, "utf8"); const cachefilename = `${file}/${subfile}`; + filenames[cachefilename] = subfile; + const { pageContent, ...metadata } = JSON.parse(rawData); - const pinnedInWorkspaces = await Document.getOnlyWorkspaceIds({ - docpath: cachefilename, - pinned: true, - }); + const watchedInWorkspaces = liveSyncAvailable ? await Document.getOnlyWorkspaceIds({ docpath: cachefilename, @@ -67,7 +67,6 @@ async function viewLocalFiles() { type: "file", ...metadata, cached: await cachedVectorInformation(cachefilename, true), - pinnedWorkspaces: pinnedInWorkspaces, canWatch: liveSyncAvailable ? DocumentSyncQueue.canWatch(metadata) : false, @@ -75,6 +74,39 @@ async function viewLocalFiles() { watched: watchedInWorkspaces.length !== 0, }); } + + // Get documents pinned to at least one workspace. + const pinnedWorkspacesByDocument = ( + await Document.where( + { + docpath: { + in: Object.keys(filenames), + }, + pinned: true, + }, + null, + null, + null, + { + workspaceId: true, + docpath: true, + } + ) + ).reduce((result, { workspaceId, docpath }) => { + const filename = filenames[docpath]; + if (!result[filename]) { + result[filename] = []; + } + if (!result[filename].includes(workspaceId)) { + result[filename].push(workspaceId); + } + return result; + }, {}); + + for (const item of subdocs.items) { + item.pinnedWorkspaces = pinnedWorkspacesByDocument[item.name] || []; + } + directory.items.push(subdocs); } } From 76f7e800793839e04fe9f74701189545d8f37b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Owczarczyk?= Date: Mon, 23 Sep 2024 22:47:15 +0200 Subject: [PATCH 2/2] Reorder the lines to keeps const declarations together. --- server/utils/files/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/utils/files/index.js b/server/utils/files/index.js index f68375f3b8b..4ec16a76a11 100644 --- a/server/utils/files/index.js +++ b/server/utils/files/index.js @@ -51,10 +51,7 @@ async function viewLocalFiles() { const filePath = path.join(folderPath, subfile); const rawData = fs.readFileSync(filePath, "utf8"); const cachefilename = `${file}/${subfile}`; - filenames[cachefilename] = subfile; - const { pageContent, ...metadata } = JSON.parse(rawData); - const watchedInWorkspaces = liveSyncAvailable ? await Document.getOnlyWorkspaceIds({ docpath: cachefilename, @@ -73,6 +70,7 @@ async function viewLocalFiles() { // Is file watched in any workspace since sync updates all workspaces where file is referenced watched: watchedInWorkspaces.length !== 0, }); + filenames[cachefilename] = subfile; } // Get documents pinned to at least one workspace.