mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* use new peeringdb client (1.0.0) for pdb_load_data sync (#599) * drop django-mobi for lack of py3/dj2 support (#492) remove django-forms-bootstrap for lack of py3/dj2 support (#492) * black formatted * django2.2 and py3 upgrade (#492) * drop ixlans (#21) ui and api changes * drop local_asn (#168) * org search (#193) * phone number validation (#50) * implement help text tooltips (#228) * Mark own ASN as transit-free (#394) * py3 fix for `pdb_migrate_ixlans` command when writing migration report * pdb_migrate_ixlans: properly handle py3 Runtime error if ixlan dict changes during iteration * set rest DEFAULT_SCHEMA_CLASS to coreapi to fix swagger apidocs fix migration 0027 missing from facsimile manifest * fix swagger doc strings * fix tests that were broken from api doc fixes * fix UniqueFieldValidator for netixlan ipaddress validation that broke during django/drf upgrade * fix org merge tool layout issues * travis config * update pipfile and lock * black formatting * update travis dist * beta mode banner (#411) * add beta banner template (#411) * automatically scheduled sync may not always be on, add a flag that lets us reflect that state in the beta banner message clean up beta banner implementation (#411) * add tests for beta banner (#411)
127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
from django.core.management.base import BaseCommand
|
|
import os
|
|
import traceback
|
|
import peeringdb_server.models as pdbm
|
|
import peeringdb_server.rest as pdbr
|
|
import datetime
|
|
import time
|
|
from peeringdb_server.renderers import MetaJSONRenderer
|
|
from django.conf import settings
|
|
from optparse import make_option
|
|
from rest_framework.test import APIRequestFactory
|
|
|
|
MODELS = [
|
|
pdbm.Organization,
|
|
pdbm.Network,
|
|
pdbm.InternetExchange,
|
|
pdbm.Facility,
|
|
pdbm.NetworkContact,
|
|
pdbm.NetworkFacility,
|
|
pdbm.IXLan,
|
|
pdbm.IXLanPrefix,
|
|
pdbm.NetworkIXLan,
|
|
]
|
|
|
|
VIEWSETS = {
|
|
"org": pdbr.OrganizationViewSet,
|
|
"net": pdbr.NetworkViewSet,
|
|
"ix": pdbr.InternetExchangeViewSet,
|
|
"fac": pdbr.FacilityViewSet,
|
|
"ixlan": pdbr.IXLanViewSet,
|
|
"ixfac": pdbr.InternetExchangeFacilityViewSet,
|
|
"ixpfx": pdbr.IXLanPrefixViewSet,
|
|
"netfac": pdbr.NetworkFacilityViewSet,
|
|
"netixlan": pdbr.NetworkIXLanViewSet,
|
|
"poc": pdbr.NetworkContactViewSet,
|
|
}
|
|
|
|
settings.DEBUG = False
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Regen the api cache files"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--only", action="store", default=False, help="only run specified type"
|
|
)
|
|
parser.add_argument(
|
|
"--date",
|
|
action="store",
|
|
default=None,
|
|
help="generate cache for objects create before or at the specified date (YYYYMMDD)",
|
|
)
|
|
|
|
def log(self, id, msg):
|
|
if self.log_file:
|
|
self.log_file.write("%s: %s" % (id, msg))
|
|
self.log_file.flush()
|
|
print("%s: %s" % (id, msg))
|
|
|
|
def row_datetime(self, row, field="created"):
|
|
return datetime.datetime.strptime(row.get(field), "%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
def handle(self, *args, **options):
|
|
only = options.get("only", None)
|
|
date = options.get("date", None)
|
|
|
|
if only:
|
|
only = only.split(",")
|
|
|
|
if date:
|
|
dt = datetime.datetime.strptime(date, "%Y%m%d")
|
|
else:
|
|
dt = datetime.datetime.now()
|
|
dtstr = dt.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
self.log_file = open(settings.API_CACHE_LOG, "w+")
|
|
self.log("info", "Regnerating cache files to '%s'" % settings.API_CACHE_ROOT)
|
|
self.log("info", "Caching data for timestamp: %s" % dtstr)
|
|
rf = APIRequestFactory()
|
|
renderer = MetaJSONRenderer()
|
|
|
|
t = time.time()
|
|
|
|
su = pdbm.User.objects.filter(is_superuser=True).first()
|
|
|
|
settings.API_DEPTH_ROW_LIMIT = 0
|
|
|
|
try:
|
|
cache = {}
|
|
|
|
for tag, viewset in list(VIEWSETS.items()):
|
|
if only and tag not in only:
|
|
continue
|
|
|
|
for depth in [0, 1, 2, 3]:
|
|
self.log(tag, "generating depth %d" % depth)
|
|
if depth:
|
|
req = rf.get(
|
|
"/api/%s?depth=%d&updated__lte=%s&_ctf"
|
|
% (tag, depth, dtstr)
|
|
)
|
|
else:
|
|
req = rf.get("/api/%s?updated__lte=%s&_ctf" % (tag, dtstr))
|
|
req.user = su
|
|
vs = viewset.as_view({"get": "list"})
|
|
res = vs(req)
|
|
cache["%s-%s" % (tag, depth)] = renderer.render(
|
|
res.data, renderer_context={"response": res}
|
|
)
|
|
del res
|
|
del vs
|
|
|
|
for id, data in list(cache.items()):
|
|
self.log(id, "saving file")
|
|
with open(
|
|
os.path.join(settings.API_CACHE_ROOT, "%s.json" % (id)), "w+"
|
|
) as output:
|
|
output.write(data)
|
|
|
|
except Exception:
|
|
self.log("error", traceback.format_exc())
|
|
raise
|
|
|
|
t2 = time.time()
|
|
|
|
print("Finished after %.2f seconds" % (t2 - t))
|