mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-12-16 09:02:49 +00:00
Compare commits
1 Commits
dev
...
pr@dev@top
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1f3aa21be |
@@ -1,5 +1,6 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
from django.db.models import Count, Max, F, CharField, Q
|
from django.db.models import Count, Max, F, CharField, Q
|
||||||
from django.db.models.functions import Cast
|
from django.db.models.functions import Cast
|
||||||
from django.http.response import JsonResponse
|
from django.http.response import JsonResponse
|
||||||
@@ -144,6 +145,7 @@ class DateTimeMixin:
|
|||||||
|
|
||||||
|
|
||||||
class DatesLoginMetricMixin:
|
class DatesLoginMetricMixin:
|
||||||
|
days: int
|
||||||
dates_list: list
|
dates_list: list
|
||||||
date_start_end: tuple
|
date_start_end: tuple
|
||||||
command_type_queryset_list: list
|
command_type_queryset_list: list
|
||||||
@@ -155,6 +157,8 @@ class DatesLoginMetricMixin:
|
|||||||
operate_logs_queryset: OperateLog.objects
|
operate_logs_queryset: OperateLog.objects
|
||||||
password_change_logs_queryset: PasswordChangeLog.objects
|
password_change_logs_queryset: PasswordChangeLog.objects
|
||||||
|
|
||||||
|
CACHE_TIMEOUT = 60
|
||||||
|
|
||||||
@lazyproperty
|
@lazyproperty
|
||||||
def get_type_to_assets(self):
|
def get_type_to_assets(self):
|
||||||
result = Asset.objects.annotate(type=F('platform__type')). \
|
result = Asset.objects.annotate(type=F('platform__type')). \
|
||||||
@@ -214,19 +218,34 @@ class DatesLoginMetricMixin:
|
|||||||
return date_metrics_dict.get('id', [])
|
return date_metrics_dict.get('id', [])
|
||||||
|
|
||||||
def get_dates_login_times_assets(self):
|
def get_dates_login_times_assets(self):
|
||||||
|
cache_key = f"stats:top10_assets:{self.days}"
|
||||||
|
data = cache.get(cache_key)
|
||||||
|
if data is not None:
|
||||||
|
return data
|
||||||
|
|
||||||
assets = self.sessions_queryset.values("asset") \
|
assets = self.sessions_queryset.values("asset") \
|
||||||
.annotate(total=Count("asset")) \
|
.annotate(total=Count("asset")) \
|
||||||
.annotate(last=Cast(Max("date_start"), output_field=CharField())) \
|
.annotate(last=Cast(Max("date_start"), output_field=CharField())) \
|
||||||
.order_by("-total")
|
.order_by("-total")
|
||||||
return list(assets[:10])
|
|
||||||
|
result = list(assets[:10])
|
||||||
|
cache.set(cache_key, result, self.CACHE_TIMEOUT)
|
||||||
|
return result
|
||||||
|
|
||||||
def get_dates_login_times_users(self):
|
def get_dates_login_times_users(self):
|
||||||
|
cache_key = f"stats:top10_users:{self.days}"
|
||||||
|
data = cache.get(cache_key)
|
||||||
|
if data is not None:
|
||||||
|
return data
|
||||||
|
|
||||||
users = self.sessions_queryset.values("user_id") \
|
users = self.sessions_queryset.values("user_id") \
|
||||||
.annotate(total=Count("user_id")) \
|
.annotate(total=Count("user_id")) \
|
||||||
.annotate(user=Max('user')) \
|
.annotate(user=Max('user')) \
|
||||||
.annotate(last=Cast(Max("date_start"), output_field=CharField())) \
|
.annotate(last=Cast(Max("date_start"), output_field=CharField())) \
|
||||||
.order_by("-total")
|
.order_by("-total")
|
||||||
return list(users[:10])
|
result = list(users[:10])
|
||||||
|
cache.set(cache_key, result, self.CACHE_TIMEOUT)
|
||||||
|
return result
|
||||||
|
|
||||||
def get_dates_login_record_sessions(self):
|
def get_dates_login_record_sessions(self):
|
||||||
sessions = self.sessions_queryset.order_by('-date_start')
|
sessions = self.sessions_queryset.order_by('-date_start')
|
||||||
|
|||||||
Reference in New Issue
Block a user