Skip to content

Commit b3c608e

Browse files
committed
Merge branch 'feature/QPR-13658' into 'master'
QPR-13658 Zip reports that have suffix .gz Closes QPR-13658 See merge request qs/oreplus!3091
2 parents 39c2f4d + ba8cd90 commit b3c608e

4 files changed

Lines changed: 109 additions & 6 deletions

File tree

OREAnalytics/orea/app/analyticsmanager.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,22 @@ void AnalyticsManager::toFile(const ore::analytics::Analytic::analytic_reports&
280280

281281
// attach a suffix only if it does not have one already
282282
string suffix = "";
283-
if (!endsWith(fileName,".csv") && !endsWith(fileName, ".txt"))
284-
suffix = ".csv";
285283
std::string fullFileName = outputPath + "/" + fileName + suffix;
286-
287-
report->toFile(fullFileName, sep, commentCharacter, quoteChar, nullString,
288-
lowerHeaderReportNames.find(reportName) != lowerHeaderReportNames.end());
284+
if (!endsWith(fileName,".csv") && !endsWith(fileName, ".txt") && !endsWith(fileName, ".gz")){
285+
suffix = ".csv";
286+
fullFileName = outputPath + "/" + fileName + suffix;
287+
report->toFile(fullFileName, sep, commentCharacter, quoteChar, nullString,
288+
lowerHeaderReportNames.find(reportName) != lowerHeaderReportNames.end());
289+
}else if(endsWith(fileName,".gz")){
290+
fullFileName = outputPath + "/" + fileName;
291+
report->toZip(fullFileName, sep, commentCharacter, quoteChar, nullString,
292+
lowerHeaderReportNames.find(reportName) != lowerHeaderReportNames.end());
293+
}else{
294+
fullFileName = outputPath + "/" + fileName + suffix;
295+
report->toFile(fullFileName, sep, commentCharacter, quoteChar, nullString,
296+
lowerHeaderReportNames.find(reportName) != lowerHeaderReportNames.end());
297+
}
298+
289299
LOG("report " << reportName << " written to " << fullFileName);
290300
}
291301
}

OREData/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ else()
1414
SET(COMPONENTS_CONDITIONAL "")
1515
endif()
1616

17-
find_package (Boost REQUIRED COMPONENTS ${COMPONENTS_CONDITIONAL} date_time thread serialization timer log filesystem OPTIONAL_COMPONENTS system chrono)
17+
if(MSVC)
18+
add_compile_definitions(BOOST_IOSTREAMS_NO_LIB)
19+
endif()
20+
21+
if(ORE_USE_ZLIB)
22+
find_package(ZLIB REQUIRED)
23+
endif()
24+
25+
if(MSVC AND ORE_USE_ZLIB)
26+
LIST(APPEND COMPONENT_LIST zlib)
27+
endif()
28+
29+
find_package (Boost REQUIRED COMPONENTS ${COMPONENTS_CONDITIONAL} date_time thread serialization timer log filesystem OPTIONAL_COMPONENTS system chrono iostreams)
1830

1931

2032
include_directories(${Boost_INCLUDE_DIRS})

OREData/ored/report/inmemoryreport.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#include <boost/archive/binary_iarchive.hpp>
2929
#include <boost/filesystem.hpp>
3030

31+
#include <boost/iostreams/device/file_descriptor.hpp>
32+
#ifdef ORE_USE_ZLIB
33+
#include <boost/iostreams/filter/gzip.hpp>
34+
#endif
35+
#include <boost/iostreams/filtering_stream.hpp>
36+
3137
#include <fstream>
3238

3339
namespace ore {
@@ -197,5 +203,78 @@ void InMemoryReport::toFile(const string& filename, const char sep, const bool c
197203
cReport.end();
198204
}
199205

206+
bool use_compression(const std::string& filename) {
207+
#ifdef ORE_USE_ZLIB
208+
// assume compression for all filenames that do not end with csv or txt
209+
210+
std::string extension = boost::filesystem::path(filename).extension().string();
211+
return extension != ".csv" && extension != ".txt";
212+
#else
213+
return false;
214+
#endif
215+
}
216+
217+
void InMemoryReport::toZip(const string& filename, const char sep, const bool commentCharacter, char quoteChar,
218+
const string& nullString, bool lowerHeader) {
219+
220+
bool gzip = use_compression(filename);
221+
std::ofstream out1(filename, gzip ? (std::ios::binary | std::ios::out) : std::ios::out);
222+
boost::iostreams::filtering_stream<boost::iostreams::output> out;
223+
#ifdef ORE_USE_ZLIB
224+
if (gzip)
225+
out.push(boost::iostreams::gzip_compressor(/*boost::iostreams::gzip_params(9)*/));
226+
#endif
227+
out.push(out1);
228+
229+
for (Size i = 0; i < headers_.size(); ++i) {
230+
if (i > 0)
231+
out << sep;
232+
233+
std::string h = headers_[i];
234+
if (lowerHeader)
235+
std::transform(h.begin(), h.end(), h.begin(), ::tolower);
236+
out << quoteChar << h << quoteChar;
237+
}
238+
out << "\n";
239+
240+
Size numColumns = columns();
241+
if (numColumns > 0) {
242+
for (Size cacheIndex = 0; cacheIndex < files_.size(); ++cacheIndex) {
243+
const auto& data = cache(cacheIndex);
244+
Size numRows = data[0].size();
245+
246+
for (Size row = 0; row < numRows; ++row) {
247+
for (Size col = 0; col < numColumns; ++col) {
248+
if (col > 0)
249+
out << sep;
250+
251+
const ReportType& val = data[col][row];
252+
if (val.empty())
253+
out << nullString;
254+
else
255+
out << quoteChar << val << quoteChar;
256+
}
257+
out << "\n";
258+
}
259+
}
260+
261+
Size numRows = data_[0].size();
262+
for (Size row = 0; row < numRows; ++row) {
263+
for (Size col = 0; col < numColumns; ++col) {
264+
if (col > 0)
265+
out << sep;
266+
267+
const ReportType& val = data_[col][row];
268+
if (val.empty())
269+
out << nullString;
270+
else
271+
out << quoteChar << val << quoteChar;
272+
}
273+
out << "\n";
274+
}
275+
}
276+
out.flush();
277+
}
278+
200279
} // namespace data
201280
} // namespace ore

OREData/ored/report/inmemoryreport.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class InMemoryReport : public Report {
6161
const ReportType& data(Size i, Size j) const;
6262
void toFile(const string& filename, const char sep = ',', const bool commentCharacter = true, char quoteChar = '\0',
6363
const string& nullString = "#N/A", bool lowerHeader = false);
64+
void toZip(const string& filename, const char sep = ',', const bool commentCharacter = true, char quoteChar = '\0',
65+
const string& nullString = "#N/A", bool lowerHeader = false);
6466
void jumpToColumn(Size i) { i_ = i; }
6567

6668
//! Return the position of a column, throws an exception if columnName not in report

0 commit comments

Comments
 (0)