commit 40174345257913b871b87e8e513b911b0ec07205 Author: Kamran Riaz Khan krkhan@inspirated.com Date: Sat Jun 18 19:36:03 2011 +0500
Implement bandwidth stats in graph labels. --- src/gui/arm.xml | 10 ++++---- src/gui/controller.py | 4 ++- src/gui/graphing/bandwidthStats.py | 37 ++++++++++++++++++++++++---- src/gui/graphing/graphStats.py | 47 +++++++++++++++++++++++++++++------ 4 files changed, 79 insertions(+), 19 deletions(-)
diff --git a/src/gui/arm.xml b/src/gui/arm.xml index 23230b6..e62c622 100644 --- a/src/gui/arm.xml +++ b/src/gui/arm.xml @@ -303,7 +303,7 @@ <child> <object class="GtkLabel" id="label_graph_top"> <property name="visible">True</property> - <property name="label" translatable="yes">Limit: 40 Mb/s, Burst: 80 Mb/s</property> + <property name="label" translatable="yes">Limit: 0 B/s, Burst: 0 B/s</property> </object> <packing> <property name="expand">False</property> @@ -321,7 +321,7 @@ <child> <object class="GtkLabel" id="label_graph_primary_top"> <property name="visible">True</property> - <property name="label" translatable="yes">Download (0.0 b/sec)</property> + <property name="label" translatable="yes">Download: 0.00 Kb/s</property> </object> <packing> <property name="expand">False</property> @@ -344,7 +344,7 @@ <child> <object class="GtkLabel" id="label_graph_primary_bottom"> <property name="visible">True</property> - <property name="label" translatable="yes">avg: 3.5 Kb/sec, total: 2.0 MB</property> + <property name="label" translatable="yes">avg: 0.00 b/s, total: 0.00 B</property> </object> <packing> <property name="expand">False</property> @@ -364,7 +364,7 @@ <child> <object class="GtkLabel" id="label_graph_secondary_top"> <property name="visible">True</property> - <property name="label" translatable="yes">Upload (0.0 b/sec)</property> + <property name="label" translatable="yes">Upload: 0.00 Kb/s</property> </object> <packing> <property name="expand">False</property> @@ -387,7 +387,7 @@ <child> <object class="GtkLabel" id="label_graph_secondary_bottom"> <property name="visible">True</property> - <property name="label" translatable="yes">avg: 3.5 Kb/sec, total: 2.0 MB</property> + <property name="label" translatable="yes">avg: 0.00 KB/s, total: 0.00 MB</property> </object> <packing> <property name="expand">False</property> diff --git a/src/gui/controller.py b/src/gui/controller.py index 91b9a03..7f85b9a 100644 --- a/src/gui/controller.py +++ b/src/gui/controller.py @@ -26,10 +26,12 @@ class GuiController: self._pack_graph_widget('primary') self._pack_graph_widget('secondary')
- self.bwStats = bandwidthStats.BandwidthStats(self.widgets) + self.bwStats = bandwidthStats.BandwidthStats(self.builder, self.widgets)
gobject.timeout_add(1000, self.bwStats.draw_graph, 'primary') gobject.timeout_add(1000, self.bwStats.draw_graph, 'secondary') + gobject.timeout_add(1000, self.bwStats.update_labels, 'primary') + gobject.timeout_add(1000, self.bwStats.update_labels, 'secondary')
def run(self): window = self.builder.get_object('window_main') diff --git a/src/gui/graphing/bandwidthStats.py b/src/gui/graphing/bandwidthStats.py index 304574e..a8278d8 100644 --- a/src/gui/graphing/bandwidthStats.py +++ b/src/gui/graphing/bandwidthStats.py @@ -9,18 +9,45 @@ import gtk
from TorCtl import TorCtl from gui.graphing import graphStats -from util import torTools +from util import uiTools, torTools
class BandwidthStats(graphStats.GraphStats): - def __init__(self, widgets): - graphStats.GraphStats.__init__(self, widgets) + def __init__(self, builder, widgets): + graphStats.GraphStats.__init__(self, builder, widgets)
conn = torTools.getConn() if not conn.isAlive(): conn.init() - conn.setControllerEvents(["BW"]) + + conn.setControllerEvents(["BW", "NEWDESC"]) conn.addEventListener(self)
+ self.new_desc_event(None) + + def new_desc_event(self, event): + conn = torTools.getConn() + + if not conn.isAlive(): + return + + bwRate = conn.getMyBandwidthRate() + bwBurst = conn.getMyBandwidthBurst() + bwObserved = conn.getMyBandwidthObserved() + bwMeasured = conn.getMyBandwidthMeasured() + + if bwRate and bwBurst: + bwRateLabel = uiTools.getSizeLabel(bwRate, 1, False, isBytes=False) + bwBurstLabel = uiTools.getSizeLabel(bwBurst, 1, False, isBytes=False) + + msg = "Limit: %s/s, Burst: %s/s" % (bwRateLabel, bwBurstLabel) + label = self.builder.get_object('label_graph_top') + label.set_text(msg) + def bandwidth_event(self, event): - self._processEvent(event.read / 1024.0, event.written / 1024.0) + self._processEvent(event.read, event.written) + + msg = 'Download: %s/s' % uiTools.getSizeLabel(event.read, 2, isBytes=False) + self.update_header('primary', msg) + msg = 'Upload: %s/s' % uiTools.getSizeLabel(event.written, 2, isBytes=False) + self.update_header('secondary', msg)
diff --git a/src/gui/graphing/graphStats.py b/src/gui/graphing/graphStats.py index 5037c39..c000ff4 100644 --- a/src/gui/graphing/graphStats.py +++ b/src/gui/graphing/graphStats.py @@ -11,19 +11,23 @@ import gobject import gtk
from TorCtl import TorCtl -from util import torTools +from util import uiTools, torTools
GRAPH_INTERVAL = 30
class GraphStats(TorCtl.PostEventListener): - def __init__(self, widgets): + def __init__(self, builder, widgets): TorCtl.PostEventListener.__init__(self)
+ self.builder = builder self.widgets = widgets
self.data = { - 'primary' : deque([0] * GRAPH_INTERVAL), - 'secondary' : deque([0] * GRAPH_INTERVAL)} + 'primary' : deque([0.0] * GRAPH_INTERVAL), + 'secondary' : deque([0.0] * GRAPH_INTERVAL)} + + self.total = {'primary': 0.0, 'secondary' : 0.0} + self.ticks = {'primary': 0, 'secondary' : 0}
def get_graph_data(self, name): packed_data = [] @@ -51,11 +55,38 @@ class GraphStats(TorCtl.PostEventListener): graph.auto_set_yrange(index)
graph.queue_draw() + return True
+ def update_labels(self, name): + avg = 0 + + try: + avg = self.total[name] / float(self.ticks[name]) + except ZeroDivisionError: + pass + + msg = "avg: %s/s, total: %s" % (uiTools.getSizeLabel(avg, 2, isBytes=False), + uiTools.getSizeLabel(self.total[name], 2)) + label = self.builder.get_object('label_graph_%s_bottom' % name) + label.set_text(msg) + + return True + + def update_header(self, name, msg): + label = self.builder.get_object('label_graph_%s_top' % name) + label.set_text(msg) + def _processEvent(self, primary, secondary): - self.data['primary'].rotate(1) - self.data['primary'][0] = primary - self.data['secondary'].rotate(1) - self.data['secondary'][0] = secondary + values = {'primary' : primary, 'secondary' : secondary} + + for name in ('primary', 'secondary'): + # right shift and store kbytes in left-most location + self.data[name].rotate(1) + self.data[name][0] = values[name] / 1024 + + self.total[name] = self.total[name] + values[name] + + if values[name] > 0: + self.ticks[name] = self.ticks[name] + 1