perf: gather account api adjustment

pull/9034/head
feng 2022-11-09 15:42:21 +08:00
parent 6ef5154d4d
commit 3b4e388ed0
5 changed files with 23 additions and 13 deletions

View File

@ -232,5 +232,6 @@ class BasePlaybookManager:
except Exception as e:
self.on_runner_failed(runner, e)
print('\n')
self.execution.status = 'success'
self.execution.date_finished = timezone.now()
self.execution.save()

View File

@ -15,12 +15,8 @@ from assets.const import AutomationTypes
class BaseAutomation(CommonModelMixin, PeriodTaskModelMixin, OrgModelMixin):
accounts = models.JSONField(default=list, verbose_name=_("Accounts"))
nodes = models.ManyToManyField(
'assets.Node', blank=True, verbose_name=_("Nodes")
)
assets = models.ManyToManyField(
'assets.Asset', blank=True, verbose_name=_("Assets")
)
nodes = models.ManyToManyField('assets.Node', blank=True, verbose_name=_("Nodes"))
assets = models.ManyToManyField('assets.Asset', blank=True, verbose_name=_("Assets"))
type = models.CharField(max_length=16, choices=AutomationTypes.choices, verbose_name=_('Type'))
is_active = models.BooleanField(default=True, verbose_name=_("Is active"))
comment = models.TextField(blank=True, verbose_name=_('Comment'))
@ -92,7 +88,7 @@ class AutomationExecution(OrgModelMixin):
'BaseAutomation', related_name='executions', on_delete=models.CASCADE,
verbose_name=_('Automation task')
)
status = models.CharField(max_length=16, default='pending')
status = models.CharField(max_length=16, default='pending', verbose_name=_('Status'))
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True)
date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished"))

View File

@ -13,3 +13,7 @@ class GatherAccountsAutomation(BaseAutomation):
class Meta:
verbose_name = _("Gather asset accounts")
@property
def executed_amount(self):
return self.executions.count()

View File

@ -3,9 +3,10 @@ from rest_framework import serializers
from ops.mixin import PeriodTaskSerializerMixin
from assets.const import AutomationTypes
from assets.models import Asset, BaseAutomation, AutomationExecution
from assets.models import Asset, Node, BaseAutomation, AutomationExecution
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from common.utils import get_logger
from common.drf.fields import ObjectRelatedField
logger = get_logger(__file__)
@ -16,6 +17,9 @@ __all__ = [
class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSerializer):
assets = ObjectRelatedField(many=True, required=False, queryset=Asset.objects, label=_('Assets'))
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
class Meta:
read_only_fields = [
'date_created', 'date_updated', 'created_by', 'periodic_display'
@ -26,6 +30,7 @@ class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSe
]
extra_kwargs = {
'name': {'required': True},
'type': {'read_only': True},
'periodic_display': {'label': _('Periodic perform')},
}
@ -37,10 +42,10 @@ class AutomationExecutionSerializer(serializers.ModelSerializer):
class Meta:
model = AutomationExecution
fields = [
'id', 'automation', 'trigger', 'trigger_display',
'date_start', 'date_finished', 'snapshot', 'type'
read_only_fields = [
'trigger_display', 'date_start', 'date_finished', 'snapshot', 'status'
]
fields = ['id', 'automation', 'trigger', 'type'] + read_only_fields
@staticmethod
def get_snapshot(obj):

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
from assets.models import GatherAccountsAutomation
from common.utils import get_logger
@ -15,6 +16,9 @@ __all__ = [
class GatherAccountAutomationSerializer(BaseAutomationSerializer):
class Meta:
model = GatherAccountsAutomation
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields + ['executed_amount']
fields = BaseAutomationSerializer.Meta.fields + read_only_fields
extra_kwargs = BaseAutomationSerializer.Meta.extra_kwargs
extra_kwargs = {**BaseAutomationSerializer.Meta.extra_kwargs, **{
'executed_amount': {'label': _('Executed amount')}
}}