This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-15/src/network/stats.py

92 lines
2.6 KiB
Python
Raw Normal View History

2019-09-10 16:30:42 +02:00
"""
src/network/stats.py
====================
"""
import time
import asyncore_pollchoose as asyncore
from network.connectionpool import BMConnectionPool
from objectracker import missingObjects
2019-09-10 16:14:44 +02:00
lastReceivedTimestamp = time.time()
lastReceivedBytes = 0
currentReceivedSpeed = 0
lastSentTimestamp = time.time()
lastSentBytes = 0
currentSentSpeed = 0
2019-09-10 16:14:44 +02:00
def connectedHostsList():
2019-09-10 16:30:42 +02:00
"""List of all the connected hosts"""
retval = []
2017-08-22 13:49:27 +02:00
for i in BMConnectionPool().inboundConnections.values() + \
BMConnectionPool().outboundConnections.values():
if not i.fullyEstablished:
continue
try:
retval.append(i)
except AttributeError:
pass
return retval
2019-09-10 16:14:44 +02:00
def sentBytes():
2019-09-10 16:30:42 +02:00
"""Sending Bytes"""
return asyncore.sentBytes
2019-09-10 16:14:44 +02:00
def uploadSpeed():
2019-09-10 16:30:42 +02:00
"""Getting upload speed"""
# pylint: disable=global-statement
global lastSentTimestamp, lastSentBytes, currentSentSpeed
currentTimestamp = time.time()
if int(lastSentTimestamp) < int(currentTimestamp):
currentSentBytes = asyncore.sentBytes
currentSentSpeed = int((currentSentBytes - lastSentBytes) / (currentTimestamp - lastSentTimestamp))
lastSentBytes = currentSentBytes
lastSentTimestamp = currentTimestamp
return currentSentSpeed
2019-09-10 16:14:44 +02:00
def receivedBytes():
2019-09-10 16:30:42 +02:00
"""Receiving Bytes"""
return asyncore.receivedBytes
2019-09-10 16:14:44 +02:00
def downloadSpeed():
2019-09-10 16:30:42 +02:00
"""Getting download speed"""
# pylint: disable=global-statement
global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
currentTimestamp = time.time()
if int(lastReceivedTimestamp) < int(currentTimestamp):
currentReceivedBytes = asyncore.receivedBytes
2019-09-10 16:14:44 +02:00
currentReceivedSpeed = int(
(currentReceivedBytes - lastReceivedBytes) / (currentTimestamp - lastReceivedTimestamp))
lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed
2019-09-10 16:14:44 +02:00
def pendingDownload():
2019-09-10 16:30:42 +02:00
"""Getting pending downloads"""
return len(missingObjects)
2019-09-10 16:14:44 +02:00
# tmp = {}
# for connection in BMConnectionPool().inboundConnections.values() + \
# BMConnectionPool().outboundConnections.values():
# for k in connection.objectsNewToMe.keys():
# tmp[k] = True
# return len(tmp)
def pendingUpload():
2019-09-10 16:30:42 +02:00
"""Getting pending uploads"""
2019-09-10 16:14:44 +02:00
# tmp = {}
# for connection in BMConnectionPool().inboundConnections.values() + \
# BMConnectionPool().outboundConnections.values():
# for k in connection.objectsNewToThem.keys():
# tmp[k] = True
# This probably isn't the correct logic so it's disabled
# return len(tmp)
2017-08-22 13:49:27 +02:00
return 0