html_output.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include <util/string/printf.h>
  3. #include <util/stream/str.h>
  4. #include <util/generic/set.h>
  5. #include "data.h"
  6. namespace NAnalytics {
  7. inline TString ToHtml(const TTable& in)
  8. {
  9. TSet<TString> cols;
  10. bool hasName = false;
  11. for (const TRow& row : in) {
  12. hasName = hasName || !row.Name.empty();
  13. for (const auto& kv : row) {
  14. cols.insert(kv.first);
  15. }
  16. }
  17. TStringStream ss;
  18. ss << "<table>";
  19. ss << "<thead><tr>";
  20. if (hasName) {
  21. ss << "<th>Name</th>";
  22. }
  23. for (const TString& c : cols) {
  24. ss << "<th>" << c << "</th>";
  25. }
  26. ss << "</tr></thead><tbody>";
  27. for (const TRow& row : in) {
  28. ss << "<tr>";
  29. if (hasName) {
  30. ss << "<th>" << row.Name << "</th>";
  31. }
  32. for (const TString& c : cols) {
  33. TString value;
  34. ss << "<td>" << (row.GetAsString(c, value) ? value : TString("-")) << "</td>";
  35. }
  36. ss << "</tr>";
  37. }
  38. ss << "</tbody></table>";
  39. return ss.Str();
  40. }
  41. inline TString ToTransposedHtml(const TTable& in)
  42. {
  43. TSet<TString> cols;
  44. bool hasName = false;
  45. for (const TRow& row : in) {
  46. hasName = hasName || !row.Name.empty();
  47. for (const auto& kv : row) {
  48. cols.insert(kv.first);
  49. }
  50. }
  51. TStringStream ss;
  52. ss << "<table><thead>";
  53. if (hasName) {
  54. ss << "<tr>";
  55. ss << "<th>Name</th>";
  56. for (const TRow& row : in) {
  57. ss << "<th>" << row.Name << "</th>";
  58. }
  59. ss << "</tr>";
  60. }
  61. ss << "</thead><tbody>";
  62. for (const TString& c : cols) {
  63. ss << "<tr>";
  64. ss << "<th>" << c << "</th>";
  65. for (const TRow& row : in) {
  66. TString value;
  67. ss << "<td>" << (row.GetAsString(c, value) ? value : TString("-")) << "</td>";
  68. }
  69. ss << "</tr>";
  70. }
  71. ss << "</tbody></table>";
  72. return ss.Str();
  73. }
  74. }