stats.py
1 """ 2 Network statistics 3 """ 4 import time 5 6 import asyncore_pollchoose as asyncore 7 import urllib3 8 from objectracker import missingObjects 9 10 11 lastReceivedTimestamp = time.time() 12 lastReceivedBytes = 0 13 currentReceivedSpeed = 0 14 lastSentTimestamp = time.time() 15 lastSentBytes = 0 16 currentSentSpeed = 0 17 18 # Create a PoolManager instance. 19 http = urllib3.PoolManager() 20 21 # A list to keep track of connected hosts 22 connected_hosts = set() 23 24 def connectedHostsList(): 25 """List of all the connected hosts""" 26 return list(connected_hosts) 27 28 def make_request(url): 29 """Make a request to a given URL and track the host""" 30 host = urllib3.util.parse_url(url).host 31 connected_hosts.add(host) # Add the host to the connected hosts list 32 response = http.request('GET', url) 33 return response.data.decode('utf-8') 34 35 36 def sentBytes(): 37 """Sending Bytes""" 38 return asyncore.sentBytes 39 40 41 def uploadSpeed(): 42 """Getting upload speed""" 43 # pylint: disable=global-statement 44 global lastSentTimestamp, lastSentBytes, currentSentSpeed 45 currentTimestamp = time.time() 46 if int(lastSentTimestamp) < int(currentTimestamp): 47 currentSentBytes = asyncore.sentBytes 48 currentSentSpeed = int( 49 (currentSentBytes - lastSentBytes) / ( 50 currentTimestamp - lastSentTimestamp)) 51 lastSentBytes = currentSentBytes 52 lastSentTimestamp = currentTimestamp 53 return currentSentSpeed 54 55 56 def receivedBytes(): 57 """Receiving Bytes""" 58 return asyncore.receivedBytes 59 60 61 def downloadSpeed(): 62 """Getting download speed""" 63 # pylint: disable=global-statement 64 global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed 65 currentTimestamp = time.time() 66 if int(lastReceivedTimestamp) < int(currentTimestamp): 67 currentReceivedBytes = asyncore.receivedBytes 68 currentReceivedSpeed = int( 69 (currentReceivedBytes - lastReceivedBytes) / ( 70 currentTimestamp - lastReceivedTimestamp)) 71 lastReceivedBytes = currentReceivedBytes 72 lastReceivedTimestamp = currentTimestamp 73 return currentReceivedSpeed 74 75 76 def pendingDownload(): 77 """Getting pending downloads""" 78 return len(missingObjects) 79 80 81 def pendingUpload(): 82 """Getting pending uploads""" 83 # tmp = {} 84 # for connection in connectionpool.pool.inboundConnections.values() + \ 85 # connectionpool.pool.outboundConnections.values(): 86 # for k in connection.objectsNewToThem.keys(): 87 # tmp[k] = True 88 # This probably isn't the correct logic so it's disabled 89 # return len(tmp) 90 return 0