# Generated by Django 2.2.17 on 2021-01-05 21:39

from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations
from django.db.models import Count


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)


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)


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)


class Migration(migrations.Migration):
    dependencies = [
        ("peeringdb_server", "0070_add_obj_counts"),
    ]

    operations = [
        migrations.RunPython(populate_network_counts, migrations.RunPython.noop),
        migrations.RunPython(populate_ix_counts, migrations.RunPython.noop),
        migrations.RunPython(populate_facility_counts, migrations.RunPython.noop),
    ]
