From a911b50df09f9d65a40d827e211e81cf8a8cf26a Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Thu, 9 Jan 2025 16:11:37 +0800 Subject: [PATCH] perf: dashboard --- apps/accounts/api/account/pam_dashboard.py | 34 +++++++++++++++++-- .../automations/change_secret_dashboard.py | 33 +++++++++--------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/apps/accounts/api/account/pam_dashboard.py b/apps/accounts/api/account/pam_dashboard.py index cba3aaef7..f51a641d9 100644 --- a/apps/accounts/api/account/pam_dashboard.py +++ b/apps/accounts/api/account/pam_dashboard.py @@ -4,7 +4,10 @@ from django.db.models import Count, F, Q from django.http.response import JsonResponse from rest_framework.views import APIView -from accounts.models import Account, RiskChoice +from accounts.models import ( + Account, RiskChoice, GatherAccountsAutomation, + PushAccountAutomation, BackupAccountAutomation, AccountRisk, IntegrationApplication +) from assets.const import AllTypes from common.utils.timezone import local_monday @@ -85,9 +88,34 @@ class PamDashboardApi(APIView): data['total_long_time_change_password_accounts'] = Account.get_risks( risk_type=RiskChoice.long_time_password).count() - if _all or query_params.get('total_count_type_to_accounts_amount'): + if _all or query_params.get('total_count_type_to_accounts'): data.update({ - 'total_count_type_to_accounts_amount': self.get_type_to_accounts(), + 'total_count_type_to_accounts': self.get_type_to_accounts(), + }) + + if _all or query_params.get('total_count_gathered_account_automation'): + data.update({ + 'total_count_gathered_account_automation': GatherAccountsAutomation.objects.count() + }) + + if _all or query_params.get('total_count_push_account_automation'): + data.update({ + 'total_count_push_account_automation': PushAccountAutomation.objects.count() + }) + + if _all or query_params.get('total_count_backup_account_automation'): + data.update({ + 'total_count_backup_account_automation': BackupAccountAutomation.objects.count() + }) + + if _all or query_params.get('total_count_risk_account'): + data.update({ + 'total_count_risk_account': AccountRisk.objects.count() + }) + + if _all or query_params.get('total_count_integration_application'): + data.update({ + 'total_count_integration_application': IntegrationApplication.objects.count() }) return JsonResponse(data, status=200) diff --git a/apps/accounts/api/automations/change_secret_dashboard.py b/apps/accounts/api/automations/change_secret_dashboard.py index c3393f36c..2744cc757 100644 --- a/apps/accounts/api/automations/change_secret_dashboard.py +++ b/apps/accounts/api/automations/change_secret_dashboard.py @@ -117,28 +117,26 @@ class ChangeSecretDashboardApi(APIView): query_params = self.request.query_params data = {} - if query_params.get('total_count_change_secrets'): + _all = query_params.get('all') + + if _all or query_params.get('total_count_change_secrets'): data['total_count_change_secrets'] = self.get_filtered_counts( self.change_secrets_queryset, 'date_updated' ) - if query_params.get('total_count_periodic_change_secrets'): + if _all or query_params.get('total_count_periodic_change_secrets'): data['total_count_periodic_change_secrets'] = self.get_filtered_counts( self.change_secrets_queryset.filter(is_periodic=True), 'date_updated' ) - if query_params.get('total_count_change_secret_assets'): + if _all or query_params.get('total_count_change_secret_assets'): data['total_count_change_secret_assets'] = self.get_change_secret_asset_queryset().count() - if query_params.get('total_count_change_secret_status'): + if _all or query_params.get('total_count_change_secret_status'): records = self.get_queryset_date_filter(self.change_secret_records_queryset, 'date_finished') data.update(self.get_status_counts(records)) - if query_params.get('total_count_change_secret_status'): - records = self.get_queryset_date_filter(self.change_secret_records_queryset, 'date_finished') - data.update(self.get_status_counts(records)) - - if query_params.get('daily_success_and_failure_metrics'): + if _all or query_params.get('daily_success_and_failure_metrics'): success, failed = self.get_daily_success_and_failure_metrics() data.update({ 'dates_metrics_date': [date.strftime('%m-%d') for date in self.date_range_list] or ['0'], @@ -146,17 +144,18 @@ class ChangeSecretDashboardApi(APIView): 'dates_metrics_total_count_failed': failed, }) - if query_params.get('total_count_ongoing_change_secret'): + if _all or query_params.get('total_count_ongoing_change_secret'): execution_ids = [] inspect = app.control.inspect() active_tasks = inspect.active() - for tasks in active_tasks.values(): - for task in tasks: - _id = task.get('id') - name = task.get('name') - tp = task.kwargs.get('tp') - if name == self.task_name and tp == self.tp: - execution_ids.append(_id) + if active_tasks: + for tasks in active_tasks.values(): + for task in tasks: + _id = task.get('id') + name = task.get('name') + tp = task.kwargs.get('tp') + if name == self.task_name and tp == self.tp: + execution_ids.append(_id) snapshots = self.change_secret_executions_queryset.filter( id__in=execution_ids).values_list('id', 'snapshot')