Skip to content

Commit

Permalink
[Mongo] optimizations (huggingface#859)
Browse files Browse the repository at this point in the history
* [Mongo] remove duplicated assistants query

* Update src/routes/+layout.server.ts

Co-authored-by: Eliott C. <[email protected]>

* fix avatarHash problem

* limit convo to last 300

---------

Co-authored-by: Eliott C. <[email protected]>
  • Loading branch information
Mishig and coyotte508 authored Feb 22, 2024
1 parent 442f413 commit a8db017
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
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();

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;

0 comments on commit a8db017

Please sign in to comment.