From 264303ba9858b13529d068377995f8618ca8b25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E4=B8=8D=E6=B1=9F?= <74400272+Seaver-Zhu@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:25:50 +0800 Subject: [PATCH] fix delete selected chunks display wrong (#1612) ### 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 --- api/apps/chunk_app.py | 8 +++++++- api/db/services/document_service.py | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api/apps/chunk_app.py b/api/apps/chunk_app.py index 360e7250f..c42ec4a98 100644 --- a/api/apps/chunk_app.py +++ b/api/apps/chunk_app.py @@ -185,13 +185,19 @@ def switch(): @manager.route('/rm', methods=['POST']) @login_required -@validate_request("chunk_ids") +@validate_request("chunk_ids","doc_id") def rm(): req = request.json try: if not ELASTICSEARCH.deleteByQuery( Q("ids", values=req["chunk_ids"]), search.index_name(current_user.id)): return get_data_error_result(retmsg="Index updating failure") + e, doc = DocumentService.get_by_id(req["doc_id"]) + if not e: + return get_data_error_result(retmsg="Document not found!") + deleted_chunk_ids = req["chunk_ids"] + chunk_number = len(deleted_chunk_ids) + DocumentService.decrement_chunk_num(doc.id, doc.kb_id, 1, chunk_number, 0) return get_json_result(data=True) except Exception as e: return server_error_response(e) diff --git a/api/db/services/document_service.py b/api/db/services/document_service.py index f3195a502..b3bc55513 100644 --- a/api/db/services/document_service.py +++ b/api/db/services/document_service.py @@ -168,7 +168,26 @@ class DocumentService(CommonService): chunk_num).where( Knowledgebase.id == kb_id).execute() return num - + + @classmethod + @DB.connection_context() + def decrement_chunk_num(cls, doc_id, kb_id, token_num, chunk_num, duation): + num = cls.model.update(token_num=cls.model.token_num - token_num, + chunk_num=cls.model.chunk_num - chunk_num, + process_duation=cls.model.process_duation + duation).where( + cls.model.id == doc_id).execute() + if num == 0: + raise LookupError( + "Document not found which is supposed to be there") + num = Knowledgebase.update( + token_num=Knowledgebase.token_num - + token_num, + chunk_num=Knowledgebase.chunk_num - + chunk_num + ).where( + Knowledgebase.id == kb_id).execute() + return num + @classmethod @DB.connection_context() def clear_chunk_num(cls, doc_id):