[tor-commits] [arm/master] Initial commit for bandwidth graphs.

atagar at torproject.org atagar at torproject.org
Tue Jun 14 02:34:22 UTC 2011


commit f874c839fdf7bee393492bd6b38a5e468b1b1a06
Author: Kamran Riaz Khan <krkhan at inspirated.com>
Date:   Tue Jun 14 02:41:29 2011 +0500

    Initial commit for bandwidth graphs.
---
 src/gui/controller.py              |   51 +++++++++++++++++++++++++++++-
 src/gui/graphing/__init__.py       |    6 +++
 src/gui/graphing/bandwidthStats.py |   26 +++++++++++++++
 src/gui/graphing/graphStats.py     |   61 ++++++++++++++++++++++++++++++++++++
 4 files changed, 143 insertions(+), 1 deletions(-)

diff --git a/src/gui/controller.py b/src/gui/controller.py
index 0e444d3..91b9a03 100644
--- a/src/gui/controller.py
+++ b/src/gui/controller.py
@@ -1,14 +1,36 @@
+import gobject
+import gtk
+
 from util import torTools
+from gui.graphing import graphStats, bandwidthStats
 
-import gtk
+from cagraph.ca_graph import CaGraph
+from cagraph.axis.xaxis import CaGraphXAxis
+from cagraph.axis.yaxis import CaGraphYAxis
+from cagraph.ca_graph_grid import CaGraphGrid
+from cagraph.series.area import CaGraphSeriesArea
+
+gobject.threads_init()
 
 class GuiController:
   def __init__(self):
     filename = 'src/gui/arm.xml'
+
     self.builder = gtk.Builder()
     self.builder.add_from_file(filename)
     self.builder.connect_signals(self)
 
+    # for custom widgets not present in builder xml
+    self.widgets = {}
+
+    self._pack_graph_widget('primary')
+    self._pack_graph_widget('secondary')
+
+    self.bwStats = bandwidthStats.BandwidthStats(self.widgets)
+
+    gobject.timeout_add(1000, self.bwStats.draw_graph, 'primary')
+    gobject.timeout_add(1000, self.bwStats.draw_graph, 'secondary')
+
   def run(self):
     window = self.builder.get_object('window_main')
 
@@ -23,6 +45,33 @@ class GuiController:
   def on_window_main_delete_event(self, widget, data=None):
     gtk.main_quit()
 
+  def _pack_graph_widget(self, name):
+    graph = CaGraph()
+    placeholder = self.builder.get_object('placeholder_graph_%s' % name)
+    placeholder.pack_start(graph)
+
+    xaxis = CaGraphXAxis(graph)
+    yaxis = CaGraphYAxis(graph)
+
+    xaxis.min = 0
+    xaxis.max = graphStats.GRAPH_INTERVAL - 1
+    xaxis.axis_style.draw_labels = False
+
+    graph.axiss.append(xaxis)
+    graph.axiss.append(yaxis)
+
+    series = CaGraphSeriesArea(graph, 0, 1)
+
+    line_colors = {'primary' : (1.0, 0.0, 1.0, 1.0), 'secondary' : (0.0, 1.0, 0.0, 1.0)}
+    fill_colors = {'primary' : (1.0, 0.0, 1.0, 0.3), 'secondary' : (0.0, 1.0, 0.0, 0.3)}
+    series.style.line_color = line_colors[name]
+    series.style.fill_color = fill_colors[name]
+
+    graph.seriess.append(series)
+    graph.grid = CaGraphGrid(graph, 0, 1)
+
+    self.widgets['graph_%s' % name] = graph
+
 def startGui():
   controller = GuiController()
   controller.run()
diff --git a/src/gui/graphing/__init__.py b/src/gui/graphing/__init__.py
new file mode 100644
index 0000000..acc8645
--- /dev/null
+++ b/src/gui/graphing/__init__.py
@@ -0,0 +1,6 @@
+"""
+Graphs.
+"""
+
+__all__ = ["graphStats", "bandwidthStats"]
+
diff --git a/src/gui/graphing/bandwidthStats.py b/src/gui/graphing/bandwidthStats.py
new file mode 100644
index 0000000..304574e
--- /dev/null
+++ b/src/gui/graphing/bandwidthStats.py
@@ -0,0 +1,26 @@
+"""
+Bandwidth monitors.
+"""
+
+import sys
+
+import gobject
+import gtk
+
+from TorCtl import TorCtl
+from gui.graphing import graphStats
+from util import torTools
+
+class BandwidthStats(graphStats.GraphStats):
+  def __init__(self, widgets):
+    graphStats.GraphStats.__init__(self, widgets)
+
+    conn = torTools.getConn()
+    if not conn.isAlive():
+      conn.init()
+    conn.setControllerEvents(["BW"])
+    conn.addEventListener(self)
+
+  def bandwidth_event(self, event):
+    self._processEvent(event.read / 1024.0, event.written / 1024.0)
+
diff --git a/src/gui/graphing/graphStats.py b/src/gui/graphing/graphStats.py
new file mode 100644
index 0000000..5037c39
--- /dev/null
+++ b/src/gui/graphing/graphStats.py
@@ -0,0 +1,61 @@
+"""
+Base class for implementing graphing functionality.
+"""
+
+import random
+import sys
+
+from collections import deque
+
+import gobject
+import gtk
+
+from TorCtl import TorCtl
+from util import torTools
+
+GRAPH_INTERVAL = 30
+
+class GraphStats(TorCtl.PostEventListener):
+  def __init__(self, widgets):
+    TorCtl.PostEventListener.__init__(self)
+
+    self.widgets = widgets
+
+    self.data = {
+        'primary'   : deque([0] * GRAPH_INTERVAL),
+        'secondary' : deque([0] * GRAPH_INTERVAL)}
+
+  def get_graph_data(self, name):
+    packed_data = []
+
+    for (index, value) in enumerate(self.data[name]):
+      packed_data.append((index, value))
+
+    return packed_data
+
+  def is_graph_data_zero(self, name):
+    data = self.data[name]
+    return len(data) == map(int, data).count(0)
+
+  def draw_graph(self, name):
+    graph = self.widgets['graph_%s' % name]
+    data = self.get_graph_data(name)
+
+    if self.is_graph_data_zero(name):
+      graph.seriess[0].data = []
+    else:
+      graph.seriess[0].data = data
+
+      for (index, axis) in enumerate(graph.axiss):
+        if axis.type != 'xaxis':
+          graph.auto_set_yrange(index)
+
+    graph.queue_draw()
+    return True
+
+  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
+





More information about the tor-commits mailing list