perf: 修改 asset info

pull/8605/head
ibuler 2022-04-06 11:29:16 +08:00
parent 8688781e15
commit ce13b194a5
11 changed files with 45 additions and 12 deletions

View File

@ -1,2 +1,3 @@
from .common import *
from .host import *
from .permission import *

View File

@ -36,7 +36,7 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet)
'protocols': ['exact', 'icontains']
}
search_fields = ("hostname", "ip")
ordering_fields = ("hostname", "ip", "port", "cpu_cores")
ordering_fields = ("hostname", "ip", "port")
ordering = ('hostname', )
serializer_classes = {
'default': serializers.AssetSerializer,

View File

@ -0,0 +1,15 @@
from assets.models import Host
from assets.serializers import HostSerializer
from .common import AssetViewSet
__all__ = ['HostViewSet']
class HostViewSet(AssetViewSet):
model = Host
def get_serializer_classes(self):
serializer_classes = super().get_serializer_classes()
serializer_classes['default'] = HostSerializer
return serializer_classes

View File

@ -13,7 +13,7 @@ class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name='HostInfo',
name='DeviceInfo',
fields=[
('vendor', models.CharField(blank=True, max_length=64, null=True, verbose_name='Vendor')),
('model', models.CharField(blank=True, max_length=54, null=True, verbose_name='Model')),
@ -36,7 +36,7 @@ class Migration(migrations.Migration):
('host', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='assets.host', related_name='info', verbose_name='Host')),
],
options={
'verbose_name': 'HostInfo',
'verbose_name': 'DeviceInfo',
},
),
]

View File

@ -7,7 +7,7 @@ from django.db import migrations
def migrate_hardware(apps, *args):
host_model = apps.get_model('assets', 'Host')
asset_model = apps.get_model('assets', 'Asset')
hardware_model = apps.get_model('assets', 'HostInfo')
hardware_model = apps.get_model('assets', 'DeviceInfo')
created = 0
batch_size = 1000

View File

@ -9,8 +9,8 @@ class Host(Asset):
pass
class HostInfo(CommonModelMixin):
host = models.OneToOneField(Host, related_name='info', on_delete=models.CASCADE,
class DeviceInfo(CommonModelMixin):
host = models.OneToOneField(Host, related_name='device_info', on_delete=models.CASCADE,
verbose_name=_("Host"), unique=True)
# Collect
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
@ -53,4 +53,4 @@ class HostInfo(CommonModelMixin):
return '{} of {}'.format(self.hardware_info, self.host.hostname)
class Meta:
verbose_name = _("HostInfo")
verbose_name = _("DeviceInfo")

View File

@ -0,0 +1,5 @@
from .common import Asset
class RemoteApp(Asset):
pass

View File

@ -1 +1,2 @@
from .common import *
from .host import *

View File

@ -1,12 +1,14 @@
from rest_framework import serializers
from .common import AssetSerializer
from assets.models import HostInfo
from assets.models import DeviceInfo, Host
__all__ = ['DeviceSerializer', 'HostSerializer']
class HardwareSerializer(serializers.ModelSerializer):
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = HostInfo
model = DeviceInfo
fields = [
'id', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
@ -16,4 +18,8 @@ class HardwareSerializer(serializers.ModelSerializer):
class HostSerializer(AssetSerializer):
hardware_info = HardwareSerializer(read_only=True)
device_info = DeviceSerializer(read_only=True, allow_null=True)
class Meta(AssetSerializer.Meta):
model = Host
fields = AssetSerializer.Meta.fields + ['device_info']

View File

@ -11,6 +11,7 @@ app_name = 'assets'
router = BulkRouter()
router.register(r'assets', api.AssetViewSet, 'asset')
router.register(r'hosts', api.HostViewSet, 'asset')
router.register(r'accounts', api.AccountViewSet, 'account')
router.register(r'account-secrets', api.AccountSecretsViewSet, 'account-secret')
router.register(r'platforms', api.AssetPlatformViewSet, 'platform')

View File

@ -15,8 +15,12 @@ class SerializerMixin:
serializer_classes = None
single_actions = ['put', 'retrieve', 'patch']
def get_serializer_classes(self):
return getattr(self, 'serializer_classes', None)
def get_serializer_class_by_view_action(self):
if not hasattr(self, 'serializer_classes'):
serializer_classes = self.get_serializer_classes()
if serializer_classes is None:
return None
if not isinstance(self.serializer_classes, dict):
return None