1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/stats.py

112 lines
3.0 KiB
Python
Raw Permalink Normal View History

2018-11-08 19:45:21 +00:00
"""
Load and maintain global stats (displayed in peeringdb footer).
2018-11-08 19:45:21 +00:00
"""
from django.conf import settings
from django.utils import timezone
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
from peeringdb_server.models import (
Campus,
Support 202211 (#1304) * Carrier object implementation #909 * API keys: disabling of user account by a PeeringDB admin does not disable access via a User API key. Also no disable mech, only revoke. #1140 * Ops: django needs lightweight healthcheck route that confirms database connectivity #1284 * Ops: various indexes are needed #1285 * API requests with invalid Authentication headers should notify users in some way. #1220 * Allow user to change account username #1130 * UX to remove carriers from facilities more inline the other similar UX * more UX fixes for removing carriers from facilities * Cache hints are needed for optimal CDN use #970 * fixes Commandline tool "Run command" button gone #1278 * RIR status gets deleted when changes are made to the network #1279 * Improve MTU field #658 * CSRF cookie not set error from email confirmation view #1296 * expose CSP_CONNECT_SRC * fix confirm email path checking in session middleware * Ops: Emails to OPERATIONS_EMAIL need to be rate-limited #1282 * add website field to carrier ux * website field on carrier optional with org fallback * linting * add *.google-analytics.com to CSP_CONNECT_SRC * poetry relock * fix issues with confirm-email reverse during session creation validation * fix tests * fix tests * pin django-peeringdb to support_202211 * linting * django ratelimit to <4 * regen docs * fix automated net stats to only include networks with status `ok` #1283 * linting * poetry lock Co-authored-by: Matt Griswold <grizz@20c.com>
2023-01-18 18:32:46 +02:00
Carrier,
2019-12-05 16:57:52 +00:00
Facility,
InternetExchange,
Network,
2019-12-05 16:57:52 +00:00
NetworkFacility,
NetworkIXLan,
Organization,
User,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
__STATS = {"data": {}, "mod": None}
2018-11-08 19:45:21 +00:00
def reset_stats():
"""
Resets global stats to empty. Useful to reset for testing purposes.
"""
__STATS["data"] = {}
__STATS["mod"] = None
def gen_stats():
"""
Regenerates global statics to stats.__STATS['data']
"""
__STATS["data"] = {
2018-11-08 19:45:21 +00:00
Network.handleref.tag: Network.handleref.filter(status="ok").count(),
2019-12-05 16:57:52 +00:00
InternetExchange.handleref.tag: InternetExchange.handleref.filter(
status="ok"
).count(),
2018-11-08 19:45:21 +00:00
Facility.handleref.tag: Facility.handleref.filter(status="ok").count(),
Carrier.handleref.tag: Carrier.handleref.filter(status="ok").count(),
Campus.handleref.tag: Campus.handleref.filter(status="ok").count(),
2019-12-05 16:57:52 +00:00
NetworkIXLan.handleref.tag: NetworkIXLan.handleref.filter(status="ok").count(),
NetworkFacility.handleref.tag: NetworkFacility.handleref.filter(
status="ok"
).count(),
Support 202211 (#1304) * Carrier object implementation #909 * API keys: disabling of user account by a PeeringDB admin does not disable access via a User API key. Also no disable mech, only revoke. #1140 * Ops: django needs lightweight healthcheck route that confirms database connectivity #1284 * Ops: various indexes are needed #1285 * API requests with invalid Authentication headers should notify users in some way. #1220 * Allow user to change account username #1130 * UX to remove carriers from facilities more inline the other similar UX * more UX fixes for removing carriers from facilities * Cache hints are needed for optimal CDN use #970 * fixes Commandline tool "Run command" button gone #1278 * RIR status gets deleted when changes are made to the network #1279 * Improve MTU field #658 * CSRF cookie not set error from email confirmation view #1296 * expose CSP_CONNECT_SRC * fix confirm email path checking in session middleware * Ops: Emails to OPERATIONS_EMAIL need to be rate-limited #1282 * add website field to carrier ux * website field on carrier optional with org fallback * linting * add *.google-analytics.com to CSP_CONNECT_SRC * poetry relock * fix issues with confirm-email reverse during session creation validation * fix tests * fix tests * pin django-peeringdb to support_202211 * linting * django ratelimit to <4 * regen docs * fix automated net stats to only include networks with status `ok` #1283 * linting * poetry lock Co-authored-by: Matt Griswold <grizz@20c.com>
2023-01-18 18:32:46 +02:00
"automated_nets": Network.handleref.filter(
status="ok", allow_ixp_update=True
).count(),
"organizations": Organization.objects.filter(status="ok").count(),
"registered_users": User.objects.count(),
2018-11-08 19:45:21 +00:00
}
__STATS["mod"] = timezone.now()
def stats():
"""
Returns dict of global statistics
Will return cached statistics according to `GLOBAL_STATS_CACHE_DURATION` setting
"""
if __STATS["mod"]:
diff = timezone.now() - __STATS["mod"]
if diff.total_seconds() < settings.GLOBAL_STATS_CACHE_DURATION:
return __STATS["data"]
gen_stats()
return __STATS["data"]
def get_fac_stats(netfac, ixfac):
return {
"networks": netfac.filter(status="ok").count(),
"ix": ixfac.filter(status="ok").count(),
}
def get_ix_stats(netixlan, ixlan):
peer_count = netixlan.values("network").distinct().filter(status="ok").count()
connections_count = netixlan.filter(ixlan=ixlan, status="ok").count()
open_peer_count = (
netixlan.values("network")
.distinct()
.filter(network__policy_general="Open", status="ok")
.count()
)
ipv6_percentage = 0
total_speed = 0
try:
ipv6_percentage = int(
(
netixlan.filter(status="ok", ixlan=ixlan, ipaddr6__isnull=False).count()
/ netixlan.filter(ixlan=ixlan, status="ok").count()
)
* 100
)
except ZeroDivisionError:
pass
for n in netixlan.filter(status="ok", ixlan=ixlan):
total_speed += n.speed
return {
"peer_count": peer_count,
"connection_count": connections_count,
"open_peer_count": open_peer_count,
"ipv6_percentage": ipv6_percentage,
"total_speed": total_speed,
}