mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-04-27 19:17:01 +00:00
perf: execution type
This commit is contained in:
parent
9992fb35be
commit
7c4931b6af
@ -32,5 +32,5 @@ class BackupAccountExecutionViewSet(AutomationExecutionViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.filter(automation__type=self.tp)
|
||||
queryset = queryset.filter(type=self.tp)
|
||||
return queryset
|
||||
|
@ -130,7 +130,7 @@ class ChangSecretExecutionViewSet(AutomationExecutionViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.filter(automation__type=self.tp)
|
||||
queryset = queryset.filter(type=self.tp)
|
||||
return queryset
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ from rest_framework.views import APIView
|
||||
from accounts.const import AutomationTypes, ChangeSecretRecordStatusChoice
|
||||
from accounts.models import ChangeSecretAutomation, AutomationExecution, ChangeSecretRecord
|
||||
from assets.models import Node, Asset
|
||||
from common.const import Status
|
||||
from common.utils import lazyproperty
|
||||
from common.utils.timezone import local_zero_hour, local_now
|
||||
from ops.celery import app
|
||||
@ -97,17 +98,19 @@ class ChangeSecretDashboardApi(APIView):
|
||||
return qs.count()
|
||||
return self.get_queryset_date_filter(qs, field).count()
|
||||
|
||||
@staticmethod
|
||||
def get_status_counts(records):
|
||||
pending = ChangeSecretRecordStatusChoice.pending
|
||||
failed = ChangeSecretRecordStatusChoice.failed
|
||||
total_ids = {str(i) for i in records.exclude(status=pending).values('execution_id').distinct()}
|
||||
failed_ids = {str(i) for i in records.filter(status=failed).values('execution_id').distinct()}
|
||||
total = len(total_ids)
|
||||
failed = len(total_ids & failed_ids)
|
||||
def get_status_counts(self, executions):
|
||||
executions = executions.filter(type=self.tp)
|
||||
total, failed, success = 0, 0, 0
|
||||
for status in executions.values_list('status', flat=True):
|
||||
total += 1
|
||||
if status in [Status.failed, Status.error]:
|
||||
failed += 1
|
||||
elif status == Status.success:
|
||||
success += 1
|
||||
|
||||
return {
|
||||
'total_count_change_secret_executions': total,
|
||||
'total_count_success_change_secret_executions': total - failed,
|
||||
'total_count_success_change_secret_executions': success,
|
||||
'total_count_failed_change_secret_executions': failed,
|
||||
}
|
||||
|
||||
@ -131,8 +134,8 @@ class ChangeSecretDashboardApi(APIView):
|
||||
data['total_count_change_secret_assets'] = self.get_change_secret_asset_queryset().count()
|
||||
|
||||
if _all or query_params.get('total_count_change_secret_status'):
|
||||
records = self.get_queryset_date_filter(self.change_secret_records_queryset, 'date_finished')
|
||||
data.update(self.get_status_counts(records))
|
||||
executions = self.get_queryset_date_filter(AutomationExecution.objects.all(), 'date_start')
|
||||
data.update(self.get_status_counts(executions))
|
||||
|
||||
if _all or query_params.get('daily_success_and_failure_metrics'):
|
||||
success, failed = self.get_daily_success_and_failure_metrics()
|
||||
|
@ -55,7 +55,7 @@ class CheckAccountExecutionViewSet(AutomationExecutionViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.filter(automation__type=self.tp)
|
||||
queryset = queryset.filter(type=self.tp)
|
||||
return queryset
|
||||
|
||||
@action(methods=["get"], detail=False, url_path="adhoc")
|
||||
|
@ -46,7 +46,7 @@ class DiscoverAccountsExecutionViewSet(AutomationExecutionViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.filter(automation__type=self.tp)
|
||||
queryset = queryset.filter(type=self.tp)
|
||||
return queryset
|
||||
|
||||
@xframe_options_sameorigin
|
||||
|
@ -38,7 +38,7 @@ class PushAccountExecutionViewSet(AutomationExecutionViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = queryset.filter(automation__type=self.tp)
|
||||
queryset = queryset.filter(type=self.tp)
|
||||
return queryset
|
||||
|
||||
|
||||
|
@ -3,10 +3,10 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from socket import gethostname
|
||||
|
||||
import time
|
||||
import yaml
|
||||
from django.conf import settings
|
||||
from django.template.loader import render_to_string
|
||||
@ -175,7 +175,7 @@ class BaseManager:
|
||||
self.do_run(*args, **kwargs)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
self.status = 'error'
|
||||
self.status = Status.error
|
||||
finally:
|
||||
self.post_run()
|
||||
|
||||
@ -485,8 +485,11 @@ class BasePlaybookManager(PlaybookPrepareMixin, BaseManager):
|
||||
self.on_host_error(host, error, detail)
|
||||
|
||||
def post_run(self):
|
||||
if self.summary['fail_assets']:
|
||||
self.status = 'failed'
|
||||
if any(
|
||||
self.summary.get(key, 0) > 0
|
||||
for key in ("fail_assets", "fail_accounts")
|
||||
):
|
||||
self.status = Status.failed
|
||||
super().post_run()
|
||||
|
||||
def on_runner_success(self, runner, cb):
|
||||
|
28
apps/assets/migrations/0015_automationexecution_type.py
Normal file
28
apps/assets/migrations/0015_automationexecution_type.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Generated by Django 4.1.13 on 2025-03-13 09:14
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def migrate_execution_type(apps, schema_editor):
|
||||
execution_model = apps.get_model('assets', 'AutomationExecution')
|
||||
execution_objs = []
|
||||
for execution in execution_model.objects.all():
|
||||
snapshot = execution.snapshot
|
||||
execution.type = snapshot.get('type', '')
|
||||
execution_objs.append(execution)
|
||||
execution_model.objects.bulk_update(execution_objs, ['type'])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('assets', '0014_alter_automationexecution_duration'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='automationexecution',
|
||||
name='type',
|
||||
field=models.CharField(default='', max_length=16, verbose_name='Type'),
|
||||
),
|
||||
migrations.RunPython(migrate_execution_type)
|
||||
]
|
@ -142,6 +142,7 @@ class AutomationExecution(OrgModelMixin):
|
||||
null=True, verbose_name=_("Date start"), db_index=True
|
||||
)
|
||||
date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished"))
|
||||
type = models.CharField(default='', max_length=16, verbose_name=_("Type"))
|
||||
duration = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name=_("Duration"))
|
||||
snapshot = EncryptJsonDictTextField(
|
||||
default=dict, blank=True, null=True, verbose_name=_("Automation snapshot")
|
||||
|
Loading…
Reference in New Issue
Block a user