周期分布,即两个时间差的数量占比。
比较常用的有:
缺陷修复周期 = 修复时间 - 创建时间
缺陷验证周期 = 验证时间 - 修复时间
缺陷存活周期 = 关闭时间 - 创建时间
所以,只需要查出要统计的时间,然后计算时间差,然后按时间差统计数量,即可得出周期占比
一、实现
1、数据库统计数据
从上面的时间差计算公式上可以看出,时间覆盖了全部的缺陷生命周期
所以我们可以从已解决的缺陷表 jira_resolution_issues 中获取时间(未解决的缺陷统计周期可能并不准确,虽然已关闭的缺陷也可以重新打开,但相对靠谱)
例如,缺陷存活周期我们可以这样写
2、存活周期分布实现
对于存货周期大于等于7天的数据,我们进行合并,因为缺陷存活7天以上我们认为都是不合格的,而且数据看上去不会太多太乱
def cake_cycle(self):
"""
存活周期 TIMESTAMPDIFF(DAY,DATE_FORMAT(created,'%Y-%m-%d'),DATE_FORMAT(resolutiondate,'%Y-%m-%d')) AS date_diff
:return:
"""
sql = """
SELECT A.date_diff,COUNT(A.date_diff) AS counts
from
(
SELECT
project,
TIMESTAMPDIFF(DAY,DATE_FORMAT(created,'%Y-%m-%d'),DATE_FORMAT(resolutiondate,'%Y-%m-%d')) AS date_diff
FROM
jira_resolution_issues
WHERE
project='{}' AND issuetype='故障' AND repair_date IS NOT NULL
) A
GROUP BY
A.project,
A.date_diff
ORDER BY A.date_diff;
""".format(self.project_name)
datas = MysqlUntil().mysql_select(sql)
# 开发人员以及各严重等级BUG数量
date_diff, counts, k = [], [], 0
for data in datas:
if data['date_diff'] >= 7:
k += int(data['counts'])
if '>=7 天' not in date_diff:
date_diff.append('>=7 Day')
else:
date_diff.append(str(data['date_diff']) + ' Day')
counts.append(int(data['counts']))
counts.append(k)
if sum(counts) > 0:
Draw(self.report_path).drawing_cake(counts, date_diff, self.project_name,
' Issues Survive Cycle Distribution (ALL CYCLE)')
print('项目{}最近{}周缺陷存活周期占比分布图统计完成'.format(self.project_name, self.weeks))
else:
print('项目{}最近{}周未验证缺陷,无法统计存活周期占比分布图'.format(self.project_name, self.weeks))
3、执行统计
cake_cycle()
二、优化
1、优化思路
存活周期分布统计完成,接下来是修复周期和验证周期
但是,我们可以发现,三个周期分布有一个共同点,就是都是求两个日期的时间差,然后按照数量看占比
既然如此,我们完全可以复用存活周期的方法,只需要稍加修改
我们将需要计算的时间以参数的形式传进来,那么存活周期、修复周期、验证周期分布就都有了,而且还可以拓展其他周期分布,例如审查周期
def cake_cycle(self, date_early, date_late):
"""
存活周期 TIMESTAMPDIFF(DAY,DATE_FORMAT(created,'%Y-%m-%d'),DATE_FORMAT(resolutiondate,'%Y-%m-%d')) AS date_diff
修复周期 TIMESTAMPDIFF(DAY,DATE_FORMAT(created,'%Y-%m-%d'),DATE_FORMAT(repair_date,'%Y-%m-%d')) AS date_diff
验证周期 TIMESTAMPDIFF(DAY,DATE_FORMAT(repair_date,'%Y-%m-%d'),DATE_FORMAT(verify_date,'%Y-%m-%d')) AS date_diff
:return:
"""
sql = """
SELECT A.date_diff,COUNT(A.date_diff) AS counts
from
(
SELECT
project,
TIMESTAMPDIFF(DAY,DATE_FORMAT({date_early},'%Y-%m-%d'),DATE_FORMAT({date_late},'%Y-%m-%d')) AS date_diff
FROM
jira_resolution_issues
WHERE
project='{project_name}' AND issuetype='故障'
) A
GROUP BY
A.project,
A.date_diff
HAVING A.date_diff <> ''
ORDER BY A.date_diff;
""".format(date_early=date_early, date_late=date_late, project_name=self.project_name)
print(sql)
datas = MysqlUntil().mysql_select(sql)
print(datas)
# 开发人员以及各严重等级BUG数量
date_diff, counts, k = [], [], 0
for data in datas:
if data['date_diff'] >= 7:
k += int(data['counts'])
if '>=7 天' not in date_diff:
date_diff.append('>=7 Day')
else:
date_diff.append(str(data['date_diff']) + ' Day')
counts.append(int(data['counts']))
counts.append(k)
if sum(counts) > 0:
Draw(self.report_path).drawing_cake(counts, date_diff, self.project_name,
' Issues Cycle Distribution (ALL CYCLE)')
print('项目{}最近{}周缺陷存活周期占比分布图统计完成'.format(self.project_name, self.weeks))
else:
print('项目{}最近{}周未验证缺陷,无法统计存活周期占比分布图'.format(self.project_name, self.weeks))
2、执行统计
c = Cake(project_name, test_jira, first_day, last_day, types, weeks, project_report_path)
c.cake_cycle('created', 'resolutiondate') # 存活周期
c.cake_cycle('created', 'repair_date') # 修复周期
c.cake_cycle('repair_date', 'verify_date') # 验证周期