Skip to content

Commit

Permalink
fix(backend): process_todo、batch_process_todo接口优化 #8303
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 25412
  • Loading branch information
ygcyao committed Nov 29, 2024
1 parent b075199 commit 7ec6d7b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
44 changes: 44 additions & 0 deletions dbm-ui/backend/flow/utils/process_todo_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import asyncio
import concurrent.futures
import logging.config

from backend.ticket.models import Todo
from backend.ticket.todos import TodoActorFactory

logger = logging.getLogger("flow")


def process_single_todo(operation, act, username):
"""
处理单个待办的辅助函数
"""
todo_id = operation["todo_id"]
params = operation["params"]
todo = Todo.objects.get(id=todo_id)
TodoActorFactory.actor(todo).process(username, act, params)


async def process_operations_async(operations, act, username):
"""
异步处理待办操作
"""
loop = asyncio.get_running_loop()

# 自定义线程池
with concurrent.futures.ThreadPoolExecutor() as executor:
# 将所有的任务提交到线程池
tasks = [
loop.run_in_executor(executor, process_single_todo, operation, act, username) for operation in operations
]
# 等待所有任务完成
await asyncio.gather(*tasks)
13 changes: 8 additions & 5 deletions dbm-ui/backend/ticket/models/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,25 @@ def current_flow(self) -> Flow:
1. 取 TicketFlow 中最后一个 flow_obj_id 非空的流程
2. 若 TicketFlow 中都流程都为空,则代表整个单据未开始,取第一个流程
"""
if Flow.objects.filter(ticket=self).exclude(status=TicketFlowStatus.PENDING).exists():
return Flow.objects.filter(ticket=self).exclude(status=TicketFlowStatus.PENDING).last()
non_pending_flows = [flow for flow in self.flows.all() if flow.status != TicketFlowStatus.PENDING]
if non_pending_flows:
# 返回最后一个符合条件的 Flow 对象
return non_pending_flows[-1]
# 初始化时,当前节点和下一个节点为同一个
return self.next_flow()

def next_flow(self) -> Flow:
"""
下一个流程,即 TicketFlow 中第一个为PENDING的流程
"""
next_flows = Flow.objects.filter(ticket=self, status=TicketFlowStatus.PENDING)
next_flows = [flow for flow in self.flows.all() if flow.status == TicketFlowStatus.PENDING]

# 支持跳过人工审批和确认环节
if env.ITSM_FLOW_SKIP:
next_flows = next_flows.exclude(flow_type__in=[FlowType.BK_ITSM, FlowType.PAUSE])
next_flows = [flow for flow in next_flows if flow.flow_type not in [FlowType.BK_ITSM, FlowType.PAUSE]]

return next_flows.first()
# 返回第一个符合条件的 Flow 对象
return next_flows[0] if next_flows else None

@classmethod
def create_ticket(
Expand Down
21 changes: 12 additions & 9 deletions dbm-ui/backend/ticket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import asyncio
import operator
from functools import reduce

Expand All @@ -25,6 +26,7 @@
from backend.bk_web.swagger import PaginatedResponseSwaggerAutoSchema, common_swagger_auto_schema
from backend.configuration.models import DBAdministrator
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
from backend.flow.utils.process_todo_utils import process_operations_async
from backend.iam_app.dataclass import ResourceEnum
from backend.iam_app.dataclass.actions import ActionEnum
from backend.iam_app.handlers.drf_perm.base import RejectPermission, ResourceActionPermission
Expand Down Expand Up @@ -221,6 +223,7 @@ def perform_create(self, serializer):
builder.patch_ticket_detail()
builder.init_ticket_flows()

ticket = Ticket.objects.prefetch_related("flows").get(pk=ticket.pk)
TicketFlowManager(ticket=ticket).run_next_flow()

@swagger_auto_schema(
Expand Down Expand Up @@ -437,7 +440,9 @@ def process_todo(self, request, *args, **kwargs):

validated_data = self.params_validate(self.get_serializer_class())

todo = ticket.todo_of_ticket.get(id=validated_data["todo_id"])
todo = (
Todo.objects.select_related("ticket").prefetch_related("ticket__flows").get(id=validated_data["todo_id"])
)
TodoActorFactory.actor(todo).process(request.user.username, validated_data["action"], validated_data["params"])

return Response(TodoSerializer(ticket.todo_of_ticket.all(), many=True).data)
Expand Down Expand Up @@ -644,15 +649,13 @@ def batch_process_todo(self, request, *args, **kwargs):
"""
validated_data = self.params_validate(self.get_serializer_class())
act = validated_data["action"]
operations = validated_data["operations"]

# 批量处理待办操作
results = []
for operation in validated_data["operations"]:
todo_id = operation["todo_id"]
params = operation["params"]
todo = Todo.objects.get(id=todo_id)
TodoActorFactory.actor(todo).process(request.user.username, act, params)
results.append(todo)
# 执行异步处理
asyncio.run(process_operations_async(operations, act, request.user.username))

# 获取处理后的待办事项
results = [Todo.objects.get(id=operation["todo_id"]) for operation in operations]

# 使用 TodoSerializer 序列化响应数据
return Response(TodoSerializer(results, many=True).data)

0 comments on commit 7ec6d7b

Please sign in to comment.