From c7c3f711bfc6bff534235492fea77ffde0ae62cf Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 16 May 2023 14:48:00 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E6=9C=BA=E4=B8=8D=E6=98=BE=E7=A4=BAtask=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#10450)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 优化发布机不显示task信息问题 * perf: 添加celery task execution api的task_name字段 --------- Co-authored-by: “huailei000” <2280131253@qq.com> Co-authored-by: feng <1304903146@qq.com> --- apps/assets/api/asset/asset.py | 10 +--------- apps/assets/urls/api_urls.py | 1 - apps/ops/api/celery.py | 8 ++++---- apps/ops/serializers/celery.py | 20 +++++++++++++++++--- apps/ops/templates/ops/celery_task_log.html | 10 +++------- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/apps/assets/api/asset/asset.py b/apps/assets/api/asset/asset.py index 96d55cae9..3f0b21f19 100644 --- a/apps/assets/api/asset/asset.py +++ b/apps/assets/api/asset/asset.py @@ -12,11 +12,10 @@ from accounts.tasks import push_accounts_to_assets_task, verify_accounts_connect from assets import serializers from assets.exceptions import NotSupportedTemporarilyError from assets.filters import IpInFilterBackend, LabelFilterBackend, NodeFilterBackend -from assets.models import Asset, Gateway, Platform, AutomationExecution +from assets.models import Asset, Gateway, Platform from assets.tasks import test_assets_connectivity_manual, update_assets_hardware_info_manual from common.api import SuggestionMixin from common.drf.filters import BaseFilterSet -from common.permissions import IsValidUser from common.utils import get_logger, is_uuid from orgs.mixins import generics from orgs.mixins.api import OrgBulkModelViewSet @@ -27,7 +26,6 @@ logger = get_logger(__file__) __all__ = [ "AssetViewSet", "AssetTaskCreateApi", "AssetsTaskCreateApi", 'AssetFilterSet', - "AutomationExecutionRetrieveApi", ] @@ -260,9 +258,3 @@ class AssetsTaskCreateApi(AssetsTaskMixin, generics.CreateAPIView): has = self.request.user.has_perm(perm_required) if not has: self.permission_denied(request) - - -class AutomationExecutionRetrieveApi(generics.RetrieveAPIView): - permission_classes = (IsValidUser,) - model = AutomationExecution - serializer_class = serializers.AutomationExecutionSerializer diff --git a/apps/assets/urls/api_urls.py b/apps/assets/urls/api_urls.py index 38924f333..6b5f469d0 100644 --- a/apps/assets/urls/api_urls.py +++ b/apps/assets/urls/api_urls.py @@ -47,7 +47,6 @@ urlpatterns = [ path('gateways//test-connective/', api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'), path('platform-automation-methods/', api.PlatformAutomationMethodsApi.as_view(), name='platform-automation-methods'), - path('automations-execution//', api.AutomationExecutionRetrieveApi.as_view(), name='automation-execution'), ] urlpatterns += router.urls diff --git a/apps/ops/api/celery.py b/apps/ops/api/celery.py index 72d970dee..d4540269c 100644 --- a/apps/ops/api/celery.py +++ b/apps/ops/api/celery.py @@ -4,19 +4,19 @@ import os import re from celery.result import AsyncResult -from rest_framework import generics, viewsets, mixins, status from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext as _ from django_celery_beat.models import PeriodicTask +from rest_framework import generics, viewsets, mixins, status from rest_framework.response import Response +from common.api import LogTailApi, CommonApiMixin from common.exceptions import JMSException from common.permissions import IsValidUser -from common.api import LogTailApi, CommonApiMixin from ops.celery import app -from ..models import CeleryTaskExecution, CeleryTask -from ..celery.utils import get_celery_task_log_path from ..ansible.utils import get_ansible_task_log_path +from ..celery.utils import get_celery_task_log_path +from ..models import CeleryTaskExecution, CeleryTask from ..serializers import CeleryResultSerializer, CeleryPeriodTaskSerializer from ..serializers.celery import CeleryTaskSerializer, CeleryTaskExecutionSerializer diff --git a/apps/ops/serializers/celery.py b/apps/ops/serializers/celery.py index 657600e50..5166aa755 100644 --- a/apps/ops/serializers/celery.py +++ b/apps/ops/serializers/celery.py @@ -5,13 +5,14 @@ from django.utils.translation import gettext_lazy as _ from django_celery_beat.models import PeriodicTask from rest_framework import serializers +from ops.celery import app +from ops.models import CeleryTask, CeleryTaskExecution + __all__ = [ 'CeleryResultSerializer', 'CeleryTaskExecutionSerializer', 'CeleryPeriodTaskSerializer', 'CeleryTaskSerializer' ] -from ops.models import CeleryTask, CeleryTaskExecution - class CeleryResultSerializer(serializers.Serializer): id = serializers.UUIDField() @@ -37,11 +38,24 @@ class CeleryTaskSerializer(serializers.ModelSerializer): class CeleryTaskExecutionSerializer(serializers.ModelSerializer): is_success = serializers.BooleanField(required=False, read_only=True, label=_('Success')) + task_name = serializers.SerializerMethodField() class Meta: model = CeleryTaskExecution fields = [ - "id", "name", "args", "kwargs", "time_cost", "timedelta", + "id", "name", "task_name", "args", "kwargs", "time_cost", "timedelta", "is_success", "is_finished", "date_published", "date_start", "date_finished" ] + + @staticmethod + def get_task_name(obj): + from assets.const import AutomationTypes as AssetTypes + from accounts.const import AutomationTypes as AccountTypes + tp_dict = dict(AssetTypes.choices) | dict(AccountTypes.choices) + tp = obj.kwargs.get('tp') + task = app.tasks.get(obj.name) + task_name = getattr(task, 'verbose_name', obj.name) + if tp: + task_name = f'{task_name}({tp_dict.get(tp, tp)})' + return task_name diff --git a/apps/ops/templates/ops/celery_task_log.html b/apps/ops/templates/ops/celery_task_log.html index b0f16130b..576d63be4 100644 --- a/apps/ops/templates/ops/celery_task_log.html +++ b/apps/ops/templates/ops/celery_task_log.html @@ -71,10 +71,6 @@ {% trans 'Task type' %}: -
  • - {% trans 'Trigger type' %}: - -
  • {% trans 'Date start' %}: @@ -93,6 +89,7 @@ var failOverWsURL = scheme + "://" + document.location.hostname + ':' + failOverPort + url; var term; var ws; + var extraQuery = Object.fromEntries(new URLSearchParams(window.location.search)); $(document).ready(function () { term = new Terminal({ @@ -131,7 +128,7 @@ window.fit.fit(term); }); function getAutomationExecutionInfo() { - var url = "{% url 'api-assets:automation-execution' pk=task_id %}"; + let url = "{% url 'api-ops:task-executions-detail' pk=task_id %}"; requestApi({ url: url, @@ -140,8 +137,7 @@ success(data) { const dateStart = new Date(data.date_start).toLocaleString(); $('.task-id').html(data.id); - $('.task-type').html(data.snapshot.type.label); - $('.trigger-type').html(data.trigger.label); + $('.task-type').html(data.task_name); $('.date-start').html(dateStart); } })