6 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
7 #include "otsdaq/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)
56 std::cout <<
"Recognized parameter 1. as a 'help' option. Usage was printed. Exiting." << std::endl;
61 std::string pathToSwapIn =
"";
63 sscanf(argv[1],
"%d", &flatVersion);
65 pathToSwapIn = argv[2];
67 __COUT__ <<
"flatVersion = " << flatVersion << std::endl;
68 __COUT__ <<
"pathToSwapIn = " << pathToSwapIn << std::endl;
76 setenv(
"CONFIGURATION_TYPE",
"File", 1);
77 setenv(
"CONFIGURATION_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/ConfigurationDataExamples").c_str(), 1);
78 setenv(
"TABLE_INFO_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/TableInfo").c_str(), 1);
82 setenv(
"SERVICE_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/ServiceData").c_str(), 1);
85 setenv(
"XDAQ_CONFIGURATION_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/XDAQConfigurations").c_str(), 1);
86 setenv(
"XDAQ_CONFIGURATION_XML",
"otsConfigurationNoRU_CMake", 1);
96 __COUT__ <<
"\n\n\nLoading activeGroupsMap..." << std::endl;
99 std::map<std::string, std::pair<std::string, TableGroupKey> > activeGroupsMap = cfgMgr->getActiveTableGroups();
101 std::map<std::string, std::map<std::string, TableVersion> > activeGroupMembersMap;
102 std::map<std::string, std::map<std::string , std::string > > activeGroupAliasesMap;
103 std::map<std::string, std::string> activeGroupCommentMap;
104 std::map<std::string, std::string> activeGroupAuthorMap;
105 std::string groupCreateTime;
106 std::map<std::string, time_t> activeGroupCreateTimeMap;
107 TableBase* groupMetadataTable = cfgMgr->getMetadataTable();
109 for(
auto& activeGroupPair : activeGroupsMap)
111 if(activeGroupPair.second.second.isInvalid())
113 __COUT__ <<
"Skipping invalid " << activeGroupPair.first << std::endl;
117 __COUT__ <<
"Loading members for " << activeGroupPair.first <<
"\t" << activeGroupPair.second.first <<
"(" << activeGroupPair.second.second <<
")"
130 cfgMgr->loadTableGroup(activeGroupPair.second.first,
131 activeGroupPair.second.second,
133 &activeGroupMembersMap[activeGroupPair.second.first] ,
136 &activeGroupCommentMap[activeGroupPair.second.first],
137 &activeGroupAuthorMap[activeGroupPair.second.first],
141 &activeGroupAliasesMap[activeGroupPair.second.first]);
142 sscanf(groupCreateTime.c_str(),
"%ld", &activeGroupCreateTimeMap[activeGroupPair.second.first]);
144 catch(std::runtime_error& e)
146 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first <<
"(" << groupPair.first.second <<
")" << std::endl;
147 __COUT__ << e.what() << std::endl;
152 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first <<
"(" << groupPair.first.second <<
")" << std::endl;
161 std::string currentDir = __ENV__(
"ARTDAQ_DATABASE_URI");
163 if(currentDir.find(
"filesystemdb://") != 0)
165 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << std::endl;
166 __COUT_ERR__ <<
"\n" << ss.str();
170 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
171 while(currentDir.length() && currentDir[currentDir.length() - 1] ==
'/')
172 currentDir = currentDir.substr(0, currentDir.length() - 1);
173 std::string moveToDir = currentDir +
"_" + std::to_string(time(0));
175 __COUT__ <<
"Moving current directory: \t" << currentDir << std::endl;
176 __COUT__ <<
"\t... to: \t\t" << moveToDir << std::endl;
180 __SS__ << (
"Aborting move! Must at least give version argument to flatten to!") << std::endl;
181 __COUT_ERR__ <<
"\n" << ss.str();
185 rename(currentDir.c_str(), moveToDir.c_str());
186 FILE* fp = fopen((moveToDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
188 __COUT__ <<
"\tError opening README file!" << std::endl;
196 timeinfo = localtime(&rawtime);
197 strftime(buffer, 200,
"%b %d, %Y %I:%M%p %Z", timeinfo);
200 "This database was moved from...\n\t %s \nto...\n\t %s \nat this time "
201 "\n\t %lu \t %s\n\n\n",
210 if(pathToSwapIn !=
"")
213 if((dp = opendir(pathToSwapIn.c_str())) == 0)
215 __COUT__ <<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn << std::endl;
220 __COUT__ <<
"Swapping in directory: \t" << pathToSwapIn << std::endl;
221 __COUT__ <<
"\t.. to: \t\t" << currentDir << std::endl;
223 rename(pathToSwapIn.c_str(), currentDir.c_str());
224 FILE* fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
226 __COUT__ <<
"\tError opening README file!" << std::endl;
234 timeinfo = localtime(&rawtime);
235 strftime(buffer, 200,
"%b %d, %Y %I:%M:%S%p %Z", timeinfo);
238 "This database was moved from...\t %s \t to...\t %s at this time \t "
240 pathToSwapIn.c_str(),
255 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions();
260 const std::string groupAliasesName = ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
261 const std::string versionAliasesName = ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
266 __COUT__ <<
"\n\nflatVersion " <<
TableVersion(flatVersion) <<
" is an invalid or temporary version. Skipping to end!" << std::endl;
271 if(activeMap.find(groupAliasesName) != activeMap.end())
273 __COUT__ <<
"\n\nModifying " << groupAliasesName << std::endl;
274 config = cfgMgr->getTableByName(groupAliasesName);
275 cfgView = config->getViewP();
277 unsigned int col1 = cfgView->findCol(
"GroupName");
278 unsigned int col2 = cfgView->findCol(
"GroupKey");
281 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
282 for(
auto& activeGroupPair : activeGroupsMap)
283 if(activeGroupPair.second.second.isInvalid())
285 else if(cfgView->getDataView()[row][col1] == activeGroupPair.second.first &&
286 cfgView->getDataView()[row][col2] == activeGroupPair.second.second.toString())
289 __COUT__ <<
"Changing row " << row <<
" for " << cfgView->getDataView()[row][col1] <<
" key=" << cfgView->getDataView()[row][col2]
290 <<
" to DEFAULT=" <<
TableGroupKey(TableGroupKey::getDefaultKey()) << std::endl;
291 cfgView->setValue(
TableGroupKey(TableGroupKey::getDefaultKey()).toString(), row, col2);
297 if(activeMap.find(versionAliasesName) != activeMap.end())
299 __COUT__ <<
"\n\nModifying " << versionAliasesName << std::endl;
300 config = cfgMgr->getTableByName(versionAliasesName);
301 cfgView = config->getViewP();
302 unsigned int col1 = cfgView->findCol(
"TableName");
303 unsigned int col2 = cfgView->findCol(
"Version");
306 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
307 for(
auto& activePair : activeMap)
308 if(cfgView->getDataView()[row][col1] == activePair.first && cfgView->getDataView()[row][col2] == activePair.second.toString())
311 __COUT__ <<
"Changing row " << row <<
" for " << cfgView->getDataView()[row][col1] <<
" version=" << cfgView->getDataView()[row][col2]
312 <<
" to flatVersion=" <<
TableVersion(flatVersion) << std::endl;
313 cfgView->setValue(
TableVersion(flatVersion).toString(), row, col2);
318 __COUT__ <<
"\n\nChanging versions... " << std::endl;
320 for(
auto& activePair : activeMap)
322 __COUT__ << activePair.first <<
":v" << activePair.second << std::endl;
324 config = cfgMgr->getTableByName(activePair.first);
325 cfgView = config->getViewP();
327 theInterface_->saveActiveVersion(config);
333 for(
auto& activeGroupMembersPair : activeGroupMembersMap)
335 __COUT__ <<
"Group " << activeGroupMembersPair.first << std::endl;
337 for(
auto& groupMemberPair : activeGroupMembersPair.second)
339 __COUT__ <<
"\t from...\t" << groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
340 groupMemberPair.second = flatVersion;
343 for(
auto& groupMemberPair : activeGroupMembersPair.second)
345 __COUT__ <<
"\t to...\t" << groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
351 __COUTV__(StringMacros::mapToString(activeGroupAliasesMap[activeGroupMembersPair.first]));
352 __COUTV__(activeGroupCommentMap[activeGroupMembersPair.first]);
353 __COUTV__(activeGroupAuthorMap[activeGroupMembersPair.first]);
354 __COUTV__(activeGroupCreateTimeMap[activeGroupMembersPair.first]);
358 while(groupMetadataTable->getViewP()->getNumberOfRows() > 1)
359 groupMetadataTable->getViewP()->deleteRow(0);
360 if(groupMetadataTable->getViewP()->getNumberOfRows() == 0)
361 groupMetadataTable->getViewP()->addRow();
365 groupMetadataTable->getViewP()->setValue(
366 StringMacros::mapToString(activeGroupAliasesMap[activeGroupMembersPair.first],
"," ,
":" ),
368 ConfigurationManager::METADATA_COL_ALIASES);
369 groupMetadataTable->getViewP()->setValue(activeGroupCommentMap[activeGroupMembersPair.first], 0, ConfigurationManager::METADATA_COL_COMMENT);
370 groupMetadataTable->getViewP()->setValue(activeGroupAuthorMap[activeGroupMembersPair.first], 0, ConfigurationManager::METADATA_COL_AUTHOR);
371 groupMetadataTable->getViewP()->setValue(activeGroupCreateTimeMap[activeGroupMembersPair.first], 0, ConfigurationManager::METADATA_COL_TIMESTAMP);
374 groupMetadataTable->getViewP()->setVersion(
TableVersion(flatVersion));
375 theInterface_->saveActiveVersion(groupMetadataTable);
378 activeGroupMembersPair.second[groupMetadataTable->getTableName()] = groupMetadataTable->getViewVersion();
381 theInterface_->saveTableGroup(activeGroupMembersPair.second,
382 TableGroupKey::getFullGroupString(activeGroupMembersPair.first,
TableGroupKey(TableGroupKey::getDefaultKey())));
387 __COUT__ <<
"\n\nEnd of Flattening Active Table Groups!\n\n\n" << std::endl;
389 __COUT__ <<
"Run the following to return to your previous database structure:" << std::endl;
390 __COUT__ <<
"\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
"\n\n" << std::endl;
395 int main(
int argc,
char* argv[])
397 FlattenActiveTableGroups(argc, argv);