Example #1
0
    def _refresh_object(cls, managed_object):
        from noc.sa.models.managedobject import ManagedObject
        from noc.inv.models.networksegment import NetworkSegment

        def to_dict(v):
            return {r["profile"]: r["summary"] for r in v}

        def to_list(v):
            return [{"profile": k, "summary": v[k]} for k in sorted(v)]

        if hasattr(managed_object, "id"):
            managed_object = managed_object.id
        coll = ServiceSummary._get_collection()
        bulk = []
        # Get existing summary
        old_summary = {
            x["interface"]: x
            for x in coll.find(
                {"managed_object": managed_object},
                {
                    "_id": 1,
                    "interface": 1,
                    "service": 1,
                    "subscriber": 1
                },
                comment=
                "[servicesummary._refresh_object] Refresh summary of services for managed object",
            ) if "interface" in x
        }
        # Get actual summary
        new_summary = ServiceSummary.build_summary_for_object(managed_object)
        # Merge summaries
        for iface in old_summary:
            if iface not in new_summary:
                # Stale, delete
                bulk += [DeleteOne({"_id": old_summary[iface]["_id"]})]
                continue
            oi = old_summary[iface]
            old_services = to_dict(oi["service"])
            old_subs = to_dict(oi["subscriber"])
            ni = new_summary[iface]
            if old_services != ni["service"] or old_subs != ni["subscriber"]:
                # Changed, update
                bulk += [
                    UpdateOne(
                        {"_id": oi["_id"]},
                        {
                            "$set": {
                                "service": to_list(ni["service"]),
                                "subscriber": to_list(ni["subscriber"]),
                            }
                        },
                    )
                ]
            # Mark as processed
            del new_summary[iface]
        # Process new items
        bulk += [
            InsertOne({
                "managed_object": managed_object,
                "interface": iface,
                "service": to_list(new_summary[iface]["service"]),
                "subscriber": to_list(new_summary[iface]["subscriber"]),
            }) for iface in new_summary
        ]
        if bulk:
            logger.info("Committing changes to database")
            try:
                r = coll.bulk_write(bulk, ordered=False)
                logger.info("Database has been synced")
                logger.info("Modify: %d, Deleted: %d", r.modified_count,
                            r.deleted_count)
            except BulkWriteError as e:
                logger.error("Bulk write error: '%s'", e.details)
                logger.error("Stopping check")
        mo = ManagedObject.get_by_id(managed_object)
        NetworkSegment.update_summary(mo.segment)
Example #2
0
def fix():
    for ns in NetworkSegment.objects.all():
        NetworkSegment.update_summary(ns)