第三天:内存增删改插件
思路:内存跟硬盘类似,都是以插槽来判断变更,与硬盘插件基本相同
问题:硬盘做变更日志时出现类型不同的问题,如果用硬盘的处理方式,也会出错。
解决:错误都是类型出错,所以根据数据类型调整,内存变更信息不需要都转成字符串比较。
from repository import models class Memory(object): def __init__(self,server_obj,info): self.server_obj = server_obj self.memory_dict = info def process(self): new_memory_info_dict = self.memory_dict['data'] """ {客户端发来的数据} """ old_memory_info_list = self.server_obj.memory.all() """ [obj,obj,obj,] """ # 取新旧两个集合 new_memory_slot_set = set(new_memory_info_dict.keys()) old_memory_slot_set = {obj.slot for obj in old_memory_info_list} # 得到增加删除更新的列表 add_slot_list = new_memory_slot_set.difference(old_memory_slot_set) del_slot_list = old_memory_slot_set.difference(new_memory_slot_set) update_slot_list = old_memory_slot_set.intersection(new_memory_slot_set) record_list = [] # 增加 for slot in add_slot_list: value = new_memory_info_dict[slot] record = "[%s]的插槽[%s]增加内存[%s]" % (self.server_obj, slot, value) record_list.append(record) value['server_obj'] = self.server_obj models.Memory.objects.create(**value) # 删除 models.Memory.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete() for slot in del_slot_list: record = "[%s]的插槽[%s]去除内存" % (self.server_obj, slot) record_list.append(record) # 更新 for slot in update_slot_list: value = new_memory_info_dict[slot] obj = models.Memory.objects.filter(server_obj=self.server_obj, slot=slot).first() for k, new_val in value.items(): old_val = getattr(obj, k) if old_val != new_val: setattr(obj, k, new_val) record = "[%s]的插槽[%s]内存由[%s]改为[%s]" % (self.server_obj, slot, old_val, new_val) record_list.append(record) obj.save() if record_list: models.ServerRecord.objects.create(server_obj=self.server_obj, content=';\n'.join(record_list))
内存添加:内容及变更记录
变更内存信息:
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">