mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 01:33:05 +00:00
feat: Batch cancel tasks
This commit is contained in:
parent
0cd9c8fe00
commit
a1d872ac74
|
|
@ -77,8 +77,23 @@ class FileBufferHandle:
|
||||||
return self.buffer
|
return self.buffer
|
||||||
|
|
||||||
|
|
||||||
|
class BatchCancelInstanceSerializer(serializers.Serializer):
|
||||||
|
id_list = serializers.ListField(required=True, child=serializers.UUIDField(required=True),
|
||||||
|
error_messages=ErrMessage.char("id列表"))
|
||||||
|
type = serializers.IntegerField(required=True, error_messages=ErrMessage.integer(
|
||||||
|
"任务类型"))
|
||||||
|
|
||||||
|
def is_valid(self, *, raise_exception=False):
|
||||||
|
super().is_valid(raise_exception=True)
|
||||||
|
_type = self.data.get('type')
|
||||||
|
try:
|
||||||
|
TaskType(_type)
|
||||||
|
except Exception as e:
|
||||||
|
raise AppApiException(500, '任务类型不支持')
|
||||||
|
|
||||||
|
|
||||||
class CancelInstanceSerializer(serializers.Serializer):
|
class CancelInstanceSerializer(serializers.Serializer):
|
||||||
type = serializers.IntegerField(required=True, error_messages=ErrMessage.boolean(
|
type = serializers.IntegerField(required=True, error_messages=ErrMessage.integer(
|
||||||
"任务类型"))
|
"任务类型"))
|
||||||
|
|
||||||
def is_valid(self, *, raise_exception=False):
|
def is_valid(self, *, raise_exception=False):
|
||||||
|
|
@ -1064,6 +1079,28 @@ class DocumentSerializers(ApiMixin, serializers.Serializer):
|
||||||
delete_embedding_by_document_list(document_id_list)
|
delete_embedding_by_document_list(document_id_list)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def batch_cancel(self, instance: Dict, with_valid=True):
|
||||||
|
if with_valid:
|
||||||
|
self.is_valid(raise_exception=True)
|
||||||
|
BatchCancelInstanceSerializer(data=instance).is_valid(raise_exception=True)
|
||||||
|
document_id_list = instance.get("id_list")
|
||||||
|
ListenerManagement.update_status(QuerySet(Paragraph).annotate(
|
||||||
|
reversed_status=Reverse('status'),
|
||||||
|
task_type_status=Substr('reversed_status', TaskType(instance.get('type')).value,
|
||||||
|
1),
|
||||||
|
).filter(task_type_status__in=[State.PENDING.value, State.STARTED.value]).filter(
|
||||||
|
document_id__in=document_id_list).values('id'),
|
||||||
|
TaskType(instance.get('type')),
|
||||||
|
State.REVOKE)
|
||||||
|
ListenerManagement.update_status(QuerySet(Document).annotate(
|
||||||
|
reversed_status=Reverse('status'),
|
||||||
|
task_type_status=Substr('reversed_status', TaskType(instance.get('type')).value,
|
||||||
|
1),
|
||||||
|
).filter(task_type_status__in=[State.PENDING.value, State.STARTED.value]).filter(
|
||||||
|
id__in=document_id_list).values('id'),
|
||||||
|
TaskType(instance.get('type')),
|
||||||
|
State.REVOKE)
|
||||||
|
|
||||||
def batch_edit_hit_handling(self, instance: Dict, with_valid=True):
|
def batch_edit_hit_handling(self, instance: Dict, with_valid=True):
|
||||||
if with_valid:
|
if with_valid:
|
||||||
BatchSerializer(data=instance).is_valid(model=Document, raise_exception=True)
|
BatchSerializer(data=instance).is_valid(model=Document, raise_exception=True)
|
||||||
|
|
|
||||||
|
|
@ -37,3 +37,17 @@ class DocumentApi(ApiMixin):
|
||||||
description="1|2|3 1:向量化|2:生成问题|3:同步文档")
|
description="1|2|3 1:向量化|2:生成问题|3:同步文档")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class BatchCancel(ApiMixin):
|
||||||
|
@staticmethod
|
||||||
|
def get_request_body_api():
|
||||||
|
return openapi.Schema(
|
||||||
|
type=openapi.TYPE_OBJECT,
|
||||||
|
properties={
|
||||||
|
'id_list': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_STRING),
|
||||||
|
title="文档id列表",
|
||||||
|
description="文档id列表"),
|
||||||
|
'type': openapi.Schema(type=openapi.TYPE_INTEGER, title="任务类型",
|
||||||
|
description="1|2|3 1:向量化|2:生成问题|3:同步文档", default=1)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ urlpatterns = [
|
||||||
path('dataset/<str:dataset_id>/document/<str:document_id>/sync', views.Document.SyncWeb.as_view()),
|
path('dataset/<str:dataset_id>/document/<str:document_id>/sync', views.Document.SyncWeb.as_view()),
|
||||||
path('dataset/<str:dataset_id>/document/<str:document_id>/refresh', views.Document.Refresh.as_view()),
|
path('dataset/<str:dataset_id>/document/<str:document_id>/refresh', views.Document.Refresh.as_view()),
|
||||||
path('dataset/<str:dataset_id>/document/<str:document_id>/cancel_task', views.Document.CancelTask.as_view()),
|
path('dataset/<str:dataset_id>/document/<str:document_id>/cancel_task', views.Document.CancelTask.as_view()),
|
||||||
|
path('dataset/<str:dataset_id>/document/cancel_task/_batch',
|
||||||
|
views.Document.CancelTask.Batch.as_view()),
|
||||||
path('dataset/<str:dataset_id>/document/<str:document_id>/paragraph', views.Paragraph.as_view()),
|
path('dataset/<str:dataset_id>/document/<str:document_id>/paragraph', views.Paragraph.as_view()),
|
||||||
path('dataset/<str:dataset_id>/document/batch_generate_related', views.Document.BatchGenerateRelated.as_view()),
|
path('dataset/<str:dataset_id>/document/batch_generate_related', views.Document.BatchGenerateRelated.as_view()),
|
||||||
path(
|
path(
|
||||||
|
|
|
||||||
|
|
@ -238,6 +238,24 @@ class Document(APIView):
|
||||||
request.data
|
request.data
|
||||||
))
|
))
|
||||||
|
|
||||||
|
class Batch(APIView):
|
||||||
|
authentication_classes = [TokenAuth]
|
||||||
|
|
||||||
|
@action(methods=['PUT'], detail=False)
|
||||||
|
@swagger_auto_schema(operation_summary="批量取消任务",
|
||||||
|
operation_id="批量取消任务",
|
||||||
|
request_body=DocumentApi.BatchCancel.get_request_body_api(),
|
||||||
|
manual_parameters=DocumentSerializers.Create.get_request_params_api(),
|
||||||
|
responses=result.get_default_response(),
|
||||||
|
tags=["知识库/文档"]
|
||||||
|
)
|
||||||
|
@has_permissions(
|
||||||
|
lambda r, k: Permission(group=Group.DATASET, operate=Operate.MANAGE,
|
||||||
|
dynamic_tag=k.get('dataset_id')))
|
||||||
|
def put(self, request: Request, dataset_id: str):
|
||||||
|
return result.success(
|
||||||
|
DocumentSerializers.Batch(data={'dataset_id': dataset_id}).batch_cancel(request.data))
|
||||||
|
|
||||||
class Refresh(APIView):
|
class Refresh(APIView):
|
||||||
authentication_classes = [TokenAuth]
|
authentication_classes = [TokenAuth]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue