perf: 优化审计台仪表盘命令记录总数统计数据库和所有es数据 (#12997)

Co-authored-by: feng <1304903146@qq.com>
pull/12998/head
fit2bot 2024-04-11 18:29:02 +08:00 committed by GitHub
parent 574639d5e1
commit ab76745a9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 6 deletions

View File

@ -118,9 +118,14 @@ class DateTimeMixin:
return self.get_logs_queryset_filter(qs, 'date_start') return self.get_logs_queryset_filter(qs, 'date_start')
@lazyproperty @lazyproperty
def command_queryset(self): def command_type_queryset_tuple(self):
qs = Command.objects.all() type_queryset_tuple = Command.get_all_type_queryset_tuple()
return self.get_logs_queryset_filter(qs, 'timestamp', is_timestamp=True) return (
(tp, self.get_logs_queryset_filter(
qs, 'timestamp', is_timestamp=True
))
for tp, qs in type_queryset_tuple
)
@lazyproperty @lazyproperty
def job_logs_queryset(self): def job_logs_queryset(self):
@ -131,7 +136,7 @@ class DateTimeMixin:
class DatesLoginMetricMixin: class DatesLoginMetricMixin:
dates_list: list dates_list: list
date_start_end: tuple date_start_end: tuple
command_queryset: Command.objects command_type_queryset_tuple: tuple
sessions_queryset: Session.objects sessions_queryset: Session.objects
ftp_logs_queryset: FTPLog.objects ftp_logs_queryset: FTPLog.objects
job_logs_queryset: JobLog.objects job_logs_queryset: JobLog.objects
@ -229,13 +234,29 @@ class DatesLoginMetricMixin:
def change_password_logs_amount(self): def change_password_logs_amount(self):
return self.password_change_logs_queryset.count() return self.password_change_logs_queryset.count()
@lazyproperty
def command_statistics(self):
from terminal.const import CommandStorageType
total_amount = 0
danger_amount = 0
for tp, qs in self.command_type_queryset_tuple:
if tp == CommandStorageType.es:
total_amount += qs.count(limit_to_max_result_window=False)
danger_amount += qs.filter(risk_level=RiskLevelChoices.reject).count(limit_to_max_result_window=False)
else:
total_amount += qs.count()
danger_amount += qs.filter(risk_level=RiskLevelChoices.reject).count()
return total_amount, danger_amount
@lazyproperty @lazyproperty
def commands_amount(self): def commands_amount(self):
return self.command_queryset.count() total_amount, _ = self.command_statistics
return total_amount
@lazyproperty @lazyproperty
def commands_danger_amount(self): def commands_danger_amount(self):
return self.command_queryset.filter(risk_level=RiskLevelChoices.reject).count() _, danger_amount = self.command_statistics
return danger_amount
@lazyproperty @lazyproperty
def job_logs_running_amount(self): def job_logs_running_amount(self):

View File

@ -60,6 +60,16 @@ class Command(AbstractSessionCommand):
commands.append(command) commands.append(command)
return commands return commands
@staticmethod
def get_all_type_queryset_tuple():
from terminal.models import CommandStorage
storage_qs = CommandStorage.objects.exclude(name='null')
return (
(storage.type, storage.get_command_queryset())
for storage in storage_qs
if storage.is_valid()
)
class Meta: class Meta:
db_table = "terminal_command" db_table = "terminal_command"
ordering = ('-timestamp',) ordering = ('-timestamp',)