Skip to content

Commit

Permalink
Mintplex-Labs#2317 Fetch pinned documents once per folder to reduce t…
Browse files Browse the repository at this point in the history
…he number of queries.
  • Loading branch information
blazeyo committed Sep 23, 2024
1 parent 48c9c2e commit 4c373f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
4 changes: 3 additions & 1 deletion server/models/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ const Document = {
clause = {},
limit = null,
orderBy = null,
include = null
include = null,
select = null
) {
try {
const results = await prisma.workspace_documents.findMany({
where: clause,
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null ? { orderBy } : {}),
...(include !== null ? { include } : {}),
...(select !== null ? { select: { ...select } } : {}),
});
return results;
} catch (error) {
Expand Down
42 changes: 37 additions & 5 deletions server/utils/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -67,14 +67,46 @@ async function viewLocalFiles() {
type: "file",
...metadata,
cached: await cachedVectorInformation(cachefilename, true),
pinnedWorkspaces: pinnedInWorkspaces,
canWatch: liveSyncAvailable
? DocumentSyncQueue.canWatch(metadata)
: false,
// Is file watched in any workspace since sync updates all workspaces where file is referenced
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);
}
}
Expand Down

0 comments on commit 4c373f3

Please sign in to comment.