perf: Pam dashboard

pull/14770/head
feng 2025-01-06 16:52:12 +08:00 committed by feng626
parent 2273275c23
commit ff9adb45b7
2 changed files with 12 additions and 3 deletions

View File

@ -4,6 +4,7 @@ from django.http.response import JsonResponse
from rest_framework.views import APIView
from accounts.models import Account, RiskChoice
from common.utils.timezone import local_monday
__all__ = ['PamDashboardApi']
@ -24,6 +25,10 @@ class PamDashboardApi(APIView):
if query_params.get('total_accounts'):
data['total_accounts'] = account_count
if query_params.get('total_week_add_accounts'):
monday_time = local_monday()
data['total_week_add_accounts'] = Account.objects.filter(date_created__gte=monday_time).count()
if query_params.get('total_privileged_accounts'):
data['total_privileged_accounts'] = privileged_account_count
@ -31,15 +36,18 @@ class PamDashboardApi(APIView):
data['total_ordinary_accounts'] = account_count - privileged_account_count
if query_params.get('total_unmanaged_accounts'):
data['total_unmanaged_accounts'] = Account.get_risks(RiskChoice.new_found).count()
data['total_unmanaged_accounts'] = Account.get_risks(risk_type=RiskChoice.new_found).count()
if query_params.get('total_unavailable_accounts'):
data['total_unavailable_accounts'] = Account.objects.filter(is_active=False).count()
if query_params.get('total_long_time_no_login_accounts'):
data['total_long_time_no_login_accounts'] = Account.get_risks(risk_type=RiskChoice.long_time_no_login).count()
if query_params.get('total_weak_password_accounts'):
data['total_weak_password_accounts'] = Account.get_risks(RiskChoice.weak_password)
data['total_weak_password_accounts'] = Account.get_risks(risk_type=RiskChoice.weak_password).count()
if query_params.get('total_long_time_change_password_accounts'):
data['total_long_time_change_password_accounts'] = Account.get_risks(RiskChoice.long_time_password)
data['total_long_time_change_password_accounts'] = Account.get_risks(risk_type=RiskChoice.long_time_password).count()
return JsonResponse(data, status=200)

View File

@ -168,6 +168,7 @@ class Account(AbsConnectivity, LabeledMixin, BaseAccount):
@classmethod
def get_risks(cls, queryset=None, risk_type=None):
# TODO 数据量大时子查询性能不佳考虑用原生sql或者在模型层面做出改动
from accounts.models import AccountRisk
subquery = AccountRisk.objects.filter(
asset_id=OuterRef('asset_id'),