Add statistics reporting to the main program (untested).
[quix0rs-apt-p2p.git] / apt_p2p_Khashmir / stats.py
index 5afc17ab077112c2c9748eadfe17d698dc0eba43..aee9bbbe5f72ab0b18c01ffc36e23eba758c3326 100644 (file)
@@ -4,6 +4,8 @@
 from datetime import datetime, timedelta
 from StringIO import StringIO
 
+from util import byte_format
+
 class StatsLogger:
     """Store the statistics for the Khashmir DHT.
     
@@ -102,45 +104,47 @@ class StatsLogger:
         out.write('<h2>DHT Statistics</h2>\n')
         out.write("<table border='0' cellspacing='20px'>\n<tr>\n")
         out.write('<td>\n')
-        out.write("<table border='1' cellpadding='4px'>\n")
 
         # General
+        out.write("<table border='1' cellpadding='4px'>\n")
         out.write("<tr><th><h3>General</h3></th><th>Value</th></tr>\n")
         out.write("<tr title='Elapsed time since the DHT was started'><td>Up time</td><td>" + str(elapsed) + '</td></tr>\n')
         out.write("<tr title='Whether this node is reachable by other nodes'><td>Reachable</td><td>" + str(self.reachable) + '</td></tr>\n')
         out.write("</table>\n")
         out.write('</td><td>\n')
-        out.write("<table border='1' cellpadding='4px'>\n")
         
         # Routing
+        out.write("<table border='1' cellpadding='4px'>\n")
         out.write("<tr><th><h3>Routing Table</h3></th><th>Value</th></tr>\n")
         out.write("<tr title='The number of connected nodes'><td>Number of nodes</td><td>" + str(self.nodes) + '</td></tr>\n')
         out.write("<tr title='The estimated number of connected users in the entire DHT'><td>Total number of users</td><td>" + str(self.users) + '</td></tr>\n')
         out.write("</table>\n")
         out.write('</td><td>\n')
-        out.write("<table border='1' cellpadding='4px'>\n")
         
         # Database
+        out.write("<table border='1' cellpadding='4px'>\n")
         out.write("<tr><th><h3>Database</h3></th><th>Value</th></tr>\n")
         out.write("<tr title='Number of distinct keys in the database'><td>Keys</td><td>" + str(self.keys) + '</td></tr>\n')
         out.write("<tr title='Total number of values stored locally'><td>Values</td><td>" + str(self.values) + '</td></tr>\n')
         out.write("</table>\n")
         out.write("</td></tr><tr><td colspan='3'>\n")
+        
+        # Transport
         out.write("<table border='1' cellpadding='4px'>\n")
-        out.write("<tr><th><h3>Transport</h3></th><th>Packets</th><th>Bytes</th><th>Bytes/second</th></tr>\n")
+        out.write("<tr><th><h3>Transport</h3></th><th>Packets</th><th>Bytes</th><th>Speed</th></tr>\n")
         out.write("<tr title='Stats for packets received from the DHT'><td>Downloaded</td>")
         out.write('<td>' + str(self.downPackets) + '</td>')
-        out.write('<td>' + str(self.downBytes) + '</td>')
-        out.write('<td>%0.2f</td></tr>\n' % (self.downBytes / (elapsed.days*86400.0 + elapsed.seconds), ))
+        out.write('<td>' + byte_format(self.downBytes) + '</td>')
+        out.write('<td>' + byte_format(self.downBytes / (elapsed.days*86400.0 + elapsed.seconds)) + '/sec</td></tr>\n')
         out.write("<tr title='Stats for packets sent to the DHT'><td>Uploaded</td>")
         out.write('<td>' + str(self.upPackets) + '</td>')
-        out.write('<td>' + str(self.upBytes) + '</td>')
-        out.write('<td>%0.2f</td></tr>\n' % (self.upBytes / (elapsed.days*86400.0 + elapsed.seconds), ))
+        out.write('<td>' + byte_format(self.upBytes) + '</td>')
+        out.write('<td>' + byte_format(self.upBytes / (elapsed.days*86400.0 + elapsed.seconds)) + '/sec</td></tr>\n')
         out.write("</table>\n")
         out.write("</td></tr><tr><td colspan='3'>\n")
-        out.write("<table border='1' cellpadding='4px'>\n")
         
         # Actions
+        out.write("<table border='1' cellpadding='4px'>\n")
         out.write("<tr><th><h3>Actions</h3></th><th>Started</th><th>Sent</th><th>OK</th><th>Failed</th><th>Received</th><th>Error</th></tr>\n")
         actions = self.actions.keys()
         actions.sort()