1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| def reports_top_slow_export(request): data = [] name = "top10慢的接口" excel_name = os.path.join(Element.REPORT_FILE, name) + ".xlsx" if os.path.exists(excel_name): os.remove(excel_name) print("文件已经删除" + excel_name)
with open(excel_name, 'a+', encoding="utf-8") as f: print("创建文件成功" + excel_name)
item_entry = ReportItem.objects.all().order_by("-sum_time")[:10]
for i in item_entry: url = i.protocol + "//" + i.url if i.result == 1: result = "通过" else: result = "失败"
data.append({"url": url, "params": i.params, "name": i.name, "method": i.method, "hope": i.hope, "result": result, "sum_time": i.sum_time + "ms", "fact": i.fact})
workbook = xlsxwriter.Workbook(excel_name, {"string_to_urls": False}) worksheet = workbook.add_worksheet("响应时间最慢的top10") worksheet.write("A1", "用例名") worksheet.set_column("B:B", 60) worksheet.write("B1", "url") worksheet.write("C1", "请求方法") worksheet.write("D1", "入参") worksheet.write("E1", "期望结果") worksheet.write("F1", "实际结果") worksheet.set_column("F:F", 60) worksheet.write("G1", "是否通过") worksheet.write("H1", "耗时") temp = 2 for j in data: worksheet.write("A" + str(temp), j["name"]) worksheet.write("B" + str(temp), j["url"]) worksheet.write("C" + str(temp), j["method"]) worksheet.write("D" + str(temp), j["params"]) worksheet.write("E" + str(temp), j["hope"]) worksheet.write("F" + str(temp), j["fact"]) worksheet.write("G" + str(temp), j["result"]) worksheet.write("H" + str(temp), j["sum_time"]) temp += 1 workbook.close()
file_name1 = os.path.join(settings.BASE_DIR, "myapi/Report",name + ".xlsx") file = open(file_name1, 'rb') response = StreamingHttpResponse(file) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="{}"'.format(name)
return response
|