GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.8% 154 / 0 / 166
Functions: 100.0% 15 / 0 / 15
Branches: 83.1% 265 / 0 / 319

src/workflow/Util.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "Util.hpp"
7
8 #include "../model/Model.hpp"
9 #include "../osversion/VersionTranslator.hpp"
10 #include "../utilities/core/Logger.hpp"
11 #include "../utilities/core/Filesystem.hpp"
12 #include "../utilities/core/FilesystemHelpers.hpp"
13 #include "../utilities/core/StringHelpers.hpp"
14 #include "../utilities/core/ZipFile.hpp"
15 #include "../utilities/bcl/BCLXML.hpp"
16 #include "../utilities/idf/Workspace.hpp"
17 #include "../utilities/idf/IdfFile.hpp"
18 #include "../utilities/idf/IdfObject.hpp"
19 #include "../utilities/idd/IddObject.hpp"
20 #include "../utilities/idf/IdfExtensibleGroup.hpp"
21 #include "../utilities/filetypes/WorkflowStepResult.hpp"
22 #include "../utilities/bcl/BCLMeasure.hpp"
23 #include "../utilities/time/DateTime.hpp"
24
25 #include <algorithm>
26 #include <boost/filesystem/operations.hpp>
27 #include <utilities/idd/IddEnums.hxx>
28
29 #include <boost/regex.hpp>
30 #include <fmt/format.h>
31 #include <fmt/std.h> // Formatting for std::filesystem::path
32 #include <fmt/chrono.h> // Formatting for std::chrono
33
34 #include <algorithm>
35 #include <array>
36 #include <iterator>
37 #include <map>
38 #include <string_view>
39 #include <regex>
40 #include <string>
41
42 namespace openstudio::workflow::util {
43
44 15 model::Model loadOSM(const openstudio::filesystem::path& osmPath) {
45
46
5/5
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
45 LOG_FREE(Info, "openstudio.worklow.Util", "Loading OSM model");
47
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
45 LOG_FREE(Info, "openstudio.worklow.Util", "Reading in OSM model " << osmPath);
48
49
1/1
✓ Branch 1 taken 15 times.
15 openstudio::osversion::VersionTranslator vt;
50
1/1
✓ Branch 1 taken 15 times.
15 auto m_ = vt.loadModel(osmPath);
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!m_) {
52 ✗ throw std::runtime_error(fmt::format("Failed to load OSM file {}\n", openstudio::toString(osmPath)));
53 }
54
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
30 return m_.get();
55 15 }
56
57 6 Workspace loadIDF(const openstudio::filesystem::path& idfPath) {
58
5/5
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
18 LOG_FREE(Info, "openstudio.worklow.Util", "Loading IDF workspace");
59
6/6
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
18 LOG_FREE(Info, "openstudio.worklow.Util", "Reading in IDF workspace " << idfPath);
60
61
1/1
✓ Branch 1 taken 6 times.
6 auto idf_ = Workspace::load(idfPath);
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!idf_) {
63 ✗ throw std::runtime_error(fmt::format("Failed to load IDF file {}\n", openstudio::toString(idfPath)));
64 }
65
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 return idf_.get();
66 6 };
67
68 18 void gatherReports(const openstudio::filesystem::path& runDirPath, const openstudio::filesystem::path& rootDirPath) {
69 namespace fs = openstudio::filesystem;
70
71
6/6
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
✓ Branch 10 taken 18 times.
✓ Branch 13 taken 18 times.
✓ Branch 16 taken 18 times.
54 LOG_FREE(Info, "openstudio.workflow.Util", "Run directory: " << runDirPath);
72
73 // It's been wiped out during runInitialization
74
1/1
✓ Branch 1 taken 18 times.
36 auto reportsDirPath = rootDirPath / "reports";
75
1/1
✓ Branch 1 taken 18 times.
18 fs::create_directories(reportsDirPath);
76
77
6/6
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
✓ Branch 10 taken 18 times.
✓ Branch 13 taken 18 times.
✓ Branch 16 taken 18 times.
54 LOG_FREE(Info, "openstudio.workflow.Util", "Reports directory" << rootDirPath);
78
79 // Try to find E+ result file
80
1/1
✓ Branch 1 taken 18 times.
36 auto epHTMPath = runDirPath / "eplustbl.htm";
81
3/3
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 7 times.
18 if (fs::is_regular_file(epHTMPath)) {
82 // TODO: try to force UTF-8? html = File.read(eplus_html); html = html.force_encoding('ISO-8859-1').encode('utf-8', replace: nil)
83
7/7
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
✓ Branch 19 taken 11 times.
33 LOG_FREE(Info, "openstudio.workflow.Util", "Saving EnergyPlus HTML report to " << reportsDirPath << "/eplustbl.html");
84
85
2/2
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
22 fs::copy_file(epHTMPath, reportsDirPath / "eplustbl.html", fs::copy_options::overwrite_existing);
86 }
87
88 // Find any `/report.*\..*/` files
89
3/7
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 18 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
18 static const boost::regex runDirNumbers(R"(^[0-9]+[0-9]+[0-9]+_(.*)$)");
90
3/3
✓ Branch 1 taken 18 times.
✓ Branch 8 taken 331 times.
✓ Branch 9 taken 18 times.
349 for (auto& fPath : fs::recursive_directory_files(runDirPath)) {
91 auto fnamePath = fPath.filename();
92
1/1
✓ Branch 2 taken 331 times.
331 std::string fnamePathStr = fnamePath.string();
93
9/13
✓ Branch 1 taken 331 times.
✓ Branch 4 taken 331 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 323 times.
✓ Branch 9 taken 8 times.
✓ Branch 11 taken 8 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 331 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 8 times.
✓ Branch 17 taken 323 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
331 if (fnamePathStr.substr(0, 6) == "report" && fnamePath.has_extension()) {
94 // TODO: lots of workarounds happening in https://github.com/NREL/OpenStudio-workflow-gem/blob/cf99e6096f33b717df79a71c245a4d7aa4ccb7a6/lib/openstudio/workflow/util/post_process.rb#L188-L216
95 // TODO: I don't like it one bit. Instead we should store a stepRunDir on the workflowJSON Steps and iterate on that or something
96
1/1
✓ Branch 1 taken 8 times.
8 boost::smatch matches;
97
2/2
✓ Branch 1 taken 8 times.
✓ Branch 5 taken 8 times.
16 const std::string dirNameStr = fPath.parent_path().filename().string();
98
2/3
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 if (boost::regex_search(dirNameStr, matches, runDirNumbers)) {
99
3/3
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
8 const std::string measure_dir_name(matches[1].first, matches[1].second);
100
1/1
✓ Branch 1 taken 8 times.
16 const fs::path measureXmlPath = rootDirPath / "measures" / measure_dir_name / "measure.xml";
101 8 std::string measure_class_name;
102
2/3
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 if (fs::is_regular_file(measureXmlPath)) {
103
2/2
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
8 measure_class_name = BCLXML(measureXmlPath).className();
104 } else {
105 ✗ measure_class_name = measure_dir_name;
106 }
107
1/1
✓ Branch 1 taken 8 times.
8 measure_class_name = openstudio::toUnderscoreCase(measure_class_name);
108
1/1
✓ Branch 2 taken 8 times.
24 const fs::path outPath = reportsDirPath / fmt::format("{}_{}", measure_class_name, fnamePathStr);
109
110
1/1
✓ Branch 1 taken 8 times.
16 const fs::path oriPath = runDirPath / fPath;
111
8/8
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
✓ Branch 16 taken 8 times.
✓ Branch 19 taken 8 times.
✓ Branch 22 taken 8 times.
24 LOG_FREE(Info, "openstudio.workflow.Util", "Saving report " << oriPath << " to " << outPath);
112
1/1
✓ Branch 1 taken 8 times.
8 fs::copy_file(oriPath, outPath, fs::copy_options::overwrite_existing);
113 8 }
114 8 }
115 349 }
116
117 // Remove empty directories
118
7/7
✓ Branch 1 taken 18 times.
✓ Branch 5 taken 18 times.
✓ Branch 9 taken 357 times.
✓ Branch 12 taken 357 times.
✓ Branch 15 taken 375 times.
✓ Branch 17 taken 357 times.
✓ Branch 18 taken 18 times.
375 for (const auto& dirEnt : fs::directory_iterator{runDirPath}) {
119 357 const auto& dirEntryPath = dirEnt.path();
120
8/8
✓ Branch 1 taken 357 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 323 times.
✓ Branch 6 taken 34 times.
✓ Branch 8 taken 26 times.
✓ Branch 9 taken 8 times.
✓ Branch 10 taken 26 times.
✓ Branch 11 taken 331 times.
357 if (fs::is_directory(dirEntryPath) && fs::is_empty(dirEntryPath)) {
121
6/6
✓ Branch 1 taken 26 times.
✓ Branch 4 taken 26 times.
✓ Branch 7 taken 26 times.
✓ Branch 10 taken 26 times.
✓ Branch 13 taken 26 times.
✓ Branch 16 taken 26 times.
78 LOG_FREE(Info, "openstudio.workflow.Util", "Removing empty directory " << dirEntryPath);
122
1/1
✓ Branch 1 taken 26 times.
26 fs::remove(dirEntryPath);
123 }
124 18 }
125 18 }
126
127 12 void cleanup(const openstudio::filesystem::path& runDirPath) {
128 namespace fs = openstudio::filesystem;
129
130 static constexpr std::array<std::string_view, 2> removeExtensions{".mtr", ".epw"};
131
132
7/7
✓ Branch 1 taken 12 times.
✓ Branch 5 taken 12 times.
✓ Branch 9 taken 253 times.
✓ Branch 12 taken 253 times.
✓ Branch 15 taken 265 times.
✓ Branch 17 taken 253 times.
✓ Branch 18 taken 12 times.
265 for (const auto& dirEnt : fs::directory_iterator{runDirPath}) {
133 253 const auto& dirEntryPath = dirEnt.path();
134
3/3
✓ Branch 1 taken 253 times.
✓ Branch 3 taken 249 times.
✓ Branch 4 taken 4 times.
253 if (fs::is_regular_file(dirEntryPath)) {
135
3/3
✓ Branch 4 taken 249 times.
✓ Branch 8 taken 19 times.
✓ Branch 9 taken 230 times.
249 if (std::find(removeExtensions.cbegin(), removeExtensions.cend(), dirEntryPath.extension().string()) != removeExtensions.cend()) {
136
1/1
✓ Branch 1 taken 19 times.
19 fs::remove(dirEntryPath);
137 }
138 }
139 12 }
140 12 }
141
142 /*****************************************************************************************************************************************************
143 * E N E R G Y P L U S O U T P U T R E Q U E S T S *
144 *****************************************************************************************************************************************************/
145
146 2 bool mergeOutputTableSummaryReports(IdfObject& existingObject, const IdfObject& newObject) {
147
148 2 bool added = false;
149
150 // Merge
151 2 std::vector<std::string> reports;
152 {
153
1/1
✓ Branch 1 taken 2 times.
2 auto existinEgs = existingObject.extensibleGroups();
154
1/1
✓ Branch 2 taken 2 times.
2 reports.reserve(existinEgs.size());
155
5/5
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 12 taken 2 times.
✓ Branch 17 taken 2 times.
6 std::transform(existinEgs.cbegin(), existinEgs.cend(), std::back_inserter(reports), [](const auto& eg) { return eg.getString(0).get(); });
156 2 }
157
158 2 std::vector<std::string> reportsToAdd;
159 {
160
1/1
✓ Branch 1 taken 2 times.
2 auto newEgs = newObject.extensibleGroups();
161
5/5
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 12 taken 2 times.
✓ Branch 17 taken 2 times.
8 std::transform(newEgs.cbegin(), newEgs.cend(), std::back_inserter(reportsToAdd), [](const auto& eg) { return eg.getString(0).get(); });
162 2 }
163
164
2/2
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 2 times.
8 for (const auto& newReport : reportsToAdd) {
165
3/3
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 2 times.
6 if (std::find(reports.cbegin(), reports.cend(), newReport) == reports.cend()) {
166
4/6
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 4 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
12 existingObject.pushExtensibleGroup({newReport});
167 4 added = true;
168 }
169 }
170
171 2 return added;
172 6 }
173
174 22 bool isEnergyPlusOutputRequestPotentiallyUnsafe(const IddObjectType& iddObjectType) {
175
1/1
✓ Branch 1 taken 22 times.
22 std::string const valueName = iddObjectType.valueName();
176
1/1
✓ Branch 3 taken 22 times.
22 return std::none_of(util::safe_idd_prefixes.cbegin(), util::safe_idd_prefixes.cend(),
177 129 [&valueName](std::string_view s) { return valueName.starts_with(s); });
178 22 }
179
180 9 bool addEnergyPlusOutputRequest(Workspace& workspace, IdfObject& idfObject) {
181
1/1
✓ Branch 1 taken 9 times.
9 const auto iddObject = idfObject.iddObject();
182
1/1
✓ Branch 1 taken 9 times.
9 const bool is_unique = iddObject.properties().unique;
183
1/1
✓ Branch 1 taken 9 times.
9 const auto iddObjectType = iddObject.type();
184
185 // If not marked safe, we Warn, but we let you do it anyways
186
2/3
✓ Branch 1 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
9 if (isEnergyPlusOutputRequestPotentiallyUnsafe(iddObjectType)) {
187 ✗ LOG_FREE(Warn, "openstudio.worklow.Util",
188 "Requested to add an EnergyPlus Output Request of type '"
189 << iddObjectType.valueName() << "' which is not marked as safe: make sure you aren't going to modify the energy usage of the model.");
190 }
191
192
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (!is_unique) {
193
194 // If already present, don't do it
195
3/3
✓ Branch 1 taken 6 times.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 3 times.
11 for (const auto& wo : workspace.getObjectsByType(iddObjectType)) {
196
3/3
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5 times.
8 if (idfObject.dataFieldsEqual(wo)) {
197 3 return false;
198 }
199 6 }
200
201
1/1
✓ Branch 1 taken 3 times.
3 workspace.addObject(idfObject);
202
203 3 return true;
204
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
3 } else if (iddObjectType == IddObjectType::Output_Table_SummaryReports) {
205
1/1
✓ Branch 1 taken 1 time.
1 auto summaryReports = workspace.getObjectsByType(iddObjectType);
206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (summaryReports.empty()) {
207 ✗ workspace.addObject(idfObject);
208 ✗ return true;
209 } else {
210
1/1
✓ Branch 2 taken 1 time.
1 return mergeOutputTableSummaryReports(summaryReports.front(), idfObject);
211 }
212 1 } else {
213 // It's a unique one, for which we didn't write a merge, so remove it first
214
1/1
✓ Branch 1 taken 2 times.
2 auto existingUniqueObjects = workspace.getObjectsByType(iddObjectType);
215
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
2 if (!existingUniqueObjects.empty()) {
216 // Technically it should be size == 1
217
2/3
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
2 if (std::ranges::any_of(existingUniqueObjects, [&idfObject](const auto& wo) { return idfObject.dataFieldsEqual(wo); })) {
218 ✗ return false;
219 }
220
2/2
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
2 for (auto& wo : existingUniqueObjects) {
221
1/1
✓ Branch 1 taken 1 time.
1 wo.remove();
222 }
223 }
224
1/1
✓ Branch 1 taken 2 times.
2 workspace.addObject(idfObject);
225 2 return true;
226 2 }
227 9 }
228
229 /*****************************************************************************************************************************************************
230 * O U T P U T A D A P T E R *
231 *****************************************************************************************************************************************************/
232
233 22 void zipDirectory(const openstudio::path& dirPath, const openstudio::path& zipFilePath) {
234
235 namespace fs = openstudio::filesystem;
236
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (fs::exists(zipFilePath)) {
238 ✗ fs::remove(zipFilePath);
239 }
240
241 {
242
1/1
✓ Branch 1 taken 22 times.
22 openstudio::ZipFile zf(zipFilePath, false); // Passing add = false means you create the zip, not append
243
244 static constexpr std::array<std::string_view, 3> filterOutDirNames{"seed", "measures", "weather"};
245
246
7/7
✓ Branch 1 taken 22 times.
✓ Branch 5 taken 22 times.
✓ Branch 9 taken 344 times.
✓ Branch 12 taken 344 times.
✓ Branch 15 taken 366 times.
✓ Branch 17 taken 344 times.
✓ Branch 18 taken 22 times.
366 for (const auto& dirEnt : fs::directory_iterator{dirPath}) {
247 344 const auto& dirEntryPath = dirEnt.path();
248
3/3
✓ Branch 1 taken 344 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 335 times.
344 if (fs::is_directory(dirEntryPath)) {
249
250 // Skip a few directory that should not be zipped as they are inputs
251
2/3
✓ Branch 4 taken 9 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 9 times.
9 if (std::find(filterOutDirNames.cbegin(), filterOutDirNames.cend(), dirEntryPath.extension().string()) != filterOutDirNames.cend()) {
252 ✗ continue;
253 }
254
255 // TODO: do I need a helper like the workflow-gem was doing with add_directory_to_zip?
256
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 zf.addDirectory(dirEntryPath, fs::relative(dirEntryPath, dirPath));
257 } else {
258
1/1
✓ Branch 2 taken 335 times.
335 auto ext = dirEntryPath.extension().string();
259
5/6
✓ Branch 1 taken 313 times.
✓ Branch 2 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 313 times.
✓ Branch 6 taken 22 times.
✓ Branch 7 taken 313 times.
335 if ((ext.find(".zip") != std::string::npos) || (ext.find(".rb") != std::string::npos)) {
260 22 continue;
261 }
262
2/2
✓ Branch 1 taken 313 times.
✓ Branch 4 taken 313 times.
313 zf.addFile(dirEntryPath, fs::relative(dirEntryPath, dirPath));
263 335 }
264 22 }
265 22 }
266
267 // chmod 644. TODO: is this necessary? 644 should be default already
268 22 boost::filesystem::permissions(zipFilePath, boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_write
269 | boost::filesystem::perms::group_read | boost::filesystem::perms::others_read);
270 22 }
271
272 22 void zipResults(const openstudio::path& dirPath) {
273
274 namespace fs = openstudio::filesystem;
275
276
5/8
✓ Branch 1 taken 22 times.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 22 times.
✓ Branch 8 taken 22 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
22 if (fs::exists(dirPath) && fs::is_directory(dirPath)) {
277
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
44 zipDirectory(dirPath, dirPath / "data_point.zip");
278 }
279
280 // TODO: the reports folder is not inside the run/ directory though?
281 // zip up only the reports folder
282
1/1
✓ Branch 1 taken 22 times.
44 auto reportDirPath = dirPath / "reports";
283
3/8
✓ Branch 1 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 22 times.
22 if (fs::exists(reportDirPath) && fs::is_directory(reportDirPath)) {
284 ✗ zipDirectory(reportDirPath, dirPath / "data_point_reports.zip");
285 }
286 22 }
287
288 // TODO: investigate removing this. `OSRunner::registerValue` already calls `OSRunner::cleanValueName`, perhaps we just reuse that
289 249 std::string sanitizeKey(std::string key) {
290
4/7
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 233 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
249 static const std::regex invalidCharsRegex(R"([|!@#$%^&*(){}\\[\];:'",<.>/?+=]+)");
291
4/7
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 233 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
249 static const std::regex squeezeUnderscoresRegex(R"(_{2,})");
292
4/7
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 233 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
249 static const std::regex trailingUnderscoreOrWhiteSpaceRegex(R"((_|\s)+$)");
293
294
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 216 times.
249 if (std::regex_search(key, invalidCharsRegex)) {
295
5/5
✓ Branch 1 taken 33 times.
✓ Branch 5 taken 33 times.
✓ Branch 9 taken 33 times.
✓ Branch 12 taken 33 times.
✓ Branch 15 taken 33 times.
132 LOG_FREE(Warn, "openstudio.worklow.Util", fmt::format("Renaming result key '{}' to remove invalid characters", key));
296 // Replace invalid characters with underscores
297
1/1
✓ Branch 1 taken 33 times.
33 key = std::regex_replace(key, invalidCharsRegex, "_");
298 }
299
300 // Squeeze consecutive underscores
301
1/1
✓ Branch 1 taken 249 times.
249 key = std::regex_replace(key, squeezeUnderscoresRegex, "_");
302 // Strip trailing underscores or whitespace
303
1/1
✓ Branch 1 taken 249 times.
249 key = std::regex_replace(key, trailingUnderscoreOrWhiteSpaceRegex, "");
304
305 249 return key;
306 }
307
308 35 bool addResultMeasureInfo(WorkflowStepResult& result, BCLMeasure& measure) {
309 try {
310
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureType(measure.measureType());
311
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureName(measure.name());
312
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureId(measure.uid());
313
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureVersionId(measure.versionId());
314
1/1
✓ Branch 1 taken 35 times.
35 auto version_modified_ = measure.versionModified();
315
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (version_modified_) {
316
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureVersionModified(*version_modified_);
317 }
318
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureXmlChecksum(measure.xmlChecksum());
319
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureClassName(measure.className());
320
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureDisplayName(measure.displayName());
321
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 result.setMeasureTaxonomy(measure.taxonomyTag());
322 35 return true;
323 35 } catch (...) {
324 ✗ return false;
325 ✗ }
326 }
327
328 } // namespace openstudio::workflow::util
329