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")
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__;
106 std::string accumulatedWarnings;
109 ConfigurationManager::LoadGroupType::ALL_TYPES,
110 &accumulatedWarnings);
112 __COUT__ <<
"Done Loading active groups: \n" << accumulatedWarnings << std::endl;
139 std::map<std::pair<std::string, TableGroupKey>,
TableGroupKey> groupSet;
141 std::map<std::pair<std::string, TableVersion>,
TableVersion> modifiedTables;
142 std::map<std::string, std::pair<TableGroupKey, TableGroupKey>> activeGroupKeys;
143 std::map<std::pair<std::string, TableGroupKey>, std::string> groupErrors;
145 std::string activeBackboneGroupName =
"";
146 std::string activeContextGroupName =
"";
147 std::string activeIterateGroupName =
"";
148 std::string activeConfigGroupName =
"";
150 std::string nowTime = std::to_string(time(0));
152 std::string thenTime =
"";
153 if(pathToSwapIn !=
"")
155 thenTime = pathToSwapIn.substr(pathToSwapIn.rfind(
'_') + 1);
156 __COUT__ <<
"thenTime = " << thenTime << __E__;
161 std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap =
164 for(
const auto& activeGroup : activeGroupsMap)
166 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
167 std::pair<std::string, TableGroupKey>(activeGroup.second.first,
168 activeGroup.second.second),
170 activeGroupKeys.insert(
171 std::pair<std::string, std::pair<TableGroupKey, TableGroupKey>>(
172 activeGroup.second.first,
173 std::pair<TableGroupKey, TableGroupKey>(activeGroup.second.second,
176 if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_BACKBONE)
178 activeBackboneGroupName = activeGroup.second.first;
179 __COUT__ <<
"found activeBackboneGroupName = " << activeBackboneGroupName
182 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_CONTEXT)
184 activeContextGroupName = activeGroup.second.first;
185 __COUT__ <<
"found activeContextGroupName = " << activeContextGroupName
188 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_ITERATE)
190 activeIterateGroupName = activeGroup.second.first;
191 __COUT__ <<
"found activeIterateGroupName = " << activeIterateGroupName
194 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_CONFIGURATION)
196 activeConfigGroupName = activeGroup.second.first;
197 __COUT__ <<
"found activeConfigGroupName = " << activeConfigGroupName
203 const std::string groupAliasesTableName =
204 ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
205 std::map<std::string, TableVersion> activeVersions = cfgMgr->
getActiveVersions();
206 if(activeVersions.find(groupAliasesTableName) == activeVersions.end())
208 __SS__ <<
"\nActive version of " << groupAliasesTableName <<
" missing! "
209 << groupAliasesTableName
210 <<
" is a required member of the Backbone configuration group."
211 <<
"\n\nLikely you need to activate a valid Backbone group." << __E__;
215 std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs =
217 for(
auto& groupPair : aliasNodePairs)
218 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>,
TableGroupKey>(
219 std::pair<std::string, TableGroupKey>(
220 groupPair.second.getNode(
"GroupName").getValueAsString(),
221 TableGroupKey(groupPair.second.getNode(
"GroupKey").getValueAsString())),
224 __COUT__ <<
"Identified groups:" << __E__;
225 for(
auto& group : groupSet)
226 __COUT__ << group.first.first <<
" " << group.first.second << __E__;
233 std::string currentDir = __ENV__(
"ARTDAQ_DATABASE_URI");
235 if(currentDir.find(
"filesystemdb://") != 0)
237 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << __E__;
241 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
242 while(currentDir.length() &&
243 currentDir[currentDir.length() - 1] ==
'/')
244 currentDir = currentDir.substr(0, currentDir.length() - 1);
245 std::string moveToDir = currentDir +
"_" + nowTime;
247 if(pathToSwapIn !=
"")
250 if((dp = opendir(pathToSwapIn.c_str())) == 0)
252 __COUT__ <<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn
260 __COUT__ <<
"Copying current directory: \t" << currentDir << __E__;
261 __COUT__ <<
"\t... to: \t\t" << moveToDir << __E__;
263 rename(currentDir.c_str(), moveToDir.c_str());
265 if(pathToSwapIn !=
"")
267 __COUT__ <<
"Swapping in directory: \t" << pathToSwapIn << __E__;
268 __COUT__ <<
"\t.. to: \t\t" << currentDir << __E__;
269 rename(pathToSwapIn.c_str(), currentDir.c_str());
273 std::string activeGroupsFile =
275 FILE* fp = fopen(activeGroupsFile.c_str(),
"r");
278 __COUT__ <<
"Swapping active groups file: \t" << activeGroupsFile << __E__;
281 rename(activeGroupsFile.c_str(),
285 __COUT__ <<
"Path swapped in. Done." << __E__;
289 std::system((
"cp -r " + moveToDir +
" " + currentDir).c_str());
304 bool errDetected =
false;
305 std::string accumulateErrors =
"";
308 std::map<std::string , std::string > groupAliases;
309 std::string groupComment;
324 for(
auto& groupPair : groupSet)
328 __COUT__ <<
"****************************" << __E__;
329 __COUT__ <<
"Loading members for " << groupPair.first.first <<
"("
330 << groupPair.first.second <<
")" << __E__;
337 groupPair.first.second,
349 catch(std::runtime_error& e)
351 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
352 <<
"(" << groupPair.first.second <<
")" << __E__;
353 __COUT__ << e.what() << __E__;
358 __COUT__ <<
"Error was caught loading members for " << groupPair.first.first
359 <<
"(" << groupPair.first.second <<
")" << __E__;
372 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
373 std::pair<std::string, TableGroupKey>(groupPair.first.first,
374 groupPair.first.second),
375 "Error caught loading the group."));
387 for(
auto& memberPair : memberMap)
389 __COUT__ << memberPair.first <<
":v" << memberPair.second << __E__;
393 if(modifiedTables.find(std::pair<std::string, TableVersion>(
394 memberPair.first, memberPair.second)) != modifiedTables.end())
396 __COUT__ <<
"Table was already modified!" << __E__;
398 modifiedTables[std::pair<std::string, TableVersion>(
399 memberPair.first, memberPair.second)];
400 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v"
401 << memberPair.second << __E__;
409 memberPair.first , memberPair.second
414 memberPair.first , temporaryVersion);
429 modifiedTables.insert(
430 std::pair<std::pair<std::string, TableVersion>,
TableVersion>(
431 std::pair<std::string, TableVersion>(memberPair.first,
435 memberPair.second = persistentVersion;
438 __COUT__ <<
"\t to...\t" << memberPair.first <<
":v" << memberPair.second
508 if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() &&
509 activeGroupKeys[groupPair.first.first].first == groupPair.first.second)
510 activeGroupKeys[groupPair.first.first].second =
513 catch(std::runtime_error& e)
515 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" ("
516 << groupPair.first.second <<
") " << __E__;
517 __COUT__ << e.what() << __E__;
520 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
521 std::pair<std::string, TableGroupKey>(groupPair.first.first,
522 groupPair.first.second),
523 "Error caught saving the group."));
527 __COUT__ <<
"Error was caught saving group " << groupPair.first.first <<
" ("
528 << groupPair.first.second <<
") " << __E__;
531 std::pair<std::pair<std::string, TableGroupKey>, std::string>(
532 std::pair<std::string, TableGroupKey>(groupPair.first.first,
533 groupPair.first.second),
534 "Error caught saving the group."));
542 FILE* fp = fopen((moveToDir +
"/README_fix_new_table_fields.txt").c_str(),
"a");
544 __COUT__ <<
"\tError opening README file!" << __E__;
552 timeinfo = localtime(&rawtime);
553 strftime(buffer, 200,
"%b %d, %Y %I:%M%p %Z", timeinfo);
556 "This database %s \n\t is a backup of %s \n\t BEFORE forcing to new "
557 "table fields \n\t and was created at this time \n\t %lu \t %s\n\n\n",
569 FILE* fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
572 __COUT__ <<
"\tError opening README file!" << __E__;
580 timeinfo = localtime(&rawtime);
581 strftime(buffer, 200,
"%b %d, %Y %I:%M:%S%p %Z", timeinfo);
584 "This database %s \n\t was forced to new table fields \n\t at this "
585 "time \n\t %lu \t %s\n\n\n",
618 if(activeBackboneGroupName ==
"")
620 __COUT__ <<
"No active Backbone table identified." << __E__;
624 __COUT__ <<
"Modifying the active Backbone table to reflect new table versions and "
631 activeGroupKeys[activeBackboneGroupName].second,
641 const std::string groupAliasesName =
642 ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
643 const std::string versionAliasesName =
644 ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
649 if(activeMap.find(groupAliasesName) != activeMap.end())
651 __COUT__ <<
"\n\nModifying " << groupAliasesName << __E__;
659 __COUT__ << groupAliasesName <<
":v" << memberMap[groupAliasesName] << __E__;
664 memberMap[groupAliasesName]
667 config = cfgMgr->getTableByName(groupAliasesName);
668 config->setActiveView(temporaryVersion);
669 cfgView = config->getViewP();
671 unsigned int col1 = cfgView->
findCol(
"GroupName");
672 unsigned int col2 = cfgView->
findCol(
"GroupKey");
679 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
682 for(
const auto& group : groupSet)
683 if(group.second.isInvalid())
685 else if(cfgView->getDataView()[row][col1] == group.first.first &&
686 cfgView->getDataView()[row][col2] ==
687 group.first.second.toString())
690 __COUT__ <<
"Changing row " << row <<
" for "
691 << cfgView->getDataView()[row][col1]
692 <<
" key=" << cfgView->getDataView()[row][col2]
693 <<
" to NEW key=" << group.second << __E__;
694 cfgView->
setValue(group.second.toString(), row, col2);
707 cfgMgr->
saveNewTable(groupAliasesName , temporaryVersion);
715 memberMap[groupAliasesName] =
718 __COUT__ <<
"\t to...\t" << groupAliasesName <<
":v"
719 << memberMap[groupAliasesName] << __E__;
724 if(activeMap.find(versionAliasesName) != activeMap.end())
726 __COUT__ <<
"\n\nModifying " << versionAliasesName << __E__;
729 __COUT__ << versionAliasesName <<
":v" << memberMap[versionAliasesName]
735 memberMap[versionAliasesName]
738 config = cfgMgr->getTableByName(versionAliasesName);
739 config->setActiveView(temporaryVersion);
740 cfgView = config->getViewP();
741 unsigned int col1 = cfgView->
findCol(
"TableName");
742 unsigned int col2 = cfgView->
findCol(
"Version");
746 for(
unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
749 for(
const auto& table : modifiedTables)
750 if(cfgView->getDataView()[row][col1] == table.first.first &&
751 cfgView->getDataView()[row][col2] == table.first.second.toString())
754 __COUT__ <<
"Changing row " << row <<
" for "
755 << cfgView->getDataView()[row][col1]
756 <<
" version=" << cfgView->getDataView()[row][col2]
757 <<
" to NEW version=" << table.second << __E__;
758 cfgView->
setValue(table.second.toString(), row, col2);
769 cfgMgr->
saveNewTable(versionAliasesName , temporaryVersion);
777 memberMap[versionAliasesName] =
780 __COUT__ <<
"\t to...\t" << versionAliasesName <<
":v"
781 << memberMap[versionAliasesName] << __E__;
789 activeBackboneGroupName ,
800 activeGroupKeys[activeBackboneGroupName].second =
TableGroupKey(newGroupKey);
802 __COUT__ <<
"New to-be-active backbone group " << activeBackboneGroupName <<
":v"
803 << activeGroupKeys[activeBackboneGroupName].second << __E__;
811 __COUT__ <<
"Manipulating the Active Groups file..." << __E__;
817 __SS__ <<
"Original active groups file '"
828 std::string renameFile =
832 __COUT__ <<
"Backup file name: " << renameFile << __E__;
834 TableGroupKey *theConfigurationTableGroupKey_, *theContextTableGroupKey_,
835 *theBackboneTableGroupKey_, *theIterateTableGroupKey_;
836 std::string theConfigurationTableGroup_, theContextTableGroup_,
837 theBackboneTableGroup_, theIterateTableGroup_;
839 theConfigurationTableGroup_ = activeConfigGroupName;
840 theConfigurationTableGroupKey_ = &(activeGroupKeys[activeConfigGroupName].second);
842 theContextTableGroup_ = activeContextGroupName;
843 theContextTableGroupKey_ = &(activeGroupKeys[activeContextGroupName].second);
845 theBackboneTableGroup_ = activeBackboneGroupName;
846 theBackboneTableGroupKey_ = &(activeGroupKeys[activeBackboneGroupName].second);
848 theIterateTableGroup_ = activeIterateGroupName;
849 theIterateTableGroupKey_ = &(activeGroupKeys[activeIterateGroupName].second);
853 __COUT__ <<
"Updating persistent active groups to "
857 FILE* fp = fopen(fn.c_str(),
"w");
861 fprintf(fp,
"%s\n", theContextTableGroup_.c_str());
864 theContextTableGroupKey_
865 ? theContextTableGroupKey_->
toString().c_str()
867 fprintf(fp,
"%s\n", theBackboneTableGroup_.c_str());
870 theBackboneTableGroupKey_
871 ? theBackboneTableGroupKey_->
toString().c_str()
873 fprintf(fp,
"%s\n", theConfigurationTableGroup_.c_str());
876 theConfigurationTableGroupKey_
877 ? theConfigurationTableGroupKey_->
toString().c_str()
879 fprintf(fp,
"%s\n", theIterateTableGroup_.c_str());
882 theIterateTableGroupKey_
883 ? theIterateTableGroupKey_->
toString().c_str()
891 __COUT__ <<
"Resulting Groups:" << __E__;
892 for(
const auto& group : groupSet)
893 __COUT__ <<
"\t" << group.first.first <<
": " << group.first.second <<
" => "
894 << group.second << __E__;
895 __COUT__ <<
"Resulting Groups end." << __E__;
899 __COUT__ <<
"Resulting Active Groups:" << __E__;
900 for(
const auto& activeGroup : activeGroupKeys)
901 __COUT__ <<
"\t" << activeGroup.first <<
": " << activeGroup.second.first
902 <<
" => " << activeGroup.second.second << __E__;
904 __COUT__ << activeBackboneGroupName <<
" is the "
905 << ConfigurationManager::GROUP_TYPE_NAME_BACKBONE <<
"." << __E__;
906 __COUT__ <<
"Resulting Active Groups end." << __E__;
910 __COUT__ <<
"End of Flattening Active Table Groups!\n\n\n" << __E__;
912 __COUT__ <<
"****************************" << __E__;
913 __COUT__ <<
"There were " << groupSet.size() <<
" groups considered, and there were "
914 << groupErrors.size() <<
" errors found handling those groups." << __E__;
915 __COUT__ <<
"The following errors were found handling the groups:" << __E__;
916 for(
auto& groupErr : groupErrors)
917 __COUT__ <<
"\t" << groupErr.first.first <<
" " << groupErr.first.second <<
": \t"
918 << groupErr.second << __E__;
919 __COUT__ <<
"End of errors.\n\n" << __E__;
921 __COUT__ <<
"Run the following to return to your previous database structure:"
923 __COUT__ <<
"\t otsdaq_fix_new_table_fiels " << moveToDir <<
"\n\n" << __E__;
928 int main(
int argc,
char* argv[])
930 FixNewTableFields(argc, argv);
void loadTableGroup(const std::string &tableGroupName, const TableGroupKey &tableGroupKey, bool doActivate=false, std::map< std::string, TableVersion > *groupMembers=0, ProgressBar *progressBar=0, std::string *accumulateWarnings=0, std::string *groupComment=0, std::string *groupAuthor=0, std::string *groupCreateTime=0, bool doNotLoadMember=false, std::string *groupTypeString=0, std::map< std::string, std::string > *groupAliases=0, ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext=ConfigurationManager::LoadGroupType::ALL_TYPES, bool ignoreVersionTracking=false)
void restoreActiveTableGroups(bool throwErrors=false, const std::string &pathToActiveGroupsFile="", ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext=ConfigurationManager::LoadGroupType::ALL_TYPES, std::string *accumulatedWarnings=0)
std::map< std::string, std::pair< std::string, TableGroupKey > > getActiveTableGroups(void) const
std::map< std::string, TableVersion > getActiveVersions(void) const
getActiveVersions
ConfigurationTree getNode(const std::string &nodeString, bool doNotThrowOnBrokenUIDLinks=false) const
"root/parent/parent/"
static const std::string ACTIVE_GROUPS_FILENAME
added env check for otsdaq_flatten_active_to_version to function
TableVersion saveNewTable(const std::string &tableName, TableVersion temporaryVersion=TableVersion(), bool makeTemporary=false)
modifiers of generic TableBase
TableVersion copyViewToCurrentColumns(const std::string &tableName, TableVersion sourceVersion)
copyViewToCurrentColumns
TableGroupKey saveNewTableGroup(const std::string &groupName, std::map< std::string, TableVersion > &groupMembers, const std::string &groupComment=TableViewColumnInfo::DATATYPE_COMMENT_DEFAULT, std::map< std::string, std::string > *groupAliases=0)
modifiers of a table group based on alias, e.g. "Physics"
std::vector< std::pair< std::string, ConfigurationTree > > getChildren(std::map< std::string, std::string > filterMap=std::map< std::string, std::string >(), bool byPriority=false, bool onlyStatusTrue=false) const
std::string toString(void) const
toString
unsigned int findCol(const std::string &name) const
void setValue(const T &value, unsigned int row, unsigned int col)
< in included .icc source
static std::string mapToString(const std::map< std::string, T > &mapToReturn, const std::string &primaryDelimeter=", ", const std::string &secondaryDelimeter=": ")