zoukankan      html  css  js  c++  java
  • esrally结果csv格式报告转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

  • 相关阅读:
    python-局部变量与全局变量作用域
    python-函数(上):函数返回值、函数调用、前向引用
    python-文件读写
    python-输入和输出
    python-模块介绍及os模块的方法
    python-continue和break的区别
    python-for循环
    python-while循环
    python-三元运算和if...else
    python-数据类型(下) :byte、列表、元组、集合
  • 原文地址:https://www.cnblogs.com/to-here/p/14086200.html
Copyright © 2011-2022 走看看