Debian Codegraph @

src/reports/Report.cpp

raw
1 #include <pbcopper/reports/Report.h>
2
3 #include <pbcopper/json/JSON.h>
4 #include <pbcopper/utility/PbcopperVersion.h>
5 #include "ReportPrinter.h"
6
7 #include <boost/uuid/uuid.hpp>
8 #include <boost/uuid/uuid_generators.hpp>
9 #include <boost/uuid/uuid_io.hpp>
10
11 #include <array>
12 #include <fstream>
13 #include <ostream>
14 #include <stdexcept>
15 #include <utility>
16
17 using Json = PacBio::JSON::Json;
18
19 namespace PacBio {
20 namespace Reports {
21
22 // -----------
23 // Attribute
24 // -----------
25
26 Attribute::Attribute(std::string id, ReportValue value)
27 : BaseReportElement{std::move(id)}, value_{std::move(value)}
28 {}
29
30 Attribute::Attribute(std::string id, ReportValue value, std::string name)
31 : BaseReportElement{std::move(id)}, name_{std::move(name)}, value_{std::move(value)}
32 {}
33
34 Attribute::operator int() const { return std::get<int>(value_); }
35
36 Attribute::operator unsigned int() const { return std::get<unsigned int>(value_); }
37
38 Attribute::operator int64_t() const { return std::get<int64_t>(value_); }
39
40 Attribute::operator double() const { return std::get<double>(value_); }
41
42 Attribute::operator bool() const { return std::get<bool>(value_); }
43
44 Attribute::operator std::string() const { return std::get<std::string>(value_); }
45
46 const std::string& Attribute::Name() const { return name_; }
47
48 Attribute& Attribute::Name(std::string name)
49 {
50 name_ = std::move(name);
51 return *this;
52 }
53
54 const ReportValue& Attribute::Value() const { return value_; }
55
56 Attribute& Attribute::Value(ReportValue value)
57 {
58 value_ = std::move(value);
59 return *this;
60 }
61
62 // -------------------
63 // BaseReportElement
64 // -------------------
65
66 BaseReportElement::BaseReportElement(std::string id) : id_{std::move(id)} {}
67
68 const std::string& BaseReportElement::Id() const { return id_; }
69
70 // --------
71 // Column
72 // --------
73
74 Column::Column(std::string id, std::vector<ReportValue> values, std::string header)
75 : BaseReportElement{id}, header_{std::move(header)}, values_(std::move(values))
76 {}
77
78 Column& Column::AddValue(ReportValue val)
79 {
80 values_.emplace_back(std::move(val));
81 return *this;
82 }
83
84 const std::string& Column::Header() const { return header_; }
85
86 Column& Column::Header(std::string header)
87 {
88 header_ = std::move(header);
89 return *this;
90 }
91
92 const std::vector<ReportValue>& Column::Values() const { return values_; }
93
94 Column& Column::Values(std::vector<ReportValue> values)
95 {
96 values_ = std::move(values);
97 return *this;
98 }
99
100 // --------
101 // Report
102 // --------
103
104 Report::Report(std::string id) : Report{std::move(id), ""} {}
105
106 Report::Report(std::string id, std::string title)
107 : BaseReportElement{std::move(id)}, title_{std::move(title)}
108 {
109 static boost::uuids::random_generator gen;
110 const boost::uuids::uuid uuid = gen();
111 uuid_ = boost::uuids::to_string(uuid);
112 }
113
114 Report& Report::AddAttribute(Attribute attribute)
115 {
116 attributes_.emplace_back(std::move(attribute));
117 return *this;
118 }
119
120 Report& Report::AddDatasetUuid(std::string uuid)
121 {
122 datasetUuids_.emplace_back(std::move(uuid));
123 return *this;
124 }
125
126 Report& Report::AddTable(Table table)
127 {
128 tables_.emplace_back(std::move(table));
129 return *this;
130 }
131
132 const std::vector<Attribute>& Report::Attributes() const { return attributes_; }
133
134 Report& Report::Attributes(std::vector<Attribute> attributes)
135 {
136 attributes_ = std::move(attributes);
137 return *this;
138 }
139
140 const std::vector<std::string>& Report::DatasetUuids() const { return datasetUuids_; }
141
142 Report& Report::DatasetUuids(std::vector<std::string> uuids)
143 {
144 datasetUuids_ = std::move(uuids);
145 return *this;
146 }
147
148 const std::vector<Table>& Report::Tables() const { return tables_; }
149
150 Report& Report::Tables(std::vector<Table> tables)
151 {
152 tables_ = std::move(tables);
153 return *this;
154 }
155
156 void Report::Print(const std::filesystem::path& filename, const std::string& prefix) const
157 {
158 std::ofstream f{filename};
159 Print(f, prefix);
160 }
161
162 void Report::Print(std::ostream& out, const std::string& prefix) const
163 {
164 internal::ReportPrinter::Print(out, *this, prefix);
165 }
166
167 void Report::PrintSummary(const std::filesystem::path& filename) const
168 {
169 std::ofstream f{filename};
170 PrintSummary(f);
171 }
172
173 void Report::PrintSummary(std::ostream& out) const
174 {
175 internal::ReportPrinter::PrintSummary(out, *this);
176 }
177
178 const std::string& Report::Title() const { return title_; }
179
180 Report& Report::Title(std::string title)
181 {
182 title_ = std::move(title);
183 return *this;
184 }
185
186 const std::string& Report::Uuid() const { return uuid_; }
187
188 Report& Report::Uuid(std::string uuid)
189 {
190 uuid_ = std::move(uuid);
191 return *this;
192 }
193
194 // -------
195 // Table
196 // -------
197
198 Table::Table(std::string id, std::vector<Column> columns, std::string title)
199 : BaseReportElement(std::move(id)), title_(std::move(title)), columns_(std::move(columns))
200 {}
201
202 Table::Table(std::string id) : BaseReportElement(std::move(id)) {}
203
204 Table& Table::AddColumn(Column column)
205 {
206 columns_.emplace_back(std::move(column));
207 return *this;
208 }
209
210 const std::vector<Column>& Table::Columns() const { return columns_; }
211
212 Table& Table::Columns(std::vector<Column> columns)
213 {
214 columns_ = std::move(columns);
215 return *this;
216 }
217
218 const std::string& Table::Title() const { return title_; }
219
220 Table& Table::Title(std::string title)
221 {
222 title_ = title;
223 return *this;
224 }
225
226 } // namespace Reports
227 } // namespace PacBio