| 1 |
#include "ReportPrinter.h" |
| 2 |
|
| 3 |
#include <pbcopper/json/JSON.h> |
| 4 |
#include <pbcopper/reports/Report.h> |
| 5 |
#include <pbcopper/utility/PbcopperVersion.h> |
| 6 |
|
| 7 |
#include <ostream> |
| 8 |
#include <string_view> |
| 9 |
|
| 10 |
using Json = PacBio::JSON::Json; |
| 11 |
|
| 12 |
namespace { |
| 13 |
|
| 14 |
// clang-format off |
| 15 |
Json VariantToJson(const PacBio::Reports::ReportValue& value) |
| 16 |
{ |
| 17 |
// std::monostate, int, double, bool, std::string |
| 18 |
switch(value.index()) { |
| 19 |
case 0 : return Json{}; |
| 20 |
case 1 : return Json::number_integer_t{std::get<int>(value)}; |
| 21 |
case 2 : return Json::number_unsigned_t{std::get<unsigned int>(value)}; |
| 22 |
case 3 : return Json::number_integer_t{std::get<int64_t>(value)}; |
| 23 |
case 4 : return Json::number_float_t{std::get<double>(value)}; |
| 24 |
case 5 : return Json::boolean_t{std::get<bool>(value)}; |
| 25 |
case 6 : return Json::string_t{std::get<std::string>(value)}; |
| 26 |
default: |
| 27 |
throw std::runtime_error{"[pbcopper] reports ERROR: invalid report value type"}; |
| 28 |
} |
| 29 |
} |
| 30 |
// clang-format on |
| 31 |
|
| 32 |
Json ToJson(const PacBio::Reports::Attribute& attribute, const std::string& prefix) |
| 33 |
{ |
| 34 |
Json result = Json::object(); |
| 35 |
result["id"] = prefix + attribute.Id(); |
| 36 |
result["name"] = attribute.Name(); |
| 37 |
result["value"] = VariantToJson(attribute.Value()); |
| 38 |
return result; |
| 39 |
} |
| 40 |
|
| 41 |
Json ToJson(const PacBio::Reports::Column& column, const std::string& prefix) |
| 42 |
{ |
| 43 |
Json result = Json::object(); |
| 44 |
|
| 45 |
result["id"] = prefix + column.Id(); |
| 46 |
result["header"] = column.Header(); |
| 47 |
|
| 48 |
Json values = Json::array(); |
| 49 |
for (const auto& value : column.Values()) { |
| 50 |
values.emplace_back(VariantToJson(value)); |
| 51 |
} |
| 52 |
result["values"] = values; |
| 53 |
|
| 54 |
return result; |
| 55 |
} |
| 56 |
|
| 57 |
Json ToJson(const PacBio::Reports::Table& table, const std::string& prefix) |
| 58 |
{ |
| 59 |
Json result = Json::object(); |
| 60 |
|
| 61 |
result["id"] = prefix + table.Id(); |
| 62 |
result["title"] = table.Title(); |
| 63 |
|
| 64 |
Json columns = Json::array(); |
| 65 |
for (const auto& col : table.Columns()) { |
| 66 |
columns.emplace_back(ToJson(col, prefix)); |
| 67 |
} |
| 68 |
result["columns"] = columns; |
| 69 |
|
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
} // namespace |
| 74 |
|
| 75 |
namespace PacBio { |
| 76 |
namespace Reports { |
| 77 |
namespace internal { |
| 78 |
|
| 79 |
constexpr std::string_view PB_REPORT_SCHEMA_VERSION{"1.0.1"}; |
| 80 |
|
| 81 |
void ReportPrinter::Print(std::ostream& out, const Report& report, const std::string& prefix) |
| 82 |
{ |
| 83 |
Json result = Json::object(); |
| 84 |
|
| 85 |
result["_comment"] = "Created by pbcopper v" + Utility::LibraryVersionString(); |
| 86 |
result["version"] = PB_REPORT_SCHEMA_VERSION; |
| 87 |
result["id"] = prefix + report.Id(); |
| 88 |
result["title"] = report.Title(); |
| 89 |
result["uuid"] = report.Uuid(); |
| 90 |
result["dataset_uuids"] = report.DatasetUuids(); |
| 91 |
|
| 92 |
Json attributes = Json::array(); |
| 93 |
for (const auto& attr : report.Attributes()) { |
| 94 |
attributes.emplace_back(ToJson(attr, prefix)); |
| 95 |
} |
| 96 |
result["attributes"] = attributes; |
| 97 |
|
| 98 |
Json tables = Json::array(); |
| 99 |
for (const auto& table : report.Tables()) { |
| 100 |
tables.emplace_back(ToJson(table, prefix)); |
| 101 |
} |
| 102 |
result["tables"] = tables; |
| 103 |
|
| 104 |
result["plotGroups"] = Json::array(); |
| 105 |
|
| 106 |
out << result.dump(4); |
| 107 |
} |
| 108 |
|
| 109 |
void ReportPrinter::PrintSummary(std::ostream& out, const Report& report) |
| 110 |
{ |
| 111 |
Json result = Json::object(); |
| 112 |
for (const auto& attr : report.Attributes()) { |
| 113 |
result[attr.Id()] = VariantToJson(attr.Value()); |
| 114 |
} |
| 115 |
out << result.dump(4); |
| 116 |
} |
| 117 |
|
| 118 |
} // namespace internal |
| 119 |
} // namespace Reports |
| 120 |
} // namespace PacBio |