import xlrd
import xlwt
import os,time
import json
from xlrd import xldate_as_tuple
from datetime import datetime
from toydir.logutil import logger
logger = logger("root", rootstdout=True, handlerList=['I','E'])
# from matplotlib import font_manager
# my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/simsun.ttc")
class ExcelUnit(object):
def __init__(self,excel_path):
self.excelObject=xlrd.open_workbook(excel_path)
self.sheetObject=self.excelObject.sheet_by_index(0)
self.sheet_name=self.sheetObject.name
self.rows=self.sheetObject.nrows
self.cols=self.sheetObject.ncols
def getSpecialCell(self,x,y):
return self.sheetObject.cell(x,y).value
def read_excel(self):
list=[]
for row in range(1,self.rows):
lists=self.sheetObject.row_values(row)[:self.cols]
cell=self.sheetObject.cell_value(row,1)
try:
global d
d= datetime(*xldate_as_tuple(cell, 0))
except Exception as e:
pass
cells= d.strftime('%Y/%m/%d')
lists[1]=cells
list.append(tuple(lists))
return list
def getAllExcellContent(Mode):
"""Mode =0 ->week ,Mode=1->month"""
absxslFileList = []
xslList=[]
excelDir=None
if Mode==0:
excelDir=os.getcwd()+"\\sourcexsldir"
elif Mode==1:
excelDir=os.getcwd()+"\\months_sourcexsl"
if os.path.isdir(excelDir):
for o in os.listdir(excelDir):
absxslfile=excelDir+'\\'+o
if o.split('.')[-1]=="xlsx" and os.path.isfile(absxslfile):
absxslFileList.append(absxslfile)
for xsl in absxslFileList:
excelInst=ExcelUnit(xsl)
xslList.append(excelInst.read_excel())
return xslList
def Dataset(get_excels):
error_sum=[]
news=[]
contents=[]
work_attr=["用例编写", "测试准备", "功能用例执行", "其它测试执行", "bug验证", "测试结果整理", "其它", "运维开发"]
for excel in get_excels:
for row in excel:
# print(row)
if row[0] and row[1] and row[2]and row[3]:
if row[1].count("/")==2 and row[4] in work_attr:
# logger.info(row)
contents.append(row)
else:
logger.error("发现异常数据,见异常数据汇总sheet")
logger.error("++++++++++++++++++++")
logger.error(row)
error_sum.append(row)
#raise ValueError("DATE FORMAT or Work Nature NOT CORRECT,Check please!")
else:
print("======出现格式异常数据==========")
logger.error(row)
#order by user
newdt=sorted(contents, key=lambda x: x[0])
users_set=list(set([i[0] for i in newdt]))
#user groupby
for u in users_set:
per_list=list(filter(lambda x:x[0]==u,newdt))
news.extend(per_list)
new_d=[]
for r in error_sum:
j=list(r)
j[1],j[5]=j[5],j[1]
k=tuple(j)
new_d.append(k)
return news,users_set,new_d
def splitdate(mode):
datas,users,err=Dataset(getAllExcellContent(Mode=mode))
all_dts=[]
for user in users:
my_list = list(filter(lambda o: o[0]== user, datas))
# print(my_list)
my_program=my_list[0][3]
my_date_list=sorted(list(set([i[1] for i in my_list])))
for index,day in enumerate(my_date_list):
lk = []
work_times = []
useable_times=[]
casewriteNumbers = []
case_excuteNum = []
bug_submitNum = []
bug_regNum = []
for dt in my_list:
if dt[1]==day:
if dt[4]!="其它":
useable_time=float(dt[5]) if dt[5] else 0
useable_times.append(useable_time)
wh=float(dt[5]) if str(dt[5]).strip() else 0
cw=int(dt[6]) if str(dt[6]).strip() else 0
ce=int(dt[7]) if str(dt[7]).strip() else 0
bug_sub=float(dt[8]) if str(dt[8]).strip() else 0
bug_reg=float(dt[9]) if str(dt[9]).strip() else 0
casewriteNumbers.append(cw)
case_excuteNum.append(ce)
bug_submitNum.append(int(bug_sub))
bug_regNum.append(int(bug_reg))
work_times.append(float(wh))
work_saturation =float('%.3f' % (sum(useable_times)/7))
lk.insert(0,user)
lk.insert(1,work_saturation)
lk.insert(2,day)
lk.insert(3, my_program)
lk.insert(4,sum(work_times))
lk.insert(5,sum(useable_times))
all_dts.append(tuple(lk))
# for i in all_dts:
# logger.debug(i)
return all_dts
def week_sum(daily_dt):
persons=list(set([j[0] for j in daily_dt ]))
allperson_week=[]
for p in persons:
# my_weekdts = list(filter(lambda o: o[0] == p, daily_dt))
weekTime=[]
useable_weekTime=[]
prog=[]
man=p
pg_list = []
oneperson_oneweek=[]
for dt in daily_dt:
oneday_hour=float(dt[4]) if str(dt[4]).strip() else 0
oneday_usebaletime=float(dt[5]) if str(dt[5]).strip() else 0
if dt[0]==p:
weekTime.append(oneday_hour)
useable_weekTime.append(oneday_usebaletime)
pg_list.append(dt[3])
prog.append(pg_list)
if len(weekTime) ==5:
week_saturation = float('%.3f' % (sum(useable_weekTime) / 5/7))
else:
week_saturation=float('%.3f' %(sum(useable_weekTime)/len(weekTime)/7))
# for pg in list(set(prog)):
oneperson_oneweek.insert(0,man)
oneperson_oneweek.insert(1,week_saturation)
oneperson_oneweek.insert(2,sum(weekTime))
allperson_week.append(tuple(oneperson_oneweek))
orderProgramWeek=sorted(allperson_week,key=lambda x:x[1],reverse=True)
orderProgramWeek.insert(0,("姓名","周日人均和度","周生产时长"))
return orderProgramWeek
def sara_bypro():
#get init data contain ot work
datas,users,err=Dataset(getAllExcellContent(Mode=0))
# set program
pro=list(set([i[3] for i in datas]))
allpg_list=[]
for pg in pro :
pg_user=[]
onepro=[]
useable_list = []
work_time = []
for dt in datas:
if dt[3]==pg:
pg_user.append(dt[0])
one_dayhour=float(dt[5]) if dt[5] else 0
if dt[4]!="其它":
useable_time=one_dayhour
useable_list.append(useable_time)
work_time.append(one_dayhour)
#user set this pro
real_user=list(set(pg_user))
# program
onepro.append(pg)
#program week sara
onepro.append(float('%.3f' % (sum(useable_list)/35/len(real_user))))
# week_pro有效时长
onepro.append(float('%.3f' % (sum(useable_list))))
#week pro worktimes
onepro.append(float('%.3f' % (sum(work_time))))
#汇总each program week sara
allpg_list.append(tuple(onepro))
allpg_list=sorted(allpg_list,key=lambda oneRow:oneRow[1])
return allpg_list
def create_sheet():
pro_tbheader=["项目","项目人均人力饱和度","生产时长","工作时长"]
#err_tbheader=["姓名","工作时长", "工作内容", "所属项目组", "工作性质", "日期", "用例编写量", "用例执行量", "bug提交量", "bug验证量"]
headers_summary = ["姓名","工作时长", "工作内容", "所属项目组", "工作性质", "日期", "用例编写量", "用例执行量", "bug提交量", "bug验证量"]
xlsx = xlwt.Workbook()
person_saraday=week_sum(splitdate(0))
daily_list,user,err=Dataset(getAllExcellContent(Mode=0))
pro_saralist=sara_bypro()
pro_saralist.insert(0,tuple(pro_tbheader))
#add errtb header
err.insert(0,tuple(headers_summary))
error_sheet=xlsx.add_sheet("异常数据汇总")
summaryContainOt=xlsx.add_sheet("日报汇总")
cur_sheet = xlsx.add_sheet('项目人力投入')
day_sheet=xlsx.add_sheet("日饱和度")
pro_sheet=xlsx.add_sheet("项目周饱和度")
mon_program=xlsx.add_sheet("月项目饱和度")
mon_day_sheet=xlsx.add_sheet("月度日饱和")
group_sheet_week=xlsx.add_sheet("自由组合")
month_init_data=xlsx.add_sheet("月度原始数据")
#grep_sheet=xlsx.add_sheet("过滤汇总")
#summaryContainOt=xlsx.add_sheet("日报汇总")
new_d=[]
for r in daily_list:
j=list(r)
j[1],j[5]=j[5],j[1]
k=tuple(j)
new_d.append(k)
# input summary contain ot
call_new_d=new_d
#插入是个动作
call_new_d.insert(0, tuple(headers_summary))
for e_index,one_err in enumerate(call_new_d):
for e_col in range(len(headers_summary)):
summaryContainOt.write(e_index,e_col,one_err[e_col])
# action add tb header
new_s = list(filter(lambda x: x[4] != "其它", new_d))
# new_s.insert(0,tuple(headers_summary))
#input data_summary
for e_index,one_err in enumerate(new_s):
for e_col in range(len(headers_summary)):
cur_sheet.write(e_index,e_col,one_err[e_col])
# input person_sara
for e_index,one_err in enumerate(person_saraday):
for e_col in range(3):
day_sheet.write(e_index,e_col,one_err[e_col])
# input program_sara
for e_index,one_err in enumerate(pro_saralist):
for e_col in range(4):
pro_sheet.write(e_index,e_col,one_err[e_col])
# input err sheet
for e_index,one_err in enumerate(err):
for e_col in range(len(headers_summary)):
error_sheet.write(e_index,e_col,one_err[e_col])
#get month data
program_month,month_daily=sum_month()
logger.debug(program_month)
logger.debug(month_daily)
reload_mon_pro=[]
for i in program_month:
i[1].insert(0,(i[0],'','',''))
i[1].insert(1,("项目组","月饱和","工时","有效时长"))
reload_mon_pro.extend(i[1])
logger.info(reload_mon_pro)
for index,row in enumerate(reload_mon_pro):
for col in range(4):
mon_program.write(index,col,row[col])
#input month day sheet
reload_mon_data=[]
for i in month_daily:
i[1].insert(0,(i[0],'',''))
reload_mon_data.extend(i[1])
for index,row in enumerate(reload_mon_data):
for col in range(3):
mon_day_sheet.write(index, col, row[col])
# input month init data
for index ,row in enumerate(sum_mutiMonth()):
for col in range(10):
month_init_data.write(index,col,row[col])
#input grep config
for index,row in enumerate(filter_config_program(Mode=0)):
for col in range(2):
group_sheet_week.write(index,col,row[col])
strf = time.strftime("%Y%m%d_%H_%M", time.localtime())
xlsx.save(os.getcwd()+"\\result_summarydir\\summary_{}.xls".format(strf))
def sum_month():
daily_dt=splitdate(mode=1)
month_list=[str(i) for i in range(1,13)]
excel_month=list(set([str(int(it[2].split('/')[1])) for it in daily_dt]))
intersect_month=list(set(month_list).intersection(set(excel_month)))
logger.info("交集月份如下:%s" % intersect_month)
month_data=[(str(m)+"月份日饱和度汇总",list(filter(lambda x:str(int(x[2].split('/')[1]))==m,daily_dt))) for m in intersect_month]
logger.info("all月份汇总: %s" % month_data)
# for meta in month_data:
# print(meta)
month_daily=[(it[0],reduce_month(it[1])) for it in iter(month_data)]
program_month=reduce_program_month(intersect_month)
print(program_month)
return program_month,month_daily
def reduce_month(daily_dt):
persons=list(set([j[0] for j in daily_dt ]))
allperson_month=[]
for p in persons:
monthTime=[]
useable_monthTime=[]
prog=[]
man=p
pg_list = []
oneperson_onemonth=[]
for dt in daily_dt:
oneday_hour=float(dt[4]) if str(dt[4]).strip() else 0
oneday_usebaletime=float(dt[5]) if str(dt[5]).strip() else 0
if dt[0]==p:
monthTime.append(oneday_hour)
useable_monthTime.append(oneday_usebaletime)
pg_list.append(dt[3])
prog.append(pg_list)
mon_saturation=float('%.3f' % (sum(useable_monthTime) / len(monthTime)/7))
oneperson_onemonth.insert(0,man)
oneperson_onemonth.insert(1,mon_saturation)
oneperson_onemonth.insert(2,sum(monthTime))
allperson_month.append(tuple(oneperson_onemonth))
orderMonths=sorted(allperson_month,key=lambda x:x[1],reverse=True)
orderMonths.insert(0,("姓名","月人均饱和度","月生产时长"))
return orderMonths
def reduce_program_month(intersect_months):
# get init data contain ot work
datas, users, err = Dataset(getAllExcellContent(Mode=1))
Mons_progarm=[]
for m in intersect_months:
mondatas=list(filter(lambda x:str(int(x[1].split('/')[1]))==m,datas))
counter_workdate=list(set([i[1] for i in mondatas]))
logger.debug("%s月 项目工作日天数: %s" % (m,len(counter_workdate)))
logger.debug(" %s月过滤数据: %s" %(m,mondatas))
# set program
pro = list(set([i[3] for i in mondatas]))
allpg_list = []
for pg in pro:
pg_user = []
onepro = []
useable_list = []
work_time = []
for dt in datas:
if dt[3] == pg:
pg_user.append(dt[0])
one_dayhour = float(dt[5]) if str(dt[5]).strip() else 0
if dt[4] != "其它":
useable_time = one_dayhour
useable_list.append(useable_time)
work_time.append(one_dayhour)
real_user = list(set(pg_user))
onepro.append(pg)
onepro.append(float('%.3f' % (sum(useable_list) /len(counter_workdate)/7/ len(real_user))))
onepro.append(float('%.3f' % (sum(useable_list))))
onepro.append(float('%.3f' % (sum(work_time))))
allpg_list.append(tuple(onepro))
Mon_allpg_list = sorted(allpg_list, key=lambda oneRow: oneRow[1])
Mons_progarm.append((m+"月项目饱和度",Mon_allpg_list))
return Mons_progarm
def init_json():
# if os.path.exists(os.getcwd()+'./config.json'):
# pass
# else:
# datas={"磨刀小分队1": ["陈权","王大伟"],"磨刀分队2":["易铭"]}
# with open(os.getcwd()+'./config.json','w+',encoding="utf-8")as f:
# json.dump(datas,f,ensure_ascii=False,indent=3)
with open(os.getcwd()+"./config.json",'r+',encoding="utf-8")as e:
init_data=json.load(e)
datas=[(k,v) for k,v in init_data.items()]
return datas
def filter_config_program(Mode):
if int(Mode)==1:
data=splitdate(Mode)
else:
data=splitdate(0)
def iter_res():
summarys=[]
for gp ,user in init_json():
get_coinfig_data=list(filter(lambda x:x[0] in user,data))
# print(get_coinfig_data)
gp_sara=sum([i[-1] for i in get_coinfig_data])/len(list(set([j[2] for j in get_coinfig_data])))/7/len(user)
summarys.append((gp,gp_sara))
return summarys
data=iter_res()
data.insert(0,("组名","组饱和度"))
return data
def sum_mutiMonth():
headers=["姓名","日期", "工作内容", "所属项目组", "工作性质", "工作时长", "用例编写量", "用例执行量", "bug提交量", "bug验证量"]
data,user,err=Dataset(getAllExcellContent(1))
data.insert(0,tuple(headers))
return data
if __name__ == '__main__':
create_sheet()
# sum_month()
# a=filter_config_program(0)
# print("分组结果:%s" %a)
# print(sum_mutiMonth())