From d182d14e26c2ac0760df1b27cc028be0f4e658a0 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 1 Aug 2023 18:17:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B4=A6=E5=8F=B7=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96=20(#11151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- .../automations/backup_account/handlers.py | 51 +++++++++---------- .../automations/backup_account/manager.py | 12 ++--- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/apps/accounts/automations/backup_account/handlers.py b/apps/accounts/automations/backup_account/handlers.py index 65c96c67d..7b087b101 100644 --- a/apps/accounts/automations/backup_account/handlers.py +++ b/apps/accounts/automations/backup_account/handlers.py @@ -1,22 +1,17 @@ import os import time -from openpyxl import Workbook from collections import defaultdict, OrderedDict from django.conf import settings -from django.db.models import F +from openpyxl import Workbook from rest_framework import serializers -from accounts.models import Account -from assets.const import AllTypes -from accounts.serializers import AccountSecretSerializer from accounts.notifications import AccountBackupExecutionTaskMsg -from users.models import User -from common.utils import get_logger -from common.utils.timezone import local_now_display +from accounts.serializers import AccountSecretSerializer +from assets.const import AllTypes from common.utils.file import encrypt_and_compress_zip_file - -logger = get_logger(__file__) +from common.utils.timezone import local_now_display +from users.models import User PATH = os.path.join(os.path.dirname(settings.BASE_DIR), 'tmp') @@ -99,7 +94,7 @@ class AssetAccountHandler(BaseAccountHandler): data = AccountSecretSerializer(_accounts, many=True).data data_map.update(cls.add_rows(data, header_fields, sheet_name)) - logger.info('\n\033[33m- 共备份 {} 条账号\033[0m'.format(accounts.count())) + print('\n\033[33m- 共备份 {} 条账号\033[0m'.format(accounts.count())) return data_map @@ -110,7 +105,7 @@ class AccountBackupHandler: self.is_frozen = False # 任务状态冻结标志 def create_excel(self): - logger.info( + print( '\n' '\033[32m>>> 正在生成资产或应用相关备份信息文件\033[0m' '' @@ -133,14 +128,14 @@ class AccountBackupHandler: wb.save(filename) files.append(filename) timedelta = round((time.time() - time_start), 2) - logger.info('步骤完成: 用时 {}s'.format(timedelta)) + print('步骤完成: 用时 {}s'.format(timedelta)) return files def send_backup_mail(self, files, recipients): if not files: return recipients = User.objects.filter(id__in=list(recipients)) - logger.info( + print( '\n' '\033[32m>>> 发送备份邮件\033[0m' '' @@ -155,7 +150,7 @@ class AccountBackupHandler: encrypt_and_compress_zip_file(attachment, password, files) attachment_list = [attachment, ] AccountBackupExecutionTaskMsg(plan_name, user).publish(attachment_list) - logger.info('邮件已发送至{}({})'.format(user, user.email)) + print('邮件已发送至{}({})'.format(user, user.email)) for file in files: os.remove(file) @@ -163,13 +158,13 @@ class AccountBackupHandler: self.execution.reason = reason[:1024] self.execution.is_success = is_success self.execution.save() - logger.info('已完成对任务状态的更新') + print('已完成对任务状态的更新') def step_finished(self, is_success): if is_success: - logger.info('任务执行成功') + print('任务执行成功') else: - logger.error('任务执行失败') + print('任务执行失败') def _run(self): is_success = False @@ -177,7 +172,7 @@ class AccountBackupHandler: try: recipients = self.execution.plan_snapshot.get('recipients') if not recipients: - logger.info( + print( '\n' '\033[32m>>> 该备份任务未分配收件人\033[0m' '' @@ -187,9 +182,9 @@ class AccountBackupHandler: self.send_backup_mail(files, recipients) except Exception as e: self.is_frozen = True - logger.error('任务执行被异常中断') - logger.info('下面打印发生异常的 Traceback 信息 : ') - logger.error(e, exc_info=True) + print('任务执行被异常中断') + print('下面打印发生异常的 Traceback 信息 : ') + print(e) error = str(e) else: is_success = True @@ -199,15 +194,15 @@ class AccountBackupHandler: self.step_finished(is_success) def run(self): - logger.info('任务开始: {}'.format(local_now_display())) + print('任务开始: {}'.format(local_now_display())) time_start = time.time() try: self._run() except Exception as e: - logger.error('任务运行出现异常') - logger.error('下面显示异常 Traceback 信息: ') - logger.error(e, exc_info=True) + print('任务运行出现异常') + print('下面显示异常 Traceback 信息: ') + print(e) finally: - logger.info('\n任务结束: {}'.format(local_now_display())) + print('\n任务结束: {}'.format(local_now_display())) timedelta = round((time.time() - time_start), 2) - logger.info('用时: {}'.format(timedelta)) + print('用时: {}'.format(timedelta)) diff --git a/apps/accounts/automations/backup_account/manager.py b/apps/accounts/automations/backup_account/manager.py index 311361c69..f523803d5 100644 --- a/apps/accounts/automations/backup_account/manager.py +++ b/apps/accounts/automations/backup_account/manager.py @@ -4,13 +4,9 @@ import time from django.utils import timezone -from common.utils import get_logger from common.utils.timezone import local_now_display - from .handlers import AccountBackupHandler -logger = get_logger(__name__) - class AccountBackupManager: def __init__(self, execution): @@ -23,7 +19,7 @@ class AccountBackupManager: def do_run(self): execution = self.execution - logger.info('\n\033[33m# 账号备份计划正在执行\033[0m') + print('\n\033[33m# 账号备份计划正在执行\033[0m') handler = AccountBackupHandler(execution) handler.run() @@ -35,10 +31,10 @@ class AccountBackupManager: self.time_end = time.time() self.date_end = timezone.now() - logger.info('\n\n' + '-' * 80) - logger.info('计划执行结束 {}\n'.format(local_now_display())) + print('\n\n' + '-' * 80) + print('计划执行结束 {}\n'.format(local_now_display())) self.timedelta = self.time_end - self.time_start - logger.info('用时: {}s'.format(self.timedelta)) + print('用时: {}s'.format(self.timedelta)) self.execution.timedelta = self.timedelta self.execution.save()