From 73c3b710a143cf8acf92e149589b0462d1107d6f Mon Sep 17 00:00:00 2001 From: xiaodong-ji Date: Tue, 24 Sep 2024 11:09:14 +0800 Subject: [PATCH] support html for check report --- handler/checker/check_handler.py | 4 +- handler/checker/check_report.py | 189 +++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 2 deletions(-) diff --git a/handler/checker/check_handler.py b/handler/checker/check_handler.py index 1e899562..4a11a756 100644 --- a/handler/checker/check_handler.py +++ b/handler/checker/check_handler.py @@ -134,8 +134,8 @@ def handle(self): # change self.export_report_type if Util.get_option(self.options, 'report_type'): self.export_report_type = Util.get_option(self.options, 'report_type') - if self.export_report_type not in ["table", "json", "xml", "yaml"]: - raise CheckException("report_type must be table, json, xml, yaml") + if self.export_report_type not in ["table", "json", "xml", "yaml", "html"]: + raise CheckException("report_type must be table, json, xml, yaml, html") self.stdio.verbose("export_report_path is " + self.export_report_path) # get package's by package_name self.tasks = {} diff --git a/handler/checker/check_report.py b/handler/checker/check_report.py index 1b0c478e..f737d642 100644 --- a/handler/checker/check_report.py +++ b/handler/checker/check_report.py @@ -31,6 +31,8 @@ from handler.checker.check_exception import CheckException from telemetry.telemetry import telemetry +from jinja2 import Template +from common.version import OBDIAG_VERSION class CheckReport: @@ -49,6 +51,7 @@ def __init__(self, context, report_target="observer", export_report_path="./chec now = datetime.datetime.now() date_format = now.strftime("%Y-%m-%d-%H-%M-%S") + self.report_time = now.strftime("%Y-%m-%d %H:%M:%S") file_name = "/obdiag_check_report_{0}_".format(report_target) + date_format self.report_target = report_target @@ -71,6 +74,8 @@ def export_report(self): self.export_report_xml() elif self.export_report_type == "yaml": self.export_report_yaml() + elif self.export_report_type == "html": + self.export_report_html() else: raise CheckrReportException("export_report_type: {0} is not support".format(self.export_report_type)) self.export_report_path = self.export_report_path + "." + self.export_report_type @@ -186,6 +191,190 @@ def export_report_table(self): except Exception as e: raise CheckrReportException("export report {0}".format(e)) + def export_report_html(self): + try: + html_template_head = """ + + + + + + obdiag Check Report + + + +
+ + + + + + + + + + + + + + + + +
+ """ + html_template_tail = """ + + + """ + html_template_report_info_table = """ +
+ +
obdiag Check Report
+

+ + + + + + + + + + + + +
Report Timeobdiag VersionOB Cluster IpOB Version
{{ report_time }}{{ obdiag_version }}{{ ob_cluster_ip }}{{ ob_version }}
+
+ """ + html_template_data_table = """ +
+ + + + + + + + {% for task in tasks %} + + + + + + {% endfor %} +
{{ task_name }}
IdTaskTask Report
{{ loop.index0 }}{{ task.task }}{{ task.task_report }}
+
+ """ + self.stdio.verbose("export report start") + fail_map_html = [] + critical_map_html = [] + warning_map_html = [] + report_all_html = [] + + for task in self.tasks: + if len(task.all_fail()) != 0: + fail_map_html.append({"task": task.name, "task_report": '
'.join([item.replace("\\n", "
") for item in task.all_fail()])}) + if len(task.all_critical()) != 0: + critical_map_html.append({"task": task.name, "task_report": '
'.join([item.replace("\\n", "
") for item in task.all_critical()])}) + if len(task.all_warning()) != 0: + warning_map_html.append({"task": task.name, "task_report": '
'.join([item.replace("\\n", "
") for item in task.all_warning()])}) + if len(task.all()) != 0: + report_all_html.append({"task": task.name, "task_report": '
'.join([item.replace("\\n", "
") for item in task.all()])}) + if len(task.all_fail()) == 0 and len(task.all_critical()) == 0 and len(task.all_warning()) == 0: + report_all_html.append({"task": task.name, "task_report": "all pass"}) + + fp = open(self.report_path + ".html", 'a+', encoding='utf-8') + template_head = Template(html_template_head) + template_table = Template(html_template_data_table) + fp.write(template_head.render() + "\n") + template_report_info_table = Template(html_template_report_info_table) + cluster_ips = "" + for server in self.context.cluster_config["servers"]: + cluster_ips += server["ip"] + cluster_ips += ";" + fp.write(template_report_info_table.render(report_time=self.report_time, obdiag_version=OBDIAG_VERSION, ob_cluster_ip=cluster_ips, ob_version=self.context.cluster_config["version"]) + "\n") + + if len(fail_map_html) != 0: + rendered_fail_map_html = template_table.render(task_name="Fail Tasks Report", tasks=fail_map_html) + fp.write(rendered_fail_map_html + "\n") + if len(critical_map_html) != 0: + rendered_critical_map_html = template_table.render(task_name="Critical Tasks Report", tasks=critical_map_html) + fp.write(rendered_critical_map_html + "\n") + if len(warning_map_html) != 0: + rendered_warning_map_html = template_table.render(task_name="Warning Tasks Report", tasks=warning_map_html) + fp.write(rendered_warning_map_html + "\n") + if len(report_all_html) != 0: + rendered_report_all_html = template_table.render(task_name="All Tasks Report", tasks=report_all_html) + fp.write(rendered_report_all_html + "\n") + + template_tail = Template(html_template_tail) + fp.write(template_tail.render()) + fp.close() + self.stdio.verbose("export report end") + except Exception as e: + raise CheckrReportException("export report {0}".format(e)) + class TaskReport: def __init__(self, context, task_name, level="normal"):