Commit 3d2cb3f1 authored by Jan Kiene's avatar Jan Kiene
Browse files

add script for creating report subpage from mld csv files

parent 9a4f2c35
Loading
Loading
Loading
Loading
+148 −0
Original line number Diff line number Diff line
import csv
import pathlib
import argparse


CSV_DELIM = ";"
SUBPAGE_TMPL_CSS = """
<style type="text/css">
.tbase  {border-collapse:collapse;border-spacing:0;}
.tbase td{border-color:black;border-style:solid;border-width:1px;font-family:sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tbase th{border-color:black;border-style:solid;border-width:1px;font-family:sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tbase .tunder{font-weight:bold;text-align:center;text-decoration:underline;vertical-align:top}
.tbase .tcenter{font-weight:bold;text-align:center;vertical-align:top}
.tbase .tleft{text-align:left;vertical-align:top}
</style>
"""

SUBPAGE_TMPL_HTML = """

<table class="tbase"><thead>
  <tr>
    <th class="tunder" rowspan="2">Testcase</th>
    <th class="tunder" colspan="2">MLD</th>
    <th class="tunder" colspan="2">Max Abs Diff</th>
  </tr>
  <tr>
    <th class="tcenter">{id_current}</th>
    <th class="tcenter">{id_previous}</th>
    <th class="tcenter">{id_current}</th>
    <th class="tcenter">{id_previous}</th>
  </tr></thead>
<tbody>
{table_body}
</tbody>
</table>
"""
TD_TMPL = "<td class='tleft'>{}</td>"
TR_TMPL = "<tr>{}</tr>"

COLUMNS = ["testcase", "MLD", "MAXIMUM ABS DIFF"]


def create_subpage(
    html_out, csv_current: str, csv_previous: str, id_current: int, id_previous: int
):
    merged_reports = merge_and_cleanup_mld_reports(
        csv_current, csv_previous, id_current, id_previous
    )
    table_body = "\n".join(
        tr_from_row(row, id_current, id_previous) for row in merged_reports
    )
    new_subpage = SUBPAGE_TMPL_CSS + SUBPAGE_TMPL_HTML.format(
        id_current=id_current, id_previous=id_previous, table_body=table_body
    )
    with open(html_out, "w") as f:
        f.write(new_subpage)


def tr_from_row(row, id_current, id_previous):
    tr = list()
    for c in COLUMNS:
        try:
            tr.append(TD_TMPL.format(row[c]))
        except KeyError:
            tr.append(TD_TMPL.format(row[f"{c}-{id_current}"]))
            tr.append(TD_TMPL.format(row[f"{c}-{id_previous}"]))

    return TR_TMPL.format("\n".join(tr))


def merge_and_cleanup_mld_reports(
    csv_current: str, csv_previous: str, id_current: int, id_previous: int
):
    with open(csv_current) as f:
        current_reader = csv.DictReader(f, delimiter=CSV_DELIM)
        current = list(current_reader)
    with open(csv_previous) as f:
        previous = list(csv.DictReader(f, delimiter=CSV_DELIM))

    # TODO: handle newly added testcases - for now assume that both have the same columns
    merge_key = "testcase"
    other_keys = [k for k in current_reader.fieldnames if k != merge_key]
    merged = merge_tables(
        current, previous, id_current, id_previous, merge_key, other_keys
    )

    # TODO: sort on result as well
    mld_col = f"MLD-{id_current}"

    def sort_func(x):
        return float("inf") if x[mld_col] == "None" else float(x[mld_col])

    merged = sorted(merged, key=sort_func, reverse=True)
    print(merged)

    # remove the unecessary whole path from the testcase names
    for row in merged:
        row["testcase"] = pathlib.Path(row["testcase"]).name

    return merged


def merge_tables(tbl1, tbl2, suffix1, suffix2, merge_key, other_keys):
    merged = list()

    for row1 in tbl1:
        new_row = dict()
        for key in other_keys:
            new_row[f"{key}-{suffix1}"] = row1[key]

        for row2 in tbl2:
            if row1[merge_key] == row2[merge_key]:
                new_row[merge_key] = row1[merge_key]
                for key in other_keys:
                    new_row[f"{key}-{suffix2}"] = row2[key]
                break

        merged.append(new_row)

    return merged


create_subpage(
    "/tmp/html_out.html",
    "/Users/knj/Downloads/mld(1).csv",
    "/Users/knj/Downloads/mld(2).csv",
    "CURR",
    "PREV",
)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("html_out")
    parser.add_argument("csv_current")
    parser.add_argument("csv_previous")
    parser.add_argument("id_current", type=int)
    parser.add_argument("id_previous", type=int)
    args = parser.parse_args()

    create_subpage(
        args.html_out,
        args.csv_current,
        args.csv_previous,
        args.id_current,
        args.id_previous,
    )