7 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
8 #include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h"
20 void FixNewTableFields(
int argc,
char* argv[])
22 std::cout <<
"=================================================\n";
23 std::cout <<
"=================================================\n";
24 std::cout <<
"=================================================\n";
25 __COUT__ <<
"\nFixing new table fields!" << __E__;
27 std::cout <<
"\n\nusage: Two arguments:\n\t <pathToSwapIn (optional)> \n\n"
28 <<
"\t Default values: pathToSwapIn = \"\" \n\n"
31 std::cout <<
"\n\nNote: This assumes artdaq db file type interface. "
32 <<
"The current database/ will be moved to database_<linuxtime>/ "
33 <<
"unless a pathToSwapIn is specified, in which case the path will "
34 <<
"be copied overwriting database/ \n\n"
37 std::cout <<
"argc = " << argc << __E__;
38 for(
int i = 0; i < argc; i++)
39 std::cout <<
"argv[" << i <<
"] = " << argv[i] << __E__;
43 std::cout <<
"Error! Must provide at most one parameter.\n\n" << __E__;
48 std::string pathToSwapIn =
"";
50 pathToSwapIn = argv[1];
52 if(pathToSwapIn ==
"-h" || pathToSwapIn ==
"--help")
54 std::cout <<
"Recognized parameter 1 as a 'help' option. Usage was printed. Exiting." << __E__;
58 __COUTV__(pathToSwapIn);
67 setenv(
"CONFIGURATION_TYPE",
"File", 1);
68 setenv(
"CONFIGURATION_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/ConfigurationDataExamples").c_str(), 1);
69 setenv(
"TABLE_INFO_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/TableInfo").c_str(), 1);
73 setenv(
"SERVICE_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/ServiceData").c_str(), 1);
77 setenv(
"OTSDAQ_LIB", (std::string(__ENV__(
"USER_DATA")) +
"/").c_str(), 1);
78 setenv(
"OTSDAQ_UTILITIES_LIB", (std::string(__ENV__(
"USER_DATA")) +
"/").c_str(), 1);
81 setenv(
"OTS_MAIN_PORT",
"2015", 1);
84 setenv(
"XDAQ_CONFIGURATION_DATA_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/XDAQConfigurations").c_str(), 1);
85 setenv(
"XDAQ_CONFIGURATION_XML",
"otsConfigurationNoRU_CMake", 1);
92 __COUT__ <<
"Loading active Aliases..." << __E__;
120 std::map<std::pair<std::string, TableGroupKey>,
TableGroupKey> groupSet;
122 std::map<std::pair<std::string, TableVersion>,
TableVersion> modifiedTables;
123 std::map<std::string, std::pair<TableGroupKey, TableGroupKey>> activeGroupKeys;
124 std::map<std::pair<std::string, TableGroupKey>, std::string> groupErrors;
126 std::string activeBackboneGroupName =
"";
127 std::string activeContextGroupName =
"";
128 std::string activeIterateGroupName =
"";
129 std::string activeConfigGroupName =
"";
131 std::string nowTime = std::to_string(time(0));
133 std::string thenTime =
"";
134 if(pathToSwapIn !=
"")
136 thenTime = pathToSwapIn.substr(pathToSwapIn.rfind(
'_') + 1);
137 __COUT__ <<
"thenTime = " << thenTime << __E__;
142 std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap = cfgMgr->getActiveTableGroups();
144 for(
const auto& activeGroup : activeGroupsMap)
146 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
147 std::pair<std::string, TableGroupKey>(activeGroup.second.first, activeGroup.second.second),
TableGroupKey()));
148 activeGroupKeys.insert(std::pair<std::string, std::pair<TableGroupKey, TableGroupKey>>(
149 activeGroup.second.first, std::pair<TableGroupKey, TableGroupKey>(activeGroup.second.second,
TableGroupKey())));
151 if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE)
153 activeBackboneGroupName = activeGroup.second.first;
154 __COUT__ <<
"found activeBackboneGroupName = " << activeBackboneGroupName << __E__;
156 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONTEXT)
158 activeContextGroupName = activeGroup.second.first;
159 __COUT__ <<
"found activeContextGroupName = " << activeContextGroupName << __E__;
161 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_ITERATE)
163 activeIterateGroupName = activeGroup.second.first;
164 __COUT__ <<
"found activeIterateGroupName = " << activeIterateGroupName << __E__;
166 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONFIGURATION)
168 activeConfigGroupName = activeGroup.second.first;
169 __COUT__ <<
"found activeConfigGroupName = " << activeConfigGroupName << __E__;
174 const std::string groupAliasesTableName = ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
175 std::map<std::string, TableVersion> activeVersions = cfgMgr->getActiveVersions();
176 if(activeVersions.find(groupAliasesTableName) == activeVersions.end())
178 __SS__ <<
"\nActive version of " << groupAliasesTableName <<
" missing! " << groupAliasesTableName
179 <<
" is a required member of the Backbone configuration group."
180 <<
"\n\nLikely you need to activate a valid Backbone group." << __E__;
184 std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs = cfgMgr->getNode(groupAliasesTableName).getChildren();
185 for(
auto& groupPair : aliasNodePairs)
186 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
187 std::pair<std::string, TableGroupKey>(groupPair.second.getNode(
"GroupName").getValueAsString(),
188 TableGroupKey(groupPair.second.getNode(
"GroupKey").getValueAsString())),
191 __COUT__ <<
"Identified groups:" << __E__;
192 for(
auto& group : groupSet)
193 __COUT__ << group.first.first <<
" " << group.first.second << __E__;
200 std::string currentDir = __ENV__(
"ARTDAQ_DATABASE_URI");
202 if(currentDir.find(
"filesystemdb://") != 0)
204 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << __E__;
208 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
209 while(currentDir.length() && currentDir[currentDir.length() - 1] ==
'/')
210 currentDir = currentDir.substr(0, currentDir.length() - 1);
211 std::string moveToDir = currentDir +
"_" + nowTime;
213 if(pathToSwapIn !=
"")
216 if((dp = opendir(pathToSwapIn.c_str())) == 0)
218 __COUT__ <<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn << __E__;
225 __COUT__ <<
"Copying current directory: \t" << currentDir << __E__;
226 __COUT__ <<
"\t... to: \t\t" << moveToDir << __E__;
228 rename(currentDir.c_str(), moveToDir.c_str());
230 if(pathToSwapIn !=
"")
232 __COUT__ <<
"Swapping in directory: \t" << pathToSwapIn << __E__;
233 __COUT__ <<
"\t.. to: \t\t" << currentDir << __E__;
234 rename(pathToSwapIn.c_str(), currentDir.c_str());
238 std::string activeGroupsFile = ConfigurationManager::ACTIVE_GROUPS_FILENAME +
"." + thenTime;
239 FILE* fp = fopen(activeGroupsFile.c_str(),
"r");
242 __COUT__ <<
"Swapping active groups file: \t" << activeGroupsFile << __E__;
243 __COUT__ <<
"\t.. to: \t\t" << ConfigurationManager::ACTIVE_GROUPS_FILENAME << __E__;
244 rename(activeGroupsFile.c_str(), ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str());
247 __COUT__ <<
"Path swapped in. Done." << __E__;
251 std::system((
"cp -r " + moveToDir +
" " + currentDir).c_str());
266 bool errDetected =
false;
267 std::string accumulateErrors =
"";
271 std::map<std::string , std::string > groupAliases;
272 std::string groupComment;
287 for(
auto& groupPair : groupSet)
291 __COUT__ <<
"****************************" << __E__;
292 __COUT__ <<
"Loading members for " << groupPair.first.first <<
"(" << groupPair.first.second <<
")" << __E__;
298 cfgMgr->loadTableGroup(groupPair.first.first,
299 groupPair.first.second,
311 catch(std::runtime_error& e)
313 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first <<
"(" << groupPair.first.second <<
")" << __E__;
314 __COUT__ << e.what() << __E__;
319 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first <<
"(" << groupPair.first.second <<
")" << __E__;
331 groupErrors.insert(std::pair<std::pair<std::string, TableGroupKey>, std::string>(
332 std::pair<std::string, TableGroupKey>(groupPair.first.first, groupPair.first.second),
"Error caught loading the group."));
340 __COUT__ <<
"Before member map: " << StringMacros::mapToString(memberMap) << __E__;
343 for(
auto& memberPair : memberMap)
345 __COUT__ << memberPair.first <<
":v" << memberPair.second << __E__;
349 if(modifiedTables.find(std::pair<std::string, TableVersion>(memberPair.first, memberPair.second)) != modifiedTables.end())
351 __COUT__ <<
"Table was already modified!" << __E__;
352 memberPair.second = modifiedTables[std::pair<std::string, TableVersion>(memberPair.first, memberPair.second)];
353 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v" << memberPair.second << __E__;
360 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(memberPair.first , memberPair.second
364 TableVersion persistentVersion = cfgMgr->saveNewTable(memberPair.first , temporaryVersion);
379 modifiedTables.insert(std::pair<std::pair<std::string, TableVersion>,
TableVersion>(
380 std::pair<std::string, TableVersion>(memberPair.first, memberPair.second), persistentVersion));
382 memberPair.second = persistentVersion;
385 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v" << memberPair.second << __E__;
389 __COUT__ <<
"After member map: " << StringMacros::mapToString(memberMap) << __E__;
393 TableGroupKey newGroupKey = cfgMgr->saveNewTableGroup(groupPair.first.first , memberMap, groupComment, &groupAliases);
449 if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() && activeGroupKeys[groupPair.first.first].first == groupPair.first.second)
450 activeGroupKeys[groupPair.first.first].second =
TableGroupKey(newGroupKey);
452 catch(std::runtime_error& e)
454 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" (" << groupPair.first.second <<
") " << __E__;
455 __COUT__ << e.what() << __E__;
457 groupErrors.insert(std::pair<std::pair<std::string, TableGroupKey>, std::string>(
458 std::pair<std::string, TableGroupKey>(groupPair.first.first, groupPair.first.second),
"Error caught saving the group."));
462 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" (" << groupPair.first.second <<
") " << __E__;
464 groupErrors.insert(std::pair<std::pair<std::string, TableGroupKey>, std::string>(
465 std::pair<std::string, TableGroupKey>(groupPair.first.first, groupPair.first.second),
"Error caught saving the group."));
474 FILE* fp = fopen((moveToDir +
"/README_fix_new_table_fields.txt").c_str(),
"a");
476 __COUT__ <<
"\tError opening README file!" << __E__;
484 timeinfo = localtime(&rawtime);
485 strftime(buffer, 200,
"%b %d, %Y %I:%M%p %Z", timeinfo);
488 "This database %s \n\t is a backup of %s \n\t BEFORE forcing to new "
489 "table fields \n\t and was created at this time \n\t %lu \t %s\n\n\n",
501 FILE* fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
504 __COUT__ <<
"\tError opening README file!" << __E__;
512 timeinfo = localtime(&rawtime);
513 strftime(buffer, 200,
"%b %d, %Y %I:%M:%S%p %Z", timeinfo);
516 "This database %s \n\t was forced to new table fields \n\t at this "
517 "time \n\t %lu \t %s\n\n\n",
550 if(activeBackboneGroupName ==
"")
552 __COUT__ <<
"No active Backbone table identified." << __E__;
556 __COUT__ <<
"Modifying the active Backbone table to reflect new table versions and "
562 cfgMgr->loadTableGroup(activeBackboneGroupName,
563 activeGroupKeys[activeBackboneGroupName].second,
573 const std::string groupAliasesName = ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
574 const std::string versionAliasesName = ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
576 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions();
579 if(activeMap.find(groupAliasesName) != activeMap.end())
581 __COUT__ <<
"\n\nModifying " << groupAliasesName << __E__;
584 __COUT__ <<
"Before member map: " << StringMacros::mapToString(memberMap) << __E__;
588 __COUT__ << groupAliasesName <<
":v" << memberMap[groupAliasesName] << __E__;
591 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(groupAliasesName , memberMap[groupAliasesName]
594 config = cfgMgr->getTableByName(groupAliasesName);
595 config->setActiveView(temporaryVersion);
596 cfgView = config->getViewP();
598 unsigned int col1 = cfgView->findCol(
"GroupName");
599 unsigned int col2 = cfgView->findCol(
"GroupKey");
606 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
609 for(
const auto& group : groupSet)
610 if(group.second.isInvalid())
612 else if(cfgView->getDataView()[row][col1] == group.first.first && cfgView->getDataView()[row][col2] == group.first.second.toString())
615 __COUT__ <<
"Changing row " << row <<
" for " << cfgView->getDataView()[row][col1] <<
" key=" << cfgView->getDataView()[row][col2]
616 <<
" to NEW key=" << group.second << __E__;
617 cfgView->setValue(group.second.toString(), row, col2);
623 cfgView->deleteRow(row--);
629 TableVersion persistentVersion = cfgMgr->saveNewTable(groupAliasesName , temporaryVersion);
637 memberMap[groupAliasesName] = persistentVersion;
639 __COUT__ <<
"\t to...\t" << groupAliasesName <<
":v" << memberMap[groupAliasesName] << __E__;
644 if(activeMap.find(versionAliasesName) != activeMap.end())
646 __COUT__ <<
"\n\nModifying " << versionAliasesName << __E__;
649 __COUT__ << versionAliasesName <<
":v" << memberMap[versionAliasesName] << __E__;
652 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(versionAliasesName , memberMap[versionAliasesName]
655 config = cfgMgr->getTableByName(versionAliasesName);
656 config->setActiveView(temporaryVersion);
657 cfgView = config->getViewP();
658 unsigned int col1 = cfgView->findCol(
"TableName");
659 unsigned int col2 = cfgView->findCol(
"Version");
663 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
666 for(
const auto& table : modifiedTables)
667 if(cfgView->getDataView()[row][col1] == table.first.first && cfgView->getDataView()[row][col2] == table.first.second.toString())
670 __COUT__ <<
"Changing row " << row <<
" for " << cfgView->getDataView()[row][col1] <<
" version=" << cfgView->getDataView()[row][col2]
671 <<
" to NEW version=" << table.second << __E__;
672 cfgView->setValue(table.second.toString(), row, col2);
678 cfgView->deleteRow(row--);
682 TableVersion persistentVersion = cfgMgr->saveNewTable(versionAliasesName , temporaryVersion);
690 memberMap[versionAliasesName] = persistentVersion;
692 __COUT__ <<
"\t to...\t" << versionAliasesName <<
":v" << memberMap[versionAliasesName] << __E__;
697 __COUT__ <<
"After member map: " << StringMacros::mapToString(memberMap) << __E__;
700 activeBackboneGroupName , memberMap, groupComment, 0 );
708 activeGroupKeys[activeBackboneGroupName].second =
TableGroupKey(newGroupKey);
710 __COUT__ <<
"New to-be-active backbone group " << activeBackboneGroupName <<
":v" << activeGroupKeys[activeBackboneGroupName].second << __E__;
718 __COUT__ <<
"Manipulating the Active Groups file..." << __E__;
721 FILE* fp = fopen(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(),
"r");
724 __SS__ <<
"Original active groups file '" << ConfigurationManager::ACTIVE_GROUPS_FILENAME <<
"' not found." << __E__;
728 __COUT__ <<
"Backing up file: " << ConfigurationManager::ACTIVE_GROUPS_FILENAME << __E__;
732 std::string renameFile = ConfigurationManager::ACTIVE_GROUPS_FILENAME +
"." + nowTime;
733 rename(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), renameFile.c_str());
735 __COUT__ <<
"Backup file name: " << renameFile << __E__;
737 TableGroupKey *theConfigurationTableGroupKey_, *theContextTableGroupKey_, *theBackboneTableGroupKey_, *theIterateTableGroupKey_;
738 std::string theConfigurationTableGroup_, theContextTableGroup_, theBackboneTableGroup_, theIterateTableGroup_;
740 theConfigurationTableGroup_ = activeConfigGroupName;
741 theConfigurationTableGroupKey_ = &(activeGroupKeys[activeConfigGroupName].second);
743 theContextTableGroup_ = activeContextGroupName;
744 theContextTableGroupKey_ = &(activeGroupKeys[activeContextGroupName].second);
746 theBackboneTableGroup_ = activeBackboneGroupName;
747 theBackboneTableGroupKey_ = &(activeGroupKeys[activeBackboneGroupName].second);
749 theIterateTableGroup_ = activeIterateGroupName;
750 theIterateTableGroupKey_ = &(activeGroupKeys[activeIterateGroupName].second);
754 __COUT__ <<
"Updating persistent active groups to " << ConfigurationManager::ACTIVE_GROUPS_FILENAME <<
" ..." << __E__;
756 std::string fn = ConfigurationManager::ACTIVE_GROUPS_FILENAME;
757 FILE* fp = fopen(fn.c_str(),
"w");
761 fprintf(fp,
"%s\n", theContextTableGroup_.c_str());
762 fprintf(fp,
"%s\n", theContextTableGroupKey_ ? theContextTableGroupKey_->toString().c_str() :
"-1");
763 fprintf(fp,
"%s\n", theBackboneTableGroup_.c_str());
764 fprintf(fp,
"%s\n", theBackboneTableGroupKey_ ? theBackboneTableGroupKey_->toString().c_str() :
"-1");
765 fprintf(fp,
"%s\n", theConfigurationTableGroup_.c_str());
766 fprintf(fp,
"%s\n", theConfigurationTableGroupKey_ ? theConfigurationTableGroupKey_->toString().c_str() :
"-1");
767 fprintf(fp,
"%s\n", theIterateTableGroup_.c_str());
768 fprintf(fp,
"%s\n", theIterateTableGroupKey_ ? theIterateTableGroupKey_->toString().c_str() :
"-1");
775 __COUT__ <<
"Resulting Groups:" << __E__;
776 for(
const auto& group : groupSet)
777 __COUT__ <<
"\t" << group.first.first <<
": " << group.first.second <<
" => " << group.second << __E__;
778 __COUT__ <<
"Resulting Groups end." << __E__;
782 __COUT__ <<
"Resulting Active Groups:" << __E__;
783 for(
const auto& activeGroup : activeGroupKeys)
784 __COUT__ <<
"\t" << activeGroup.first <<
": " << activeGroup.second.first <<
" => " << activeGroup.second.second << __E__;
786 __COUT__ << activeBackboneGroupName <<
" is the " << ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE <<
"." << __E__;
787 __COUT__ <<
"Resulting Active Groups end." << __E__;
791 __COUT__ <<
"End of Flattening Active Table Groups!\n\n\n" << __E__;
793 __COUT__ <<
"****************************" << __E__;
794 __COUT__ <<
"There were " << groupSet.size() <<
" groups considered, and there were " << groupErrors.size() <<
" errors found handling those groups."
796 __COUT__ <<
"The following errors were found handling the groups:" << __E__;
797 for(
auto& groupErr : groupErrors)
798 __COUT__ <<
"\t" << groupErr.first.first <<
" " << groupErr.first.second <<
": \t" << groupErr.second << __E__;
799 __COUT__ <<
"End of errors.\n\n" << __E__;
801 __COUT__ <<
"Run the following to return to your previous database structure:" << __E__;
802 __COUT__ <<
"\t otsdaq_fix_new_table_fiels " << moveToDir <<
"\n\n" << __E__;
807 int main(
int argc,
char* argv[])
809 FixNewTableFields(argc, argv);