7 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
8 #include "otsdaq-core/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")
55 <<
"Recognized parameter 1 as a 'help' option. Usage was printed. Exiting."
60 __COUTV__(pathToSwapIn);
69 setenv(
"CONFIGURATION_TYPE",
"File", 1);
70 setenv(
"CONFIGURATION_DATA_PATH",
71 (std::string(__ENV__(
"USER_DATA")) +
"/ConfigurationDataExamples").c_str(),
74 "TABLE_INFO_PATH", (std::string(__ENV__(
"USER_DATA")) +
"/TableInfo").c_str(), 1);
78 setenv(
"SERVICE_DATA_PATH",
79 (std::string(__ENV__(
"USER_DATA")) +
"/ServiceData").c_str(),
84 setenv(
"OTSDAQ_LIB", (std::string(__ENV__(
"USER_DATA")) +
"/").c_str(), 1);
85 setenv(
"OTSDAQ_UTILITIES_LIB", (std::string(__ENV__(
"USER_DATA")) +
"/").c_str(), 1);
88 setenv(
"OTS_MAIN_PORT",
"2015", 1);
91 setenv(
"XDAQ_CONFIGURATION_DATA_PATH",
92 (std::string(__ENV__(
"USER_DATA")) +
"/XDAQConfigurations").c_str(),
94 setenv(
"XDAQ_CONFIGURATION_XML",
"otsConfigurationNoRU_CMake", 1);
101 __COUT__ <<
"Loading active Aliases..." << __E__;
129 std::map<std::pair<std::string, TableGroupKey>,
TableGroupKey> groupSet;
131 std::map<std::pair<std::string, TableVersion>,
TableVersion> modifiedTables;
132 std::map<std::string, std::pair<TableGroupKey, TableGroupKey>> activeGroupKeys;
133 std::map<std::pair<std::string, TableGroupKey>, std::string> groupErrors;
135 std::string activeBackboneGroupName =
"";
136 std::string activeContextGroupName =
"";
137 std::string activeIterateGroupName =
"";
138 std::string activeConfigGroupName =
"";
140 std::string nowTime = std::to_string(time(0));
142 std::string thenTime =
"";
143 if(pathToSwapIn !=
"")
145 thenTime = pathToSwapIn.substr(pathToSwapIn.rfind(
'_') + 1);
146 __COUT__ <<
"thenTime = " << thenTime << __E__;
151 std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap =
152 cfgMgr->getActiveTableGroups();
154 for(
const auto& activeGroup : activeGroupsMap)
156 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
157 std::pair<std::string, TableGroupKey>(activeGroup.second.first,
158 activeGroup.second.second),
160 activeGroupKeys.insert(
161 std::pair<std::string, std::pair<TableGroupKey, TableGroupKey>>(
162 activeGroup.second.first,
163 std::pair<TableGroupKey, TableGroupKey>(activeGroup.second.second,
166 if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE)
168 activeBackboneGroupName = activeGroup.second.first;
169 __COUT__ <<
"found activeBackboneGroupName = " << activeBackboneGroupName
172 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONTEXT)
174 activeContextGroupName = activeGroup.second.first;
175 __COUT__ <<
"found activeContextGroupName = " << activeContextGroupName
178 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_ITERATE)
180 activeIterateGroupName = activeGroup.second.first;
181 __COUT__ <<
"found activeIterateGroupName = " << activeIterateGroupName
184 else if(activeGroup.first ==
185 ConfigurationManager::ACTIVE_GROUP_NAME_CONFIGURATION)
187 activeConfigGroupName = activeGroup.second.first;
188 __COUT__ <<
"found activeConfigGroupName = " << activeConfigGroupName
194 const std::string groupAliasesTableName =
195 ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
196 std::map<std::string, TableVersion> activeVersions = cfgMgr->getActiveVersions();
197 if(activeVersions.find(groupAliasesTableName) == activeVersions.end())
199 __SS__ <<
"\nActive version of " << groupAliasesTableName <<
" missing! "
200 << groupAliasesTableName
201 <<
" is a required member of the Backbone configuration group."
202 <<
"\n\nLikely you need to activate a valid Backbone group." << __E__;
206 std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs =
207 cfgMgr->getNode(groupAliasesTableName).getChildren();
208 for(
auto& groupPair : aliasNodePairs)
209 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
210 std::pair<std::string, TableGroupKey>(
211 groupPair.second.getNode(
"GroupName").getValueAsString(),
212 TableGroupKey(groupPair.second.getNode(
"GroupKey").getValueAsString())),
215 __COUT__ <<
"Identified groups:" << __E__;
216 for(
auto& group : groupSet)
217 __COUT__ << group.first.first <<
" " << group.first.second << __E__;
224 std::string currentDir = __ENV__(
"ARTDAQ_DATABASE_URI");
226 if(currentDir.find(
"filesystemdb://") != 0)
228 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << __E__;
232 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
233 while(currentDir.length() &&
234 currentDir[currentDir.length() - 1] ==
'/')
235 currentDir = currentDir.substr(0, currentDir.length() - 1);
236 std::string moveToDir = currentDir +
"_" + nowTime;
238 if(pathToSwapIn !=
"")
241 if((dp = opendir(pathToSwapIn.c_str())) == 0)
243 __COUT__ <<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn
251 __COUT__ <<
"Copying current directory: \t" << currentDir << __E__;
252 __COUT__ <<
"\t... to: \t\t" << moveToDir << __E__;
254 rename(currentDir.c_str(), moveToDir.c_str());
256 if(pathToSwapIn !=
"")
258 __COUT__ <<
"Swapping in directory: \t" << pathToSwapIn << __E__;
259 __COUT__ <<
"\t.. to: \t\t" << currentDir << __E__;
260 rename(pathToSwapIn.c_str(), currentDir.c_str());
264 std::string activeGroupsFile =
265 ConfigurationManager::ACTIVE_GROUPS_FILENAME +
"." + thenTime;
266 FILE* fp = fopen(activeGroupsFile.c_str(),
"r");
269 __COUT__ <<
"Swapping active groups file: \t" << activeGroupsFile << __E__;
270 __COUT__ <<
"\t.. to: \t\t" << ConfigurationManager::ACTIVE_GROUPS_FILENAME
272 rename(activeGroupsFile.c_str(),
273 ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str());
276 __COUT__ <<
"Path swapped in. Done." << __E__;
280 std::system((
"cp -r " + moveToDir +
" " + currentDir).c_str());
295 bool errDetected =
false;
296 std::string accumulateErrors =
"";
300 std::map<std::string , std::string > groupAliases;
301 std::string groupComment;
316 for(
auto& groupPair : groupSet)
320 __COUT__ <<
"****************************" << __E__;
321 __COUT__ <<
"Loading members for " << groupPair.first.first <<
"("
322 << groupPair.first.second <<
")" << __E__;
328 cfgMgr->loadTableGroup(groupPair.first.first,
329 groupPair.first.second,
341 catch(std::runtime_error& e)
343 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
344 <<
"(" << groupPair.first.second <<
")" << __E__;
345 __COUT__ << e.what() << __E__;
350 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
351 <<
"(" << groupPair.first.second <<
")" << __E__;
364 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
365 std::pair<std::string, TableGroupKey>(groupPair.first.first,
366 groupPair.first.second),
367 "Error caught loading the group."));
375 __COUT__ <<
"Before member map: " << StringMacros::mapToString(memberMap)
379 for(
auto& memberPair : memberMap)
381 __COUT__ << memberPair.first <<
":v" << memberPair.second << __E__;
385 if(modifiedTables.find(std::pair<std::string, TableVersion>(
386 memberPair.first, memberPair.second)) != modifiedTables.end())
388 __COUT__ <<
"Table was already modified!" << __E__;
390 modifiedTables[std::pair<std::string, TableVersion>(
391 memberPair.first, memberPair.second)];
392 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v"
393 << memberPair.second << __E__;
400 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
401 memberPair.first , memberPair.second
406 memberPair.first , temporaryVersion);
421 modifiedTables.insert(
422 std::pair<std::pair<std::string, TableVersion>,
TableVersion>(
423 std::pair<std::string, TableVersion>(memberPair.first,
427 memberPair.second = persistentVersion;
430 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v" << memberPair.second
435 __COUT__ <<
"After member map: " << StringMacros::mapToString(memberMap)
441 cfgMgr->saveNewTableGroup(groupPair.first.first ,
500 if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() &&
501 activeGroupKeys[groupPair.first.first].first == groupPair.first.second)
502 activeGroupKeys[groupPair.first.first].second =
505 catch(std::runtime_error& e)
507 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" ("
508 << groupPair.first.second <<
") " << __E__;
509 __COUT__ << e.what() << __E__;
512 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
513 std::pair<std::string, TableGroupKey>(groupPair.first.first,
514 groupPair.first.second),
515 "Error caught saving the group."));
519 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" ("
520 << groupPair.first.second <<
") " << __E__;
523 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
524 std::pair<std::string, TableGroupKey>(groupPair.first.first,
525 groupPair.first.second),
526 "Error caught saving the group."));
535 FILE* fp = fopen((moveToDir +
"/README_fix_new_table_fields.txt").c_str(),
"a");
537 __COUT__ <<
"\tError opening README file!" << __E__;
545 timeinfo = localtime(&rawtime);
546 strftime(buffer, 200,
"%b %d, %Y %I:%M%p %Z", timeinfo);
549 "This database %s \n\t is a backup of %s \n\t BEFORE forcing to new "
550 "table fields \n\t and was created at this time \n\t %lu \t %s\n\n\n",
562 FILE* fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
565 __COUT__ <<
"\tError opening README file!" << __E__;
573 timeinfo = localtime(&rawtime);
574 strftime(buffer, 200,
"%b %d, %Y %I:%M:%S%p %Z", timeinfo);
577 "This database %s \n\t was forced to new table fields \n\t at this "
578 "time \n\t %lu \t %s\n\n\n",
611 if(activeBackboneGroupName ==
"")
613 __COUT__ <<
"No active Backbone table identified." << __E__;
617 __COUT__ <<
"Modifying the active Backbone table to reflect new table versions and "
623 cfgMgr->loadTableGroup(activeBackboneGroupName,
624 activeGroupKeys[activeBackboneGroupName].second,
634 const std::string groupAliasesName =
635 ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
636 const std::string versionAliasesName =
637 ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
639 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions();
642 if(activeMap.find(groupAliasesName) != activeMap.end())
644 __COUT__ <<
"\n\nModifying " << groupAliasesName << __E__;
647 __COUT__ <<
"Before member map: " << StringMacros::mapToString(memberMap)
652 __COUT__ << groupAliasesName <<
":v" << memberMap[groupAliasesName] << __E__;
655 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
657 memberMap[groupAliasesName]
660 config = cfgMgr->getTableByName(groupAliasesName);
661 config->setActiveView(temporaryVersion);
662 cfgView = config->getViewP();
664 unsigned int col1 = cfgView->findCol(
"GroupName");
665 unsigned int col2 = cfgView->findCol(
"GroupKey");
672 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
675 for(
const auto& group : groupSet)
676 if(group.second.isInvalid())
678 else if(cfgView->getDataView()[row][col1] == group.first.first &&
679 cfgView->getDataView()[row][col2] ==
680 group.first.second.toString())
683 __COUT__ <<
"Changing row " << row <<
" for "
684 << cfgView->getDataView()[row][col1]
685 <<
" key=" << cfgView->getDataView()[row][col2]
686 <<
" to NEW key=" << group.second << __E__;
687 cfgView->setValue(group.second.toString(), row, col2);
693 cfgView->deleteRow(row--);
700 cfgMgr->saveNewTable(groupAliasesName , temporaryVersion);
708 memberMap[groupAliasesName] =
711 __COUT__ <<
"\t to...\t" << groupAliasesName <<
":v"
712 << memberMap[groupAliasesName] << __E__;
717 if(activeMap.find(versionAliasesName) != activeMap.end())
719 __COUT__ <<
"\n\nModifying " << versionAliasesName << __E__;
722 __COUT__ << versionAliasesName <<
":v" << memberMap[versionAliasesName]
726 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
728 memberMap[versionAliasesName]
731 config = cfgMgr->getTableByName(versionAliasesName);
732 config->setActiveView(temporaryVersion);
733 cfgView = config->getViewP();
734 unsigned int col1 = cfgView->findCol(
"TableName");
735 unsigned int col2 = cfgView->findCol(
"Version");
739 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
742 for(
const auto& table : modifiedTables)
743 if(cfgView->getDataView()[row][col1] == table.first.first &&
744 cfgView->getDataView()[row][col2] == table.first.second.toString())
747 __COUT__ <<
"Changing row " << row <<
" for "
748 << cfgView->getDataView()[row][col1]
749 <<
" version=" << cfgView->getDataView()[row][col2]
750 <<
" to NEW version=" << table.second << __E__;
751 cfgView->setValue(table.second.toString(), row, col2);
757 cfgView->deleteRow(row--);
762 cfgMgr->saveNewTable(versionAliasesName , temporaryVersion);
770 memberMap[versionAliasesName] =
773 __COUT__ <<
"\t to...\t" << versionAliasesName <<
":v"
774 << memberMap[versionAliasesName] << __E__;
779 __COUT__ <<
"After member map: " << StringMacros::mapToString(memberMap) << __E__;
782 activeBackboneGroupName ,
793 activeGroupKeys[activeBackboneGroupName].second =
TableGroupKey(newGroupKey);
795 __COUT__ <<
"New to-be-active backbone group " << activeBackboneGroupName <<
":v"
796 << activeGroupKeys[activeBackboneGroupName].second << __E__;
804 __COUT__ <<
"Manipulating the Active Groups file..." << __E__;
807 FILE* fp = fopen(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(),
"r");
810 __SS__ <<
"Original active groups file '"
811 << ConfigurationManager::ACTIVE_GROUPS_FILENAME <<
"' not found."
816 __COUT__ <<
"Backing up file: " << ConfigurationManager::ACTIVE_GROUPS_FILENAME
821 std::string renameFile =
822 ConfigurationManager::ACTIVE_GROUPS_FILENAME +
"." + nowTime;
823 rename(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), renameFile.c_str());
825 __COUT__ <<
"Backup file name: " << renameFile << __E__;
827 TableGroupKey *theConfigurationTableGroupKey_, *theContextTableGroupKey_,
828 *theBackboneTableGroupKey_, *theIterateTableGroupKey_;
829 std::string theConfigurationTableGroup_, theContextTableGroup_,
830 theBackboneTableGroup_, theIterateTableGroup_;
832 theConfigurationTableGroup_ = activeConfigGroupName;
833 theConfigurationTableGroupKey_ = &(activeGroupKeys[activeConfigGroupName].second);
835 theContextTableGroup_ = activeContextGroupName;
836 theContextTableGroupKey_ = &(activeGroupKeys[activeContextGroupName].second);
838 theBackboneTableGroup_ = activeBackboneGroupName;
839 theBackboneTableGroupKey_ = &(activeGroupKeys[activeBackboneGroupName].second);
841 theIterateTableGroup_ = activeIterateGroupName;
842 theIterateTableGroupKey_ = &(activeGroupKeys[activeIterateGroupName].second);
846 __COUT__ <<
"Updating persistent active groups to "
847 << ConfigurationManager::ACTIVE_GROUPS_FILENAME <<
" ..." << __E__;
849 std::string fn = ConfigurationManager::ACTIVE_GROUPS_FILENAME;
850 FILE* fp = fopen(fn.c_str(),
"w");
854 fprintf(fp,
"%s\n", theContextTableGroup_.c_str());
857 theContextTableGroupKey_
858 ? theContextTableGroupKey_->toString().c_str()
860 fprintf(fp,
"%s\n", theBackboneTableGroup_.c_str());
863 theBackboneTableGroupKey_
864 ? theBackboneTableGroupKey_->toString().c_str()
866 fprintf(fp,
"%s\n", theConfigurationTableGroup_.c_str());
869 theConfigurationTableGroupKey_
870 ? theConfigurationTableGroupKey_->toString().c_str()
872 fprintf(fp,
"%s\n", theIterateTableGroup_.c_str());
875 theIterateTableGroupKey_
876 ? theIterateTableGroupKey_->toString().c_str()
884 __COUT__ <<
"Resulting Groups:" << __E__;
885 for(
const auto& group : groupSet)
886 __COUT__ <<
"\t" << group.first.first <<
": " << group.first.second <<
" => "
887 << group.second << __E__;
888 __COUT__ <<
"Resulting Groups end." << __E__;
892 __COUT__ <<
"Resulting Active Groups:" << __E__;
893 for(
const auto& activeGroup : activeGroupKeys)
894 __COUT__ <<
"\t" << activeGroup.first <<
": " << activeGroup.second.first
895 <<
" => " << activeGroup.second.second << __E__;
897 __COUT__ << activeBackboneGroupName <<
" is the "
898 << ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE <<
"." << __E__;
899 __COUT__ <<
"Resulting Active Groups end." << __E__;
903 __COUT__ <<
"End of Flattening Active Table Groups!\n\n\n" << __E__;
905 __COUT__ <<
"****************************" << __E__;
906 __COUT__ <<
"There were " << groupSet.size() <<
" groups considered, and there were "
907 << groupErrors.size() <<
" errors found handling those groups." << __E__;
908 __COUT__ <<
"The following errors were found handling the groups:" << __E__;
909 for(
auto& groupErr : groupErrors)
910 __COUT__ <<
"\t" << groupErr.first.first <<
" " << groupErr.first.second <<
": \t"
911 << groupErr.second << __E__;
912 __COUT__ <<
"End of errors.\n\n" << __E__;
914 __COUT__ <<
"Run the following to return to your previous database structure:"
916 __COUT__ <<
"\t otsdaq_fix_new_table_fiels " << moveToDir <<
"\n\n" << __E__;
921 int main(
int argc,
char* argv[])
923 FixNewTableFields(argc, argv);