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

[Mongo] optimizations #859

merged 6 commits into from
Feb 22, 2024

Conversation

mishig25
Copy link
Collaborator

@mishig25 mishig25 commented Feb 21, 2024

Mongo optimizations

  1. remove duplicated assistants query. Read this comment
  2. limit conversations to last 300 c862458

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

@mishig25 mishig25 marked this pull request as ready for review February 21, 2024 16:26
@@ -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
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)
];

@mishig25 mishig25 changed the title [Mongo] remove duplicated assistants query [Mongo] optimizations Feb 22, 2024
Copy link
Collaborator

@nsarrazin nsarrazin left a comment

Choose a reason for hiding this comment

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

Looks good now!

Copy link
Member

@coyotte508 coyotte508 left a comment

Choose a reason for hiding this comment

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

We should probably add some monitoring at some point

eg create a timeseries collection (we can add a TTL index to it too) with url, method, timestamps, user / session id, and time taken for different parts of the code (just the +layout.server.ts's load at the start)

Then we can calculate stats like p50/p90/p99 and also see if the slow times are triggered by specific users or urls/parts of the code

@nsarrazin nsarrazin added enhancement New feature or request back This issue is related to the Svelte backend or the DB labels Feb 22, 2024
@mishig25 mishig25 merged commit cd9f246 into main Feb 22, 2024
3 checks passed
@mishig25 mishig25 deleted the mongo_remove_duplicated_query branch February 22, 2024 12:44
mishig25 pushed a commit that referenced this pull request Feb 22, 2024
Follow up to #859,
specifically to [this
comment](#859 (comment))

Bug: when a user removes an assistant, the assistant was NOT being
removed from the settings list.
[This PR fixes this
problem](#869 (comment))
@zacps
Copy link
Contributor

zacps commented Feb 25, 2024

We should probably add some monitoring at some point

Could be addressed by #692, I think everyone is kind of standardising around opentelemetry at the moment so that would be my vote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
back This issue is related to the Svelte backend or the DB enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants