perf: dashboard user amount (#9582)

Co-authored-by: feng <1304903146@qq.com>
pull/9585/head
fit2bot 2023-02-16 14:27:54 +08:00 committed by GitHub
parent 7d94e17e03
commit ea7d3c0651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 21 deletions

View File

@ -45,7 +45,7 @@ class AuthValidateMixin(serializers.Serializer):
validated_data['secret'] = secret
for field in ('secret',):
value = validated_data.get(field)
if value is None:
if not value:
validated_data.pop(field, None)
validated_data.pop('passphrase', None)

View File

@ -59,7 +59,7 @@ class ChangeSecretAutomationSerializer(AuthValidateMixin, BaseAutomationSerializ
}}
def validate_password_rules(self, password_rules):
secret_type = self.initial_secret_type
secret_type = self.initial_data['secret_type']
if secret_type != SecretType.PASSWORD:
return password_rules

View File

@ -6,10 +6,10 @@ from django.conf import settings
from rest_framework import permissions
from authentication.const import ConfirmType
from common.exceptions import UserConfirmRequired
from orgs.utils import tmp_to_root_org
from authentication.models import ConnectionToken
from common.exceptions import UserConfirmRequired
from common.utils import get_object_or_none
from orgs.utils import tmp_to_root_org
class IsValidUser(permissions.IsAuthenticated, permissions.BasePermission):
@ -17,13 +17,13 @@ class IsValidUser(permissions.IsAuthenticated, permissions.BasePermission):
def has_permission(self, request, view):
return super().has_permission(request, view) \
and request.user.is_valid
and request.user.is_valid
class IsValidUserOrConnectionToken(IsValidUser):
def has_permission(self, request, view):
return super().has_permission(request, view) \
or self.is_valid_connection_token(request)
or self.is_valid_connection_token(request)
@staticmethod
def is_valid_connection_token(request):
@ -38,13 +38,13 @@ class IsValidUserOrConnectionToken(IsValidUser):
class OnlySuperUser(IsValidUser):
def has_permission(self, request, view):
return super().has_permission(request, view) \
and request.user.is_superuser
and request.user.is_superuser
class IsServiceAccount(IsValidUser):
def has_permission(self, request, view):
return super().has_permission(request, view) \
and request.user.is_service_account
and request.user.is_service_account
class WithBootstrapToken(permissions.BasePermission):

View File

@ -1,28 +1,28 @@
import time
from django.core.cache import cache
from django.utils import timezone
from django.utils.timesince import timesince
from django.db.models import Count, Max, F
from django.http.response import JsonResponse, HttpResponse
from rest_framework.views import APIView
from django.utils import timezone
from django.utils.timesince import timesince
from rest_framework.permissions import AllowAny
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from users.models import User
from assets.models import Asset
from assets.const import AllTypes
from terminal.models import Session, Command
from terminal.utils import ComponentsPrometheusMetricsUtil
from orgs.utils import current_org
from assets.models import Asset
from audits.const import LoginStatusChoices
from audits.models import UserLoginLog, PasswordChangeLog, OperateLog, FTPLog
from common.utils import lazyproperty
from common.utils.timezone import local_now, local_zero_hour
from ops.const import JobStatus
from ops.models import Job, JobExecution
from common.utils import lazyproperty
from audits.models import UserLoginLog, PasswordChangeLog, OperateLog, FTPLog
from audits.const import LoginStatusChoices
from common.utils.timezone import local_now, local_zero_hour
from orgs.caches import OrgResourceStatisticsCache
from orgs.utils import current_org
from terminal.models import Session, Command
from terminal.utils import ComponentsPrometheusMetricsUtil
from users.models import User
__all__ = ['IndexApi']
@ -296,9 +296,13 @@ class DatesLoginMetricMixin:
return self.login_logs_queryset.filter(status=LoginStatusChoices.success).count()
@lazyproperty
def user_login_amount(self):
def user_login_logs_amount(self):
return self.login_logs_queryset.values('username').count()
@lazyproperty
def user_login_amount(self):
return self.login_logs_queryset.values('username').distinct().count()
@lazyproperty
def operate_logs_amount(self):
return self.operate_logs_queryset.count()
@ -379,6 +383,11 @@ class IndexApi(DateTimeMixin, DatesLoginMetricMixin, APIView):
'total_count_login_users': self.user_login_amount
})
if _all or query_params.get('total_count') or query_params.get('total_count_login_user_logs'):
data.update({
'total_count_login_user_logs': self.user_login_logs_amount
})
if _all or query_params.get('total_count') or query_params.get('total_count_today_active_assets'):
data.update({
'total_count_today_active_assets': caches.total_count_today_active_assets,