基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析

接前文  基于jira的缺陷自动化报表分析 (九)matplotlib 横向叠加柱状图:缺陷人员、模块、原因、引入阶段分析

还剩以下5种图形未实现:缺陷引入数量与加权数量对比分布、缺陷发现数量与加权数量对比分布、缺陷发现数量与无效数量对比分布、缺陷修复数量与被打回数量对比分布、缺陷发现与修复数量对比分布,我们需要绘制一个竖向并列叠加柱状图解决以上五种缺陷分析图表

 

一、绘图类

1、继续复制黏贴pass

# coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
 
plt.rcParams['font.sans-serif'] = ['simhei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
 
 
class Draw:
    """
    绘图
    """
    def __init__(self, reportpath):
        self.reportpath = reportpath
 
    def drawing_cake(self, amounts, lebals, project_name, title):
        """
        绘制饼状图
        """
        pass
 
    def drawing_linechart_more(self, project_name, title, x_lebal, *y_labels, **kw):
        """
        绘制折线图:单线条或多线条
        """
        pass

    def drawing_histogram_verlay(self, title, x_lables, *args, **kwargs):

        """ 绘制横向叠加柱状图:可支持叠加任意多次 """

        pass

 

2、开始编写绘制竖向并列可叠加柱状图

def drawing_histogram_tied(self, project_name, title, name_list, total_accounts, *num_lists, **kw):
    """
    绘制并列柱状图(对比图,第二列可叠加任意多次)
    """
    total_width, n = 0.8, 2
    width = total_width / n
    x = list(range(len(total_accounts)))
    plt.bar(x, total_accounts, width=width, label=kw['lables'][0], fc=kw['fc'][0])  # 绘制第一列:总数
    for i in range(len(x)):
        x[i] = x[i] + width
    for s in range(len(num_lists)):
        bottom = None if s == 0 else num_lists[s-1]
        # 绘制第二列:叠加
        plt.bar(x, num_lists[s], width=width, bottom=bottom,
                label=kw['lables'][s+1], tick_label=name_list, fc=kw['fc'][s+1])
    plt.legend()
    plt.title(project_name + ' ' + title)
    plt.savefig(self.reportpath + '/' + project_name + ' ' + title + ".png")
    plt.show()
    plt.cla()
    plt.close("all")

 

二、统计

1、继续复制黏贴(我们仍然绘制的是柱状图,所以继续在 Histogram 类编写,不再新建类)

# coding=utf-8
import datetime
from ana_jira import Ana_jira
from draw import Draw
from common.mysqluntil import MysqlUntil

# 柱状图颜色
fc = ('r', 'y', 'b', 'g')

class Histogram:
    def __init__(self, project_name, test_jira, first_day, last_day, types, weeks, report_path, assignee):
        self.project_name = project_name
        self.test_jira = test_jira
        self.first_day = first_day
        self.last_day = last_day
        self.types = types
        self.weeks = weeks
        self.report_path = report_path
        self.assignee = assignee

2、缺陷版本发现数量与修复数量对比分析

def histogram_find_repair(self):
    """
    :return:
    """
    sql = """
        SELECT A.test_version,A.resolution_counts,B.find_counts FROM
        (
        SELECT test_version,COUNT(id) AS resolution_counts FROM `jira_resolution_issues` 
        WHERE project='{project}' AND issuetype='故障'
        GROUP BY test_version
        )A, 
        (
        SELECT test_version,COUNT(id) AS find_counts FROM `jira_issues` 
        WHERE project='{project}' AND issuetype='故障'
        GROUP BY test_version
        )B
        WHERE A.test_version = B.test_version
    """.format(project=self.project_name)
    datas = MysqlUntil().mysql_select(sql)
    # 版本发现数量、修复数量对比
    test_version, resolution_counts, find_counts = [], [], []
    for data in datas:
        test_version.append(data['test_version'] if data['test_version'] else 'unfilled')
        resolution_counts.append(int(data['resolution_counts']) if data['resolution_counts'] else 0)
        find_counts.append(int(data['find_counts']) if data['find_counts'] else 0)
    if len(test_version):
        # 版本发现数量、修复数量对比
        lable = ('Total Find', 'Total Solve')
        Draw(self.report_path).drawing_histogram_tied(self.project_name,
                                                      ' Issues Find and Solve Compared (All Cycle)',
                                                      test_version,
                                                      find_counts,
                                                      resolution_counts,
                                                      lables=lable, fc=fc)
        print('缺陷发现总数与解决总数对比图:最近{}周数据统计完成'.format(self.weeks))
    else:
        print('缺陷发现总数与解决总数对比图:最近{}周无数据'.format(self.weeks))

3、缺陷修复数量与被打回数量对比分析

def histogram_dev_re(self):
    """
    绘制柱状图(被打回、重新引入)
    :param project_name:
    :return:
    """
    sql = """
        SELECT 
        assignee,
        count(id) as total_accounts,
        SUM(reopen_count) AS reopen_count,
        SUM(re_open_count) AS re_open_count
        FROM jira_resolution_issues 
        WHERE  project='{}' AND issuetype='故障' AND resolution='完成' 
        GROUP BY project,assignee 
        ORDER BY project;
        """.format(self.project_name)
    datas = MysqlUntil().mysql_select(sql)
    # 修复缺陷数与被打回、重复引入对比图
    assignee, total_accounts, reopen_count, re_open_count = [], [], [], []
    for data in datas:
        assignee.append(data['assignee'] if data['assignee'] else 'unfilled')
        total_accounts.append(int(data['total_accounts']) if data['total_accounts'] else 0)
        reopen_count.append(int(data['reopen_count']) if data['reopen_count'] else 0)
        re_open_count.append(int(data['re_open_count']) if data['re_open_count'] else 0)
    if len(assignee):
        lables = ('Total Repair', 'Reopen', 'Re Open')
        # 修复缺陷数与被打回、重复引入对比图
        Draw(self.report_path).drawing_histogram_tied(
            self.project_name, ' Issues Repair and Re-Open Compared (All Cycle)',
            assignee, total_accounts, reopen_count, re_open_count, lables=lables, fc=fc)
        print('总修复缺陷数与被打回、重新引入对比图:最近{}周数据统计完成'.format(self.weeks))
    else:
        print('总修复缺陷数与被打回、重新引入对比图:最近{}周无数据'.format(self.weeks))

4、缺陷开发引入数量与加权数量对比分析

def histogram_assignee_weighted_quantity(self):
    """
    绘制柱状图:开发人员单项目缺陷数量分布
    :param project_name:
    :return:
    """
    sql = """
    SELECT assign_name, SUM(introduce_amount) as introduce_amount, SUM(weighted_quantity) AS weighted_quantity
    FROM daily_assign_bug 
    WHERE project_name = '{}' AND date BETWEEN "{}" AND "{}" 
       GROUP BY user_name
       HAVING weighted_quantity>0;
    """.format(self.project_name, self.first_day, self.last_day)
    datas = MysqlUntil().mysql_select(sql)
    # 缺陷加权数量
    assign_name, introduce_amount, weighted_quantity = [], [], []
    for data in datas:
        assign_name.append(data['assign_name'] if data['assign_name'] else 'unfilled')
        introduce_amount.append(int(data['introduce_amount']) if data['introduce_amount'] else 0)
        weighted_quantity.append(int(data['weighted_quantity']) if data['weighted_quantity'] else 0)
    if assign_name:
        lables = ('total introduce', 'weighted quantity')
        Draw(self.report_path).drawing_histogram_tied(
            self.project_name, ' Issues Dev Weighted Quantity Distribution (week)',
            assign_name,
            introduce_amount, weighted_quantity,
            lables=lables, fc=fc)
        print('项目{}最近{}周开发人员缺陷分布统计完成'.format(self.project_name, self.weeks))
    else:
        print('项目{}最近{}周未提交缺陷,无法统计开发人员缺陷分布'.format(self.project_name, self.weeks))

5、缺陷测试发现数量与加权数量对比分析

def histogram_test_weighted_quantity(self):
    """
    绘制柱状图:开发人员单项目缺陷数量分布
    :param project_name:
    :return:
    """
    sql = """
    SELECT test_name, SUM(find_amount) as find_amount,SUM(weighted_quantity) AS weighted_quantity
    FROM daily_test_bug 
    WHERE project_name = '{}' AND date BETWEEN "{}" AND "{}" 
       GROUP BY user_name
       HAVING weighted_quantity>0;
    """.format(self.project_name, self.first_day, self.last_day)
    datas = MysqlUntil().mysql_select(sql)
    # 缺陷加权数量
    test_name, find_amount, weighted_quantity = [], [], []
    for data in datas:
        test_name.append(data['test_name'] if data['test_name'] else 'unfilled')
        find_amount.append(int(data['find_amount']) if data['find_amount'] else 0)
        weighted_quantity.append(int(data['weighted_quantity']) if data['weighted_quantity'] else 0)
    if test_name:
        lables = ('total find', 'weighted quantity')
        Draw(self.report_path).drawing_histogram_tied(
            self.project_name, ' Issues Test Weighted Quantity Distribution (week)',
            test_name,
            find_amount, weighted_quantity,
            lables=lables, fc=fc)
        print('项目{}最近{}周开发人员缺陷分布统计完成'.format(self.project_name, self.weeks))
    else:
        print('项目{}最近{}周未提交缺陷,无法统计开发人员缺陷分布'.format(self.project_name, self.weeks))

6、缺陷测试发现与无效数量对比分析

def histogram_test_re(self):
    """
    绘制柱状图(不是BUG、重复提交)
    :param project_name:
    :return:
    """
    sql = """
            SELECT
            test_name,
            SUM(find_amount) AS total_amounts,
            SUM(refuse_amount) AS refuse_amount,
            SUM(repeat_amount) AS repeat_amount
            FROM
                daily_test_bug
            where project_name = '{}'
            GROUP BY
                project_name,
                user_name
            HAVING
                SUM(find_amount) + SUM(refuse_amount) > 0
            ORDER BY
                project_name;
            """.format(self.project_name)
    datas = MysqlUntil().mysql_select(sql)
    # 发现缺陷总数与无效BUG(不是BUG、重复提交)对比
    test_name, total_accounts, refuse_amount, repeat_amount = [], [], [], []
    for data in datas:
        test_name.append(data['test_name'] if data['test_name'] else 'unfilled')
        total_accounts.append(int(data['total_amounts']) if data['total_amounts'] else 0)
        refuse_amount.append(int(data['refuse_amount']) if data['refuse_amount'] else 0)
        repeat_amount.append(int(data['repeat_amount']) if data['repeat_amount'] else 0)
    if len(test_name):
        lable = ('Total Find', 'Not Bug', 'Repeated')
        # 发现缺陷总数与无效BUG(不是BUG、重复提交)对比
        Draw(self.report_path).drawing_histogram_tied(self.project_name,
                                                      ' Issues Find and Not-Bug Compared (All Cycle)',
                                                      test_name,
                                                      total_accounts,
                                                      refuse_amount, repeat_amount,
                                                      lables=lable, fc=fc)
        print('缺陷总发现数与无效BUG对比图:最近{}周数据统计完成'.format(self.weeks))
    else:
        print('缺陷总发现数与无效BUG对比图:最近{}周无数据'.format(self.weeks))

 

三、执行统计

his = Histogram(project_name, test_jira, first_day, last_day, types, weeks, project_report_path, assignee)
his.histogram_find_repair()  # (项目、版本)发现、修复对比
his.histogram_dev_re()  # (开发人员)修复缺陷数与被打回、重复引入对比图
his.histogram_assignee_weighted_quantity()  # (开发人员)缺陷数量、加权数量对比分布
his.histogram_test_weighted_quantity()  # (测试人员) 缺陷数量、加权数量对比分布
his.histogram_test_re()  # (测试人员)发现缺陷总数与无效BUG(不是BUG、重复提交)对比

1、缺陷引入数量与加权数量对比分布

基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析_第1张图片

 

2、缺陷发现数量与加权数量对比分布

基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析_第2张图片

 

3、缺陷发现数量与无效数量对比分布

基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析_第3张图片

 

4、缺陷修复数量与被打回数量对比分布

基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析_第4张图片

 

5、缺陷发现与修复数量对比分布(按版本对比)

基于jira的缺陷自动化报表分析 (十)matplotlib 竖向并列叠加柱状图:各种对比分析_第5张图片

 

 

 

你可能感兴趣的:(缺陷自动化分析,数据可视化,python,matplotlib)