Mishig commited on
Commit
db943f9
1 Parent(s): 3109a5e

[Assistants] Add new indices for efficient querying (#810)

Browse files

* [Assistants] Add new indeices for efficient querying

* update indices & use `createdById`

* stronger typing

src/lib/server/database.ts CHANGED
@@ -75,8 +75,9 @@ client.on("open", () => {
75
  messageEvents.createIndex({ createdAt: 1 }, { expireAfterSeconds: 60 }).catch(console.error);
76
  sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }).catch(console.error);
77
  sessions.createIndex({ sessionId: 1 }, { unique: true }).catch(console.error);
78
- assistants.createIndex({ createdBy: 1 }).catch(console.error);
79
  assistants.createIndex({ userCount: 1 }).catch(console.error);
80
- assistants.createIndex({ featured: 1 }).catch(console.error);
 
81
  reports.createIndex({ assistantId: 1 }).catch(console.error);
82
  });
 
75
  messageEvents.createIndex({ createdAt: 1 }, { expireAfterSeconds: 60 }).catch(console.error);
76
  sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }).catch(console.error);
77
  sessions.createIndex({ sessionId: 1 }, { unique: true }).catch(console.error);
78
+ assistants.createIndex({ createdById: 1, userCount: -1 }).catch(console.error);
79
  assistants.createIndex({ userCount: 1 }).catch(console.error);
80
+ assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
81
+ assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
82
  reports.createIndex({ assistantId: 1 }).catch(console.error);
83
  });
src/routes/assistants/+page.server.ts CHANGED
@@ -2,6 +2,7 @@ import { base } from "$app/paths";
2
  import { ENABLE_ASSISTANTS } from "$env/static/private";
3
  import { collections } from "$lib/server/database.js";
4
  import type { Assistant } from "$lib/types/Assistant";
 
5
  import { error, redirect } from "@sveltejs/kit";
6
  import type { Filter } from "mongodb";
7
 
@@ -14,13 +15,17 @@ export const load = async ({ url, locals }) => {
14
 
15
  const modelId = url.searchParams.get("modelId");
16
  const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
17
- const createdByName = url.searchParams.get("user");
18
- const createdByCurrentUser = locals.user?.username && locals.user.username === createdByName;
19
 
20
- if (createdByName) {
21
- const existingUser = await collections.users.findOne({ username: createdByName });
22
- if (!existingUser) {
23
- throw error(404, `User "${createdByName}" doesn't exist`);
 
 
 
 
24
  }
25
  }
26
 
@@ -28,7 +33,7 @@ export const load = async ({ url, locals }) => {
28
  const filter: Filter<Assistant> = {
29
  ...(modelId && { modelId }),
30
  ...(!createdByCurrentUser && { userCount: { $gt: 1 } }),
31
- ...(createdByName ? { createdByName } : { featured: true }),
32
  };
33
  const assistants = await collections.assistants
34
  .find(filter)
 
2
  import { ENABLE_ASSISTANTS } from "$env/static/private";
3
  import { collections } from "$lib/server/database.js";
4
  import type { Assistant } from "$lib/types/Assistant";
5
+ import type { User } from "$lib/types/User";
6
  import { error, redirect } from "@sveltejs/kit";
7
  import type { Filter } from "mongodb";
8
 
 
15
 
16
  const modelId = url.searchParams.get("modelId");
17
  const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
18
+ const username = url.searchParams.get("user");
19
+ const createdByCurrentUser = locals.user?.username && locals.user.username === username;
20
 
21
+ let user: Pick<User, "_id"> | null = null;
22
+ if (username) {
23
+ user = await collections.users.findOne<Pick<User, "_id">>(
24
+ { username },
25
+ { projection: { _id: 1 } }
26
+ );
27
+ if (!user) {
28
+ throw error(404, `User "${username}" doesn't exist`);
29
  }
30
  }
31
 
 
33
  const filter: Filter<Assistant> = {
34
  ...(modelId && { modelId }),
35
  ...(!createdByCurrentUser && { userCount: { $gt: 1 } }),
36
+ ...(user ? { createdById: user._id } : { featured: true }),
37
  };
38
  const assistants = await collections.assistants
39
  .find(filter)