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

79 lines
2.2 KiB
Python
Raw Normal View History

2019-09-10 16:30:42 +02:00
"""
2019-12-19 12:24:53 +01:00
Network statistics
2019-09-10 16:30:42 +02:00
"""
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"""
return BMConnectionPool().establishedConnections()
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
2019-12-19 12:24:53 +01:00
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(
2019-12-19 12:24:53 +01:00
(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
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