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 1 commit
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
10 changes: 7 additions & 3 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
})
.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) ?? [];
Copy link
Collaborator Author

@mishig25 mishig25 Feb 21, 2024

Choose a reason for hiding this comment

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

with this change, it is possible to re-use the results from collections.assistants.find query below & completely remove the query being done in src/routes/settings/+layout.server.ts

cc: @nsarrazin is this change good?

Copy link
Collaborator Author

@mishig25 mishig25 Feb 21, 2024

Choose a reason for hiding this comment

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

the change is: getting all the assistants of a user, instead of only ones user has conversation with

Copy link
Member

@coyotte508 coyotte508 Feb 21, 2024

Choose a reason for hiding this comment

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

but a user can delete assistants from their list, see "Remove" link

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

could you elaborate on what's expected behavior?

Copy link
Member

Choose a reason for hiding this comment

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

we need all assistants for conversations in order to compute their avatarHash below:

				avatarHash:
					conv.assistantId &&
					assistants.find((a) => a._id.toString() === conv.assistantId?.toString())?.avatar,

If a user removes an assistant from their settings, with this PR, the avatar of the assitant won't show up in the conversation list next to the conversation title. Eg julien's avatar wouldn't show here:

image

(if I removed him in my settings)

Copy link
Member

Choose a reason for hiding this comment

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

You can still do this:

Suggested change
const assistantIds = settings?.assistants?.map((assistantId) => assistantId) ?? [];
const assistantIds = [
...(settings?.assistants?.map((assistantId) => assistantId) ?? []),
...conversations.map(conv => conv.assistantId)
];

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

handled in 07bd2d1


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

Expand Down Expand Up @@ -157,6 +155,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