perf: 添加 types

pull/8605/head
ibuler 3 years ago
parent ce13b194a5
commit 1b9efff6c7

@ -33,7 +33,7 @@ class Migration(migrations.Migration):
('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.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='device_info', verbose_name='Host')),
],
options={
'verbose_name': 'DeviceInfo',

@ -0,0 +1,26 @@
# Generated by Django 3.1.14 on 2022-04-06 07:41
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('assets', '0094_auto_20220402_1736'),
]
operations = [
migrations.AddField(
model_name='asset',
name='category',
field=models.CharField(choices=[('host', 'Host'), ('network', 'Networking'), ('database', 'Database'), ('remote_app', 'Remote app'), ('cloud', 'Clouding')], default='host', max_length=16, verbose_name='Category'),
preserve_default=False,
),
migrations.AddField(
model_name='asset',
name='type',
field=models.CharField(default='linux', max_length=128, verbose_name='Type'),
preserve_default=False,
),
]

@ -0,0 +1,53 @@
# Generated by Django 3.1.14 on 2022-04-06 07:46
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'),
'MacOS': ('host', 'macos'),
'BSD': ('host', 'unix'),
'Windows': ('host', 'windows'),
'Other': ('host', 'other_host'),
}
def migrate_category_and_type(apps, *args):
asset_model = apps.get_model('assets', 'Asset')
assets_bases = asset_model.objects.all()\
.annotate(base=F("platform__base"))\
.values_list('id', 'base')\
.order_by('base')
base_assets = groupby(assets_bases, lambda a: a[1])
print("")
for base, grouper in base_assets:
asset_ids = [g[0] for g in grouper]
category_type = category_mapper.get(base)
if not category_type:
continue
print("Migrate {} to => {}".format(base, category_type))
category, tp = category_type
assets = asset_model.objects.filter(id__in=asset_ids)
assets.update(category=category, type=tp)
class Migration(migrations.Migration):
dependencies = [
('assets', '0095_auto_20220406_1541'),
]
operations = [
migrations.RunPython(migrate_category_and_type)
]

@ -0,0 +1,73 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
class Category(models.TextChoices):
HOST = 'host', _('Host')
NETWORK = 'network', _("Networking")
DATABASE = 'database', _("Database")
REMOTE_APP = 'remote_app', _("Remote app")
CLOUD = 'cloud', _("Clouding")
class HostTypes(models.TextChoices):
LINUX = 'linux', 'Linux'
UNIX = 'unix', 'Unix'
WINDOWS = 'windows', 'Windows'
MACOS = 'macos', 'MacOS'
MAINFRAME = 'mainframe', _("Mainframe")
OTHER_HOST = 'other_host', _("Other host")
def __new__(cls, value):
"""
添加 Category
:param value:
"""
obj = str.__new__(cls)
obj.category = Category.HOST
return obj
class NetworkTypes(models.TextChoices):
SWITCH = 'switch', _("Switch")
ROUTER = 'router', _("Router")
FIREWALL = 'firewall', _("Firewall")
OTHER_NETWORK = 'other_network', _("Other device")
class DatabaseTypes(models.TextChoices):
MYSQL = 'mysql', 'MySQL'
MARIADB = 'mariadb', 'MariaDB'
POSTGRESQL = 'postgresql', 'PostgreSQL'
ORACLE = 'oracle', 'Oracle'
SQLSERVER = 'sqlserver', 'SQLServer'
MONGODB = 'mongodb', 'MongoDB'
REDIS = 'redis', 'Redis'
class RemoteAppTypes(models.TextChoices):
CHROME = 'chrome', 'Chrome'
VSPHERE = 'vsphere', 'vSphere client'
MYSQL_WORKBENCH = 'mysql_workbench', 'MySQL workbench'
CUSTOM_REMOTE_APP = 'custom_remote_app', _("Custom")
class CloudTypes(models.TextChoices):
K8S = 'k8s', 'Kubernetes'
class AllTypes:
includes = [
HostTypes, NetworkTypes, DatabaseTypes,
RemoteAppTypes, CloudTypes
]
@classmethod
def choices(cls):
choices = []
for tp in cls.includes:
choices.extend(tp.choices)
return choices

@ -15,6 +15,7 @@ from common.utils import lazyproperty
from orgs.mixins.models import OrgModelMixin, OrgManager
from ..platform import Platform
from ..base import AbsConnectivity
from ._category import Category, AllTypes
__all__ = ['Asset', 'ProtocolsMixin', 'AssetQuerySet', 'default_node', 'default_cluster']
logger = logging.getLogger(__name__)
@ -125,6 +126,8 @@ class Asset(AbsConnectivity, ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
hostname = models.CharField(max_length=128, verbose_name=_('Hostname'))
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
category = models.CharField(max_length=16, choices=Category.choices, verbose_name=_("Category"))
type = models.CharField(max_length=128, choices=AllTypes.choices(), verbose_name=_("Type"))
protocol = models.CharField(max_length=128, default=ProtocolsMixin.Protocol.ssh,
choices=ProtocolsMixin.Protocol.choices, verbose_name=_('Protocol'))
port = models.IntegerField(default=22, verbose_name=_('Port'))
@ -180,6 +183,14 @@ class Asset(AbsConnectivity, ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
return False, warning
return True, warning
@property
def category_display(self):
return self.get_category_display()
@property
def type_display(self):
pass
@lazyproperty
def platform_base(self):
return self.platform.base

@ -1,5 +1,9 @@
from django.utils.translation import gettext_lazy as _
from django.db import models
from .common import Asset
class RemoteApp(Asset):
pass
app_path = models.CharField(max_length=1024, verbose_name=_("App path"))
attrs = models.JSONField(default=dict, verbose_name=_('Attrs'))

@ -77,9 +77,12 @@ class AssetSerializer(BulkOrgResourceModelSerializer):
class Meta:
model = Asset
fields_mini = ['id', 'hostname', 'ip', 'platform', 'protocols']
fields_mini = [
'id', 'category', 'category_display', 'type',
'hostname', 'ip', 'platform', 'protocols'
]
fields_small = fields_mini + [
'protocol', 'port', 'protocols', 'is_active',
'protocol', 'port', 'is_active',
'public_ip', 'number', 'comment',
]
fields_fk = [

@ -1 +0,0 @@
Subproject commit 244ace5a95503ffaf41b73037692e1121f7c066f
Loading…
Cancel
Save