From 16018a7e0bd7878d70b4cfb7132eb65ebafb9258 Mon Sep 17 00:00:00 2001
From: archer <545436317@qq.com>
Date: Tue, 6 Jun 2023 11:12:13 +0800
Subject: [PATCH] perf: mongo index;fix: loading status and abort chat, system
prompt empty
---
src/api/fetch.ts | 2 +-
src/pages/api/model/create.ts | 2 +-
src/pages/api/model/share/getModels.ts | 2 +-
src/pages/api/user/getBill.ts | 20 ++++++++++----------
src/pages/kb/components/KbList.tsx | 6 +++---
src/pages/model/components/ModelList.tsx | 10 +++++-----
src/service/models/bill.ts | 7 +++++++
src/service/models/model.ts | 7 +++++++
8 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/src/api/fetch.ts b/src/api/fetch.ts
index bfd0b7bb4..a2c0b1ef7 100644
--- a/src/api/fetch.ts
+++ b/src/api/fetch.ts
@@ -50,7 +50,7 @@ export const streamFetch = ({ url, data, onMessage, abortSignal }: StreamFetchPr
read();
} catch (err: any) {
if (err?.message === 'The user aborted a request.') {
- return resolve({ responseText, newChatId, quoteLen, systemPrompt: '' });
+ return resolve({ responseText, newChatId, quoteLen, systemPrompt });
}
reject(typeof err === 'string' ? err : err?.message || '请求异常');
}
diff --git a/src/pages/api/model/create.ts b/src/pages/api/model/create.ts
index 44454a58c..1c0c507ad 100644
--- a/src/pages/api/model/create.ts
+++ b/src/pages/api/model/create.ts
@@ -26,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
userId
});
if (authCount >= 30) {
- throw new Error('上限 30 个助手');
+ throw new Error('上限 30 个应用');
}
// 创建模型
diff --git a/src/pages/api/model/share/getModels.ts b/src/pages/api/model/share/getModels.ts
index 49f35e83b..f9bb43fd2 100644
--- a/src/pages/api/model/share/getModels.ts
+++ b/src/pages/api/model/share/getModels.ts
@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
-import { connectToDatabase, Collection, Model } from '@/service/mongo';
+import { connectToDatabase, Model } from '@/service/mongo';
import type { PagingData } from '@/types';
import type { ShareModelItem } from '@/types/model';
import { parseCookie } from '@/service/utils/auth';
diff --git a/src/pages/api/user/getBill.ts b/src/pages/api/user/getBill.ts
index 8412af4f1..f8f13a06a 100644
--- a/src/pages/api/user/getBill.ts
+++ b/src/pages/api/user/getBill.ts
@@ -16,18 +16,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
- // 根据 id 获取用户账单
- const bills = await Bill.find({
+ const where = {
userId
- })
- .sort({ _id: -1 }) // 按照创建时间倒序排列
- .skip((pageNum - 1) * pageSize)
- .limit(pageSize);
+ };
- // 获取total
- const total = await Bill.countDocuments({
- userId
- });
+ // get bill record and total by record
+ const [bills, total] = await Promise.all([
+ Bill.find(where)
+ .sort({ time: -1 }) // 按照创建时间倒序排列
+ .skip((pageNum - 1) * pageSize)
+ .limit(pageSize),
+ Bill.countDocuments(where)
+ ]);
jsonRes(res, {
data: {
diff --git a/src/pages/kb/components/KbList.tsx b/src/pages/kb/components/KbList.tsx
index ae19a2965..5f10d71d9 100644
--- a/src/pages/kb/components/KbList.tsx
+++ b/src/pages/kb/components/KbList.tsx
@@ -24,7 +24,7 @@ const KbList = ({ kbId }: { kbId: string }) => {
);
/* 加载模型 */
- const { isLoading } = useQuery(['loadModels'], () => loadKbList(false));
+ const { isFetching } = useQuery(['loadModels'], () => loadKbList(false));
const handleCreateModel = useCallback(async () => {
setIsLoading(true);
@@ -133,7 +133,7 @@ const KbList = ({ kbId }: { kbId: string }) => {
))}
- {!isLoading && myKbList.length === 0 && (
+ {!isFetching && myKbList.length === 0 && (
@@ -142,7 +142,7 @@ const KbList = ({ kbId }: { kbId: string }) => {
)}
-
+
);
};
diff --git a/src/pages/model/components/ModelList.tsx b/src/pages/model/components/ModelList.tsx
index 58d35a132..5330e6d96 100644
--- a/src/pages/model/components/ModelList.tsx
+++ b/src/pages/model/components/ModelList.tsx
@@ -19,7 +19,7 @@ const ModelList = ({ modelId }: { modelId: string }) => {
const [searchText, setSearchText] = useState('');
/* 加载模型 */
- const { isLoading } = useQuery(['loadModels'], () => loadMyModels(false));
+ const { isFetching } = useQuery(['loadModels'], () => loadMyModels(false));
const onclickCreateModel = useCallback(async () => {
setIsLoading(true);
@@ -77,7 +77,7 @@ const ModelList = ({ modelId }: { modelId: string }) => {
setSearchText(e.target.value)}
/>
@@ -149,16 +149,16 @@ const ModelList = ({ modelId }: { modelId: string }) => {
))}
- {!isLoading && totalModels === 0 && (
+ {!isFetching && totalModels === 0 && (
- 还没有 AI 助手~
+ 还没有 AI 应用~
)}
-
+
);
};
diff --git a/src/service/models/bill.ts b/src/service/models/bill.ts
index 241e9addc..013551aa3 100644
--- a/src/service/models/bill.ts
+++ b/src/service/models/bill.ts
@@ -43,4 +43,11 @@ const BillSchema = new Schema({
}
});
+try {
+ BillSchema.index({ time: -1 });
+ BillSchema.index({ userId: 1 });
+} catch (error) {
+ error;
+}
+
export const Bill: Model = models['bill'] || model('bill', BillSchema);
diff --git a/src/service/models/model.ts b/src/service/models/model.ts
index 952dc5f25..370588f0e 100644
--- a/src/service/models/model.ts
+++ b/src/service/models/model.ts
@@ -82,4 +82,11 @@ const ModelSchema = new Schema({
}
});
+try {
+ ModelSchema.index({ updateTime: -1 });
+ ModelSchema.index({ 'share.collection': -1 });
+} catch (error) {
+ error;
+}
+
export const Model: MongoModel = models['model'] || model('model', ModelSchema);