Mishig
commited on
Commit
•
19329ab
1
Parent(s):
b63aabe
[Mongo] Optimize `reports` collection query (#832)
Browse files* [Mongo] Optimize `reports` collection query
* perform reports query only once
* rm unused async
* keep both indices
src/lib/server/database.ts
CHANGED
@@ -86,4 +86,5 @@ client.on("open", () => {
|
|
86 |
assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
|
87 |
assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
|
88 |
reports.createIndex({ assistantId: 1 }).catch(console.error);
|
|
|
89 |
});
|
|
|
86 |
assistants.createIndex({ featured: 1, userCount: -1 }).catch(console.error);
|
87 |
assistants.createIndex({ modelId: 1, userCount: -1 }).catch(console.error);
|
88 |
reports.createIndex({ assistantId: 1 }).catch(console.error);
|
89 |
+
reports.createIndex({ createdBy: 1, assistantId: 1 }).catch(console.error);
|
90 |
});
|
src/routes/settings/+layout.server.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import { collections } from "$lib/server/database";
|
2 |
import { ObjectId } from "mongodb";
|
3 |
import type { LayoutServerLoad } from "./$types";
|
|
|
4 |
|
5 |
export const load = (async ({ locals, parent }) => {
|
6 |
const { settings } = await parent();
|
@@ -12,20 +13,22 @@ export const load = (async ({ locals, parent }) => {
|
|
12 |
})
|
13 |
.toArray();
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return {
|
16 |
-
assistants:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
reported:
|
24 |
-
(await collections.reports.countDocuments({
|
25 |
-
assistantId: el._id,
|
26 |
-
createdBy: locals.user?._id ?? locals.sessionId,
|
27 |
-
})) > 0,
|
28 |
-
}))
|
29 |
-
),
|
30 |
};
|
31 |
}) satisfies LayoutServerLoad;
|
|
|
1 |
import { collections } from "$lib/server/database";
|
2 |
import { ObjectId } from "mongodb";
|
3 |
import type { LayoutServerLoad } from "./$types";
|
4 |
+
import type { Report } from "$lib/types/Report";
|
5 |
|
6 |
export const load = (async ({ locals, parent }) => {
|
7 |
const { settings } = await parent();
|
|
|
13 |
})
|
14 |
.toArray();
|
15 |
|
16 |
+
let reportsByUser: ObjectId[] = [];
|
17 |
+
const createdBy = locals.user?._id ?? locals.sessionId;
|
18 |
+
if (createdBy) {
|
19 |
+
const reports = await collections.reports
|
20 |
+
.find<Pick<Report, "assistantId">>({ createdBy }, { projection: { _id: 0, assistantId: 1 } })
|
21 |
+
.toArray();
|
22 |
+
reportsByUser = reports.map((r) => r.assistantId);
|
23 |
+
}
|
24 |
+
|
25 |
return {
|
26 |
+
assistants: assistants.map((el) => ({
|
27 |
+
...el,
|
28 |
+
_id: el._id.toString(),
|
29 |
+
createdById: undefined,
|
30 |
+
createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
|
31 |
+
reported: reportsByUser.includes(el._id),
|
32 |
+
})),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
};
|
34 |
}) satisfies LayoutServerLoad;
|
src/routes/settings/assistants/[assistantId]/+page.server.ts
CHANGED
@@ -57,8 +57,8 @@ export const actions: Actions = {
|
|
57 |
report: async ({ request, params, locals, url }) => {
|
58 |
// is there already a report from this user for this model ?
|
59 |
const report = await collections.reports.findOne({
|
60 |
-
assistantId: new ObjectId(params.assistantId),
|
61 |
createdBy: locals.user?._id ?? locals.sessionId,
|
|
|
62 |
});
|
63 |
|
64 |
if (report) {
|
|
|
57 |
report: async ({ request, params, locals, url }) => {
|
58 |
// is there already a report from this user for this model ?
|
59 |
const report = await collections.reports.findOne({
|
|
|
60 |
createdBy: locals.user?._id ?? locals.sessionId,
|
61 |
+
assistantId: new ObjectId(params.assistantId),
|
62 |
});
|
63 |
|
64 |
if (report) {
|