def get_direct_summary(self): objects = dict( (d["object_profile"], d["count"]) for d in self.managed_objects.values("object_profile").annotate( count=Count("id")).order_by("count")) # Direct services mo_ids = self.managed_objects.values_list("id", flat=True) services, subscribers = ServiceSummary.get_direct_summary(mo_ids) return services, subscribers, objects
def update_summary(self): """ Update summaries :return: """ def update_dict(d1, d2): for k in d2: if k in d1: d1[k] += d2[k] else: d1[k] = d2[k] objects = dict( (d["object_profile"], d["count"]) for d in self.managed_objects.values( "object_profile" ).annotate( count=Count("id") ).order_by("count")) # Direct services mo_ids = self.managed_objects.values_list("id", flat=True) services, subscribers = ServiceSummary.get_direct_summary(mo_ids) self.direct_services = SummaryItem.dict_to_items(services) self.direct_subscribers = SummaryItem.dict_to_items(subscribers) self.direct_objects = ObjectSummaryItem.dict_to_items(objects) siblings = set() for ns in NetworkSegment.objects.filter(parent=self.id): if ns.id in siblings: continue update_dict(services, SummaryItem.items_to_dict(ns.total_services)) update_dict(subscribers, SummaryItem.items_to_dict(ns.total_subscribers)) update_dict(objects, ObjectSummaryItem.items_to_dict(ns.total_objects)) siblings.add(ns.id) if ns.sibling: siblings.add(ns.sibling.id) self.total_services = SummaryItem.dict_to_items(services) self.total_subscribers = SummaryItem.dict_to_items(subscribers) self.total_objects = ObjectSummaryItem.dict_to_items(objects) self.save() # Update upwards ns = self.parent while ns: services = SummaryItem.items_to_dict(ns.direct_services) subscribers = SummaryItem.items_to_dict(ns.direct_subscribers) objects = ObjectSummaryItem.items_to_dict(ns.direct_objects) for nsc in NetworkSegment.objects.filter(parent=ns.id): update_dict(services, SummaryItem.items_to_dict(nsc.total_services)) update_dict(subscribers, SummaryItem.items_to_dict(nsc.total_subscribers)) update_dict(objects, ObjectSummaryItem.items_to_dict(nsc.total_objects)) ns.total_services = SummaryItem.dict_to_items(services) ns.total_subscribers = SummaryItem.dict_to_items(subscribers) ns.total_objects = ObjectSummaryItem.dict_to_items(objects) ns.save() ns = ns.parent
def get_ajax_data(self, **kwargs): object_id = self.handler.get_argument("object_id") if self.current_user.is_superuser: moss = ManagedObject.objects.filter(is_managed=True) else: moss = ManagedObject.objects.filter( is_managed=True, administrative_domain__in=self.get_user_domains()) objects = [] objects_status = { "error": [], "warning": [], "good": [], "maintenance": [] } sss = {"error": {}, "warning": {}, "good": {}, "maintenance": {}} services = defaultdict(list) try: object_root = Object.objects.filter(id=object_id).first() except ValidationError: object_root = None if object_root: con = [str(c) for c in self.get_containers_by_root(object_root.id)] moss = moss.filter(container__in=con).order_by("container") else: moss = moss.exclude(container=None).order_by("container") con = list(moss.values_list("container", flat=True)) mo_ids = list(moss.values_list("id", flat=True)) # Getting Alarms severity dict MO: Severity @todo List alarms if not object_root: alarms = self.get_alarms_info(None, alarms_all=True) else: alarms = self.get_alarms_info(mo_ids) # Get maintenance maintenance = Maintenance.currently_affected() # Getting services if not object_root: services_map = self.get_objects_summary_met(mo_ids, info_all=True) else: services_map = self.get_objects_summary_met(mo_ids) # Getting containers name and coordinates containers = { str(o["_id"]): ( o["name"], { "%s.%s" % (item["interface"], item["attr"]): item["value"] for item in o.get("data", []) }, ) for o in Object.objects. filter(data__match={ "interface": "geopoint" }, id__in=con).read_preference(ReadPreference.SECONDARY_PREFERRED). fields(id=1, name=1, data=1).as_pymongo() } # Main Loop. Get ManagedObject group by container for container, mol in itertools.groupby(moss.values_list( "id", "name", "container").order_by("container"), key=lambda o: o[2]): name, data = containers.get(container, ("", {"geopoint": {}})) x = data.get("geopoint.x") y = data.get("geopoint.y") address = data.get("address.text", "") ss = { "objects": [], "total": 0, "error": 0, "warning": 0, "good": 0, "maintenance": 0 } for mo_id, mo_name, container in mol: # Status by alarm severity # s_service = s_services.get(mo_id, s_def) status = "good" if mo_id in maintenance: status = "maintenance" elif 100 < alarms.get(mo_id, 0) <= 2000: status = "warning" elif alarms.get(mo_id, 0) > 2000: status = "error" objects_status[status] += [mo_id] ss[status] += 1 ss["total"] += 1 services_ss = [ "%s-%s" % (sm, status) for sm in services_map.get(mo_id, [self.fake_service]) ] ss["objects"] += [{ "id": mo_id, "name": mo_name, "status": status, "services": services_ss }] if not x or not y: continue objects += [{ "name": address or name, "id": str(container), "x": x if x > -168 else x + 360, # For Chukotskiy AO "y": y, "objects": [], "total": 0, "error": 0, "warning": 0, "good": 0, "maintenance": 0, }] objects[-1].update(ss) profiles = set() for r in ["error", "warning", "good", "maintenance"]: if not objects_status[r]: continue if not object_root and r == "good": m_services, m_subscribers = ServiceSummary.get_direct_summary( objects_status[r], summary_all=True) else: m_services, m_subscribers = ServiceSummary.get_direct_summary( objects_status[r]) profiles |= set(m_services) sss[r] = m_services for r in sorted(sss, key=lambda k: ("error", "warning", "good", "maintenance").index(k)): for p in profiles: services[p] += [(r, sss[r].get(p, None))] return { "objects": objects, "summary": self.f_glyph_summary({"service": services}), }
def get_ajax_data(self, **kwargs): def update_dict(d, s): for k in s: if k in d: d[k] -= s[k] else: d[k] = s[k] object_id = self.handler.get_argument("object_id") # zoom = int(self.handler.get_argument("z")) # west = float(self.handler.get_argument("w")) # east = float(self.handler.get_argument("e")) # north = float(self.handler.get_argument("n")) # south = float(self.handler.get_argument("s")) # ms = int(self.handler.get_argument("maintenance")) # active_layers = [l for l in self.get_pop_layers() if l.min_zoom <= zoom <= l.max_zoom] if self.current_user.is_superuser: moss = ManagedObject.objects.filter(is_managed=True) else: moss = ManagedObject.objects.filter( is_managed=True, administrative_domain__in=self.get_user_domains()) objects = [] objects_status = { "error": [], "warning": [], "good": [], "maintenance": [] } sss = {"error": {}, "warning": {}, "good": {}, "maintenance": {}} # s_def = { # "service": {}, # "subscriber": {}, # "interface": {} # } services = defaultdict(list) try: object_root = Object.objects.filter(id=object_id).first() except ValidationError: object_root = None if object_root: con = [str(c) for c in self.get_containers_by_root(object_root.id)] moss = moss.filter(container__in=con).order_by("container") else: moss = moss.exclude(container=None).order_by("container") con = list(moss.values_list("container", flat=True)) mo_ids = list(moss.values_list("id", flat=True)) # Getting Alarms severity dict MO: Severity @todo List alarms if not object_root: alarms = self.get_alarms_info(None, alarms_all=True) else: alarms = self.get_alarms_info(mo_ids) # Get maintenance maintenance = Maintenance.currently_affected() # Getting services if not object_root: services_map = self.get_objects_summary_met(mo_ids, info_all=True) else: services_map = self.get_objects_summary_met(mo_ids) # Getting containers name and coordinates containers = { str(o["_id"]): (o["name"], o["data"]) for o in Object.objects.filter( data__geopoint__exists=True, id__in=con, read_preference=ReadPreference.SECONDARY_PREFERRED, ).fields(id=1, name=1, data__geopoint__x=1, data__geopoint__y=1).as_pymongo() } # Main Loop. Get ManagedObject group by container for container, mol in itertools.groupby(moss.values_list( "id", "name", "container").order_by("container"), key=lambda o: o[2]): name, data = containers.get(container, ("", {"geopoint": {}})) x = data["geopoint"].get("x") y = data["geopoint"].get("y") ss = { "objects": [], "total": 0, "error": 0, "warning": 0, "good": 0, "maintenance": 0 } for mo_id, mo_name, container in mol: # Status by alarm severity # s_service = s_services.get(mo_id, s_def) status = "good" if mo_id in maintenance: status = "maintenance" elif 100 < alarms.get(mo_id) <= 2000: status = "warning" elif alarms.get(mo_id) > 2000: status = "error" objects_status[status] += [mo_id] # update_dict(sss[status], s_service["service"]) ss[status] += 1 ss["total"] += 1 services_ss = [ "%s-%s" % (sm, status) for sm in services_map.get(mo_id, [self.fake_service]) ] ss["objects"] += [{ "id": mo_id, "name": mo_name, "status": status, "services": services_ss }] if not x or not y: continue objects += [{ "name": name, "id": str(container), "x": x if x > -168 else x + 360, # For Chukotskiy AO "y": y, "objects": [], "total": 0, "error": 0, "warning": 0, "good": 0, "maintenance": 0, }] objects[-1].update(ss) profiles = set() for r in ["error", "warning", "good", "maintenance"]: if not objects_status[r]: continue if not object_root and r == "good": m_services, m_subscribers = ServiceSummary.get_direct_summary( objects_status[r], summary_all=True) else: m_services, m_subscribers = ServiceSummary.get_direct_summary( objects_status[r]) # update_dict(s_services["service"], m["serivce"]) # if not object_root and r == "good": # for s in s_services["service"]: # if s in m["service"]: # s_services["service"][s] -= m["service"][s] # m = s_services profiles |= set(m_services) sss[r] = m_services for r in sorted(sss, key=lambda k: ("error", "warning", "good", "maintenance").index(k)): # for p in sss[r]: for p in profiles: services[p] += [(r, sss[r].get(p, None))] return { "objects": objects, "summary": self.f_glyph_summary({ "service": services # "subscriber": subscribers }), }