PyBitmessage/src/network/stats.py

77 lines
2.2 KiB
Python
Raw Normal View History

2019-09-10 14:30:42 +00:00
"""
src/network/stats.py
====================
"""
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():
2019-09-10 14:30:42 +00:00
"""List of all the connected hosts"""
return BMConnectionPool().establishedConnections()
2019-09-10 14:14:44 +00:00
def sentBytes():
2019-09-10 14:30:42 +00:00
"""Sending Bytes"""
return asyncore.sentBytes
2019-09-10 14:14:44 +00:00
def uploadSpeed():
2019-09-10 14:30:42 +00: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 14:14:44 +00:00
def receivedBytes():
2019-09-10 14:30:42 +00:00
"""Receiving Bytes"""
return asyncore.receivedBytes
2019-09-10 14:14:44 +00:00
def downloadSpeed():
2019-09-10 14:30:42 +00: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 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():
2019-09-10 14:30:42 +00:00
"""Getting pending downloads"""
return len(missingObjects)
2019-09-10 14:14:44 +00:00
def pendingUpload():
2019-09-10 14:30:42 +00:00
"""Getting pending uploads"""
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