一、获取数据
之前的文章 基于jira的缺陷自动化报表分析 (五)按项目统计缺陷情况 我们有按照项目每天统计了项目缺陷发现数量、遗留数量、修复数量。
这里我们可以直接获取该数据使用
二、绘制折线图
我们接 基于jira的缺陷自动化报表分析 (七)matplotlib 饼图:缺陷严重性分布 一文,在 Draw 类新增一个绘制折线图方法
从数据我们可以看出,每天的数据都分别有缺陷发现、修复、遗留数量三条数据,意味着以日期为横轴、数量为纵轴,需要绘制三条折线
# 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 # 只显示>1标签 def my_autopct(self, pct, limit=1): return ('%.2f%%' % pct) if pct > limit else '' def drawing_cake(self): """ 绘制饼状图 """ pass def drawing_histogram(self): """ 柱状图 """ pass def drawing_linechart_more(self, project_name, title, x_lebal, *y_labels, **kw): """ 绘制折线图:支持多折线 """ fig, ax = plt.subplots(1, 1) # 设置 x 轴显示密度 tick_spacing = 10 ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) for i in range(len(y_labels)): plt.plot(x_lebal, y_labels[i], kw['marker'][i], linewidth=1, label=kw['label'][i]) plt.legend() # 让图例生效 x = range(len(x_lebal)) plt.xticks(x, x_lebal, rotation=90, fontsize=6) plt.margins(0) plt.subplots_adjust(bottom=0.15) plt.xlabel("Date(day)") # X轴标签 plt.ylabel("Number") # Y轴标签 plt.title(project_name + title) # 标题 plt.savefig(self.reportpath + "/" + project_name + title + '.png') plt.show() plt.cla() plt.close("all")
三、统计数据并绘图
1、导入依赖模块
from ana_jira import Ana_jira from draw import Draw from common.mysqluntil import MysqlUntil
2、定义一个类用来统计折线数据
class Line: def __init__(self, project_name, test_jira, first_day, last_day, types, weeks, report_path): 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 # 折线图——发现、修复、遗留数量趋势图 def line_lave(self): sql = ''' SELECT `date`, `amount_find`, `amount_lave`, `amount_repair` FROM `project_issues` WHERE `project` = '{}' AND `date` BETWEEN '{}' AND '{}'; '''.format(self.project_name, self.first_day, self.last_day) datas = MysqlUntil().mysql_select(sql) date, amount_find, amount_lave, amount_repair = [], [], [], [] for data in datas: date.append(data['date'] if data['date'] else '未填写') amount_find.append(int(data['amount_find']) if data['amount_find'] else 0) # amount_lave.append(int(data['amount_lave']) if data['amount_lave'] else 0) # 遗留数量 amount_repair.append(int(data['amount_repair']) if data['amount_repair'] else 0) if date: marker = ('b-.', 'r-', 'g--') # 线段样式、颜色 label = ('find', 'lave', 'repair') # 图例 Draw(self.report_path).drawing_linechart_more(self.project_name, " Issues Trend Analysis (IT WEEK)", date, amount_find, amount_lave, amount_repair, marker=marker, label=label ) print('缺陷综合趋势图:最近{}周数据统计完成'.format(self.weeks)) else: print('缺陷综合趋势图:最近{}周无数据'.format(self.weeks))
3、拓展统计方法
上述是按照每日统计的项目缺陷数据进行统计,除此之外,我们还可以直接执行JQL查询数据
例如,统计发现数量趋势图
def days(start_time, weeks, m=0): days = [] for i in range(m, 7*weeks+m): enddays = start_time + datetime.timedelta(days=i) time_Stamp1 = int(time.mktime(enddays.timetuple())) # 转换为时间戳: t1 = time.localtime(time_Stamp1) # 转换成时间元祖 day = time.strftime("%Y-%m-%d", t1) # 转换为其他字符串格式: days.append(day) return days
def line(self): """ 绘制折线图:发现数量 :param project_name: :return: """ start_time = datetime.datetime.strptime(self.first_day + ' 00:00:00', "%Y-%m-%d %H:%M:%S") # x轴 start = days(start_time, self.weeks) end = days(start_time, self.weeks, 1) y_lebal = [] for m, n in zip(start, end): everyday_find_jql = 'project="{}" AND created >= {} AND created <= {}'.format(self.project_name, m, n) one_line = Ana_jira(self.test_jira, everyday_find_jql).req_jira() # 缺陷数量 y_lebal.append(one_line) if sum(y_lebal) > 0: marker = ('b-.') label = ('find') Draw(self.report_path).drawing_linechart_more(self.project_name, " Issues Discovery Trend Analysis (IT WEEK)", start, y_lebal, marker=marker, label=label ) print('项目{}最近{}周缺陷发现趋势统计完成'.format(self.project_name, self.weeks)) else: print('项目{}最近{}周未提交BUG,无法分析缺陷发现趋势'.format(self.project_name, self.weeks))
四、执行统计
li = Line(project_name, test_jira, first_day, last_day, types, weeks, project_report_path) # li.line() # (项目)发现数量趋势 li.line_lave() # (项目) 综合趋势分析:发现、修复、遗留数量(趋势是否收敛)