def write_contents(self, root): """ Rendering of tables and figures for address statistics. """ # get data from statistics module iaw period selection stats_instance = self.get_address_stats() self.write_memory_statistics(root, stats_instance.mem) self.write_processor_statistics(root, stats_instance.cpu) self.write_network_statistics(root, stats_instance.io) # write CPU / Memory / Network plots try: from supvisors.plot import StatisticsPlot # build CPU image cpu_img = StatisticsPlot() cpu_img.add_plot('CPU #{}'.format(self.cpu_id_to_string(HostAddressView.cpu_id_stats)), '%', stats_instance.cpu[HostAddressView.cpu_id_stats]) cpu_img.export_image(address_cpu_image) # build Memory image mem_img = StatisticsPlot() mem_img.add_plot('MEM', '%', stats_instance.mem) mem_img.export_image(address_mem_image) # build Network image if HostAddressView.interface_stats: io_img = StatisticsPlot() io_img.add_plot('{} recv'.format(HostAddressView.interface_stats), 'kbits/s', stats_instance.io[HostAddressView.interface_stats][0]) io_img.add_plot('{} sent'.format(HostAddressView.interface_stats), 'kbits/s', stats_instance.io[HostAddressView.interface_stats][1]) io_img.export_image(address_io_image) except ImportError: self.logger.warn("matplotlib module not found")
def test_plot(self): """ Test a simple plot. Complex to test anything. Just check that there is no exception. """ from supvisors.plot import StatisticsPlot from supvisors.viewimage import StatsImage plot = StatisticsPlot() self.assertEqual({}, plot.ydata) # add series of data plot.add_plot('dummy_title_1', 'unit_1', [1, 2, 3]) plot.add_plot('dummy_title_2', 'unit_2', [10, 20, 30]) self.assertDictEqual( { ('dummy_title_1', 'unit_1'): [1, 2, 3], ('dummy_title_2', 'unit_2'): [10, 20, 30] }, plot.ydata) # export image in buffer contents = StatsImage() plot.export_image(contents) # test that result is a PNG file self.assertEqual('png', imghdr.what('', h=contents.contents.getvalue()))
def write_process_statistics(self, root): """ Display detailed statistics about the selected process. """ stats_elt = root.findmeld('pstats_div_mid') # get data from statistics module iaw period selection if ViewHandler.namespec_stats: nbcores, proc_stats = self.get_process_stats( ViewHandler.namespec_stats) if proc_stats and (len(proc_stats[0]) > 0 or \ len(proc_stats[1]) > 0): # set titles elt = stats_elt.findmeld('process_h_mid') elt.content(ViewHandler.namespec_stats) # set CPU statistics if len(proc_stats[0]) > 0: avg, rate, (a, b), dev = get_stats(proc_stats[0]) # print last CPU value of process elt = stats_elt.findmeld('pcpuval_td_mid') if rate is not None: self.set_slope_class(elt, rate) cpuvalue = proc_stats[0][-1] if not self.supvisors.options.stats_irix_mode: cpuvalue /= nbcores elt.content('{:.2f}%'.format(cpuvalue)) # set mean value elt = stats_elt.findmeld('pcpuavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope value between last 2 values elt = stats_elt.findmeld('pcpuslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = stats_elt.findmeld('pcpudev_td_mid') elt.content('{:.2f}'.format(dev)) # set MEM statistics if len(proc_stats[1]) > 0: avg, rate, (a, b), dev = get_stats(proc_stats[1]) # print last MEM value of process elt = stats_elt.findmeld('pmemval_td_mid') if rate is not None: self.set_slope_class(elt, rate) elt.content('{:.2f}%'.format(proc_stats[1][-1])) # set mean value elt = stats_elt.findmeld('pmemavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope value between last 2 values elt = stats_elt.findmeld('pmemslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = stats_elt.findmeld('pmemdev_td_mid') elt.content('{:.2f}'.format(dev)) # write CPU / Memory plots try: from supvisors.plot import StatisticsPlot # build CPU image cpu_img = StatisticsPlot() cpu_img.add_plot('CPU', '%', proc_stats[0]) cpu_img.export_image(process_cpu_image) # build Memory image mem_img = StatisticsPlot() mem_img.add_plot('MEM', '%', proc_stats[1]) mem_img.export_image(process_mem_image) except ImportError: self.logger.warn("matplotlib module not found") else: if ViewHandler.namespec_stats: self.logger.warn( 'unselect Process Statistics for {}'.format( ViewHandler.namespec_stats)) ViewHandler.namespec_stats = '' # remove stats part if empty if not ViewHandler.namespec_stats: stats_elt.replace('')
def write_process_statistics(self, root): """ Display detailed statistics about the selected process. """ stats_elt = root.findmeld('pstats_div_mid') # get data from statistics module iaw period selection if ViewHandler.namespec_stats: nbcores, proc_stats = self.get_process_stats( ViewHandler.namespec_stats) if proc_stats and (len(proc_stats[0]) > 0 or \ len(proc_stats[1]) > 0): # set titles elt = stats_elt.findmeld('process_h_mid') elt.content(ViewHandler.namespec_stats) # set CPU statistics if len(proc_stats[0]) > 0: avg, rate, (a, b), dev = get_stats(proc_stats[0]) # print last CPU value of process elt = stats_elt.findmeld('pcpuval_td_mid') if rate is not None: self.set_slope_class(elt, rate) cpuvalue = proc_stats[0][-1] if not self.supvisors.options.stats_irix_mode: cpuvalue /= nbcores elt.content('{:.2f}%'.format(cpuvalue)) # set mean value elt = stats_elt.findmeld('pcpuavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope value between last 2 values elt = stats_elt.findmeld('pcpuslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = stats_elt.findmeld('pcpudev_td_mid') elt.content('{:.2f}'.format(dev)) # set MEM statistics if len(proc_stats[1]) > 0: avg, rate, (a, b), dev = get_stats(proc_stats[1]) # print last MEM value of process elt = stats_elt.findmeld('pmemval_td_mid') if rate is not None: self.set_slope_class(elt, rate) elt.content('{:.2f}%'.format(proc_stats[1][-1])) # set mean value elt = stats_elt.findmeld('pmemavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope value between last 2 values elt = stats_elt.findmeld('pmemslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = stats_elt.findmeld('pmemdev_td_mid') elt.content('{:.2f}'.format(dev)) # write CPU / Memory plots try: from supvisors.plot import StatisticsPlot # build CPU image cpu_img = StatisticsPlot() cpu_img.add_plot('CPU', '%', proc_stats[0]) cpu_img.export_image(process_cpu_image) # build Memory image mem_img = StatisticsPlot() mem_img.add_plot('MEM', '%', proc_stats[1]) mem_img.export_image(process_mem_image) except ImportError: self.logger.warn("matplotlib module not found") else: if ViewHandler.namespec_stats: self.logger.warn('unselect Process Statistics for {}' .format(ViewHandler.namespec_stats)) ViewHandler.namespec_stats = '' # remove stats part if empty if not ViewHandler.namespec_stats: stats_elt.replace('')