aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/mon/analytics/html_output.h
blob: f775f216b9c0962c6a26c19edbd3854d4c82e2b0 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once

#include <util/string/printf.h>
#include <util/stream/str.h>
#include <util/generic/set.h>
#include "data.h"

namespace NAnalytics {

inline TString ToHtml(const TTable& in)
{
    TSet<TString> cols;
    bool hasName = false;
    for (const TRow& row : in) {
        hasName = hasName || !row.Name.empty();
        for (const auto& kv : row) {
            cols.insert(kv.first);
        }
    }

    TStringStream ss;
    ss << "<table>";
    ss << "<thead><tr>";
    if (hasName) {
        ss << "<th>Name</th>";
    }
    for (const TString& c : cols) {
        ss << "<th>" << c << "</th>";
    }
    ss << "</tr></thead><tbody>";

    for (const TRow& row : in) {
        ss << "<tr>";
        if (hasName) {
            ss << "<th>" << row.Name << "</th>";
        }
        for (const TString& c : cols) {
            TString value;
            ss << "<td>" << (row.GetAsString(c, value) ? value : TString("-")) << "</td>";
        }
        ss << "</tr>";
    }
    ss << "</tbody></table>";

    return ss.Str();
}

inline TString ToTransposedHtml(const TTable& in)
{
    TSet<TString> cols;
    bool hasName = false;
    for (const TRow& row : in) {
        hasName = hasName || !row.Name.empty();
        for (const auto& kv : row) {
            cols.insert(kv.first);
        }
    }

    TStringStream ss;
    ss << "<table><thead>";
    if (hasName) {
        ss << "<tr>";
        ss << "<th>Name</th>";
        for (const TRow& row : in) {
            ss << "<th>" << row.Name << "</th>";
        }
        ss << "</tr>";
    }

    ss << "</thead><tbody>";

    for (const TString& c : cols) {
        ss << "<tr>";
        ss << "<th>" << c << "</th>";
        for (const TRow& row : in) {
            TString value;
            ss << "<td>" << (row.GetAsString(c, value) ? value : TString("-")) << "</td>";
        }
        ss << "</tr>";
    }
    ss << "</tbody></table>";

    return ss.Str();
}

}