PyBitmessage-2021-04-27/src/network/stats.py

79 lines
2.3 KiB
Python
Raw Normal View History

import time
import asyncore_pollchoose as asyncore
from network.connectionpool import BMConnectionPool
from objectracker import missingObjects
2019-09-10 14:14:44 +00:00
lastReceivedTimestamp = time.time()
lastReceivedBytes = 0
currentReceivedSpeed = 0
lastSentTimestamp = time.time()
lastSentBytes = 0
currentSentSpeed = 0
2019-09-10 14:14:44 +00:00
def connectedHostsList():
retval = []
2017-08-22 11:49:27 +00: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 14:14:44 +00:00
def sentBytes():
return asyncore.sentBytes
2019-09-10 14:14:44 +00:00
def uploadSpeed():
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 14:14:44 +00:00
def receivedBytes():
return asyncore.receivedBytes
2019-09-10 14:14:44 +00:00
def downloadSpeed():
global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
currentTimestamp = time.time()
if int(lastReceivedTimestamp) < int(currentTimestamp):
currentReceivedBytes = asyncore.receivedBytes
2019-09-10 14:14:44 +00:00
currentReceivedSpeed = int(
(currentReceivedBytes - lastReceivedBytes) / (currentTimestamp - lastReceivedTimestamp))
lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed
2019-09-10 14:14:44 +00:00
def pendingDownload():
return len(missingObjects)
2019-09-10 14:14:44 +00: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 14:14:44 +00: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 11:49:27 +00:00
return 0