diff options
Diffstat (limited to 'library/cpp/lwtrace/mon/analytics/csv_output.h')
-rw-r--r-- | library/cpp/lwtrace/mon/analytics/csv_output.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/library/cpp/lwtrace/mon/analytics/csv_output.h b/library/cpp/lwtrace/mon/analytics/csv_output.h new file mode 100644 index 0000000000..90ded32f5d --- /dev/null +++ b/library/cpp/lwtrace/mon/analytics/csv_output.h @@ -0,0 +1,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(); +} + +} |