fix: Fixed the issue of inaccurate calculation of the number of dashboard commands.
parent
019a657ec3
commit
206c43cf75
|
|
@ -1,6 +1,6 @@
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.db.models import Count, Max, F, CharField
|
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
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
@ -18,8 +18,8 @@ from common.utils import lazyproperty
|
||||||
from common.utils.timezone import local_now, local_zero_hour
|
from common.utils.timezone import local_now, local_zero_hour
|
||||||
from ops.const import JobStatus
|
from ops.const import JobStatus
|
||||||
from orgs.caches import OrgResourceStatisticsCache
|
from orgs.caches import OrgResourceStatisticsCache
|
||||||
from orgs.utils import current_org
|
from orgs.utils import current_org, filter_org_queryset
|
||||||
from terminal.const import RiskLevelChoices
|
from terminal.const import RiskLevelChoices, CommandStorageType
|
||||||
from terminal.models import Session, CommandStorage
|
from terminal.models import Session, CommandStorage
|
||||||
|
|
||||||
__all__ = ['IndexApi']
|
__all__ = ['IndexApi']
|
||||||
|
|
@ -123,15 +123,18 @@ 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_list(self):
|
def command_type_queryset_list(self):
|
||||||
qs_list = []
|
qs_list = []
|
||||||
for storage in CommandStorage.objects.all():
|
for storage in CommandStorage.objects.exclude(name='null'):
|
||||||
if not storage.is_valid():
|
if not storage.is_valid():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
qs = storage.get_command_queryset()
|
qs = storage.get_command_queryset()
|
||||||
qs_list.append(self.get_logs_queryset_filter(
|
qs = filter_org_queryset(qs)
|
||||||
|
qs = self.get_logs_queryset_filter(
|
||||||
qs, 'timestamp', is_timestamp=True
|
qs, 'timestamp', is_timestamp=True
|
||||||
))
|
)
|
||||||
|
qs_list.append((storage.type, qs))
|
||||||
return qs_list
|
return qs_list
|
||||||
|
|
||||||
@lazyproperty
|
@lazyproperty
|
||||||
|
|
@ -143,7 +146,7 @@ class DateTimeMixin:
|
||||||
class DatesLoginMetricMixin:
|
class DatesLoginMetricMixin:
|
||||||
dates_list: list
|
dates_list: list
|
||||||
date_start_end: tuple
|
date_start_end: tuple
|
||||||
command_queryset_list: list
|
command_type_queryset_list: list
|
||||||
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
|
||||||
|
|
@ -261,11 +264,25 @@ class DatesLoginMetricMixin:
|
||||||
|
|
||||||
@lazyproperty
|
@lazyproperty
|
||||||
def command_statistics(self):
|
def command_statistics(self):
|
||||||
|
def _count_pair(_tp, _qs):
|
||||||
|
if _tp == CommandStorageType.es:
|
||||||
|
total = _qs.count(limit_to_max_result_window=False)
|
||||||
|
danger = _qs.filter(risk_level=RiskLevelChoices.reject) \
|
||||||
|
.count(limit_to_max_result_window=False)
|
||||||
|
return total, danger
|
||||||
|
|
||||||
|
agg = _qs.aggregate(
|
||||||
|
total=Count('pk'),
|
||||||
|
danger=Count('pk', filter=Q(risk_level=RiskLevelChoices.reject)),
|
||||||
|
)
|
||||||
|
return (agg['total'] or 0), (agg['danger'] or 0)
|
||||||
|
|
||||||
total_amount = 0
|
total_amount = 0
|
||||||
danger_amount = 0
|
danger_amount = 0
|
||||||
for qs in self.command_queryset_list:
|
for tp, qs in self.command_type_queryset_list:
|
||||||
total_amount += qs.count()
|
t, d = _count_pair(tp, qs)
|
||||||
danger_amount += qs.filter(risk_level=RiskLevelChoices.reject).count()
|
total_amount += t
|
||||||
|
danger_amount += d
|
||||||
return total_amount, danger_amount
|
return total_amount, danger_amount
|
||||||
|
|
||||||
@lazyproperty
|
@lazyproperty
|
||||||
|
|
|
||||||
|
|
@ -36,16 +36,16 @@ class CommandFilter(filters.FilterSet):
|
||||||
date_from = self.form.cleaned_data.get('date_from')
|
date_from = self.form.cleaned_data.get('date_from')
|
||||||
date_to = self.form.cleaned_data.get('date_to')
|
date_to = self.form.cleaned_data.get('date_to')
|
||||||
|
|
||||||
filters = {}
|
_filters = {}
|
||||||
if date_from:
|
if date_from:
|
||||||
date_from = date_from.timestamp()
|
date_from = date_from.timestamp()
|
||||||
filters['timestamp__gte'] = date_from
|
_filters['timestamp__gte'] = date_from
|
||||||
|
|
||||||
if date_to:
|
if date_to:
|
||||||
date_to = date_to.timestamp()
|
date_to = date_to.timestamp()
|
||||||
filters['timestamp__lte'] = date_to
|
_filters['timestamp__lte'] = date_to
|
||||||
|
|
||||||
qs = qs.filter(**filters)
|
qs = qs.filter(**_filters)
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
def filter_by_asset_id(self, queryset, name, value):
|
def filter_by_asset_id(self, queryset, name, value):
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ class Command(AbstractSessionCommand):
|
||||||
'timestamp': int(d.timestamp()),
|
'timestamp': int(d.timestamp()),
|
||||||
'org_id': str(org.id)
|
'org_id': str(org.id)
|
||||||
})
|
})
|
||||||
for i in range(count)
|
for __ in range(count)
|
||||||
]
|
]
|
||||||
cls.objects.bulk_create(commands)
|
cls.objects.bulk_create(commands)
|
||||||
print(f'Create {len(commands)} commands of org ({org})')
|
print(f'Create {len(commands)} commands of org ({org})')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue