mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* Add pointer from API docs to tutorial #650 * Sorting by clicking table headers should use local-compare #356 * Mark IXP peering LAN as bogon #352 * Add help text to "Add (Facility, Network, Exchange)" tab #669 * Add Looking Glass field to the IX object #672 * Add read-only Superuser #679 * Make spelling of traffic levels consistent #519 (#723) * Offer 2FA (#290) * Show "Last Updated" fields on fac, ix, org records (#526) * Enable sort and reverse sort of IP column in IX display (#72) * IRR validation not handling unexpected characters gracefully (#712) * Support alternative direction of writing, e.g. Arabic (#618) * Undeleting an ixlan with an emtpy IPv4 XOR IPv6 field throws a silly error (#644) * Changing org while adding net results in 500 #654 * missing delete button for organisations (#121) * When changing owner of an ix admin GUI borks because of "Ixlan for exchange already exists" #666 * Selection should only present undeleted objects (#664) * change default encoding of API calls to 'utf-8' #663 * Posting https://www.peeringdb.com onto social media doesn't select a good preview image #537 * Revert "Add Looking Glass field to the IX object #672" This reverts commit 4daf2520043c241fabe9a521757efa86a274e28a. Conflicts: peeringdb_server/migrations/0037_ix_looking_glass.py peeringdb_server/views.py * 500 Internal Error when creating IX where prefix already exists elsewhere #718 * Fix graceful restore of soft-deleted objects with translation active (#580) * Don't return any POC data with status=deleted #569 Hard delete soft-deleted pocs after grace period #566 * django-peeringdb from github@2.0.0.2-beta Co-authored-by: Stefan Pratter <stefan@20c.com>
143 lines
3.7 KiB
Python
143 lines
3.7 KiB
Python
import datetime
|
|
import json
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.core.management.base import BaseCommand
|
|
from django.contrib.contenttypes.models import ContentType
|
|
import reversion
|
|
from reversion.models import Version, Revision
|
|
|
|
from peeringdb_server.models import REFTAG_MAP, UTC
|
|
|
|
|
|
class Command(BaseCommand):
|
|
"""
|
|
Posts stat breakdown for any given date, if not date is supplied
|
|
today will be used
|
|
"""
|
|
|
|
tags = ["fac", "ix", "net", "org"]
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--date", action="store", default=None, help="generate stats for this date"
|
|
)
|
|
parser.add_argument(
|
|
"--format", action="store", default="text", help="output format to use"
|
|
)
|
|
|
|
def status_at_date(self, obj, dt):
|
|
versions = Version.objects.get_for_object(obj)
|
|
version = (
|
|
versions.filter(revision__date_created__lte=dt)
|
|
.order_by("-revision__date_created")
|
|
.first()
|
|
)
|
|
if version:
|
|
return version.field_dict["status"]
|
|
else:
|
|
return obj.status
|
|
|
|
def handle(self, *args, **options):
|
|
date = options.get("date", None)
|
|
if date:
|
|
dt = datetime.datetime.strptime(date, "%Y%m%d")
|
|
stats = self.generate_for_past_date(dt)
|
|
else:
|
|
stats = self.generate_for_current_date()
|
|
|
|
self.print_stats(stats, output_format=options.get("format"))
|
|
|
|
def stats(self, dt):
|
|
"""
|
|
Generates and returns a fresh stats dict with user count
|
|
for the specified date
|
|
|
|
Argument(s)
|
|
|
|
- dt: datetime object
|
|
|
|
Return(s)
|
|
|
|
`dict`
|
|
"""
|
|
|
|
stats = {"users": 0}
|
|
|
|
for user in get_user_model().objects.filter(created__lte=dt):
|
|
if user.is_verified_user:
|
|
stats["users"] += 1
|
|
|
|
return stats
|
|
|
|
def print_stats(self, stats, output_format="text"):
|
|
"""
|
|
Output generated stats in a userfriendly format
|
|
|
|
Argument(s)
|
|
|
|
- stats: `dict` generated via `generate_for_current_date`
|
|
|
|
Keyword Argument(s)
|
|
|
|
- output_format: `str` ("text" or "json")
|
|
"""
|
|
|
|
dt = stats["dt"]
|
|
stats = stats["stats"]
|
|
|
|
date = dt.replace(tzinfo=None).strftime("%Y-%m-%d")
|
|
if output_format == "text":
|
|
self.stdout.write(date)
|
|
self.stdout.write("-------------")
|
|
for each in self.tags + ["users"]:
|
|
self.stdout.write("{}: {}".format(each, stats[each]))
|
|
elif output_format == "json":
|
|
self.stdout.write(json.dumps({date: stats}))
|
|
else:
|
|
raise Exception("unknown format {}".format(output_format))
|
|
|
|
def generate_for_current_date(self):
|
|
"""
|
|
Generate and return stats for current date
|
|
|
|
Returns
|
|
|
|
`dict` with `stats` and `dt` keys
|
|
"""
|
|
|
|
dt = datetime.datetime.now().replace(tzinfo=UTC())
|
|
|
|
stats = self.stats(dt)
|
|
|
|
for tag in self.tags:
|
|
model = REFTAG_MAP[tag]
|
|
stats[tag] = model.objects.filter(status="ok").count()
|
|
|
|
return {"stats": stats, "dt": dt}
|
|
|
|
def generate_for_past_date(self, dt):
|
|
"""
|
|
Generate and return stats for past date
|
|
|
|
Argument(s)
|
|
|
|
- dt: `datetime` instance
|
|
|
|
Returns
|
|
|
|
`dict` with `stats` and `dt` keys
|
|
"""
|
|
|
|
dt = dt.replace(hour=23, minute=23, second=59, tzinfo=UTC())
|
|
stats = self.stats(dt)
|
|
|
|
for tag in self.tags:
|
|
model = REFTAG_MAP[tag]
|
|
stats[tag] = 0
|
|
for obj in model.objects.filter(created__lte=dt):
|
|
if self.status_at_date(obj, dt) == "ok":
|
|
stats[tag] += 1
|
|
|
|
return {"stats": stats, "dt": dt}
|