1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/migrations/0071_populate_obj_counts.py
Stefan Pratter 5eb925e319 Support 202301 (#1329)
* fix next redirect when using U2F 2FA auth (#1191)

* Added self identifier to API

* fix migrations hierarchy after merging in previous support branch

* campus object

Co-authored-by: Stefan Pratter <stefan@20c.com>

* fix out of bound error message
add city / country to campus view

* fix tests

* relock poetry

* linting

* linting

* fix docs regen

* regen docs

* linting

* refactor self entity view to support carrier and campus object types and also make it easier to support additional object types in the future

* remove debug message

---------

Co-authored-by: Gajanan Patil <dipaksavaliya.python@gmail.com>
2023-02-15 07:55:01 +00:00

122 lines
4.2 KiB
Python

# 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),
]