Skip to content

Commit

Permalink
optimization: 任务列表过滤 sql 语句优化
Browse files Browse the repository at this point in the history
  • Loading branch information
normal-wls authored and ZhuoZhuoCrayon committed Oct 7, 2023
1 parent 0439863 commit 9566623
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
17 changes: 9 additions & 8 deletions gcloud/apigw/views/get_task_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
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.
"""
from apigw_manager.apigw.decorators import apigw_require
from blueapps.account.decorators import login_exempt
from django.db.models import Value
from django.views.decorators.http import require_GET

from blueapps.account.decorators import login_exempt
from gcloud import err_code
from gcloud.apigw.decorators import mark_request_whether_is_trust, timezone_inject, project_inject, return_json_response
from gcloud.taskflow3.models import TaskFlowInstance
from gcloud.apigw.decorators import mark_request_whether_is_trust, project_inject, return_json_response, timezone_inject
from gcloud.apigw.forms import GetTaskListForm
from gcloud.apigw.views.utils import format_task_list_data, paginate_list_data
from gcloud.iam_auth.conf import TASK_ACTIONS
from gcloud.iam_auth.intercept import iam_intercept
from gcloud.iam_auth.view_interceptors.apigw import ProjectViewInterceptor
from gcloud.iam_auth.utils import get_task_allowed_actions_for_user
from gcloud.iam_auth.conf import TASK_ACTIONS
from gcloud.apigw.forms import GetTaskListForm
from apigw_manager.apigw.decorators import apigw_require
from gcloud.iam_auth.view_interceptors.apigw import ProjectViewInterceptor
from gcloud.taskflow3.models import TaskFlowInstance


@login_exempt
Expand All @@ -44,7 +45,7 @@ def get_task_list(request, project_id):
"is_finished": "pipeline_instance__is_finished",
"executor": "pipeline_instance__executor",
}
filter_kwargs = dict(is_deleted=False, project_id=project.id)
filter_kwargs = dict(is_deleted=Value(0), project_id=project.id)
for param, filter_key in param_mappings.items():
if param in request.GET:
filter_kwargs[filter_key] = params_validator.cleaned_data[param]
Expand Down
12 changes: 6 additions & 6 deletions gcloud/core/apis/drf/viewsets/function_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
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.
"""

from django.db.models import Value
from rest_framework import permissions

from gcloud.core.apis.drf.viewsets import GcloudListViewSet
from gcloud.contrib.function.models import FunctionTask
from gcloud.core.apis.drf.serilaziers.function_task import FunctionTaskSerializer
from gcloud.core.apis.drf.permission import IamPermission, IamPermissionInfo
from gcloud.core.apis.drf.resource_helpers import ViewSetResourceHelper
from gcloud.iam_auth import res_factory, IAMMeta
from gcloud.core.apis.drf.serilaziers.function_task import FunctionTaskSerializer
from gcloud.core.apis.drf.viewsets import GcloudListViewSet
from gcloud.iam_auth import IAMMeta, res_factory
from gcloud.iam_auth.conf import TASK_ACTIONS
from gcloud.core.apis.drf.permission import IamPermissionInfo, IamPermission


class FunctionTaskPermission(IamPermission):
Expand All @@ -29,7 +29,7 @@ class FunctionTaskPermission(IamPermission):


class FunctionTaskViewSet(GcloudListViewSet):
queryset = FunctionTask.objects.filter(task__is_deleted=False)
queryset = FunctionTask.objects.filter(task__is_deleted=Value(0))
serializer_class = FunctionTaskSerializer
iam_resource_helper = ViewSetResourceHelper(
resource_func=res_factory.resources_for_function_task_obj, actions=TASK_ACTIONS, id_field="task.id"
Expand Down
24 changes: 6 additions & 18 deletions gcloud/core/apis/drf/viewsets/taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from django.conf import settings
from django.db import transaction
from django.db.models import Q
from django.db.models import Q, Value
from django.utils.translation import ugettext_lazy as _
from django_filters import FilterSet
from drf_yasg.utils import swagger_auto_schema
Expand All @@ -32,11 +32,7 @@
from gcloud.constants import TASK_NAME_MAX_LENGTH, TaskCreateMethod
from gcloud.contrib.appmaker.models import AppMaker
from gcloud.contrib.function.models import FunctionTask
from gcloud.contrib.operate_record.constants import (
OperateSource,
OperateType,
RecordType,
)
from gcloud.contrib.operate_record.constants import OperateSource, OperateType, RecordType
from gcloud.contrib.operate_record.signal import operate_record_signal
from gcloud.contrib.operate_record.utils import extract_extra_info
from gcloud.core.apis.drf.exceptions import ValidationException
Expand Down Expand Up @@ -66,17 +62,9 @@
from gcloud.core.models import EngineConfig
from gcloud.iam_auth import IAMMeta, get_iam_client, res_factory
from gcloud.iam_auth.conf import TASK_ACTIONS
from gcloud.iam_auth.utils import (
get_common_flow_allowed_actions_for_user,
get_flow_allowed_actions_for_user,
)
from gcloud.iam_auth.utils import get_common_flow_allowed_actions_for_user, get_flow_allowed_actions_for_user
from gcloud.taskflow3.domains.auto_retry import AutoRetryNodeStrategyCreator
from gcloud.taskflow3.models import (
TaskConfig,
TaskFlowInstance,
TaskFlowRelation,
TimeoutNodeConfig,
)
from gcloud.taskflow3.models import TaskConfig, TaskFlowInstance, TaskFlowRelation, TimeoutNodeConfig
from gcloud.tasktmpl3.models import TaskTemplate
from gcloud.utils.strings import standardize_name, standardize_pipeline_node_name

Expand Down Expand Up @@ -174,7 +162,7 @@ def has_permission(self, request, view):
class TaskFlowInstanceViewSet(GcloudReadOnlyViewSet, generics.CreateAPIView, generics.DestroyAPIView):
serializer_class = TaskFlowInstanceSerializer
queryset = TaskFlowInstance.objects.filter(
pipeline_instance__isnull=False, is_deleted=False, pipeline_instance__is_expired=False
pipeline_instance__isnull=False, is_deleted=Value(0), pipeline_instance__is_expired=False
).order_by("-id")
iam_resource_helper = ViewSetResourceHelper(resource_func=res_factory.resources_for_task_obj, actions=TASK_ACTIONS)
filter_class = TaskFlowFilterSet
Expand Down Expand Up @@ -389,7 +377,7 @@ def list_children_taskflow(self, request, *args, **kwargs):
)
children_task_ids = [info["task_id"] for info in children_task_info]
queryset = TaskFlowInstance.objects.filter(
id__in=children_task_ids, pipeline_instance__isnull=False, is_deleted=False
id__in=children_task_ids, pipeline_instance__isnull=False, is_deleted=Value(0)
)
queryset = self.filter_queryset(queryset)
serializer = self.get_serializer(queryset, many=True)
Expand Down
20 changes: 10 additions & 10 deletions gcloud/iam_auth/resource_api/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
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.
"""
from django.core.cache import cache
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.core.cache import cache
from django.db.models import Q, Value
from django.utils.translation import ugettext_lazy as _
from iam.resource.provider import ListResult, ResourceProvider

from gcloud.core.models import Project
from gcloud.iam_auth.conf import SEARCH_INSTANCE_CACHE_TIME
from gcloud.taskflow3.models import TaskFlowInstance
from iam import PathEqDjangoQuerySetConverter
from iam.contrib.django.dispatcher import InvalidPageException
from iam.resource.provider import ListResult, ResourceProvider
from gcloud.core.models import Project
from gcloud.taskflow3.models import TaskFlowInstance

attr_names = {
"en": {"type": "Task type", "iam_resource_owner": "Resource owner"},
Expand Down Expand Up @@ -65,7 +65,7 @@ def search_instance(self, filter, page, **options):
if results is None:
queryset = (
TaskFlowInstance.objects.select_related("pipeline_instance")
.filter(pipeline_instance__name__icontains=keyword, is_deleted=False)
.filter(pipeline_instance__name__icontains=keyword, is_deleted=Value(0))
.only("pipeline_instance__name")
)
if project_id:
Expand Down Expand Up @@ -114,13 +114,13 @@ def list_instance(self, filter, page, **options):
with_path = False

if not (filter.parent or filter.search or filter.resource_type_chain):
queryset = TaskFlowInstance.objects.filter(is_deleted=False)
queryset = TaskFlowInstance.objects.filter(is_deleted=Value(0))
elif filter.parent:
parent_id = filter.parent["id"]
if parent_id:
queryset = TaskFlowInstance.objects.filter(project_id=str(parent_id), is_deleted=False)
queryset = TaskFlowInstance.objects.filter(project_id=str(parent_id), is_deleted=Value(0))
else:
queryset = TaskFlowInstance.objects.filter(is_deleted=False)
queryset = TaskFlowInstance.objects.filter(is_deleted=Value(0))
elif filter.search and filter.resource_type_chain:
# 返回结果需要带上资源拓扑路径信息
with_path = True
Expand All @@ -129,7 +129,7 @@ def list_instance(self, filter, page, **options):
task_keywords = filter.search.get("task", [])

project_filter = Q()
task_filter = Q(is_deleted=False)
task_filter = Q(is_deleted=Value(0))

for keyword in project_keywords:
project_filter |= Q(name__icontains=keyword)
Expand Down

0 comments on commit 9566623

Please sign in to comment.