1、用django的app作为统一调用库的好处
1、创建repository app截图如下:
2、好处如下:
1、app的本质就是一个文件夹
2、以后所有的app调用数据就只去repository调用
3、不用每个app建立一个库
4、也避免了多个app同事修改同一条数据的冲突
2、表结构设计类
3、具体代码
from django.db import models
1、服务器信息信息表
hostname = models.CharField(max_length=128, unique=True) sn = models.CharField('SN号', max_length=64, db_index=True) manufacturer = models.CharField(verbose_name='制造商', max_length=64, null=True, blank=True) model = models.CharField('型号', max_length=64, null=True, blank=True) manage_ip = models.GenericIPAddressField('管理IP', null=True, blank=True) os_platform = models.CharField('系统', max_length=16, null=True, blank=True) os_version = models.CharField('系统版本', max_length=16, null=True, blank=True) cpu_count = models.IntegerField('CPU个数', null=True, blank=True) cpu_physical_count = models.IntegerField('CPU物理个数', null=True, blank=True) cpu_model = models.CharField('CPU型号', max_length=128, null=True, blank=True) create_at = models.DateTimeField(auto_now_add=True, blank=True) class Meta: verbose_name_plural = "服务器表" def __str__(self): return self.hostname
2、硬盘信息表
class Disk(models.Model): """ 硬盘信息 """ slot = models.CharField('插槽位', max_length=8) model = models.CharField('磁盘型号', max_length=32) capacity = models.FloatField('磁盘容量GB') pd_type = models.CharField('磁盘类型', max_length=32) server_obj = models.ForeignKey('Server',related_name='disk') class Meta: verbose_name_plural = "硬盘表" def __str__(self): return self.slot
3、网卡信息表
class NIC(models.Model): """ 网卡信息 """ name = models.CharField('网卡名称', max_length=128) hwaddr = models.CharField('网卡mac地址', max_length=64) netmask = models.CharField(max_length=64) ipaddrs = models.CharField('ip地址', max_length=256) up = models.BooleanField(default=False) server_obj = models.ForeignKey('Server',related_name='nic') class Meta: verbose_name_plural = "网卡表" def __str__(self): return self.name
4、内存信息表
class Memory(models.Model): """ 内存信息 """ slot = models.CharField('插槽位', max_length=32) manufacturer = models.CharField('制造商', max_length=32, null=True, blank=True) model = models.CharField('型号', max_length=64) capacity = models.FloatField('容量', null=True, blank=True) sn = models.CharField('内存SN号', max_length=64, null=True, blank=True) speed = models.CharField('速度', max_length=16, null=True, blank=True) server_obj = models.ForeignKey('Server',related_name='memory') class Meta: verbose_name_plural = "内存表" def __str__(self): return self.slot
4、资产入库思路
1、逻辑思路图
1. 检查server表中是否有当前资产信息【主机名是唯一标识】
if not server_dict['basic']['status']: return HttpResponse('臣妾做不到') hostname = server_dict['basic']['data']['hostname'] server_obj = models.Server.objects.filter(hostname=hostname).first()
2、没有就创建,服务器和网卡,内存,硬盘...
if not server_obj: pass # 没有就创建,服务器和网卡,内存,硬盘...
3、否则不再创建server对象,新老数据对比
else: # 不再创建server对象,新老数据对比 # 硬盘
2、以硬盘数据为示例
1、新数据(客户端发送过来的数据)
new_disk = server_dict['disk']['data'] """ { '0': {'slot': '0', 'pd_type': 'SAS', 'capacity': '279.396', 'model': 'SEAGATE ST300MM0006 LS08S0K2B5NV'}, '1': {'slot': '1', 'pd_type': 'SAS', 'capacity': '279.396', 'model': 'SEAGATE ST300MM0006 LS08S0K2B5AH'}, '2': {'slot': '2', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1SZNSAFA01085L Samsung SSD 850 PRO 512GB EXM01B6Q'}, '3': {'slot': '3', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAF912433K Samsung SSD 840 PRO Series DXM06B0Q'}, '4': {'slot': '4', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAF303909M Samsung SSD 840 PRO Series DXM05B0Q'}, '5': {'slot': '5', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAFB00549A Samsung SSD 840 PRO Series }"""
2、老数据(服务器端库的数据)
old_disk = server_obj.disk_set.values('slot','model','capacity','pd_type') """ [ {'slot': 1, 'model':'xx', 'capacity':'xxx', 'pd_type':'xx'}, {'slot': 2, 'model':'xx', 'capacity':'xxx', 'pd_type':'xx'}, {'slot': 3, 'model':'xx', 'capacity':'xxx', 'pd_type':'xx'}, {'slot': 9, 'model':'xx', 'capacity':'xxx', 'pd_type':'xx'}, ] """
3、set集合实现思路
1、根据槽位进行比较: new_disk有,old_disk没有 -> 0,4,5 使用create(**dic)方法
2、 根据槽位进行比较: old_disk有,new_disk没有 -> 0,4,5 使用delete方法
3、 根据槽位进行比较: old_disk有,new_disk有 -> 1,2,3 使用update方法