Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mongo] optimizations #859

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
createdAt: 1,
assistantId: 1,
})
.limit(300)
.toArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.toArray();
.limit(100)
.toArray();

Maybe another optimization to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied the change. Will implement pagination for conversation in a subsequent PR


const assistantIds = conversations
.map((conv) => conv.assistantId)
.filter((el) => !!el) as ObjectId[];
const assistantIds = [
...(settings?.assistants?.map((assistantId) => assistantId) ?? []),
...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
];

const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();

Expand Down Expand Up @@ -160,6 +162,12 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
unlisted: model.unlisted,
})),
oldModels,
assistants: assistants.map((el) => ({
...el,
_id: el._id.toString(),
createdById: undefined,
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
})),
user: locals.user && {
id: locals.user._id.toString(),
username: locals.user.username,
Expand Down
15 changes: 2 additions & 13 deletions src/routes/settings/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import type { LayoutServerLoad } from "./$types";
import type { Report } from "$lib/types/Report";

export const load = (async ({ locals, parent }) => {
const { settings } = await parent();

// find assistants matching the settings assistants
const assistants = await collections.assistants
.find({
_id: { $in: settings.assistants.map((el) => new ObjectId(el)) },
})
.toArray();
const { assistants } = await parent();

let reportsByUser: string[] = [];
const createdBy = locals.user?._id ?? locals.sessionId;
Expand All @@ -25,10 +17,7 @@ export const load = (async ({ locals, parent }) => {
return {
assistants: assistants.map((el) => ({
...el,
_id: el._id.toString(),
createdById: undefined,
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
reported: reportsByUser.includes(el._id.toString()),
reported: reportsByUser.includes(el._id),
})),
};
}) satisfies LayoutServerLoad;
Loading