Merge pull request #9621 from jumpserver/pr@dev@perf_json_encoer_error

perf: 优化 task 参数是 uuid 可能导致的问题
pull/9622/head
老广 2 years ago committed by GitHub
commit 076a4333a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,7 +33,7 @@ def test_assets_connectivity_task(asset_ids, org_id, task_name=None):
def test_assets_connectivity_manual(assets):
task_name = gettext_noop("Test assets connectivity ")
asset_ids = [str(i.id) for i in assets]
asset_ids = [i.id for i in assets]
org_id = str(current_org.id)
return test_assets_connectivity_task.delay(asset_ids, org_id, task_name)
@ -41,6 +41,5 @@ def test_assets_connectivity_manual(assets):
def test_node_assets_connectivity_manual(node):
task_name = gettext_noop("Test if the assets under the node are connectable ")
asset_ids = node.get_all_asset_ids()
asset_ids = [str(i) for i in asset_ids]
org_id = str(current_org.id)
return test_assets_connectivity_task.delay(asset_ids, org_id, task_name)

@ -4,7 +4,7 @@ from django.utils.translation import gettext_noop, ugettext_lazy as _
from assets.const import AutomationTypes
from common.utils import get_logger
from orgs.utils import org_aware_func, tmp_to_org, current_org
from orgs.utils import tmp_to_org, current_org
from .common import quickstart_automation
logger = get_logger(__file__)
@ -19,7 +19,6 @@ __all__ = [
verbose_name=_('Test gateways connectivity'), queue='ansible',
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id)
)
@org_aware_func('assets')
def test_gateways_connectivity_task(asset_ids, org_id, local_port, task_name=None):
from assets.models import PingAutomation
if task_name is None:
@ -32,7 +31,6 @@ def test_gateways_connectivity_task(asset_ids, org_id, local_port, task_name=Non
def test_gateways_connectivity_manual(gateway_ids, local_port):
from assets.models import Asset
gateways = Asset.objects.filter(id__in=gateway_ids).values_list('id', flat=True)
task_name = gettext_noop("Test gateways connectivity")
return test_gateways_connectivity_task.delay(gateways, str(current_org.id), local_port, task_name)
gateway_ids = [str(i) for i in gateway_ids]
return test_gateways_connectivity_task.delay(gateway_ids, str(current_org.id), local_port, task_name)

@ -1,18 +1,16 @@
# -*- coding: utf-8 -*-
#
from celery import signals
from django.db.models.signals import post_save
from django.utils.translation import gettext_lazy as _, gettext_noop
from accounts.const import AutomationTypes
from accounts.models import AccountBackupAutomation
from assets.models import Asset, Node
from audits.models import ActivityLog
from common.utils import get_object_or_none, i18n_fmt, get_logger
from common.utils import i18n_fmt, get_logger
from jumpserver.utils import current_request
from ops.celery import app
from orgs.models import Organization
from orgs.utils import tmp_to_root_org, current_org
from orgs.utils import current_org
from terminal.models import Session
from users.models import User
from ..const import ActivityChoices
@ -21,89 +19,6 @@ from ..models import UserLoginLog
logger = get_logger(__name__)
class TaskActivityHandler(object):
@staticmethod
def _func_accounts_execute_automation(*args, **kwargs):
asset_ids = []
pid, tp = kwargs.get('pid'), kwargs.get('tp')
model = AutomationTypes.get_type_model(tp)
task_type_label = tp.label
with tmp_to_root_org():
instance = get_object_or_none(model, pk=pid)
if instance is not None:
asset_ids = instance.get_all_assets().values_list('id', flat=True)
return task_type_label, asset_ids
@staticmethod
def _func_accounts_push_accounts_to_assets(*args, **kwargs):
return '', args[0][1]
@staticmethod
def _func_accounts_execute_account_backup_plan(*args, **kwargs):
asset_ids, pid = [], kwargs.get('pid')
with tmp_to_root_org():
instance = get_object_or_none(AccountBackupAutomation, pk=pid)
if instance is not None:
asset_ids = Asset.objects.filter(
platform__type__in=instance.types
).values_list('id', flat=True)
return '', asset_ids
@staticmethod
def _func_assets_verify_accounts_connectivity(*args, **kwargs):
return '', args[0][1]
@staticmethod
def _func_accounts_verify_accounts_connectivity(*args, **kwargs):
return '', args[0][1]
@staticmethod
def _func_assets_test_assets_connectivity_manual(*args, **kwargs):
return '', args[0][0]
@staticmethod
def _func_assets_test_node_assets_connectivity_manual(*args, **kwargs):
asset_ids = []
node = get_object_or_none(Node, pk=args[0][0])
if node is not None:
asset_ids = node.get_all_assets().values_list('id', flat=True)
return '', asset_ids
@staticmethod
def _func_assets_update_assets_hardware_info_manual(*args, **kwargs):
return '', args[0][0]
@staticmethod
def _func_assets_update_node_assets_hardware_info_manual(*args, **kwargs):
asset_ids = []
node = get_object_or_none(Node, pk=args[0][0])
if node is not None:
asset_ids = node.get_all_assets().values_list('id', flat=True)
return '', asset_ids
@staticmethod
def get_task_display(task_name, **kwargs):
task = app.tasks.get(task_name)
return getattr(task, 'verbose_name', _('Unknown'))
def get_info_by_task_name(self, task_name, *args, **kwargs):
resource_ids = []
task_name_list = str(task_name).split('.')
if len(task_name_list) < 2:
return '', resource_ids
task_display = self.get_task_display(task_name)
model, name = task_name_list[0], task_name_list[-1]
func_name = '_func_%s_%s' % (model, name)
handle_func = getattr(self, func_name, None)
if handle_func is not None:
task_type, resource_ids = handle_func(*args, **kwargs)
if task_type:
task_display = '%s-%s' % (task_display, task_type)
return task_display, resource_ids
class ActivityLogHandler:
@staticmethod
def session_for_activity(obj):

@ -6,6 +6,7 @@ from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from rest_framework.utils.encoders import JSONEncoder
from common.local import add_encrypted_field_set
from common.utils import signer, crypto
@ -46,7 +47,7 @@ class JsonMixin:
@staticmethod
def json_encode(data):
return json.dumps(data)
return json.dumps(data, cls=JSONEncoder)
def from_db_value(self, value, expression, connection, context=None):
if value is None:

Loading…
Cancel
Save