One, encapsulate a drawing class
1. Import the module
# coding=utf-8
import matplotlib.pyplot as plt
2. Solve the garbled display of Chinese and minus signs
plt.rcParams['font.sans-serif'] = ['simhei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
3. Package drawing
After drawing the statistical graph, there needs to be a path to save
class Draw:
"""
绘图
"""
def __init__(self, reportpath):
self.reportpath = reportpath
4. Draw a pie chart (only display data greater than 1 to avoid overlap)
# 只显示>1标签
def my_autopct(self, pct, limit=1):
return ('%.2f%%' % pct) if pct > limit else ''
def drawing_cake(self, amounts, lebals, project_name, title):
"""
绘制饼状图
:param amounts:数量列表
:param lebals:图例
:param project_name:项目名称(用于保存路径、文件名称)
:param title:标题(用于保存路径、文件名称)
"""
plt.figure(figsize=(10, 10))
indic = []
# 将数据最大的突出显示
for value in amounts:
if value == max(amounts):
indic.append(0.1)
else:
indic.append(0)
plt.pie(amounts,
# labels=lebals,
startangle=90, # 起始角度
shadow=True, # 阴影
# 是否分割出饼图的哪一块 默认是None,不分割的时候为0,分割的时候传入0.1
explode=tuple(indic), # tuple方法用于将列表转化为元组
autopct=self.my_autopct # 比例数值格式
# autopct='%1.2f%%'
)
# 给图像加上图例,并显示在左上方
plt.legend(lebals, loc='upper left')
plt.title(project_name + ' ' + title)
filename = self.reportpath + "/" + project_name + ' ' + title + ".png"
plt.savefig(filename)
plt.show()
plt.cla()
plt.close("all")
def drawing_histogram_tied(self):
"""
绘制柱状图,见后文
"""
pass
def drawing_linechart_more(self):
"""
绘制折线图,见后文
"""
pass
Two, package pie chart data statistics
1. Import dependent modules
# coding=utf-8
from ana_jira import Ana_jira
from draw import Draw
2. Package data statistics
class Cake:
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
3. Statistical defect severity distribution
method one:
Directly query JQL statistics
def cake(self, ana_type):
"""
绘制饼状图(严重性分布、优先级分布)
:param project_name:
:return:
"""
# ana_type = '严重性'
# 获取不同优先级\严重等级JQL
amount = []
severity = []
for i in self.types[ana_type]:
severity.append(i)
serch_jql = """
project = "{}" AND {} = {} AND created >= {} AND created <= {}
""".format(self.project_name, ana_type, i, self.first_day, self.last_day)
onetypy_numbers = Ana_jira(self.test_jira, serch_jql).req_jira()
amount.append(onetypy_numbers) # 缺陷数量列表
# 绘制饼状图: 优先级\严重性
if sum(amount) > 0:
# severity = ['Fatal', 'Severe', 'Average', 'Minor']
Draw(self.report_path).drawing_cake(amount, severity, self.project_name,
'Issues Severity Distribution (Iteration Week)')
print('项目{}最近{}周{}分布情况统计完成'.format(self.project_name, self.weeks, ana_type))
else:
print('项目{}最近{}周未提交缺陷,无法统计{}分布情况'.format(self.project_name, self.weeks, ana_type))
def cake_repairs_cycle(self):
"""
绘制饼图:修复周期(见后文)
:param project_name:
:return:
"""
pass
Method Two:
Get the defect severity distribution by querying the database defect records, that is, the jira_issues table
def cake_db(self):
sql = """
SELECT severity_level,COUNT(id) AS amount
FROM jira_issues
WHERE project='{}' AND created BETWEEN '{}' AND '{}'
GROUP BY project,severity_level
""".format(self.project_name, self.first_day, self.last_day)
from common.mysqluntil import MysqlUntil
results = MysqlUntil().mysql_select(sql)
amount, severity = [], []
for result in results:
severity.append(result['severity_level'])
amount.append(result['amount'])
if sum(amount) > 0:
# severity = ['Fatal', 'Severe', 'Average', 'Minor']
Draw(self.report_path).drawing_cake(amount, severity, self.project_name,
'Issues Severity Distribution (Iteration Week)')
print('项目{}最近{}周统计完成'.format(self.project_name, self.weeks))
else:
print('项目{}最近{}周未提交缺陷,无法统计分布情况'.format(self.project_name, self.weeks))
Method three
The severity distribution of defects can also be calculated through the tester statistics table, which is the same as the previous method
3. Perform statistics and plot the severity distribution of defects
1. Get the date according to the time period to be counted ()
from datetime import datetime
import time
import datetime
Get a list of all dates in the previous N weeks
# 获取前N周的所有日期(weeks=N)
def getBeforeWeekDays(self, weeks):
# 0,1,2,3,4,5,6,分别对应周一到周日
week = datetime.datetime.now().weekday()
start = 7 * weeks + week
end = week
days_list = []
for index in range(start, end, -1):
day = self.getdate(index)
days_list.append(day)
return days_list
Add a judgment here. Under the restriction, the maximum statistical period is 4 weeks: the period is generally an iterative period, and the iteration should not be too long, and the statistical graph shows that the period is too long and it is easy to cause problems.
weeks = 2 # 缺陷统计N周前的数据
if isinstance(weeks, int):
if 1 <= weeks <= 4:
weeks = int(weeks)
else:
weeks = 2
else:
weeks = 2
# 缺陷统计分析的开始日期、结束日期
days_list = getBeforeWeekDays(weeks)
first_day, last_day = days_list[0], days_list[-1]
2. Get the report save path
import os
Set the save path according to the current path
file_path_now = os.path.abspath('.') # 获取当前目录
report_path = file_path_now + "/report/" + date_now # 拼接图片存储地址(拼接时间,以防重复被覆盖)
# 如果不存在,新建一个文件夹
if not os.path.exists(report_path):
os.mkdir(report_path)
3. Define the statistics type
types = {
'优先级': ['Highest', 'High', 'Medium', 'Low', 'Lowest'],
'严重性': [u'致命', u'严重', u'一般', u'优化']
}
4. Perform statistics and view report statistics
c = Cake(project_name, test_jira, first_day, last_day, types, weeks, project_report_path)
c.cake('严重性') # (项目)严重性分布、优先级分布
c.cake_db()