def export_image(self, image_contents): """ Write curves into a PNG image. """ if self.ydata: # calculate and apply max range all_ydata = [] map(all_ydata.extend, [ydata for ydata in self.ydata.values()]) plt.ylim(self.get_range(all_ydata)) # create plots for each series of data for i, ((title, unit), ydata) in enumerate(self.ydata.items()): # create X axis xdata = [x for x in range(len(ydata))] # get additional statistics avg, rate, (a, b), dev = get_stats(ydata) # plot the data dataLine, = plt.plot(xdata, ydata, label=title) plotColor = dataLine.get_color() # plot the mean line avg_data = [avg for _ in ydata] meanLine, = plt.plot(xdata, avg_data, label='Mean: {:.2f}{}'.format(avg, unit), linestyle='--', color=plotColor) if a is not None: # plot the linear regression plt.plot([xdata[0], xdata[-1]], [a * xdata[0] + b, a * xdata[-1] + b], linestyle=':', color=plotColor) if dev is not None: # plot the standard deviation plt.fill_between(xdata, avg-dev, avg+dev, facecolor=plotColor, alpha=.3) # create the legend legend = plt.legend(handles=[dataLine, meanLine], loc=i+1, fontsize='small', fancybox=True, shadow=True) # add the legend to the current axes plt.gca().add_artist(legend) # save image to internal memory buffer plt.savefig(image_contents.new_image(), dpi=80, bbox_inches='tight', format='png') # reset yData self.ydata = {} # close plot plt.close()
def write_network_statistics(self, root, io_stats): """ Rendering of the network statistics. """ if not HostAddressView.interface_stats: # choose first interface name by default address_stats = self.get_address_stats() io_stats = address_stats.io HostAddressView.interface_stats = next(iter(io_stats.keys())) # display io statistics flatten_io_stats = [(intf, lst) for intf, lsts in io_stats.items() for lst in lsts] iterator = root.findmeld('intf_tr_mid').repeat(flatten_io_stats) rowspan, shaded_tr = True, False for tr_element, (intf, single_io_stats) in iterator: selected_tr = False # set interface cell rowspan elt = tr_element.findmeld('intf_td_mid') if rowspan: elt.attrib['rowspan'] = "2" # set interface name elt = elt.findmeld('intf_a_mid') if HostAddressView.interface_stats == intf: selected_tr = True elt.attrib['class'] = 'button off active' else: elt.attributes(href='{}?intf={}'.format(HostAddressView.page_name, intf)) elt.content(intf) else: if HostAddressView.interface_stats == intf: selected_tr = True elt.replace('') # set interface direction elt = tr_element.findmeld('intfrxtx_td_mid') elt.content('Rx' if rowspan else 'Tx') if len(single_io_stats) > 0: avg, rate, (a, b), dev = get_stats(single_io_stats) # set last value elt = tr_element.findmeld('intfval_td_mid') if rate is not None: self.set_slope_class(elt, rate) elt.content('{:.2f}'.format(single_io_stats[-1])) # set mean value elt = tr_element.findmeld('intfavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope of linear regression elt = tr_element.findmeld('intfslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = tr_element.findmeld('intfdev_td_mid') elt.content('{:.2f}'.format(dev)) if selected_tr: tr_element.attrib['class'] = 'selected' elif shaded_tr: tr_element.attrib['class'] = 'shaded' if not rowspan: shaded_tr = not shaded_tr rowspan = not rowspan
def test_statistics(self): """ Test the statistics function. """ from supvisors.utils import get_stats ydata = [2, 3, 4, 5, 6] avg, rate, (a, b), dev = get_stats(ydata) self.assertAlmostEqual(4, avg) self.assertAlmostEqual(20, rate) self.assertAlmostEqual(1, a) self.assertAlmostEqual(2, b) self.assertAlmostEqual(math.sqrt(2), dev)
def test_statistics(self): """ Test the statistics function. """ from supvisors.utils import get_stats ydata = [2, 3, 4, 5, 6] avg, rate, (a, b), dev = get_stats(ydata) self.assertAlmostEqual(4, avg) self.assertAlmostEqual(20, rate) self.assertAlmostEqual(1, a) self.assertAlmostEqual(2, b) self.assertAlmostEqual(math.sqrt(2), dev)
def write_memory_statistics(self, root, mem_stats): """ Rendering of the memory statistics. """ if len(mem_stats) > 0: # get additional statistics avg, rate, (a, b), dev = get_stats(mem_stats) # set last value elt = root.findmeld('memval_td_mid') if rate is not None: self.set_slope_class(elt, rate) elt.content('{:.2f}'.format(mem_stats[-1])) # set mean value elt = root.findmeld('memavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope of linear regression elt = root.findmeld('memslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = root.findmeld('memdev_td_mid') elt.content('{:.2f}'.format(dev))
def write_processor_statistics(self, root, cpu_stats): """ Rendering of the processor statistics. """ iterator = root.findmeld('cpu_tr_mid').repeat(cpu_stats) shaded_tr = False for idx, (tr_element, single_cpu_stats) in enumerate(iterator): selected_tr = False # set CPU id elt = tr_element.findmeld('cpunum_a_mid') if HostAddressView.cpu_id_stats == idx: selected_tr = True elt.attrib['class'] = 'button off active' else: elt.attributes(href='{}?idx={}'.format(HostAddressView.page_name, idx)) elt.content('cpu#{}'.format(idx-1 if idx > 0 else 'all')) if len(single_cpu_stats) > 0: avg, rate, (a, b), dev = get_stats(single_cpu_stats) # set last value with instant slope elt = tr_element.findmeld('cpuval_td_mid') if rate is not None: self.set_slope_class(elt, rate) elt.content('{:.2f}'.format(single_cpu_stats[-1])) # set mean value elt = tr_element.findmeld('cpuavg_td_mid') elt.content('{:.2f}'.format(avg)) if a is not None: # set slope of linear regression elt = tr_element.findmeld('cpuslope_td_mid') elt.content('{:.2f}'.format(a)) if dev is not None: # set standard deviation elt = tr_element.findmeld('cpudev_td_mid') elt.content('{:.2f}'.format(dev)) if selected_tr: tr_element.attrib['class'] = 'selected' elif shaded_tr: tr_element.attrib['class'] = 'shaded' shaded_tr = not shaded_tr
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('')