perf: 优化发布机不显示task信息问题 (#10450)

* perf: 优化发布机不显示task信息问题

* perf: 添加celery task execution api的task_name字段

---------

Co-authored-by: “huailei000” <2280131253@qq.com>
Co-authored-by: feng <1304903146@qq.com>
pull/10464/head
fit2bot 2023-05-16 14:48:00 +08:00 committed by GitHub
parent ec10ee3298
commit c7c3f711bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 24 deletions

View File

@ -12,11 +12,10 @@ from accounts.tasks import push_accounts_to_assets_task, verify_accounts_connect
from assets import serializers from assets import serializers
from assets.exceptions import NotSupportedTemporarilyError from assets.exceptions import NotSupportedTemporarilyError
from assets.filters import IpInFilterBackend, LabelFilterBackend, NodeFilterBackend 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 assets.tasks import test_assets_connectivity_manual, update_assets_hardware_info_manual
from common.api import SuggestionMixin from common.api import SuggestionMixin
from common.drf.filters import BaseFilterSet from common.drf.filters import BaseFilterSet
from common.permissions import IsValidUser
from common.utils import get_logger, is_uuid from common.utils import get_logger, is_uuid
from orgs.mixins import generics from orgs.mixins import generics
from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins.api import OrgBulkModelViewSet
@ -27,7 +26,6 @@ logger = get_logger(__file__)
__all__ = [ __all__ = [
"AssetViewSet", "AssetTaskCreateApi", "AssetViewSet", "AssetTaskCreateApi",
"AssetsTaskCreateApi", 'AssetFilterSet', "AssetsTaskCreateApi", 'AssetFilterSet',
"AutomationExecutionRetrieveApi",
] ]
@ -260,9 +258,3 @@ class AssetsTaskCreateApi(AssetsTaskMixin, generics.CreateAPIView):
has = self.request.user.has_perm(perm_required) has = self.request.user.has_perm(perm_required)
if not has: if not has:
self.permission_denied(request) self.permission_denied(request)
class AutomationExecutionRetrieveApi(generics.RetrieveAPIView):
permission_classes = (IsValidUser,)
model = AutomationExecution
serializer_class = serializers.AutomationExecutionSerializer

View File

@ -47,7 +47,6 @@ urlpatterns = [
path('gateways/<uuid:pk>/test-connective/', api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'), path('gateways/<uuid:pk>/test-connective/', api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'),
path('platform-automation-methods/', api.PlatformAutomationMethodsApi.as_view(), name='platform-automation-methods'), path('platform-automation-methods/', api.PlatformAutomationMethodsApi.as_view(), name='platform-automation-methods'),
path('automations-execution/<uuid:pk>/', api.AutomationExecutionRetrieveApi.as_view(), name='automation-execution'),
] ]
urlpatterns += router.urls urlpatterns += router.urls

View File

@ -4,19 +4,19 @@ import os
import re import re
from celery.result import AsyncResult from celery.result import AsyncResult
from rest_framework import generics, viewsets, mixins, status
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django_celery_beat.models import PeriodicTask from django_celery_beat.models import PeriodicTask
from rest_framework import generics, viewsets, mixins, status
from rest_framework.response import Response from rest_framework.response import Response
from common.api import LogTailApi, CommonApiMixin
from common.exceptions import JMSException from common.exceptions import JMSException
from common.permissions import IsValidUser from common.permissions import IsValidUser
from common.api import LogTailApi, CommonApiMixin
from ops.celery import app 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 ..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 import CeleryResultSerializer, CeleryPeriodTaskSerializer
from ..serializers.celery import CeleryTaskSerializer, CeleryTaskExecutionSerializer from ..serializers.celery import CeleryTaskSerializer, CeleryTaskExecutionSerializer

View File

@ -5,13 +5,14 @@ from django.utils.translation import gettext_lazy as _
from django_celery_beat.models import PeriodicTask from django_celery_beat.models import PeriodicTask
from rest_framework import serializers from rest_framework import serializers
from ops.celery import app
from ops.models import CeleryTask, CeleryTaskExecution
__all__ = [ __all__ = [
'CeleryResultSerializer', 'CeleryTaskExecutionSerializer', 'CeleryResultSerializer', 'CeleryTaskExecutionSerializer',
'CeleryPeriodTaskSerializer', 'CeleryTaskSerializer' 'CeleryPeriodTaskSerializer', 'CeleryTaskSerializer'
] ]
from ops.models import CeleryTask, CeleryTaskExecution
class CeleryResultSerializer(serializers.Serializer): class CeleryResultSerializer(serializers.Serializer):
id = serializers.UUIDField() id = serializers.UUIDField()
@ -37,11 +38,24 @@ class CeleryTaskSerializer(serializers.ModelSerializer):
class CeleryTaskExecutionSerializer(serializers.ModelSerializer): class CeleryTaskExecutionSerializer(serializers.ModelSerializer):
is_success = serializers.BooleanField(required=False, read_only=True, label=_('Success')) is_success = serializers.BooleanField(required=False, read_only=True, label=_('Success'))
task_name = serializers.SerializerMethodField()
class Meta: class Meta:
model = CeleryTaskExecution model = CeleryTaskExecution
fields = [ fields = [
"id", "name", "args", "kwargs", "time_cost", "timedelta", "id", "name", "task_name", "args", "kwargs", "time_cost", "timedelta",
"is_success", "is_finished", "date_published", "is_success", "is_finished", "date_published",
"date_start", "date_finished" "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

View File

@ -71,10 +71,6 @@
<span>{% trans 'Task type' %}:</span> <span>{% trans 'Task type' %}:</span>
<span class="value task-type"></span> <span class="value task-type"></span>
</li> </li>
<li class="item">
<span>{% trans 'Trigger type' %}:</span>
<span class="value trigger-type"></span>
</li>
<li class="item"> <li class="item">
<span>{% trans 'Date start' %}:</span> <span>{% trans 'Date start' %}:</span>
<span class="value date-start"></span> <span class="value date-start"></span>
@ -93,6 +89,7 @@
var failOverWsURL = scheme + "://" + document.location.hostname + ':' + failOverPort + url; var failOverWsURL = scheme + "://" + document.location.hostname + ':' + failOverPort + url;
var term; var term;
var ws; var ws;
var extraQuery = Object.fromEntries(new URLSearchParams(window.location.search));
$(document).ready(function () { $(document).ready(function () {
term = new Terminal({ term = new Terminal({
@ -131,7 +128,7 @@
window.fit.fit(term); window.fit.fit(term);
}); });
function getAutomationExecutionInfo() { function getAutomationExecutionInfo() {
var url = "{% url 'api-assets:automation-execution' pk=task_id %}"; let url = "{% url 'api-ops:task-executions-detail' pk=task_id %}";
requestApi({ requestApi({
url: url, url: url,
@ -140,8 +137,7 @@
success(data) { success(data) {
const dateStart = new Date(data.date_start).toLocaleString(); const dateStart = new Date(data.date_start).toLocaleString();
$('.task-id').html(data.id); $('.task-id').html(data.id);
$('.task-type').html(data.snapshot.type.label); $('.task-type').html(data.task_name);
$('.trigger-type').html(data.trigger.label);
$('.date-start').html(dateStart); $('.date-start').html(dateStart);
} }
}) })