aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/mon/analytics/csv_output.h
blob: 90ded32f5ddc6f1473a6cadc35ac3cd1e758e360 (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
#pragma once

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

namespace NAnalytics {

inline TString ToCsv(const TTable& in, TString sep = TString("\t"), bool head = true)
{
    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;
    if (head) {
        bool first = true;
        if (hasName) {
            ss << (first? TString(): sep) << "Name";
            first = false;
        }
        for (const TString& c : cols) {
            ss << (first? TString(): sep) << c;
            first = false;
        }
        ss << Endl;
    }

    for (const TRow& row : in) {
        bool first = true;
        if (hasName) {
            ss << (first? TString(): sep) << row.Name;
            first = false;
        }
        for (const TString& c : cols) {
            ss << (first? TString(): sep);
            first = false;
            TString value;
            ss << (row.GetAsString(c, value) ? value : TString("-"));
        }
        ss << Endl;
    }
    return ss.Str();
}

}