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)
129 __SS__ <<
"filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << std::endl;
130 __COUT_ERR__ <<
"\n" << ss.str();
131 throw std::runtime_error(ss.str());
134 currentDir = currentDir.substr(std::string(
"filesystemdb://").length());
135 while(currentDir.length() && currentDir[currentDir.length()-1] ==
'/')
136 currentDir = currentDir.substr(0,currentDir.length()-1);
137 std::string moveToDir = currentDir +
"_" + std::to_string(time(0));
139 std::cout << __COUT_HDR_FL__ <<
"Moving current directory: \t" << currentDir << std::endl;
140 std::cout << __COUT_HDR_FL__ <<
"\t... to: \t\t" << moveToDir << std::endl;
144 __SS__ << (
"Aborting move! Must at least give version argument to flatten to!") << std::endl;
145 __COUT_ERR__ <<
"\n" << ss.str();
146 throw std::runtime_error(ss.str());
149 rename(currentDir.c_str(),moveToDir.c_str());
150 FILE *fp = fopen((moveToDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
152 std::cout << __COUT_HDR_FL__ <<
"\tError opening README file!" << std::endl;
156 struct tm * timeinfo;
160 timeinfo = localtime (&rawtime);
161 strftime (buffer,200,
"%b %d, %Y %I:%M%p %Z",timeinfo);
163 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",
164 currentDir.c_str(),moveToDir.c_str(),time(0),buffer);
169 if(pathToSwapIn !=
"")
172 if((dp = opendir(pathToSwapIn.c_str())) == 0)
174 std::cout << __COUT_HDR_FL__<<
"ERROR:(" << errno <<
"). Can't open directory: " << pathToSwapIn << std::endl;
179 std::cout << __COUT_HDR_FL__ <<
"Swapping in directory: \t" << pathToSwapIn << std::endl;
180 std::cout << __COUT_HDR_FL__ <<
"\t.. to: \t\t" << currentDir << std::endl;
182 rename(pathToSwapIn.c_str(),currentDir.c_str());
183 FILE *fp = fopen((currentDir +
"/README_otsdaq_flatten.txt").c_str(),
"a");
185 std::cout << __COUT_HDR_FL__ <<
"\tError opening README file!" << std::endl;
189 struct tm * timeinfo;
193 timeinfo = localtime (&rawtime);
194 strftime (buffer,200,
"%b %d, %Y %I:%M:%S%p %Z",timeinfo);
196 fprintf(fp,
"This database was moved from...\t %s \t to...\t %s at this time \t %lu \t %s\n\n",
197 pathToSwapIn.c_str(),currentDir.c_str(),time(0),buffer);
210 std::map<std::string, ConfigurationVersion> activeMap = cfgMgr->getActiveVersions();
216 const std::string groupAliasesName =
"GroupAliasesConfiguration";
217 const std::string versionAliasesName =
"VersionAliasesConfiguration";
225 " is an invalid or temporary version. Skipping to end!" << std::endl;
231 if(activeMap.find(groupAliasesName) != activeMap.end())
233 std::cout << __COUT_HDR_FL__ <<
"\n\nModifying " << groupAliasesName << std::endl;
234 config = cfgMgr->getConfigurationByName(groupAliasesName);
235 cfgView = config->getViewP();
237 unsigned int col1 = cfgView->findCol(
"GroupName");
238 unsigned int col2 = cfgView->findCol(
"GroupKey");
241 for(
unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
242 for(
auto &activeGroupPair: activeGroupsMap)
243 if(activeGroupPair.second.second.isInvalid())
continue;
244 else if(cfgView->getDataView()[row][col1] == activeGroupPair.second.first &&
245 cfgView->getDataView()[row][col2] == activeGroupPair.second.second.toString())
248 std::cout << __COUT_HDR_FL__ <<
249 "Changing row " << row <<
" for " <<
250 cfgView->getDataView()[row][col1] <<
" key=" <<
251 cfgView->getDataView()[row][col2] <<
" to DEFAULT=" <<
262 if(activeMap.find(versionAliasesName) != activeMap.end())
264 std::cout << __COUT_HDR_FL__ <<
"\n\nModifying " << versionAliasesName << std::endl;
265 config = cfgMgr->getConfigurationByName(versionAliasesName);
266 cfgView = config->getViewP();
267 unsigned int col1 = cfgView->findCol(
"ConfigurationName");
268 unsigned int col2 = cfgView->findCol(
"Version");
271 for(
unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
272 for(
auto &activePair:activeMap)
273 if(cfgView->getDataView()[row][col1] == activePair.first &&
274 cfgView->getDataView()[row][col2] == activePair.second.toString())
277 std::cout << __COUT_HDR_FL__ <<
"Changing row " << row <<
" for " <<
278 cfgView->getDataView()[row][col1] <<
" version=" <<
279 cfgView->getDataView()[row][col2] <<
" to flatVersion=" <<
286 std::cout << __COUT_HDR_FL__ <<
"\n\nChanging versions... " << std::endl;
288 for(
auto &activePair:activeMap)
290 std::cout << __COUT_HDR_FL__ << activePair.first <<
":v" << activePair.second << std::endl;
292 config = cfgMgr->getConfigurationByName(activePair.first);
293 cfgView = config->getViewP();
295 theInterface_->saveActiveVersion(config);
301 for(
auto &activeGroupMembersPair: activeGroupMembersMap)
303 std::cout << __COUT_HDR_FL__ <<
"Group " << activeGroupMembersPair.first << std::endl;
305 for(
auto &groupMemberPair: activeGroupMembersPair.second)
307 std::cout << __COUT_HDR_FL__ <<
"\t from...\t" <<
308 groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
309 groupMemberPair.second = flatVersion;
312 for(
auto &groupMemberPair: activeGroupMembersPair.second)
314 std::cout << __COUT_HDR_FL__ <<
"\t to...\t" <<
315 groupMemberPair.first <<
":v" << groupMemberPair.second << std::endl;
318 theInterface_->saveConfigurationGroup(activeGroupMembersPair.second,
319 ConfigurationGroupKey::getFullGroupString(
320 activeGroupMembersPair.first,
326 std::cout << __COUT_HDR_FL__ <<
"\n\nEnd of Flattening Active Configuration Groups!\n\n\n" << std::endl;
329 std::cout << __COUT_HDR_FL__ <<
"Run the following to return to your previous database structure:" <<
331 std::cout << __COUT_HDR_FL__ <<
"\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
338 int main(
int argc,
char* argv[])
340 FlattenActiveConfigurationGroups(argc,argv);