Best Python code snippet using autotest_python
graphing_utils.py
Source:graphing_utils.py
...528 plot_info = MetricsPlot(query_dict, plot_type, inverted_series,529 normalize_to, drilldown_callback)530 figure, area_data = _create_metrics_plot_helper(plot_info, extra_text)531 return _create_image_html(figure, area_data, plot_info)532def _get_hostnames_in_bucket(hist_data, bucket):533 """\534 Get all the hostnames that constitute a particular bucket in the histogram.535 hist_data: list containing tuples of (hostname, pass_rate)536 bucket: tuple containing the (low, high) values of the target bucket537 """538 return [hostname for hostname, pass_rate in hist_data539 if bucket[0] <= pass_rate < bucket[1]]540def _create_qual_histogram_helper(plot_info, extra_text=None):541 """\542 Create a machine qualification histogram of the given data.543 plot_info: a QualificationHistogram544 extra_text: text to show at the upper-left of the graph545 TODO(showard): move much or all of this into methods on546 QualificationHistogram547 """548 cursor = readonly_connection.cursor()549 cursor.execute(plot_info.query)550 if not cursor.rowcount:551 raise NoDataError('query did not return any data')552 # Lists to store the plot data.553 # hist_data store tuples of (hostname, pass_rate) for machines that have554 # pass rates between 0 and 100%, exclusive.555 # no_tests is a list of machines that have run none of the selected tests556 # no_pass is a list of machines with 0% pass rate557 # perfect is a list of machines with a 100% pass rate558 hist_data = []559 no_tests = []560 no_pass = []561 perfect = []562 # Construct the lists of data to plot563 for hostname, total, good in cursor.fetchall():564 if total == 0:565 no_tests.append(hostname)566 continue567 if good == 0:568 no_pass.append(hostname)569 elif good == total:570 perfect.append(hostname)571 else:572 percentage = 100.0 * good / total573 hist_data.append((hostname, percentage))574 interval = plot_info.interval575 bins = range(0, 100, interval)576 if bins[-1] != 100:577 bins.append(bins[-1] + interval)578 figure, height = _create_figure(_SINGLE_PLOT_HEIGHT)579 subplot = figure.add_subplot(1, 1, 1)580 # Plot the data and get all the bars plotted581 _,_, bars = subplot.hist([data[1] for data in hist_data],582 bins=bins, align='left')583 bars += subplot.bar([-interval], len(no_pass),584 width=interval, align='center')585 bars += subplot.bar([bins[-1]], len(perfect),586 width=interval, align='center')587 bars += subplot.bar([-3 * interval], len(no_tests),588 width=interval, align='center')589 buckets = [(bin, min(bin + interval, 100)) for bin in bins[:-1]]590 # set the x-axis range to cover all the normal bins plus the three "special"591 # ones - N/A (3 intervals left), 0% (1 interval left) ,and 100% (far right)592 subplot.set_xlim(-4 * interval, bins[-1] + interval)593 subplot.set_xticks([-3 * interval, -interval] + bins + [100 + interval])594 subplot.set_xticklabels(['N/A', '0%'] +595 ['%d%% - <%d%%' % bucket for bucket in buckets] +596 ['100%'], rotation=90, size='small')597 # Find the coordinates on the image for each bar598 x = []599 y = []600 for bar in bars:601 x.append(bar.get_x())602 y.append(bar.get_height())603 f = subplot.plot(x, y, linestyle='None')[0]604 upper_left_coords = f.get_transform().transform(zip(x, y))605 bottom_right_coords = f.get_transform().transform(606 [(x_val + interval, 0) for x_val in x])607 # Set the title attributes608 titles = ['%d%% - <%d%%: %d machines' % (bucket[0], bucket[1], y_val)609 for bucket, y_val in zip(buckets, y)]610 titles.append('0%%: %d machines' % len(no_pass))611 titles.append('100%%: %d machines' % len(perfect))612 titles.append('N/A: %d machines' % len(no_tests))613 # Get the hostnames for each bucket in the histogram614 names_list = [_get_hostnames_in_bucket(hist_data, bucket)615 for bucket in buckets]616 names_list += [no_pass, perfect]617 if plot_info.filter_string:618 plot_info.filter_string += ' AND '619 # Construct the list of drilldown parameters to be passed when the user620 # clicks on the bar.621 params = []622 for names in names_list:623 if names:624 hostnames = ','.join(_quote(hostname) for hostname in names)625 hostname_filter = 'hostname IN (%s)' % hostnames626 full_filter = plot_info.filter_string + hostname_filter627 params.append({'type': 'normal',628 'filterString': full_filter})...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!