江不江
commited on
Commit
·
07992b8
1
Parent(s):
8ef29dd
fix delete selected chunks display wrong (#1612)
Browse files### What problem does this PR solve?
This PR solves the problem that when you delete selected chunks, the
chunks can be deleted but Chunk Number doesn't change. Now you delete
one chunk, the Chunk Number is reduced by one, delete two chunks, the
Chunk Number is reduced by two...
#900
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
Signed-off-by: seaver <zhudan187@qq.com>
- api/apps/chunk_app.py +7 -1
- api/db/services/document_service.py +20 -1
api/apps/chunk_app.py
CHANGED
@@ -185,13 +185,19 @@ def switch():
|
|
185 |
|
186 |
@manager.route('/rm', methods=['POST'])
|
187 |
@login_required
|
188 |
-
@validate_request("chunk_ids")
|
189 |
def rm():
|
190 |
req = request.json
|
191 |
try:
|
192 |
if not ELASTICSEARCH.deleteByQuery(
|
193 |
Q("ids", values=req["chunk_ids"]), search.index_name(current_user.id)):
|
194 |
return get_data_error_result(retmsg="Index updating failure")
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
return get_json_result(data=True)
|
196 |
except Exception as e:
|
197 |
return server_error_response(e)
|
|
|
185 |
|
186 |
@manager.route('/rm', methods=['POST'])
|
187 |
@login_required
|
188 |
+
@validate_request("chunk_ids","doc_id")
|
189 |
def rm():
|
190 |
req = request.json
|
191 |
try:
|
192 |
if not ELASTICSEARCH.deleteByQuery(
|
193 |
Q("ids", values=req["chunk_ids"]), search.index_name(current_user.id)):
|
194 |
return get_data_error_result(retmsg="Index updating failure")
|
195 |
+
e, doc = DocumentService.get_by_id(req["doc_id"])
|
196 |
+
if not e:
|
197 |
+
return get_data_error_result(retmsg="Document not found!")
|
198 |
+
deleted_chunk_ids = req["chunk_ids"]
|
199 |
+
chunk_number = len(deleted_chunk_ids)
|
200 |
+
DocumentService.decrement_chunk_num(doc.id, doc.kb_id, 1, chunk_number, 0)
|
201 |
return get_json_result(data=True)
|
202 |
except Exception as e:
|
203 |
return server_error_response(e)
|
api/db/services/document_service.py
CHANGED
@@ -168,7 +168,26 @@ class DocumentService(CommonService):
|
|
168 |
chunk_num).where(
|
169 |
Knowledgebase.id == kb_id).execute()
|
170 |
return num
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
@classmethod
|
173 |
@DB.connection_context()
|
174 |
def clear_chunk_num(cls, doc_id):
|
|
|
168 |
chunk_num).where(
|
169 |
Knowledgebase.id == kb_id).execute()
|
170 |
return num
|
171 |
+
|
172 |
+
@classmethod
|
173 |
+
@DB.connection_context()
|
174 |
+
def decrement_chunk_num(cls, doc_id, kb_id, token_num, chunk_num, duation):
|
175 |
+
num = cls.model.update(token_num=cls.model.token_num - token_num,
|
176 |
+
chunk_num=cls.model.chunk_num - chunk_num,
|
177 |
+
process_duation=cls.model.process_duation + duation).where(
|
178 |
+
cls.model.id == doc_id).execute()
|
179 |
+
if num == 0:
|
180 |
+
raise LookupError(
|
181 |
+
"Document not found which is supposed to be there")
|
182 |
+
num = Knowledgebase.update(
|
183 |
+
token_num=Knowledgebase.token_num -
|
184 |
+
token_num,
|
185 |
+
chunk_num=Knowledgebase.chunk_num -
|
186 |
+
chunk_num
|
187 |
+
).where(
|
188 |
+
Knowledgebase.id == kb_id).execute()
|
189 |
+
return num
|
190 |
+
|
191 |
@classmethod
|
192 |
@DB.connection_context()
|
193 |
def clear_chunk_num(cls, doc_id):
|