mirror of https://github.com/jumpserver/jumpserver
perf: 修改 asset info
parent
8688781e15
commit
ce13b194a5
|
@ -1,2 +1,3 @@
|
||||||
from .common import *
|
from .common import *
|
||||||
|
from .host import *
|
||||||
from .permission import *
|
from .permission import *
|
||||||
|
|
|
@ -36,7 +36,7 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet)
|
||||||
'protocols': ['exact', 'icontains']
|
'protocols': ['exact', 'icontains']
|
||||||
}
|
}
|
||||||
search_fields = ("hostname", "ip")
|
search_fields = ("hostname", "ip")
|
||||||
ordering_fields = ("hostname", "ip", "port", "cpu_cores")
|
ordering_fields = ("hostname", "ip", "port")
|
||||||
ordering = ('hostname', )
|
ordering = ('hostname', )
|
||||||
serializer_classes = {
|
serializer_classes = {
|
||||||
'default': serializers.AssetSerializer,
|
'default': serializers.AssetSerializer,
|
||||||
|
|
|
@ -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
|
|
@ -13,7 +13,7 @@ class Migration(migrations.Migration):
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='HostInfo',
|
name='DeviceInfo',
|
||||||
fields=[
|
fields=[
|
||||||
('vendor', models.CharField(blank=True, max_length=64, null=True, verbose_name='Vendor')),
|
('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')),
|
('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')),
|
('host', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='assets.host', related_name='info', verbose_name='Host')),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'verbose_name': 'HostInfo',
|
'verbose_name': 'DeviceInfo',
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -7,7 +7,7 @@ from django.db import migrations
|
||||||
def migrate_hardware(apps, *args):
|
def migrate_hardware(apps, *args):
|
||||||
host_model = apps.get_model('assets', 'Host')
|
host_model = apps.get_model('assets', 'Host')
|
||||||
asset_model = apps.get_model('assets', 'Asset')
|
asset_model = apps.get_model('assets', 'Asset')
|
||||||
hardware_model = apps.get_model('assets', 'HostInfo')
|
hardware_model = apps.get_model('assets', 'DeviceInfo')
|
||||||
|
|
||||||
created = 0
|
created = 0
|
||||||
batch_size = 1000
|
batch_size = 1000
|
||||||
|
|
|
@ -9,8 +9,8 @@ class Host(Asset):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HostInfo(CommonModelMixin):
|
class DeviceInfo(CommonModelMixin):
|
||||||
host = models.OneToOneField(Host, related_name='info', on_delete=models.CASCADE,
|
host = models.OneToOneField(Host, related_name='device_info', on_delete=models.CASCADE,
|
||||||
verbose_name=_("Host"), unique=True)
|
verbose_name=_("Host"), unique=True)
|
||||||
# Collect
|
# Collect
|
||||||
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
|
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)
|
return '{} of {}'.format(self.hardware_info, self.host.hostname)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("HostInfo")
|
verbose_name = _("DeviceInfo")
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
from .common import Asset
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteApp(Asset):
|
||||||
|
pass
|
|
@ -1 +1,2 @@
|
||||||
from .common import *
|
from .common import *
|
||||||
|
from .host import *
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from .common import AssetSerializer
|
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:
|
class Meta:
|
||||||
model = HostInfo
|
model = DeviceInfo
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
|
'id', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
|
||||||
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
|
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
|
||||||
|
@ -16,4 +18,8 @@ class HardwareSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class HostSerializer(AssetSerializer):
|
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']
|
||||||
|
|
|
@ -11,6 +11,7 @@ app_name = 'assets'
|
||||||
|
|
||||||
router = BulkRouter()
|
router = BulkRouter()
|
||||||
router.register(r'assets', api.AssetViewSet, 'asset')
|
router.register(r'assets', api.AssetViewSet, 'asset')
|
||||||
|
router.register(r'hosts', api.HostViewSet, 'asset')
|
||||||
router.register(r'accounts', api.AccountViewSet, 'account')
|
router.register(r'accounts', api.AccountViewSet, 'account')
|
||||||
router.register(r'account-secrets', api.AccountSecretsViewSet, 'account-secret')
|
router.register(r'account-secrets', api.AccountSecretsViewSet, 'account-secret')
|
||||||
router.register(r'platforms', api.AssetPlatformViewSet, 'platform')
|
router.register(r'platforms', api.AssetPlatformViewSet, 'platform')
|
||||||
|
|
|
@ -15,8 +15,12 @@ class SerializerMixin:
|
||||||
serializer_classes = None
|
serializer_classes = None
|
||||||
single_actions = ['put', 'retrieve', 'patch']
|
single_actions = ['put', 'retrieve', 'patch']
|
||||||
|
|
||||||
|
def get_serializer_classes(self):
|
||||||
|
return getattr(self, 'serializer_classes', None)
|
||||||
|
|
||||||
def get_serializer_class_by_view_action(self):
|
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
|
return None
|
||||||
if not isinstance(self.serializer_classes, dict):
|
if not isinstance(self.serializer_classes, dict):
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue