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):