/ src / network / stats.py.bak
stats.py.bak
 1  """
 2  Network statistics
 3  """
 4  import time
 5  
 6  import asyncore_pollchoose as asyncore
 7  import connectionpool
 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  
19  def connectedHostsList():
20      """List of all the connected hosts"""
21      return connectionpool.pool.establishedConnections()
22  
23  
24  def sentBytes():
25      """Sending Bytes"""
26      return asyncore.sentBytes
27  
28  
29  def uploadSpeed():
30      """Getting upload speed"""
31      # pylint: disable=global-statement
32      global lastSentTimestamp, lastSentBytes, currentSentSpeed
33      currentTimestamp = time.time()
34      if int(lastSentTimestamp) < int(currentTimestamp):
35          currentSentBytes = asyncore.sentBytes
36          currentSentSpeed = int(
37              (currentSentBytes - lastSentBytes) / (
38                  currentTimestamp - lastSentTimestamp))
39          lastSentBytes = currentSentBytes
40          lastSentTimestamp = currentTimestamp
41      return currentSentSpeed
42  
43  
44  def receivedBytes():
45      """Receiving Bytes"""
46      return asyncore.receivedBytes
47  
48  
49  def downloadSpeed():
50      """Getting download speed"""
51      # pylint: disable=global-statement
52      global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
53      currentTimestamp = time.time()
54      if int(lastReceivedTimestamp) < int(currentTimestamp):
55          currentReceivedBytes = asyncore.receivedBytes
56          currentReceivedSpeed = int(
57              (currentReceivedBytes - lastReceivedBytes) / (
58                  currentTimestamp - lastReceivedTimestamp))
59          lastReceivedBytes = currentReceivedBytes
60          lastReceivedTimestamp = currentTimestamp
61      return currentReceivedSpeed
62  
63  
64  def pendingDownload():
65      """Getting pending downloads"""
66      return len(missingObjects)
67  
68  
69  def pendingUpload():
70      """Getting pending uploads"""
71      # tmp = {}
72      # for connection in connectionpool.pool.inboundConnections.values() + \
73      #         connectionpool.pool.outboundConnections.values():
74      #     for k in connection.objectsNewToThem.keys():
75      #         tmp[k] = True
76      # This probably isn't the correct logic so it's disabled
77      # return len(tmp)
78      return 0