def insertIp(self, ip): """Insert a new IP in the summary. Also insert its port Args: ip: an IP object to be inserted """ treevw = ttk.Treeview(self.frameTw) treevw.heading("#0", text=ip, anchor='w') treevw.column("#0", anchor='w') tags = Settings.getTags() for tag, color in tags.items(): treevw.tag_configure(tag, background=color) treevw.bind("<Double-Button-1>", self.OnDoubleClick) count = 0 self.treeviews[ip] = treevw ports = Port.fetchObjects({"ip": ip}) for port in ports: if port.proto.strip() != "" and str(port.port).strip() != "": port_text = port.port if port.proto == "udp": port_text = "udp/" + port_text treevw.insert('', 'end', str(port.getId()), text=port_text, tags=list(port.getTags())) count += 1 treevw.configure(height=count) treevw.update_idletasks()
def loadData(self): """ Fetch data from database """ self.ports = Port.fetchObjects({}) self.ips = Ip.fetchObjects({}) self.tools = Tool.fetchObjects({})
def displayData(self): """ Display loaded data in treeviews """ dialog = ChildDialogProgress( self.parent, "Loading dashboard ", "Refreshing dashboard. Please wait for a few seconds.", 200, "determinate") dialog.show(10) # Reset Ip treeview for children in self.treevw.get_children(): self.treevw.delete(children) dialog.update(1) listOfip = [] for ip in self.ips: servicesCount = len([x for x in Port.fetchObjects({"ip": ip.ip})]) listOfip.append((ip.ip, servicesCount)) dialog.update(2) listOfip.sort(key=lambda tup: tup[1], reverse=True) for i in range(len(listOfip)): self.treevw.insert('', 'end', i, text=listOfip[i][0], values=(listOfip[i][1])) dialog.update(3) # Reset Port treeview for children in self.treevwport.get_children(): self.treevwport.delete(children) dialog.update(4) portCounts = {} for port in self.ports: if port.port not in portCounts.keys(): portCounts[port.port] = 1 else: portCounts[port.port] += 1 dialog.update(5) port_id = 0 # Ordering dictionnary portCounts = { k: v for k, v in sorted( portCounts.items(), key=lambda item: item[1], reverse=True) } for portCount in portCounts: self.treevwport.insert('', 'end', port_id, text=str(portCount), values=(portCounts[portCount])) port_id += 1 dialog.update(6) # Tool part # Reset Tools treeview for children in self.treevwtools.get_children(): self.treevwtools.delete(children) dialog.update(7) listOfTools = [_ for _ in self.tools] listOfTools.sort(key=lambda x: x.status, reverse=True) result = MongoCalendar.getInstance().aggregate("tools", [{ "$group": { "_id": { "name": "$name", "status": "$status", "wave": "$wave" }, "count": { "$sum": 1 } } }]) result = [_ for _ in result] result.sort(key=lambda x: x["_id"]["name"]) dialog.update(8) tools_dashboard = {} for tool in result: toolName = tool["_id"]["name"] toolWave = tool["_id"]["wave"] tools_dashboard[toolWave] = tools_dashboard.get(toolWave, {}) tools_dashboard[toolWave][toolName] = tools_dashboard[ toolWave].get(toolName, {}) toolStatus = "Not ready" if len( tool["_id"]["status"]) == 0 else tool["_id"]["status"][0] tools_dashboard[toolWave][toolName][ toolStatus] = tools_dashboard[toolWave][toolName].get( toolStatus, 0) + 1 for wave in tools_dashboard.keys(): for toolName in tools_dashboard[wave].keys(): self.treevwtools.insert( '', 'end', None, text=str(toolName), values=(wave, tools_dashboard[wave][toolName].get( "ready", 0), tools_dashboard[wave][toolName].get("running", 0), tools_dashboard[wave][toolName].get("done", 0))) dialog.update(9) # Defect Part # reset defect TW for children in self.treevwDefaults.get_children(): self.treevwDefaults.delete(children) result = MongoCalendar.getInstance().aggregate("defects", [{ "$group": { "_id": { "risk": "$risk", "type": "$type" }, "count": { "$sum": 1 } } }]) dialog.update(10) result = [_ for _ in result] result.sort(key=lambda x: x["count"], reverse=True) for defect in result: defectRisk = defect["_id"]["risk"] defectType = " ".join(defect["_id"]["type"]) defectCount = defect["count"] self.treevwDefaults.insert('', 'end', None, text=str(defectRisk), values=(defectType, defectCount)) dialog.destroy()
def _load(self): """ Load the treeview with database information """ mongoInstance = MongoCalendar.getInstance() dialog = ChildDialogProgress(self.appli, "Loading "+str( mongoInstance.calendarName), "Opening "+str(mongoInstance.calendarName) + ". Please wait for a few seconds.", 200, "determinate") step = 0 dialog.show(100) nbObjects = mongoInstance.find("waves").count() nbObjects += mongoInstance.find("scopes").count() nbObjects += mongoInstance.find("intervals").count() nbObjects += mongoInstance.find("scopes").count() nbObjects += mongoInstance.find("ips").count() nbObjects += mongoInstance.find("ports").count() nbObjects += mongoInstance.find("tools").count() onePercentNbObject = nbObjects//100 if nbObjects > 100 else 1 nbObjectTreated = 0 for child in self.get_children(): self.delete(child) self._hidden = [] self._detached = [] self.waves_node = self.insert("", "end", str( "waves"), text="Waves", image=WaveView.getClassIcon()) # Loading every category separatly is faster than recursivly. # This is due to cursor.next function calls in pymongo # Adding wave objects waves = Wave.fetchObjects({}) for wave in waves: wave_o = WaveController(wave) wave_vw = WaveView(self, self.appli.viewframe, self.appli, wave_o) wave_vw.addInTreeview(self.waves_node, False) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) scopes = Scope.fetchObjects({}) for scope in scopes: scope_o = ScopeController(scope) scope_vw = ScopeView(self, self.appli.viewframe, self.appli, scope_o) scope_vw.addInTreeview(None, False) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) intervals = Interval.fetchObjects({}) for interval in intervals: interval_o = IntervalController(interval) interval_vw = IntervalView(self, self.appli.viewframe, self.appli, interval_o) interval_vw.addInTreeview(None, False) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) # Adding ip objects self.ips_node = self.insert("", "end", str( "ips"), text="IPs", image=IpView.getClassIcon()) ips = Ip.fetchObjects({}) for ip in ips: ip_o = IpController(ip) ip_vw = IpView(self, self.appli.viewframe, self.appli, ip_o) ip_vw.addInTreeview(None, False) self.appli.statusbar.notify(ip_vw.controller.getTags()) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) # Adding port objects ports = Port.fetchObjects({}) for port in ports: port_o = PortController(port) port_vw = PortView(self, self.appli.viewframe, self.appli, port_o) port_vw.addInTreeview(None, False) self.appli.statusbar.notify(port_vw.controller.getTags()) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) # Adding defect objects defects = Defect.fetchObjects({"ip":{"$ne":""}}) for defect in defects: defect_o = DefectController(defect) defect_vw = DefectView( self, self.appli.viewframe, self.appli, defect_o) defect_vw.addInTreeview(None) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) # Adding tool objects tools = Tool.fetchObjects({}) for tool in tools: tool_o = ToolController(tool) tool_vw = ToolView(self, self.appli.viewframe, self.appli, tool_o) tool_vw.addInTreeview(None, False) self.appli.statusbar.notify(tool_vw.controller.getTags()) nbObjectTreated += 1 if nbObjectTreated % onePercentNbObject == 0: step += 1 dialog.update(step) self.sort(self.ips_node) self.appli.statusbar.update() dialog.destroy()