6 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
7 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
19 void FlattenActiveTableGroups(
int argc,
char* argv[])
21 std::cout <<
"=================================================\n";
22 std::cout <<
"=================================================\n";
23 std::cout <<
"=================================================\n";
24 __COUT__ <<
"\nFlattening Active Table Groups!" << std::endl;
26 std::cout <<
"\n\nusage: Two arguments:\n\t pathToSwapIn <flatVersion> <pathToSwapIn "
28 <<
"\t Default values: flatVersion = 0, pathToSwapIn = \"\" \n\n"
31 std::cout <<
"\n\nNote: you can optionally just swap databases (and not modify their "
33 <<
" by providing an invalid flatVersion of -1.\n\n"
36 std::cout <<
"\n\nNote: This assumes artdaq db file type interface. "
37 <<
"The current database/ will be moved to database_<linuxtime>/ "
38 <<
"and if a pathToSwapIn is specified it will be copied to database/ "
39 <<
"before saving the currently active groups.\n\n"
42 std::cout <<
"argc = " << argc << std::endl;
43 for(
int i = 0; i < argc; i++)
44 std::cout <<
"argv[" << i <<
"] = " << argv[i] << std::endl;
48 std::cout <<
"Must provide at least one parameter.";
53 std::string flatVersionStr = argv[1];
54 if(flatVersionStr.find(
'h') != std::string::npos)
57 <<
"Recognized parameter 1. as a 'help' option. Usage was printed. Exiting."
63 std::string pathToSwapIn =
"";
65 sscanf(argv[1],
"%d", &flatVersion);
67 pathToSwapIn = argv[2];
69 __COUT__ <<
"flatVersion = " << flatVersion << std::endl;
70 __COUT__ <<
"pathToSwapIn = " << pathToSwapIn << std::endl;
78 setenv(
"CONFIGURATION_TYPE",
"File", 1);
79 setenv(
"CONFIGURATION_DATA_PATH",
80 (std::string(__ENV__(
"USER_DATA")) +
"/ConfigurationDataExamples").c_str(),
83 "TABLE_INFO_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/TableInfo").c_str(), 1);
87 setenv(
"SERVICE_DATA_PATH",
88 (std::string(__ENV__(
"USER_DATA")) +
"/ServiceData").c_str(),
92 setenv(
"XDAQ_CONFIGURATION_DATA_PATH",
93 (std::string(__ENV__(
"USER_DATA")) +
"/XDAQConfigurations").c_str(),
95 setenv(
"XDAQ_CONFIGURATION_XML",
"otsConfigurationNoRU_CMake", 1);
105 __COUT__ <<
"\n\n\nLoading activeGroupsMap..." << std::endl;
108 std::map<std::string, std::pair<std::string, TableGroupKey> > activeGroupsMap =
109 cfgMgr->getActiveTableGroups();
111 std::map<std::string, std::map<std::string, TableVersion> > activeGroupMembersMap;
112 std::map<std::string, std::map<std::string , std::string > >
113 activeGroupAliasesMap;
114 std::map<std::string, std::string> activeGroupCommentMap;
115 std::map<std::string, std::string> activeGroupAuthorMap;
116 std::string groupCreateTime;
117 std::map<std::string, time_t> activeGroupCreateTimeMap;
118 TableBase* groupMetadataTable = cfgMgr->getMetadataTable();
120 for(
auto& activeGroupPair : activeGroupsMap)
122 if(activeGroupPair.second.second.isInvalid())
124 __COUT__ <<
"Skipping invalid " << activeGroupPair.first << std::endl;
128 __COUT__ <<
"Loading members for " << activeGroupPair.first <<
"\t"
129 << activeGroupPair.second.first <<
"(" << activeGroupPair.second.second
142 cfgMgr->loadTableGroup(
143 activeGroupPair.second.first,
144 activeGroupPair.second.second,
146 &activeGroupMembersMap[activeGroupPair.second.first] ,
149 &activeGroupCommentMap[activeGroupPair.second.first],
150 &activeGroupAuthorMap[activeGroupPair.second.first],
154 &activeGroupAliasesMap[activeGroupPair.second.first]);
155 sscanf(groupCreateTime.c_str(),
157 &activeGroupCreateTimeMap[activeGroupPair.second.first]);
159 catch(std::runtime_error& e)
161 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
162 <<
"(" << groupPair.first.second <<
")" << std::endl;
163 __COUT__ << e.what() << std::endl;
168 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
169 <<
"(" << groupPair.first.second <<
")" << std::endl;
178 std::string currentDir = __ENV__(
"ARTDAQ_DATABASE_URI");
180 if(currentDir.find(
"filesystemdb://") != 0)
182 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << std::endl;
183 __COUT_ERR__ <<
"\n" << ss.str();
187 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
188 while(currentDir.length() &&
189 currentDir[currentDir.length() - 1] ==
'/')
190 currentDir = currentDir.substr(0, currentDir.length() - 1);
191 std::string moveToDir = currentDir +
"_" + std::to_string(time(0));
193 __COUT__ <<
"Moving current directory: \t" << currentDir << std::endl;
194 __COUT__ <<
"\t... to: \t\t" << moveToDir << std::endl;
198 __SS__ << (
"Aborting move! Must at least give version argument to flatten to!")
200 __COUT_ERR__ <<
"\n" << ss.str();
204 rename(currentDir.c_str(), moveToDir.c_str());
205 FILE* fp = fopen((moveToDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
207 __COUT__ <<
"\tError opening README file!" << std::endl;
215 timeinfo = localtime(&rawtime);
216 strftime(buffer, 200,
"%b %d, %Y %I:%M%p %Z", timeinfo);
219 "This database was moved from...\n\t %s \nto...\n\t %s \nat this time "
220 "\n\t %lu \t %s\n\n\n",
229 if(pathToSwapIn !=
"")
232 if((dp = opendir(pathToSwapIn.c_str())) == 0)
234 __COUT__ <<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn
240 __COUT__ <<
"Swapping in directory: \t" << pathToSwapIn << std::endl;
241 __COUT__ <<
"\t.. to: \t\t" << currentDir << std::endl;
243 rename(pathToSwapIn.c_str(), currentDir.c_str());
244 FILE* fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
246 __COUT__ <<
"\tError opening README file!" << std::endl;
254 timeinfo = localtime(&rawtime);
255 strftime(buffer, 200,
"%b %d, %Y %I:%M:%S%p %Z", timeinfo);
258 "This database was moved from...\t %s \t to...\t %s at this time \t "
260 pathToSwapIn.c_str(),
276 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions();
281 const std::string groupAliasesName = ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
282 const std::string versionAliasesName =
283 ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
289 __COUT__ <<
"\n\nflatVersion " <<
TableVersion(flatVersion)
290 <<
" is an invalid or temporary version. Skipping to end!" << std::endl;
295 if(activeMap.find(groupAliasesName) != activeMap.end())
297 __COUT__ <<
"\n\nModifying " << groupAliasesName << std::endl;
298 config = cfgMgr->getTableByName(groupAliasesName);
299 cfgView = config->getViewP();
301 unsigned int col1 = cfgView->findCol(
"GroupName");
302 unsigned int col2 = cfgView->findCol(
"GroupKey");
305 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
306 for(
auto& activeGroupPair : activeGroupsMap)
307 if(activeGroupPair.second.second.isInvalid())
309 else if(cfgView->getDataView()[row][col1] ==
310 activeGroupPair.second.first &&
311 cfgView->getDataView()[row][col2] ==
312 activeGroupPair.second.second.toString())
315 __COUT__ <<
"Changing row " << row <<
" for "
316 << cfgView->getDataView()[row][col1]
317 <<
" key=" << cfgView->getDataView()[row][col2]
330 if(activeMap.find(versionAliasesName) != activeMap.end())
332 __COUT__ <<
"\n\nModifying " << versionAliasesName << std::endl;
333 config = cfgMgr->getTableByName(versionAliasesName);
334 cfgView = config->getViewP();
335 unsigned int col1 = cfgView->findCol(
"TableName");
336 unsigned int col2 = cfgView->findCol(
"Version");
339 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
340 for(
auto& activePair : activeMap)
341 if(cfgView->getDataView()[row][col1] == activePair.first &&
342 cfgView->getDataView()[row][col2] == activePair.second.toString())
345 __COUT__ <<
"Changing row " << row <<
" for "
346 << cfgView->getDataView()[row][col1]
347 <<
" version=" << cfgView->getDataView()[row][col2]
350 cfgView->setValue(
TableVersion(flatVersion).toString(), row, col2);
355 __COUT__ <<
"\n\nChanging versions... " << std::endl;
357 for(
auto& activePair : activeMap)
359 __COUT__ << activePair.first <<
":v" << activePair.second << std::endl;
361 config = cfgMgr->getTableByName(activePair.first);
362 cfgView = config->getViewP();
364 theInterface_->saveActiveVersion(config);
370 for(
auto& activeGroupMembersPair : activeGroupMembersMap)
372 __COUT__ <<
"Group " << activeGroupMembersPair.first << std::endl;
374 for(
auto& groupMemberPair : activeGroupMembersPair.second)
376 __COUT__ <<
"\t from...\t" << groupMemberPair.first <<
":v"
377 << groupMemberPair.second << std::endl;
378 groupMemberPair.second = flatVersion;
381 for(
auto& groupMemberPair : activeGroupMembersPair.second)
383 __COUT__ <<
"\t to...\t" << groupMemberPair.first <<
":v"
384 << groupMemberPair.second << std::endl;
390 __COUTV__(StringMacros::mapToString(
391 activeGroupAliasesMap[activeGroupMembersPair.first]));
392 __COUTV__(activeGroupCommentMap[activeGroupMembersPair.first]);
393 __COUTV__(activeGroupAuthorMap[activeGroupMembersPair.first]);
394 __COUTV__(activeGroupCreateTimeMap[activeGroupMembersPair.first]);
398 while(groupMetadataTable->getViewP()->getNumberOfRows() > 1)
399 groupMetadataTable->getViewP()->deleteRow(0);
400 if(groupMetadataTable->getViewP()->getNumberOfRows() == 0)
401 groupMetadataTable->getViewP()->addRow();
405 groupMetadataTable->getViewP()->setValue(
406 StringMacros::mapToString(activeGroupAliasesMap[activeGroupMembersPair.first],
410 ConfigurationManager::METADATA_COL_ALIASES);
411 groupMetadataTable->getViewP()->setValue(
412 activeGroupCommentMap[activeGroupMembersPair.first],
414 ConfigurationManager::METADATA_COL_COMMENT);
415 groupMetadataTable->getViewP()->setValue(
416 activeGroupAuthorMap[activeGroupMembersPair.first],
418 ConfigurationManager::METADATA_COL_AUTHOR);
419 groupMetadataTable->getViewP()->setValue(
420 activeGroupCreateTimeMap[activeGroupMembersPair.first],
422 ConfigurationManager::METADATA_COL_TIMESTAMP);
425 groupMetadataTable->getViewP()->setVersion(
TableVersion(flatVersion));
426 theInterface_->saveActiveVersion(groupMetadataTable);
429 activeGroupMembersPair.second[groupMetadataTable->getTableName()] =
430 groupMetadataTable->getViewVersion();
433 theInterface_->saveTableGroup(activeGroupMembersPair.second,
434 TableGroupKey::getFullGroupString(
435 activeGroupMembersPair.first,
441 __COUT__ <<
"\n\nEnd of Flattening Active Table Groups!\n\n\n" << std::endl;
443 __COUT__ <<
"Run the following to return to your previous database structure:"
445 __COUT__ <<
"\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
"\n\n"
451 int main(
int argc,
char* argv[])
453 FlattenActiveTableGroups(argc, argv);