Example #1
0
def populate_facility_counts(apps, schema_editor):
    from peeringdb_server.util import disable_auto_now_and_save

    Facility = apps.get_model("peeringdb_server", "Facility")
    print("Populating Facility net_count and ix_count values")
    NetworkFacility = apps.get_model("peeringdb_server", "NetworkFacility")
    InternetExchangeFacility = apps.get_model(
        "peeringdb_server", "InternetExchangeFacility"
    )
    for facility in Facility.handleref.all():
        if facility.id % 10 == 0:
            print(facility)
        try:
            facility.ix_count = (
                InternetExchangeFacility.handleref.select_related("ix")
                .filter(facility_id=facility.id, status="ok")
                .aggregate(ix_count=Count("ix_id", distinct=True))
            )["ix_count"]

        except ObjectDoesNotExist:
            pass
        try:
            facility.net_count = (
                NetworkFacility.handleref.select_related("network")
                .filter(facility_id=facility.id, status="ok")
                .aggregate(net_count=Count("network_id", distinct=True))
            )["net_count"]

        except ObjectDoesNotExist:
            pass

        disable_auto_now_and_save(facility)
Example #2
0
def populate_last_updated_fields(apps, schema_editor):
    from peeringdb_server.util import disable_auto_now_and_save

    Network = apps.get_model("peeringdb_server", "Network")

    print("This migration may take a few minutes ...")

    for network in Network.handleref.all():
        try:
            latest_netfac = network.netfac_set.filter(updated__isnull=False).latest(
                "updated"
            )
            network.netfac_updated = latest_netfac.updated
        except ObjectDoesNotExist:
            pass
        try:
            latest_netixlan = network.netixlan_set.filter(updated__isnull=False).latest(
                "updated"
            )
            network.netixlan_updated = latest_netixlan.updated
        except ObjectDoesNotExist:
            pass

        try:
            latest_poc = network.poc_set.filter(updated__isnull=False).latest("updated")
            network.poc_updated = latest_poc.updated
        except ObjectDoesNotExist:
            pass

        disable_auto_now_and_save(network)
Example #3
0
def populate_network_counts(apps, schema_editor):
    from peeringdb_server.util import disable_auto_now_and_save

    Network = apps.get_model("peeringdb_server", "Network")
    NetworkIXLan = apps.get_model("peeringdb_server", "NetworkIXLan")
    NetworkFacility = apps.get_model("peeringdb_server", "NetworkFacility")

    print("This migration may take several minutes ...")
    print("Populating network ix_count and fac_count values")

    for network in Network.handleref.all():
        if network.id % 10 == 0:
            print(network)
        try:
            network.ix_count = (
                NetworkIXLan.handleref.select_related("ixlan__ix")
                .filter(network_id=network.id, status="ok")
                .aggregate(ix_count=Count("ixlan__ix_id", distinct=True))
            )["ix_count"]

        except ObjectDoesNotExist:
            pass
        try:
            network.fac_count = (
                NetworkFacility.handleref.select_related("facility")
                .filter(network_id=network.id, status="ok")
                .aggregate(fac_count=Count("facility_id", distinct=True))
            )["fac_count"]
        except ObjectDoesNotExist:
            pass

        disable_auto_now_and_save(network)
Example #4
0
def populate_ix_counts(apps, schema_editor):
    from peeringdb_server.util import disable_auto_now_and_save

    InternetExchange = apps.get_model("peeringdb_server", "InternetExchange")
    NetworkIXLan = apps.get_model("peeringdb_server", "NetworkIXLan")
    InternetExchangeFacility = apps.get_model(
        "peeringdb_server", "InternetExchangeFacility"
    )

    print("Populating Exchange net_count and fac_count values")

    for ix in InternetExchange.handleref.all():
        if ix.id % 10 == 0:
            print(ix)

        try:
            ix.net_count = (
                NetworkIXLan.handleref.select_related("network")
                .filter(ixlan__ix_id=ix.id, status="ok")
                .aggregate(net_count=Count("network_id", distinct=True))
            )["net_count"]
        except ObjectDoesNotExist:
            pass
        try:
            ix.fac_count = (
                InternetExchangeFacility.handleref.select_related("facility")
                .filter(ix_id=ix.id, status="ok")
                .aggregate(fac_count=Count("facility_id", distinct=True))
            )["fac_count"]

        except ObjectDoesNotExist:
            pass

        disable_auto_now_and_save(ix)
Example #5
0
def update_counts_for_netfac(netfac):
    """
    Whenever a netfac is saved, update the fac_count for the related Network
    and update net_count for the related Facility.
    """
    if getattr(netfac, "id"):
        network = netfac.network

        network.fac_count = network.netfac_set_active.count()

        disable_auto_now_and_save(network)

        facility = netfac.facility
        facility.net_count = facility.netfac_set_active.count()
        disable_auto_now_and_save(facility)
Example #6
0
def update_counts_for_ixfac(ixfac):
    """
    Whenever a ixfac is saved, update the fac_count for the related Exchange
    and update ix_count for the related Facility.
    """
    if getattr(ixfac, "id"):
        ix = ixfac.ix

        ix.fac_count = ix.ixfac_set_active.count()

        disable_auto_now_and_save(ix)

        facility = ixfac.facility
        facility.ix_count = facility.ixfac_set_active.count()
        disable_auto_now_and_save(facility)
Example #7
0
def update_counts_for_netixlan(netixlan):
    """
    Whenever a netixlan is saved, update the ix_count for the related Network
    and update net_count for the related InternetExchange.
    """
    if getattr(netixlan, "id"):
        network = netixlan.network

        network.ix_count = (network.netixlan_set_active.aggregate(
            ix_count=Count("ixlan__ix_id", distinct=True)))["ix_count"]

        disable_auto_now_and_save(network)

        ix = netixlan.ixlan.ix
        ix.net_count = (NetworkIXLan.objects.filter(
            ixlan__ix_id=ix.id, status="ok").aggregate(
                net_count=Count("network_id", distinct=True)))["net_count"]
        disable_auto_now_and_save(ix)
Example #8
0
    def update_region_continent_ok(apps, schema_editor):

        print("Updating fac region_continent")
        Facility = apps.get_model("peeringdb_server", "Facility")

        objects = Facility.handleref.all()

        for ob in objects:

            if ob.status != "ok":
                continue

            country = ob.country.code
            continent = None

            for ctr in const.REGION_MAPPING:
                if ctr["code"] == country:
                    continent = ctr["continent"]
                    break

            ob.region_continent = continent
            disable_auto_now_and_save(ob)
Example #9
0
def update_network_attribute(instance, attribute):
    """Updates 'attribute' field in Network whenever it's called."""
    if getattr(instance, "id"):
        network = instance.network
        setattr(network, attribute, datetime.now(timezone.utc))
        disable_auto_now_and_save(network)