Beispiel #1
0
    def get_data(self, request, sel=None):

        qs = ManagedObject.objects
        if not request.user.is_superuser:
            qs = ManagedObject.objects.filter(
                administrative_domain__in=UserAccess.get_domains(request.user))

        # Get all managed objects by selector
        mos_list = qs.filter(sel.Q)

        columns = [
            _("Managed Objects"),
            _("Address"),
            _("Vendor"),
            _("Platform"),
            _("HW Revision"),
            _("SW Version"),
            _("Serial"),
        ]
        data = []

        for mo in mos_list:
            q = Object._get_collection().count_documents(
                {"data.management.managed_object": {
                    "$in": [mo.id]
                }})
            if q == 0:
                data += [[
                    mo.name,
                    mo.address,
                    mo.vendor or None,
                    mo.platform.full_name if mo.platform else None,
                    mo.get_attr("HW version") or None,
                    mo.version.version if mo.version else None,
                    mo.get_attr("Serial Number") or None,
                    None,
                ]]
            else:
                for x in Object._get_collection().find(
                    {"data.management.managed_object": {
                        "$in": [mo.id]
                    }}):
                    data += [[
                        x["name"],
                        mo.address,
                        mo.vendor or None,
                        mo.platform.full_name if mo.platform else None,
                        mo.get_attr("HW version") or None,
                        mo.version.version if mo.version else None,
                        x["data"]["asset"]["serial"],
                    ]]

        return self.from_dataset(title=self.title,
                                 columns=columns,
                                 data=data,
                                 enumerate=True)
Beispiel #2
0
 def get_containers_by_root(root_id=None):
     """
     Getting all containers from root object
     # @todo containers only with coordinates (Filter by models)
     # @todo containers only
     # from noc.sa.models.managedobject import ManagedObject
     # from noc.inv.models.object import Object
     # If None - all objects
     """
     root = Object.get_by_id(root_id)
     coll = Object._get_collection().with_options(
         read_preference=ReadPreference.SECONDARY_PREFERRED)
     work_set = {root.id}
     os = set()
     kk = None
     for r in range(1, 9):
         work_set = set(
             o["_id"]
             for o in coll.find({"container": {
                 "$in": list(work_set)
             }}, {"_id": 1}))
         # work_set |= work_set.union(os)
         os |= work_set
         if len(work_set) == kk:
             break
         kk = len(work_set)
     return os
Beispiel #3
0
 def get_layer_objects(self, layer, x0, y0, x1, y1, srid):
     """
     Extract GeoJSON from bounding box
     """
     lr = Layer.get_by_code(layer)
     if not lr:
         return {}
     bbox = self.get_bbox(x0, y0, x1, y1, srid)
     features = [
         geojson.Feature(
             id=str(d["_id"]),
             geometry=self.transform(d["point"], self.db_proj, srid),
             properties={
                 "object": str(d["_id"]),
                 "label": d.get("name", "")
             },
         ) for d in Object._get_collection().find(
             {
                 "layer": lr.id,
                 "point": {
                     "$geoWithin": {
                         "$geometry": bbox
                     }
                 }
             },
             {
                 "_id": 1,
                 "point": 1,
                 "label": 1
             },
         )
     ]
     return geojson.FeatureCollection(features=features, crs=srid)
Beispiel #4
0
 def extract(self):
     o = Object._get_collection().with_options(
         read_preference=ReadPreference.SECONDARY_PREFERRED)
     for obj in o.find(
         {},
         {
             "_id": 1,
             "bi_id": 1,
             "name": 1,
             "container": 1,
             "data.address.text": 1
         },
             no_cursor_timeout=True,
     ):
         address = [
             a for a in obj["data"]
             if a and a["interface"] == "address" and a["attr"] == "text"
         ]
         yield (
             obj["bi_id"],
             obj["_id"],
             obj.get("name", ""),
             bi_hash(obj["container"]) if obj.get("container") else "",
             address[0] if address else "",
         )
Beispiel #5
0
def fix():
    for d in Object._get_collection().find(
        {"data.geopoint.x": {
            "$exists": True
        }}, {"_id": 1}):
        o = Object.get_by_id(d["_id"])
        o.save()
Beispiel #6
0
def fix():
    for d in Object._get_collection().find(
        {"data": {
            "$elemMatch": {
                "interface": "geopoint",
                "attr": "x"
            }
        }}, {"_id": 1}):
        o = Object.get_by_id(d["_id"])
        o.save()
Beispiel #7
0
 def extract(self):
     o = Object._get_collection().with_options(
         read_preference=ReadPreference.SECONDARY_PREFERRED)
     for obj in o.find({}, {
             "_id": 1,
             "bi_id": 1,
             "name": 1,
             "container": 1,
             "data.address.text": 1
     },
                       no_cursor_timeout=True):
         data = obj.get("data", {})
         yield (obj["bi_id"], obj["_id"], obj.get("name", ""),
                bi_hash(obj["container"]) if obj.get("container") else "",
                data["address"].get("text", "")
                if data and "address" in data else "")
Beispiel #8
0
 def get_data(self, **kwargs):
     self.model_name = {}  # oid -> name
     data = list(Object._get_collection().aggregate([{
         "$group": {
             "_id": "$model",
             "total": {
                 "$sum": 1
             }
         }
     }]))
     oms = [x["_id"] for x in data if x["_id"]]
     c = ObjectModel._get_collection()
     om_names = {}
     while oms:
         chunk, oms = oms[:500], oms[500:]
         om_names.update({
             x["_id"]: x["name"]
             for x in c.find({"_id": {
                 "$in": chunk
             }}, {
                 "_id": 1,
                 "name": 1
             })
         })
     data = sorted(
         ([om_names[x["_id"]], x["total"]]
          for x in data if x["_id"] in om_names),
         key=lambda x: -x[1],
     )
     return self.from_dataset(
         title=self.title,
         columns=[
             "Model",
             TableColumn("Count",
                         format="numeric",
                         align="right",
                         total="sum")
         ],
         data=data,
         enumerate=True,
     )
Beispiel #9
0
    def get_data(self, request, sel=None):

        qs = ManagedObject.objects
        if not request.user.is_superuser:
            qs = ManagedObject.objects.filter(
                administrative_domain__in=UserAccess.get_domains(request.user))

        # Get all managed objects by selector
        mos_list = qs.filter(sel.Q)

        columns = [
            _("Managed Objects"),
            _("Address"),
            _("Vendor"),
            _("Platform"),
            _("HW Revision"),
            _("SW Version"),
            _("Serial"),
        ]
        data = []
        mos = {
            x["id"]: x
            for x in mos_list.values("id", "name", "address", "vendor",
                                     "platform", "version")
        }
        ra = ReportObjectAttributes(sorted(mos))
        ra = ra.get_dictionary()
        objects_serials = {}
        for o in Object._get_collection().aggregate([
            {
                "$match": {
                    "data": {
                        "$elemMatch": {
                            "attr": "managed_object",
                            "value": {
                                "$in": list(mos)
                            }
                        }
                    }
                }
            },
            {
                "$project": {
                    "data": {
                        "$filter": {
                            "input": "$data",
                            "cond": {
                                "$in":
                                ["$$this.attr", ["serial", "managed_object"]]
                            },
                        }
                    }
                }
            },
        ]):
            if len(o["data"]) < 2:
                continue
            if o["data"][0]["attr"] == "serial":
                objects_serials[int(
                    o["data"][1]["value"])] = o["data"][0]["value"]
            else:
                objects_serials[int(
                    o["data"][0]["value"])] = o["data"][1]["value"]

        for mo in mos.values():
            platform = Platform.get_by_id(
                mo["platform"]) if mo.get("platform") else None
            vendor = Vendor.get_by_id(
                mo["vendor"]) if mo.get("vendor") else None
            version = Firmware.get_by_id(
                mo["version"]) if mo.get("version") else None
            sn, hw = ra[mo["id"]][:2] if mo["id"] in ra else (None, None)
            if mo["id"] in objects_serials:
                sn = objects_serials[mo["id"]]
            data += [[
                mo["name"],
                mo["address"],
                vendor,
                platform,
                hw,
                version,
                sn,
                None,
            ]]

        return self.from_dataset(title=self.title,
                                 columns=columns,
                                 data=data,
                                 enumerate=True)