2018-11-08 19:45:21 +00:00
|
|
|
"""
|
2021-10-15 03:25:38 -05:00
|
|
|
Load and maintain global stats (displayed in peeringdb footer).
|
2018-11-08 19:45:21 +00:00
|
|
|
"""
|
2022-11-08 19:25:32 +02: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 (
|
2023-02-15 09:55:01 +02:00
|
|
|
Campus,
|
2023-01-18 18:32:46 +02:00
|
|
|
Carrier,
|
2019-12-05 16:57:52 +00:00
|
|
|
Facility,
|
2021-07-10 10:12:35 -05:00
|
|
|
InternetExchange,
|
|
|
|
Network,
|
2019-12-05 16:57:52 +00:00
|
|
|
NetworkFacility,
|
2021-07-10 10:12:35 -05:00
|
|
|
NetworkIXLan,
|
2022-02-08 13:14:27 -06:00
|
|
|
Organization,
|
|
|
|
User,
|
2019-12-05 16:57:52 +00:00
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
2022-11-08 19:25:32 +02:00
|
|
|
__STATS = {"data": {}, "mod": None}
|
2018-11-08 19:45:21 +00:00
|
|
|
|
2022-11-08 19:25:32 +02:00
|
|
|
|
2023-05-16 21:04:05 +03:00
|
|
|
def reset_stats():
|
|
|
|
"""
|
|
|
|
Resets global stats to empty. Useful to reset for testing purposes.
|
|
|
|
"""
|
|
|
|
__STATS["data"] = {}
|
|
|
|
__STATS["mod"] = None
|
|
|
|
|
|
|
|
|
2022-11-08 19:25:32 +02:00
|
|
|
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(),
|
2023-06-20 03:26:06 +03:00
|
|
|
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(),
|
2023-01-18 18:32:46 +02:00
|
|
|
"automated_nets": Network.handleref.filter(
|
|
|
|
status="ok", allow_ixp_update=True
|
|
|
|
).count(),
|
2022-02-08 13:14:27 -06:00
|
|
|
"organizations": Organization.objects.filter(status="ok").count(),
|
2023-06-20 03:26:06 +03:00
|
|
|
"registered_users": User.objects.count(),
|
2018-11-08 19:45:21 +00:00
|
|
|
}
|
2022-11-08 19:25:32 +02: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"]
|
2021-10-12 11:05:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|