perf: 优化 index api 批量命令部分 (#9596)

Co-authored-by: Aaron3S <chenyang@fit2cloud.com>
pull/9598/head
fit2bot 2023-02-16 20:16:04 +08:00 committed by GitHub
parent f7500b8aff
commit e056b31b4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 20 deletions

View File

@ -17,7 +17,7 @@ from audits.models import UserLoginLog, PasswordChangeLog, OperateLog, FTPLog
from common.utils import lazyproperty from common.utils import lazyproperty
from common.utils.timezone import local_now, local_zero_hour from common.utils.timezone import local_now, local_zero_hour
from ops.const import JobStatus from ops.const import JobStatus
from ops.models import Job, JobExecution from ops.models import Job, JobExecution, JobAuditLog
from orgs.caches import OrgResourceStatisticsCache from orgs.caches import OrgResourceStatisticsCache
from orgs.utils import current_org from orgs.utils import current_org
from terminal.models import Session, Command from terminal.models import Session, Command
@ -121,9 +121,9 @@ class DateTimeMixin:
return queryset return queryset
@lazyproperty @lazyproperty
def jobs_queryset(self): def job_logs_queryset(self):
t = self.days_to_datetime t = self.days_to_datetime
queryset = Job.objects.filter(date_created__gte=t) queryset = JobAuditLog.objects.filter(date_created__gte=t)
return queryset return queryset
@lazyproperty @lazyproperty
@ -138,8 +138,7 @@ class DatesLoginMetricMixin:
command_queryset: Command.objects command_queryset: Command.objects
sessions_queryset: Session.objects sessions_queryset: Session.objects
ftp_logs_queryset: OperateLog.objects ftp_logs_queryset: OperateLog.objects
jobs_queryset: Job.objects job_logs_queryset: JobAuditLog.objects
jobs_executed_queryset: JobExecution.objects
login_logs_queryset: UserLoginLog.objects login_logs_queryset: UserLoginLog.objects
operate_logs_queryset: OperateLog.objects operate_logs_queryset: OperateLog.objects
password_change_logs_queryset: PasswordChangeLog.objects password_change_logs_queryset: PasswordChangeLog.objects
@ -316,19 +315,17 @@ class DatesLoginMetricMixin:
return self.command_queryset.filter(risk_level=Command.RISK_LEVEL_DANGEROUS).count() return self.command_queryset.filter(risk_level=Command.RISK_LEVEL_DANGEROUS).count()
@lazyproperty @lazyproperty
def jobs_amount(self): def job_logs_running_amount(self):
return self.jobs_queryset.count() return self.job_logs_queryset.filter(status__in=[JobStatus.running]).count()
@lazyproperty @lazyproperty
def jobs_unexecuted_amount(self): def job_logs_failed_amount(self):
executed_amount = self.jobs_executed_queryset.values( return self.job_logs_queryset.filter(
'job_id').order_by('job_id').distinct().count() status__in=[JobStatus.failed, JobStatus.timeout]).count()
return self.jobs_amount - executed_amount
@lazyproperty @lazyproperty
def jobs_executed_failed_amount(self): def job_logs_amount(self):
return self.jobs_executed_queryset.filter( return self.job_logs_queryset.count()
status=JobStatus.failed).count()
@lazyproperty @lazyproperty
def sessions_amount(self): def sessions_amount(self):
@ -439,19 +436,19 @@ class IndexApi(DateTimeMixin, DatesLoginMetricMixin, APIView):
'total_count_ftp_logs': self.ftp_logs_amount, 'total_count_ftp_logs': self.ftp_logs_amount,
}) })
if _all or query_params.get('total_count') or query_params.get('total_count_jobs'): if _all or query_params.get('total_count') or query_params.get('total_count_job_logs'):
data.update({ data.update({
'total_count_jobs': self.jobs_amount, 'total_count_job_logs': self.job_logs_amount,
}) })
if _all or query_params.get('total_count') or query_params.get('total_count_jobs_unexecuted'): if _all or query_params.get('total_count') or query_params.get('total_count_job_logs_running'):
data.update({ data.update({
'total_count_jobs_unexecuted': self.jobs_unexecuted_amount, 'total_count_job_logs_running': self.job_logs_running_amount,
}) })
if _all or query_params.get('total_count') or query_params.get('total_count_jobs_executed_failed'): if _all or query_params.get('total_count') or query_params.get('total_count_job_logs_failed'):
data.update({ data.update({
'total_count_jobs_executed_failed': self.jobs_executed_failed_amount, 'total_count_job_logs_failed': self.job_logs_failed_amount,
}) })
if _all or query_params.get('total_count') or query_params.get('total_count_type_to_assets_amount'): if _all or query_params.get('total_count') or query_params.get('total_count_type_to_assets_amount'):