6 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
7 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
20 void FlattenActiveConfigurationGroups(
int argc,
char* argv[])
22 std::cout <<
"=================================================\n";
23 std::cout <<
"=================================================\n";
24 std::cout <<
"=================================================\n";
25 std::cout << __COUT_HDR_FL__ <<
"\nFlattening Active Configuration Groups!" << std::endl;
27 std::cout <<
"\n\nusage: Two arguments:\n\t pathToSwapIn <flatVersion> <pathToSwapIn (optional)> \n\n" <<
28 "\t Default values: flatVersion = 0, pathToSwapIn = \"\" \n\n" <<
31 std::cout <<
"\n\nNote: you can optionally just swap databases (and not modify their contents at all)" <<
32 " by providing an invalid flatVersion of -1.\n\n" <<
35 std::cout <<
"\n\nNote: This assumes artdaq db file type interface. " <<
36 "The current database/ will be moved to database_<linuxtime>/ " <<
37 "and if a pathToSwapIn is specified it will be copied to database/ " <<
38 "before saving the currently active groups.\n\n" <<
41 std::cout <<
"argc = " << argc << std::endl;
42 for(
int i = 0; i < argc; i++)
43 std::cout <<
"argv[" << i <<
"] = " << argv[i] << std::endl;
47 std::cout <<
"Must provide at least one parameter.";
52 std::string flatVersionStr = argv[1];
53 if(flatVersionStr.find(
'h') != std::string::npos)
55 std::cout <<
"Recognized parameter 1. as a 'help' option. Usage was printed. Exiting." << std::endl;
60 std::string pathToSwapIn =
"";
62 sscanf(argv[1],
"%d",&flatVersion);
64 pathToSwapIn = argv[2];
66 std::cout << __COUT_HDR_FL__ <<
"flatVersion = " << flatVersion << std::endl;
67 std::cout << __COUT_HDR_FL__ <<
"pathToSwapIn = " << pathToSwapIn << std::endl;
75 setenv(
"CONFIGURATION_TYPE",
"File",1);
76 setenv(
"CONFIGURATION_DATA_PATH",(std::string(getenv(
"USER_DATA")) +
"/ConfigurationDataExamples").c_str(),1);
77 setenv(
"CONFIGURATION_INFO_PATH",(std::string(getenv(
"USER_DATA")) +
"/ConfigurationInfo").c_str(),1);
81 setenv(
"SERVICE_DATA_PATH",(std::string(getenv(
"USER_DATA")) +
"/ServiceData").c_str(),1);
84 setenv(
"XDAQ_CONFIGURATION_DATA_PATH",(std::string(getenv(
"USER_DATA")) +
"/XDAQConfigurations").c_str(),1);
85 setenv(
"XDAQ_CONFIGURATION_XML",
"otsConfigurationNoRU_CMake",1);
95 std::cout << __COUT_HDR_FL__ <<
"\n\n\nLoading activeGroupsMap..." << std::endl;
98 std::map<std::string, std::pair<std::string, ConfigurationGroupKey> > activeGroupsMap =
99 cfgMgr->getActiveConfigurationGroups();
101 std::map<std::string, std::map<std::string, ConfigurationVersion> > activeGroupMembersMap;
102 for(
auto &activeGroupPair: activeGroupsMap)
104 if(activeGroupPair.second.second.isInvalid())
106 std::cout << __COUT_HDR_FL__ <<
"Skipping invalid " <<
107 activeGroupPair.first << std::endl;
111 std::cout << __COUT_HDR_FL__ <<
"Loading members for " <<
112 activeGroupPair.first <<
"\t" <<
113 activeGroupPair.second.first <<
"(" << activeGroupPair.second.second <<
")" <<
116 activeGroupMembersMap[activeGroupPair.second.first] =
117 cfgMgr->getConfigurationInterface()->getConfigurationGroupMembers(
118 ConfigurationGroupKey::getFullGroupString(
119 activeGroupPair.second.first,activeGroupPair.second.second));
125 std::string currentDir = getenv(
"ARTDAQ_DATABASE_URI");
127 if(currentDir.find(
"filesystemdb://") != 0)
128 throw std::runtime_error(
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!");
130 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
131 while(currentDir.length() && currentDir[currentDir.length()-1] ==
'/')
132 currentDir = currentDir.substr(0,currentDir.length()-1);
133 std::string moveToDir = currentDir +
"_" + std::to_string(time(0));
135 std::cout << __COUT_HDR_FL__ <<
"Moving current directory: \t" << currentDir << std::endl;
136 std::cout << __COUT_HDR_FL__ <<
"\t... to: \t\t" << moveToDir << std::endl;
139 throw std::runtime_error(
"Aborting move! Must at least give version argument to flatten to!");
141 rename(currentDir.c_str(),moveToDir.c_str());
142 FILE *fp = fopen((moveToDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
144 std::cout << __COUT_HDR_FL__ <<
"\tError opening README file!" << std::endl;
148 struct tm * timeinfo;
152 timeinfo = localtime (&rawtime);
153 strftime (buffer,200,
"%b %d, %Y %I:%M%p %Z",timeinfo);
155 fprintf(fp,
"This database was moved from...\n\t %s \nto...\n\t %s \nat this time \n\t %lu \t %s\n\n\n",
156 currentDir.c_str(),moveToDir.c_str(),time(0),buffer);
161 if(pathToSwapIn !=
"")
164 if((dp = opendir(pathToSwapIn.c_str())) == 0)
166 std::cout << __COUT_HDR_FL__<<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn << std::endl;
171 std::cout << __COUT_HDR_FL__ <<
"Swapping in directory: \t" << pathToSwapIn << std::endl;
172 std::cout << __COUT_HDR_FL__ <<
"\t.. to: \t\t" << currentDir << std::endl;
174 rename(pathToSwapIn.c_str(),currentDir.c_str());
175 FILE *fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
177 std::cout << __COUT_HDR_FL__ <<
"\tError opening README file!" << std::endl;
181 struct tm * timeinfo;
185 timeinfo = localtime (&rawtime);
186 strftime (buffer,200,
"%b %d, %Y %I:%M:%S%p %Z",timeinfo);
188 fprintf(fp,
"This database was moved from...\t %s \t to...\t %s at this time \t %lu \t %s\n\n",
189 pathToSwapIn.c_str(),currentDir.c_str(),time(0),buffer);
202 std::map<std::string, ConfigurationVersion> activeMap = cfgMgr->getActiveVersions();
208 const std::string groupAliasesName =
"GroupAliasesConfiguration";
209 const std::string versionAliasesName =
"VersionAliasesConfiguration";
217 " is an invalid or temporary version. Skipping to end!" << std::endl;
223 if(activeMap.find(groupAliasesName) != activeMap.end())
225 std::cout << __COUT_HDR_FL__ <<
"\n\nModifying " << groupAliasesName << std::endl;
226 config = cfgMgr->getConfigurationByName(groupAliasesName);
227 cfgView = config->getViewP();
229 unsigned int col1 = cfgView->findCol(
"GroupName");
230 unsigned int col2 = cfgView->findCol(
"GroupKey");
233 for(
unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
234 for(
auto &activeGroupPair: activeGroupsMap)
235 if(activeGroupPair.second.second.isInvalid())
continue;
236 else if(cfgView->getDataView()[row][col1] == activeGroupPair.second.first &&
237 cfgView->getDataView()[row][col2] == activeGroupPair.second.second.toString())
240 std::cout << __COUT_HDR_FL__ <<
241 "Changing row " << row <<
" for " <<
242 cfgView->getDataView()[row][col1] <<
" key=" <<
243 cfgView->getDataView()[row][col2] <<
" to DEFAULT=" <<
254 if(activeMap.find(versionAliasesName) != activeMap.end())
256 std::cout << __COUT_HDR_FL__ <<
"\n\nModifying " << versionAliasesName << std::endl;
257 config = cfgMgr->getConfigurationByName(versionAliasesName);
258 cfgView = config->getViewP();
259 unsigned int col1 = cfgView->findCol(
"ConfigurationName");
260 unsigned int col2 = cfgView->findCol(
"Version");
263 for(
unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
264 for(
auto &activePair:activeMap)
265 if(cfgView->getDataView()[row][col1] == activePair.first &&
266 cfgView->getDataView()[row][col2] == activePair.second.toString())
269 std::cout << __COUT_HDR_FL__ <<
"Changing row " << row <<
" for " <<
270 cfgView->getDataView()[row][col1] <<
" version=" <<
271 cfgView->getDataView()[row][col2] <<
" to flatVersion=" <<
278 std::cout << __COUT_HDR_FL__ <<
"\n\nChanging versions... " << std::endl;
280 for(
auto &activePair:activeMap)
282 std::cout << __COUT_HDR_FL__ << activePair.first <<
":v" << activePair.second << std::endl;
284 config = cfgMgr->getConfigurationByName(activePair.first);
285 cfgView = config->getViewP();
287 theInterface_->saveActiveVersion(config);
293 for(
auto &activeGroupMembersPair: activeGroupMembersMap)
295 std::cout << __COUT_HDR_FL__ <<
"Group " << activeGroupMembersPair.first << std::endl;
297 for(
auto &groupMemberPair: activeGroupMembersPair.second)
299 std::cout << __COUT_HDR_FL__ <<
"\t from...\t" <<
300 groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
301 groupMemberPair.second = flatVersion;
304 for(
auto &groupMemberPair: activeGroupMembersPair.second)
306 std::cout << __COUT_HDR_FL__ <<
"\t to...\t" <<
307 groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
310 theInterface_->saveConfigurationGroup(activeGroupMembersPair.second,
311 ConfigurationGroupKey::getFullGroupString(
312 activeGroupMembersPair.first,
318 std::cout << __COUT_HDR_FL__ <<
"\n\nEnd of Flattening Active Configuration Groups!\n\n\n" << std::endl;
321 std::cout << __COUT_HDR_FL__ <<
"Run the following to return to your previous database structure:" <<
323 std::cout << __COUT_HDR_FL__ <<
"\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
330 int main(
int argc,
char* argv[])
332 FlattenActiveConfigurationGroups(argc,argv);