From 54e772741baa6b5dfab7558f6e4719cd3b7db698 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 20 Apr 2022 10:15:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/migrations/0092_hardware.py | 9 ++-- .../migrations/0093_auto_20220403_1627.py | 13 ++--- .../migrations/0096_auto_20220406_1546.py | 8 --- .../migrations/0098_auto_20220412_1907.py | 23 --------- apps/assets/models/asset/__init__.py | 1 - apps/assets/models/asset/device_info.py | 51 ------------------- apps/assets/models/asset/host.py | 51 +++++++++++++++++-- apps/assets/models/platform.py | 1 + apps/assets/models/protocol.py | 5 ++ 9 files changed, 64 insertions(+), 98 deletions(-) delete mode 100644 apps/assets/migrations/0098_auto_20220412_1907.py delete mode 100644 apps/assets/models/asset/device_info.py create mode 100644 apps/assets/models/protocol.py diff --git a/apps/assets/migrations/0092_hardware.py b/apps/assets/migrations/0092_hardware.py index 26d026640..a5e860a6d 100644 --- a/apps/assets/migrations/0092_hardware.py +++ b/apps/assets/migrations/0092_hardware.py @@ -15,6 +15,10 @@ class Migration(migrations.Migration): migrations.CreateModel( name='DeviceInfo', fields=[ + ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), + ('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), + ('date_created', models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created')), + ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), ('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')), ('sn', models.CharField(blank=True, max_length=128, null=True, verbose_name='Serial number')), @@ -29,10 +33,7 @@ class Migration(migrations.Migration): ('os_version', models.CharField(blank=True, max_length=16, null=True, verbose_name='OS version')), ('os_arch', models.CharField(blank=True, max_length=16, null=True, verbose_name='OS arch')), ('hostname_raw', models.CharField(blank=True, max_length=128, null=True, verbose_name='Hostname raw')), - ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), - ('created_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')), - ('date_created', models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date created')), - ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), + ('host', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='assets.host', verbose_name='Host')), ], options={ 'verbose_name': 'DeviceInfo', diff --git a/apps/assets/migrations/0093_auto_20220403_1627.py b/apps/assets/migrations/0093_auto_20220403_1627.py index 9aa3be14d..27e5f11ee 100644 --- a/apps/assets/migrations/0093_auto_20220403_1627.py +++ b/apps/assets/migrations/0093_auto_20220403_1627.py @@ -27,6 +27,7 @@ def migrate_hardware(apps, *args): if not hosts: break + hardware_infos = [] for host in hosts: hardware = hardware_model() asset = asset_mapper[host.asset_ptr_id] @@ -34,9 +35,10 @@ def migrate_hardware(apps, *args): hardware.date_updated = timezone.now() for name in fields: setattr(hardware, name, getattr(asset, name)) - hardware.save() - host.device_info = hardware - host.save() + hardware_infos.append(hardware) + + hardware_model.objects.bulk_create(hardware_infos, ignore_conflicts=True) + created += len(hardware_infos) class Migration(migrations.Migration): @@ -46,10 +48,5 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AddField( - model_name='host', - name='device_info', - field=models.ForeignKey(null=True, on_delete=models.deletion.SET_NULL, to='assets.deviceinfo', verbose_name='Device info'), - ), migrations.RunPython(migrate_hardware) ] diff --git a/apps/assets/migrations/0096_auto_20220406_1546.py b/apps/assets/migrations/0096_auto_20220406_1546.py index b4808fad5..e2c9400e4 100644 --- a/apps/assets/migrations/0096_auto_20220406_1546.py +++ b/apps/assets/migrations/0096_auto_20220406_1546.py @@ -4,14 +4,6 @@ from itertools import groupby from django.db import migrations from django.db.models import F - -# ('Linux', 'Linux'), -# ('Unix', 'Unix'), -# ('MacOS', 'MacOS'), -# ('BSD', 'BSD'), -# ('Windows', 'Windows'), -# ('Other', 'Other'), - category_mapper = { 'Linux': ('host', 'linux'), 'Unix': ('host', 'unix'), diff --git a/apps/assets/migrations/0098_auto_20220412_1907.py b/apps/assets/migrations/0098_auto_20220412_1907.py deleted file mode 100644 index c12bbd644..000000000 --- a/apps/assets/migrations/0098_auto_20220412_1907.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 3.1.14 on 2022-04-12 11:07 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('assets', '0097_auto_20220407_1726'), - ] - - operations = [ - migrations.RemoveField( - model_name='deviceinfo', - name='host', - ), - migrations.AddField( - model_name='host', - name='device_info', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.deviceinfo', verbose_name='Device info'), - ), - ] diff --git a/apps/assets/models/asset/__init__.py b/apps/assets/models/asset/__init__.py index 8f2ad65ee..51640e7cf 100644 --- a/apps/assets/models/asset/__init__.py +++ b/apps/assets/models/asset/__init__.py @@ -1,3 +1,2 @@ from .common import * -from .device_info import * from .host import * diff --git a/apps/assets/models/asset/device_info.py b/apps/assets/models/asset/device_info.py deleted file mode 100644 index 7410af04f..000000000 --- a/apps/assets/models/asset/device_info.py +++ /dev/null @@ -1,51 +0,0 @@ -from django.utils.translation import gettext_lazy as _ -from django.db import models - -from common.mixins.models import CommonModelMixin - -__all__ = ['DeviceInfo'] - - -class DeviceInfo(CommonModelMixin): - # Collect - vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) - model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) - sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) - - cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) - cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) - cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) - cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) - memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) - disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) - disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) - - os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) - os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) - os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) - hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) - - @property - def cpu_info(self): - info = "" - if self.cpu_model: - info += self.cpu_model - if self.cpu_count and self.cpu_cores: - info += "{}*{}".format(self.cpu_count, self.cpu_cores) - return info - - @property - def hardware_info(self): - if self.cpu_count: - return '{} Core {} {}'.format( - self.cpu_vcpus or self.cpu_count * self.cpu_cores, - self.memory, self.disk_total - ) - else: - return '' - - def __str__(self): - return '{} of {}'.format(self.hardware_info, self.host.hostname) - - class Meta: - verbose_name = _("DeviceInfo") diff --git a/apps/assets/models/asset/host.py b/apps/assets/models/asset/host.py index 5b6ade8bd..ac61ea052 100644 --- a/apps/assets/models/asset/host.py +++ b/apps/assets/models/asset/host.py @@ -1,9 +1,9 @@ -# from django.db import models -# from django.utils.translation import gettext_lazy as _ +from django.db import models +from django.utils.translation import gettext_lazy as _ +from common.mixins.models import CommonModelMixin from assets.const import Category from .common import Asset -# from .device_info import DeviceInfo class Host(Asset): @@ -12,3 +12,48 @@ class Host(Asset): return super().save(*args, **kwargs) +class DeviceInfo(CommonModelMixin): + host = models.ForeignKey(Host, on_delete=models.CASCADE, verbose_name=_("Host")) + # Collect + vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) + model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) + sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) + + cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) + cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) + cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) + cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) + memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) + disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) + disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) + + os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) + os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) + os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) + hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) + + @property + def cpu_info(self): + info = "" + if self.cpu_model: + info += self.cpu_model + if self.cpu_count and self.cpu_cores: + info += "{}*{}".format(self.cpu_count, self.cpu_cores) + return info + + @property + def hardware_info(self): + if self.cpu_count: + return '{} Core {} {}'.format( + self.cpu_vcpus or self.cpu_count * self.cpu_cores, + self.memory, self.disk_total + ) + else: + return '' + + def __str__(self): + return '{} of {}'.format(self.hardware_info, self.host.hostname) + + class Meta: + verbose_name = _("DeviceInfo") + diff --git a/apps/assets/models/platform.py b/apps/assets/models/platform.py index 675d3542f..b3c9ec76e 100644 --- a/apps/assets/models/platform.py +++ b/apps/assets/models/platform.py @@ -4,6 +4,7 @@ from django.utils.translation import gettext_lazy as _ from assets.const import Category, AllTypes from common.fields.model import JsonDictTextField + __all__ = ['Platform'] diff --git a/apps/assets/models/protocol.py b/apps/assets/models/protocol.py new file mode 100644 index 000000000..41c3d2c76 --- /dev/null +++ b/apps/assets/models/protocol.py @@ -0,0 +1,5 @@ +from common.db.models import JMSBaseModel + + +class Protocol(JMSBaseModel): + pass