esrally结果csv格式报告转html脚本

关于转html的需求:可以先把源文件上传到在线转html的网站,获取一下专业的html格式,然后根据这个html的规律去解析源文件生成html文本

 

#-*-coding:utf-8-*-
#!/bin/pyton
import csv
import pandas as pd
import requests
import json
import datetime

upload_path='report_http_logs_es6.csv'
pd.set_option('display.max_colwidth', -1)
df = pd.read_csv(upload_path, sep='|',usecols=[1,2,3,4],skipinitialspace=True)

html_to_str = df.to_html(index=False,justify='center',col_space=50)
#print(html_to_str)

 

参数说明:

pd.set_option('display.max_colwidth', -1):关闭最大列宽,防止截断

sep:分隔符

usecols:想要读取的列

skipinitialspace:忽略分隔符后的空格

col_space=50:最小列宽,但是好像没生效,没找到原因

justify='center':列标题居中

index=False:关闭表格第一列的数字行号

 

待转换csv格式:

 

 

转换后的html

 

如果还需要做些css相关的优化,可以在生成html之后通过内联的方式来定义css,如下是个简单示例

  html_string = '''
<html>
<head>
<title>HTML Pandas Dataframe with CSS</title>
<style type="text/css">
table.dataframe td {
text-align: right;
}
table.dataframe td[class="high_light"] {
color: red;
}
</style>
</head>
<body>
<p>点击链接查看近一个月的 elasticsearch 集群性能数据:</p>
''' + es_link_month_latency + es_link_month_tps_reader + es_link_month_tps_writer + es_link_month_error_rate + "<p>下表为今日凌晨时分 elasticsearch 集群性能数据</p>" + html_to_str + "</body></html>"

esrally报告指标140多项,如果需要高亮表格文字的话,由于pandas的to_html无法指定td的类型或者id,所以一种方法是通过替换字符串来改写html
如下:
#html高亮部分
html_string = re.sub(r'<td>Max Throughput</td>', r'<td class="high_light">Max Throughput</td>', html_string)
html_string = re.sub(r'<td>90th percentile latency</td>', r'<td class="high_light">90th percentile latency</td>', html_string)
html_string = re.sub(r'<td>error rate</td>', r'<td class="high_light">error rate</td>', html_string)
html_string = re.sub(r'<td>Cumulative indexing time of primary shards</td>', r'<td class="high_light">Cumulative indexing time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative merge time of primary shards</td>', r'<td class="high_light">Cumulative merge time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative merge count of primary shards</td>', r'<td class="high_light">Cumulative merge count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative refresh time of primary shards</td>', r'<td class="high_light">Cumulative refresh time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative refresh count of primary shards</td>', r'<td class="high_light">Cumulative refresh count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative flush time of primary shards</td>', r'<td class="high_light">Cumulative flush time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative flush count of primary shards</td>', r'<td class="high_light">Cumulative flush count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Total Young Gen GC</td>', r'<td class="high_light">Total Young Gen GC</td>', html_string)
html_string = re.sub(r'<td>Total Old Gen GC</td>', r'<td class="high_light">Total Old Gen GC</td>', html_string)

最终效果:

 

 

 

 

参考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

posted on 2020-12-04 15:45  该用户很懒  阅读(214)  评论(0编辑  收藏  举报