update viewer: added verbose messages plot options
authorMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 11 Dec 2009 15:59:32 +0000 (17:59 +0200)
committerMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 11 Dec 2009 15:59:32 +0000 (17:59 +0200)
viewer/p2p-nexteditor.py

index 74575dd..a26c3bc 100644 (file)
@@ -26,9 +26,6 @@ class PlotterThread(Thread):
                self.plot_figure = plot_figure
                self.options = options
                self.type = type
-               self.msg_types = {0:[0, "CHOKE"], 1:[0, "UNCHOKE"], 2:[0, "INTERESTED"], 3: [0, "NOT_INTERESTED"], \
-                 4:[0, "HAVE"], 5:[0, "BITFIELD"], 6:[0, "REQUEST"], 7:[0, "PIECE"], 8:[0, "CANCEL"], \
-                 9:[0, "DHT_PORT"]}
        
        def run(self):
                if self.type:
@@ -42,22 +39,33 @@ class PlotterThread(Thread):
                except DbObjException as e:
                        print e
                        return
+               msg_types = {0:[0, "CHOKE"], 1:[0, "UNCHOKE"], 2:[0, "INTERESTED"], 3: [0, "NOT_INTERESTED"], \
+                 4:[0, "HAVE"], 5:[0, "BITFIELD"], 6:[0, "REQUEST"], 7:[0, "PIECE"], 8:[0, "CANCEL"], \
+                 9:[0, "DHT_PORT"]}
                restrictions = [];
                date = julianToDatetime(cs.start_time)
-               if self.options[3]:
-                       date_start = self.time_from_date(date, self.options[3])            
+               if self.options[0]:
+                       date_start = self.time_from_date(date, self.options[0])            
                        restrictions.append(('timestamp', float(datetimeToJulian(date_start)), 'gte'))
                
-               if self.options[4]:
-                       date_stop = self.time_from_date(date, self.options[4])
+               if self.options[1]:
+                       date_stop = self.time_from_date(date, self.options[1])
                        restrictions.append(('timestamp', float(datetimeToJulian(date_stop)), 'lte'))
                
+               if self.options[2] == "In":
+                       restrictions.append(('direction',  '0', 'eq'))
+               elif self.options[2] == "Out":
+                       restrictions.append(('direction',  '1', 'eq'))
+                       
+               if self.options[3]:
+                       restrictions.append(('peer_ip', self.options[3], 'eq'))
+               
                vbmc = VerboseMessageCollection(self.dbname, self.cs_id, restrictions)
                while True:
                        vbm = vbmc.getNext()
                        if not vbm:
                                break
-                       self.msg_types[vbm.message_type][0] = self.msg_types[vbm.message_type][0] + 1
+                       msg_types[vbm.message_type][0] = msg_types[vbm.message_type][0] + 1
                #~ vbmc = VerboseMessageCollection(self.dbname, self.cs_id, restrictions)
                #~ vbmcit = vbmc.getIter()
                #~ while True:
@@ -67,7 +75,7 @@ class PlotterThread(Thread):
                                #~ break
                        #~ self.msg_types[vbm.message_type][0] = self.msg_types[vbm.message_type][0] + 1
                        #~ total = total+1
-               self.plot_figure(self.cs_id, self.axes_id, self.msg_types)
+               self.plot_figure(self.cs_id, self.axes_id, msg_types)
        
        def plot_graph(self):
                try:
@@ -92,7 +100,7 @@ class PlotterThread(Thread):
                if self.options[4]:
                        date_stop = self.time_from_date(date2, self.options[4])
                        restrictions.append(('timestamp', float(datetimeToJulian(date_stop)), 'lte'))
-                 
+               
                stmc = StatusMessageCollection(self.dbname, self.cs_id, restrictions)
                while True:
                        stm = stmc.getNext()
@@ -165,6 +173,8 @@ class ControlPanel(HasTraits):
        upload_speed = Bool()
        acceleration = Bool()
        stats = Bool()
+       direction = Enum("Both", "In", "Out")
+       ip = Str()
        plot = Button()
        
        
@@ -213,8 +223,12 @@ class ControlPanel(HasTraits):
                                                HGroup('download_speed',Item('30'), 'upload_speed'),
                                                HGroup(
                                                        Item('acceleration'),
-                                                       Item('58'),
+                                                       Item('57'),
                                                        Item('stats', label="Statistics")),
+                                               HGroup(
+                                                       Item('direction'),
+                                                       Item('28'),
+                                                       Item('ip', label="Peer IP"), enabled_when='object.stats'),
                                                show_border=True, label='Plotting Options'),
                                                Item('10'),
                                                Item('plot', show_label=False),
@@ -292,21 +306,23 @@ class ControlPanel(HasTraits):
        def _plot_fired(self):
                if len(self.cs_id)  and (self.download_speed or self.upload_speed):
                        dbfile = dbpath + self.dbname + dbextension
-                       options = [self.acceleration, self.download_speed, self.upload_speed, self.time_start, self.time_stop]
                        self.figure.clf();
+                       options = [self.acceleration, self.download_speed, self.upload_speed, self.time_start, self.time_stop]
                        if not self.stats:
                                axes = self.figure.add_subplot(1,1,1)
                                axes.grid(linestyle='dashed')
                                self.figure.subplots_adjust(left=0.03, right=0.97, top=0.98)
                                PlotterThread(dbfile, self.cs_id, 0, self.plot_figure, options, 1).start()
                        else:
+                               options2 = [self.time_start, self.time_stop, self.direction, self.ip]
                                axes = self.figure.add_subplot(2,1,2)
                                axes.grid(linestyle='dashed')
                                axes = self.figure.add_subplot(2,2,1)
+                               axes.grid(alpha=0.3)
                                axes = self.figure.add_subplot(2,2,2)
-                               axes.grid(linestyle='dashed')
-                               self.figure.subplots_adjust(left=0.03, right=0.97, top=0.98, hspace=0.07, wspace=0.6)
-                               PlotterThread(dbfile, self.cs_id, 1, self.plot_stats_figure, options, 0).start()
+                               axes.grid(linestyle='dashed', alpha=0.3)
+                               self.figure.subplots_adjust(left=0.03, right=0.97, top=0.98, hspace=0.07, wspace=0.7)
+                               PlotterThread(dbfile, self.cs_id, 1, self.plot_stats_figure, options2, 0).start()
                                PlotterThread(dbfile, self.cs_id, 0, self.plot_figure, options, 1).start()
        
        def _views_changed(self, new):
@@ -368,14 +384,16 @@ class ControlPanel(HasTraits):
                        ticks.append(list[1])
                axes_list = self.figure.get_axes()
                axes = axes_list[axes_id]
-               axes.pie(vals, labels=ticks, explode=explode, colors = ['b', 'c', 'g', 'm', 'r', 'y'], autopct='%1.1f%%', shadow=False)
+               patches, texts, autotexts = axes.pie(vals, labels=ticks, explode=explode, colors = ['b', 'c', 'g', 'm', 'r', 'y'], autopct='%1.1f%%', shadow=False)
+               for patch in patches:
+                       patch.set_alpha(0.6)
                
                axes = axes_list[axes_id+1]
-               axes.barh(pos, vals, color = ['b', 'c', 'g', 'm', 'r', 'y'], height=0.3, align='center')
-               for t in axes.get_xticklabels():
-                       t.set_fontsize(5.)
+               axes.barh(pos, vals, color = ['b', 'c', 'g', 'm', 'r', 'y'], height=0.3, align='center', alpha=0.6)
                axes.set_yticks(pos)
                axes.set_yticklabels(ticks)
+               for i in range(len(vals)):
+                       axes.text(vals[i], pos[i], str(vals[i]), fontsize="large", weight='semibold', ha='left', va='center', family = 'sans-serif')
                
                wx.CallAfter(self.figure.canvas.draw)