otsdaq  v1_01_03
 All Classes Namespaces Functions
otsdaq_flatten_system_aliases.cc
1 #include <string>
2 #include <iostream>
3 #include <memory>
4 #include <cassert>
5 #include <dirent.h>
6 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
7 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
8 //#include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
9 //#include "artdaq-database/JsonDocument/JSONDocument.h"
10 
11 //usage:
12 // otsdaq_flatten_system_aliases <baseFlatVersion> <pathToSwapIn (optional)>
13 //
14 // if baseFlatVersion is invalid or temporary nothing is saved in the new db
15 // (Note: this can be used to swap dbs using pathToSwapIn)
16 
17 using namespace ots;
18 
19 
20 void FlattenActiveSystemAliasConfigurationGroups(int argc, char* argv[])
21 {
22  std::cout << "=================================================\n";
23  std::cout << "=================================================\n";
24  std::cout << "=================================================\n";
25  std::cout << __COUT_HDR_FL__ << "\nFlattening Active System Aliases!" << std::endl;
26 
27  std::cout << "\n\nusage: Two arguments:\n\t pathToSwapIn <baseFlatVersion> <pathToSwapIn (optional)> \n\n" <<
28  "\t Default values: baseFlatVersion = 0, pathToSwapIn = \"\" \n\n" <<
29  std::endl;
30 
31  std::cout << "\n\nNote: you can optionally just swap databases (and not modify their contents at all)" <<
32  " by providing an invalid baseFlatVersion of -1.\n\n" <<
33  std::endl;
34 
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" <<
39  std::endl;
40 
41  std::cout << "argc = " << argc << std::endl;
42  for(int i = 0; i < argc; i++)
43  std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
44 
45  if(argc < 2)
46  {
47  std::cout << "Must provide at least one parameter.";
48  return;
49  }
50 
51  //determine if "h"elp was first parameter
52  std::string flatVersionStr = argv[1];
53  if(flatVersionStr.find('h') != std::string::npos)
54  {
55  std::cout << "Recognized parameter 1. as a 'help' option. Usage was printed. Exiting." << std::endl;
56  return;
57  }
58 
59  int flatVersion = 0;
60  std::string pathToSwapIn = "";
61  if(argc >= 2)
62  sscanf(argv[1],"%d",&flatVersion);
63  if(argc >= 3)
64  pathToSwapIn = argv[2];
65 
66  std::cout << __COUT_HDR_FL__ << "flatVersion = " << flatVersion << std::endl;
67  std::cout << __COUT_HDR_FL__ << "pathToSwapIn = " << pathToSwapIn << std::endl;
68 
69  //return;
70  //==============================================================================
71  //Define environment variables
72  // Note: normally these environment variables are set by StartOTS.sh
73 
74  //These are needed by otsdaq/otsdaq-core/ConfigurationDataFormats/ConfigurationInfoReader.cc [207]
75  setenv("CONFIGURATION_TYPE","File",1); //Can be File, Database, DatabaseTest
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);
79 
80  //Some configuration plug-ins use getenv("SERVICE_DATA_PATH") in init() so define it
81  setenv("SERVICE_DATA_PATH",(std::string(getenv("USER_DATA")) + "/ServiceData").c_str(),1);
82 
83  //Some configuration plug-ins use getenv("OTSDAQ_LIB") and getenv("OTSDAQ_UTILITIES_LIB") in init() so define it
84  // to a non-sense place is ok
85  setenv("OTSDAQ_LIB",(std::string(getenv("USER_DATA")) + "/").c_str(),1);
86  setenv("OTSDAQ_UTILITIES_LIB",(std::string(getenv("USER_DATA")) + "/").c_str(),1);
87 
88  //Some configuration plug-ins use getenv("OTS_MAIN_PORT") in init() so define it
89  setenv("OTS_MAIN_PORT","2015",1);
90 
91  //also xdaq envs for XDAQContextConfiguration
92  setenv("XDAQ_CONFIGURATION_DATA_PATH",(std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(),1);
93  setenv("XDAQ_CONFIGURATION_XML","otsConfigurationNoRU_CMake",1);
95 
96  //==============================================================================
97  //get prepared with initial source db
98 
99  //ConfigurationManager instance immediately loads active groups
100  std::cout << "\n\n\n" << __COUT_HDR_FL__ << "Loading active Aliases..." << std::endl;
101  ConfigurationManagerRW cfgMgrInst("flatten_admin");
102  ConfigurationManagerRW *cfgMgr = &cfgMgrInst;
103 
104 
105  //create set of groups to persist
106  // include active context
107  // include active backbone
108  // include active config group
109  // (keep key translation separate activeGroupKeys)
110  // include all groups with system aliases
111 
112  //for group in set
113  // load/activate group and flatten tables to flatVersion to new DB
114  // save new version to modifiedTables
115  // save group with flatVersion key to new DB
116  // save new key to groupSet
117  // ++flatVersion
118 
119  //reload the active backbone (using activeGroupKeys)
120  // modify group aliases and table aliases properly based on groupSet and modifiedTables
121  // save new backbone with flatVersion to new DB
122 
123  //backup the file ConfigurationManager::ACTIVE_GROUP_FILENAME with time
124  // and change the ConfigurationManager::ACTIVE_GROUP_FILENAME
125  // to reflect new group names/keys
126 
127 
128  /* map<<groupName, origKey>, newKey> */
129  std::map<std::pair<std::string,ConfigurationGroupKey>,
130  ConfigurationGroupKey> groupSet;
131  /* <tableName, <origVersion, newVersion> >*/
132  std::map<std::pair<std::string,ConfigurationVersion>,
133  ConfigurationVersion> modifiedTables;
134  std::map<std::string, std::pair<ConfigurationGroupKey,
135  ConfigurationGroupKey>> activeGroupKeys;
136  std::map<std::pair<std::string,ConfigurationGroupKey>,
137  std::string> groupErrors;
138 
139  std::string activeBackboneTableName = "";
140  std::string activeContextTableName = "";
141  std::string activeConfigTableName = "";
142 
143  std::string nowTime = std::to_string(time(0));
144 
145  std::string thenTime = "";
146  if(pathToSwapIn != "") //get target then time
147  {
148  thenTime = pathToSwapIn.substr(pathToSwapIn.rfind('_')+1);
149  std::cout << __COUT_HDR_FL__<< "thenTime = " << thenTime << std::endl;
150  //return;
151  }
152 
153  //add active groups to set
154  std::map<std::string, std::pair<std::string, ConfigurationGroupKey>> activeGroupsMap =
155  cfgMgr->getActiveConfigurationGroups();
156 
157  for(const auto &activeGroup: activeGroupsMap)
158  {
159  groupSet.insert(std::pair<
160  std::pair<std::string,ConfigurationGroupKey>,
161  ConfigurationGroupKey> (
162  std::pair<std::string,ConfigurationGroupKey>(
163  activeGroup.second.first,
164  activeGroup.second.second),
165  ConfigurationGroupKey())
166  );
167  activeGroupKeys.insert(std::pair<
168  std::string,
169  std::pair<ConfigurationGroupKey,ConfigurationGroupKey>> (
170  activeGroup.second.first,
171  std::pair<ConfigurationGroupKey,ConfigurationGroupKey>(
172  activeGroup.second.second,
173  ConfigurationGroupKey()))
174  );
175 
176  if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE)
177  {
178  activeBackboneTableName = activeGroup.second.first;
179  std::cout << __COUT_HDR_FL__<< "found activeBackboneTableName = " <<
180  activeBackboneTableName << std::endl;
181  }
182  else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONTEXT)
183  {
184  activeContextTableName = activeGroup.second.first;
185  std::cout << __COUT_HDR_FL__<< "found activeContextTableName = " <<
186  activeContextTableName << std::endl;
187  }
188  else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONFIGURATION)
189  {
190  activeConfigTableName = activeGroup.second.first;
191  std::cout << __COUT_HDR_FL__<< "found activeConfigTableName = " <<
192  activeConfigTableName << std::endl;
193  }
194  }
195 
196  //add system alias groups to set
197  const std::string groupAliasesTableName = "GroupAliasesConfiguration";
198  std::map<std::string, ConfigurationVersion> activeVersions = cfgMgr->getActiveVersions();
199  if(activeVersions.find(groupAliasesTableName) == activeVersions.end())
200  {
201  __SS__ << "\nActive version of GroupAliasesConfiguration missing! " <<
202  "GroupAliasesConfiguration is a required member of the Backbone configuration group." <<
203  "\n\nLikely you need to activate a valid Backbone group." <<
204  std::endl;
205  throw std::runtime_error(ss.str());
206  }
207 
208 
209  std::vector<std::pair<std::string,ConfigurationTree> > aliasNodePairs =
210  cfgMgr->getNode(groupAliasesTableName).getChildren();
211  for(auto& groupPair:aliasNodePairs)
212  groupSet.insert(std::pair<
213  std::pair<std::string,ConfigurationGroupKey>,
214  ConfigurationGroupKey> (
215  std::pair<std::string,ConfigurationGroupKey>(
216  groupPair.second.getNode("GroupName").getValueAsString(),
217  ConfigurationGroupKey(groupPair.second.getNode("GroupKey").getValueAsString())),
218  ConfigurationGroupKey())
219  );
220 
221  std::cout << __COUT_HDR_FL__<< "Identfied groups:" << std::endl;
222  for(auto& group:groupSet)
223  std::cout << __COUT_HDR_FL__<< group.first.first << " " << group.first.second << std::endl;
224  std::cout << __COUT_HDR_FL__<< std::endl;
225  std::cout << __COUT_HDR_FL__<< std::endl;
226 
227  //return;
228  //==============================================================================
229  //prepare to manipulate directories
230  std::string currentDir = getenv("ARTDAQ_DATABASE_URI");
231 
232  if(currentDir.find("filesystemdb://") != 0)
233  throw std::runtime_error("filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!");
234 
235  currentDir = currentDir.substr(std::string("filesystemdb://").length());
236  while(currentDir.length() && currentDir[currentDir.length()-1] == '/') //remove trailing '/'s
237  currentDir = currentDir.substr(0,currentDir.length()-1);
238  std::string moveToDir = currentDir + "_" + nowTime;
239  if(argc < 2)
240  throw std::runtime_error("Aborting move! Must at least give version argument to flatten to!");
241 
242  if(pathToSwapIn != "")
243  {
244  DIR *dp;
245  if((dp = opendir(pathToSwapIn.c_str())) == 0)
246  {
247  std::cout << __COUT_HDR_FL__<< "ERROR:(" << errno << "). Can't open directory: " << pathToSwapIn << std::endl;
248  exit(0);
249  }
250  closedir(dp);
251  }
252 
253  //handle directory swap
254  std::cout << __COUT_HDR_FL__ << "Moving current directory: \t" << currentDir << std::endl;
255  std::cout << __COUT_HDR_FL__ << "\t... to: \t\t" << moveToDir << std::endl;
256  //return;
257  rename(currentDir.c_str(),moveToDir.c_str());
258 
259  if(pathToSwapIn != "")
260  {
261  std::cout << __COUT_HDR_FL__ << "Swapping in directory: \t" << pathToSwapIn << std::endl;
262  std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" << currentDir << std::endl;
263  rename(pathToSwapIn.c_str(),currentDir.c_str());
264 
265  //also swap in active groups file
266  //check if original active file exists
267  std::string activeGroupsFile = ConfigurationManager::ACTIVE_GROUP_FILENAME + "." + thenTime;
268  FILE *fp = fopen(activeGroupsFile.c_str(),"r");
269  if(fp)
270  {
271  std::cout << __COUT_HDR_FL__ << "Swapping active groups file: \t" <<
272  activeGroupsFile << std::endl;
273  std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" <<
274  ConfigurationManager::ACTIVE_GROUP_FILENAME << std::endl;
275  rename(activeGroupsFile.c_str(),
276  ConfigurationManager::ACTIVE_GROUP_FILENAME.c_str());
277  }
278  }
279 
280 
281 
282 
283 
284  ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(false); //true for File interface, false for artdaq database;
285  ConfigurationView* cfgView;
286  ConfigurationBase* config;
287 
288  bool errDetected;
289  std::string accumulateErrors = "";
290  std::map<std::string, ConfigurationVersion> memberMap;
291  int count = 0;
292 
293  //don't do anything more if flatVersion is not persistent
294  if(ConfigurationVersion(flatVersion).isInvalid() ||
295  ConfigurationVersion(flatVersion).isTemporaryVersion())
296  {
297  std::cout << __COUT_HDR_FL__ << "\n\nflatVersion " << ConfigurationVersion(flatVersion) <<
298  " is an invalid or temporary version. Skipping to end!" << std::endl;
299  goto CLEAN_UP;
300  }
301 
302 
303  for(auto& groupPair:groupSet)
304  {
305  errDetected = false;
306 
307  std::cout << __COUT_HDR_FL__ << "****************************" << std::endl;
308  std::cout << __COUT_HDR_FL__ << "Loading members for " <<
309  groupPair.first.first <<
310  "(" << groupPair.first.second << ")" <<
311  std::endl;
312  std::cout << __COUT_HDR_FL__ << "flatVersion = " << flatVersion << std::endl;
313 
314  //handle directory swap BACK
315  if(pathToSwapIn != "")
316  {
317  std::cout << __COUT_HDR_FL__ << "REVERT by Swapping back directory: \t" << currentDir << std::endl;
318  std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" << pathToSwapIn << std::endl;
319  if(rename(currentDir.c_str(),pathToSwapIn.c_str()) < 0)
320  {
321  __SS__ << "Problem!" << std::endl;
322  throw std::runtime_error(ss.str());
323  }
324 
325  }
326  else if(count) //if not first time, move currentDir to temporarily holding area
327  {
328  std::cout << __COUT_HDR_FL__ << "REVERT by Moving directory: \t" << currentDir << std::endl;
329  std::cout << __COUT_HDR_FL__ << "\t.. to temporary directory: \t\t" << (moveToDir+"_tmp") << std::endl;
330  if(rename(currentDir.c_str(),(moveToDir+"_tmp").c_str()) < 0)
331  {
332  __SS__ << "Problem!" << std::endl;
333  throw std::runtime_error(ss.str());
334  }
335  }
336 
337 
338  std::cout << __COUT_HDR_FL__ << "REVERT by Moving directory: \t" << moveToDir << std::endl;
339  std::cout << __COUT_HDR_FL__ << "\t... to: \t\t" << currentDir << std::endl;
340  if(rename(moveToDir.c_str(),currentDir.c_str()) < 0)
341  {
342  __SS__ << "Problem!" << std::endl;
343  throw std::runtime_error(ss.str());
344  }
345 
346 
347  //=========================
348  //load group and tables from original DB
349  try
350  {
351  memberMap =
352  cfgMgr->loadConfigurationGroup(
353  groupPair.first.first,
354  groupPair.first.second,
355  true,0,&accumulateErrors);
356  }
357  catch(std::runtime_error& e)
358  {
359 
360  std::cout << __COUT_HDR_FL__ << "Error was caught loading members for " <<
361  groupPair.first.first <<
362  "(" << groupPair.first.second << ")" <<
363  std::endl;
364  std::cout << __COUT_HDR_FL__ << e.what() << std::endl;
365  errDetected = true;
366  }
367  catch(...)
368  {
369  std::cout << __COUT_HDR_FL__ << "Error was caught loading members for " <<
370  groupPair.first.first <<
371  "(" << groupPair.first.second << ")" <<
372  std::endl;
373  errDetected = true;
374  }
375 
376  //=========================
377 
378  //if(count == 2) break;
379 
380 
381  //handle directory swap
382  std::cout << __COUT_HDR_FL__ << "Moving current directory: \t" << currentDir << std::endl;
383  std::cout << __COUT_HDR_FL__ << "\t... to: \t\t" << moveToDir << std::endl;
384  if(rename(currentDir.c_str(),moveToDir.c_str()) < 0)
385  {
386  __SS__ << "Problem!" << std::endl;
387  throw std::runtime_error(ss.str());
388  }
389 
390  if(pathToSwapIn != "")
391  {
392  std::cout << __COUT_HDR_FL__ << "Swapping in directory: \t" << pathToSwapIn << std::endl;
393  std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" << currentDir << std::endl;
394  if(rename(pathToSwapIn.c_str(),currentDir.c_str()) < 0)
395  {
396  __SS__ << "Problem!" << std::endl;
397  throw std::runtime_error(ss.str());
398  }
399  }
400  else if(count) //if not first time, replace from temporarily holding area
401  {
402  std::cout << __COUT_HDR_FL__ << "Moving temporary directory: \t" << (moveToDir+"_tmp") << std::endl;
403  std::cout << __COUT_HDR_FL__ << "\t.. to current directory: \t\t" << currentDir << std::endl;
404  if(rename((moveToDir+"_tmp").c_str(),currentDir.c_str()) < 0)
405  {
406  __SS__ << "Problem!" << std::endl;
407  throw std::runtime_error(ss.str());
408  }
409  }
410 
411 
412  //exit loop if any (loading) failure
413  if(errDetected)
414  {
415  //goto CLEAN_UP;
416 
417  //power on if group failed
418  // and record error
419 
420  groupErrors.insert(std::pair<
421  std::pair<std::string,ConfigurationGroupKey>,
422  std::string> (
423  std::pair<std::string,ConfigurationGroupKey>(
424  groupPair.first.first,
425  groupPair.first.second),
426  "Error caught loading the group."));
427  continue;
428  }
429 
430 
431 
432 
433  //=========================
434  //save group and its tables with new key and versions!
435  try
436  {
437  //saving tables
438  for(auto &memberPair:memberMap)
439  {
440  std::cout << __COUT_HDR_FL__ << memberPair.first << ":v" << memberPair.second << std::endl;
441 
442  //check if table has already been modified by a previous group
443  // (i.e. two groups using the same version of a table)
444  if(modifiedTables.find(std::pair<std::string,ConfigurationVersion>(
445  memberPair.first,
446  memberPair.second
447  )) != modifiedTables.end())
448  {
449 
450  std::cout << __COUT_HDR_FL__ << "Table was already modified!" << std::endl;
451  memberPair.second = modifiedTables[std::pair<std::string,ConfigurationVersion>(
452  memberPair.first,
453  memberPair.second
454  )];
455  std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
456  memberPair.first << ":v" << memberPair.second << std::endl;
457  continue;
458  }
459 
460  //change the version of the active view to flatVersion and save it
461  config = cfgMgr->getConfigurationByName(memberPair.first);
462  cfgView = config->getViewP();
463  cfgView->setVersion(ConfigurationVersion(flatVersion));
464  theInterface_->saveActiveVersion(config);
465 
466  //set it back for the table so that future groups can re-use cached version
467  cfgView->setVersion(memberPair.second); //IMPORTANT
468 
469  memberPair.second = flatVersion; //change version in the member map
470 
471  std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
472  memberPair.first << ":v" << memberPair.second << std::endl;
473 
474  //save new version to modifiedTables
475  modifiedTables.insert(std::pair<
476  std::pair<std::string,ConfigurationVersion>,
478  std::pair<std::string,ConfigurationVersion>(
479  memberPair.first,
480  memberPair.second),
481  ConfigurationVersion(flatVersion))
482  );
483  }
484 
485  //memberMap should now consist of members with new flat version, so save
486  theInterface_->saveConfigurationGroup(memberMap,
487  ConfigurationGroupKey::getFullGroupString(
488  groupPair.first.first,
489  ConfigurationGroupKey(flatVersion)));
490 
491  //and modify groupSet and activeGroupKeys keys
492  groupPair.second = ConfigurationGroupKey(flatVersion);
493 
494  //if this is an active group, save key change
495  if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() &&
496  activeGroupKeys[groupPair.first.first].first == groupPair.first.second)
497  activeGroupKeys[groupPair.first.first].second =
498  ConfigurationGroupKey(flatVersion);
499  }
500  catch(std::runtime_error& e)
501  {
502  std::cout << __COUT_HDR_FL__ << "Error was caught saving group " <<
503  groupPair.first.first <<
504  " (" << groupPair.first.second << ") " <<
505  std::endl;
506  std::cout << __COUT_HDR_FL__ << e.what() << std::endl;
507 
508  groupErrors.insert(std::pair<
509  std::pair<std::string,ConfigurationGroupKey>,
510  std::string> (
511  std::pair<std::string,ConfigurationGroupKey>(
512  groupPair.first.first,
513  groupPair.first.second),
514  "Error caught saving the group."));
515  }
516  catch(...)
517  {
518  std::cout << __COUT_HDR_FL__ << "Error was caught saving group " <<
519  groupPair.first.first <<
520  " (" << groupPair.first.second << ") " <<
521  std::endl;
522 
523  groupErrors.insert(std::pair<
524  std::pair<std::string,ConfigurationGroupKey>,
525  std::string> (
526  std::pair<std::string,ConfigurationGroupKey>(
527  groupPair.first.first,
528  groupPair.first.second),
529  "Error caught saving the group."));
530  }
531  //=========================
532 
533 
534  //increment flat version
535  ++flatVersion;
536  ++count;
537  }
538 
539  //record in readme for moveto
540  {
541  FILE *fp = fopen((moveToDir + "/README_otsdaq_flatten.txt").c_str(),"a");
542  if(!fp)
543  std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
544  else
545  {
546  time_t rawtime;
547  struct tm * timeinfo;
548  char buffer [200];
549 
550  time (&rawtime);
551  timeinfo = localtime (&rawtime);
552  strftime (buffer,200,"%b %d, %Y %I:%M%p %Z",timeinfo);
553 
554  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",
555  currentDir.c_str(),moveToDir.c_str(),time(0),buffer);
556 
557  fclose(fp);
558  }
559  }
560 
561 
562  //record in readme for swapin
563  {
564  FILE *fp = fopen((currentDir + "/README_otsdaq_flatten.txt").c_str(),"a");
565 
566  if(!fp)
567  std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
568  else
569  {
570  time_t rawtime;
571  struct tm * timeinfo;
572  char buffer [200];
573 
574  time (&rawtime);
575  timeinfo = localtime (&rawtime);
576  strftime (buffer,200,"%b %d, %Y %I:%M:%S%p %Z",timeinfo);
577 
578  fprintf(fp,"This database was moved from...\t %s \t to...\t %s at this time \t %lu \t %s\n\n",
579  pathToSwapIn.c_str(),currentDir.c_str(),time(0),buffer);
580  fclose(fp);
581  }
582  }
583 
584 // //print resulting all groups
585 //
586 // std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups:" << std::endl;
587 // for(const auto &group: groupSet)
588 // std::cout << __COUT_HDR_FL__ << group.first.first << ": " <<
589 // group.first.second << " => " << group.second << std::endl;
590 // std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups end." << std::endl;
591 //
592 //
593 // //print resulting active groups
594 //
595 // std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Active Groups:" << std::endl;
596 // for(const auto &activeGroup: activeGroupKeys)
597 // std::cout << __COUT_HDR_FL__ << activeGroup.first << ": " <<
598 // activeGroup.second.first << " => " << activeGroup.second.second << std::endl;
599 //
600 // std::cout << __COUT_HDR_FL__ << activeBackboneTableName << " is the " <<
601 // ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE << "." << std::endl;
602 // std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Active Groups end." << std::endl;
603 
604 
605 
606 
607 
608  //reload the active backbone (using activeGroupKeys)
609  // modify group aliases and table aliases properly based on groupSet and modifiedTables
610  // save new backbone with flatVersion to new DB
611 
612  if(activeBackboneTableName == "")
613  {
614  std::cout << __COUT_HDR_FL__ << "No active Backbone table identified." << std::endl;
615  goto CLEAN_UP;
616  }
617 
618  std::cout << "\n\n" << __COUT_HDR_FL__ <<
619  "Modifying the active Backbone table to reflect new table versions and group keys." <<
620  std::endl;
621 
622  {
623  memberMap =
624  cfgMgr->loadConfigurationGroup(
625  activeBackboneTableName,
626  activeGroupKeys[activeBackboneTableName].second,
627  true,0,&accumulateErrors);
628 
629  //modify GroupAliasesConfiguration and VersionAliasesConfiguration to point
630  // at DEFAULT and flatVersion respectively
631 
632  const std::string groupAliasesName = "GroupAliasesConfiguration";
633  const std::string versionAliasesName = "VersionAliasesConfiguration";
634 
635 
636  std::map<std::string, ConfigurationVersion> activeMap = cfgMgr->getActiveVersions();
637 
638  //modify GroupAliasesConfiguration
639  if(activeMap.find(groupAliasesName) != activeMap.end())
640  {
641  std::cout << __COUT_HDR_FL__ << "\n\nModifying " << groupAliasesName << std::endl;
642  config = cfgMgr->getConfigurationByName(groupAliasesName);
643  cfgView = config->getViewP();
644 
645  unsigned int col1 = cfgView->findCol("GroupName");
646  unsigned int col2 = cfgView->findCol("GroupKey");
647 
648  //cfgView->print();
649 
650  //change all key entries found to the new key and delete rows for groups not found
651  bool found;
652  for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
653  {
654  found = false;
655  for(const auto &group: groupSet)
656  if(group.second.isInvalid()) continue;
657  else if(cfgView->getDataView()[row][col1] == group.first.first &&
658  cfgView->getDataView()[row][col2] == group.first.second.toString())
659  {
660  //found a matching group/key pair
661  std::cout << __COUT_HDR_FL__ <<
662  "Changing row " << row << " for " <<
663  cfgView->getDataView()[row][col1] << " key=" <<
664  cfgView->getDataView()[row][col2] << " to NEW key=" <<
665  group.second << std::endl;
666  cfgView->setValue(
667  group.second.toString(),
668  row,col2);
669  found = true;
670  break;
671  }
672 
673  if(!found) //delete row
674  cfgView->deleteRow(row--);
675  }
676  //cfgView->print();
677  }
678 
679  //modify VersionAliasesConfiguration
680  if(activeMap.find(versionAliasesName) != activeMap.end())
681  {
682  std::cout << __COUT_HDR_FL__ << "\n\nModifying " << versionAliasesName << std::endl;
683  config = cfgMgr->getConfigurationByName(versionAliasesName);
684  cfgView = config->getViewP();
685  unsigned int col1 = cfgView->findCol("ConfigurationName");
686  unsigned int col2 = cfgView->findCol("Version");
687 
688  //change all version entries to the new version and delete rows with no match
689  bool found;
690  for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
691  {
692  found = false;
693  for(const auto &table:modifiedTables)
694  if(cfgView->getDataView()[row][col1] == table.first.first &&
695  cfgView->getDataView()[row][col2] == table.first.second.toString())
696  {
697  //found a matching group/key pair
698  std::cout << __COUT_HDR_FL__ << "Changing row " << row << " for " <<
699  cfgView->getDataView()[row][col1] << " version=" <<
700  cfgView->getDataView()[row][col2] << " to NEW version=" <<
701  table.second << std::endl;
702  cfgView->setValue(table.second.toString(),row,col2);
703  found = true;
704  break;
705  }
706 
707  if(!found) //delete row
708  cfgView->deleteRow(row--);
709  }
710  }
711 
712 
713 
714  //save new "GroupAliasesConfiguration" and "VersionAliasesConfiguration"
715 
716  std::cout << __COUT_HDR_FL__ << groupAliasesName << ":v" <<
717  memberMap[groupAliasesName] << std::endl;
718  //change the version of the active view to flatVersion and save it
719  config = cfgMgr->getConfigurationByName(groupAliasesName);
720  cfgView = config->getViewP();
721  cfgView->setVersion(ConfigurationVersion(flatVersion));
722  theInterface_->saveActiveVersion(config);
723 
724  memberMap[groupAliasesName] = flatVersion; //change version in the member map
725 
726  std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
727  groupAliasesName << ":v" << memberMap[groupAliasesName] << std::endl;
728 
729 
730 
731  std::cout << __COUT_HDR_FL__ << versionAliasesName << ":v" <<
732  memberMap[versionAliasesName] << std::endl;
733  //change the version of the active view to flatVersion and save it
734  config = cfgMgr->getConfigurationByName(versionAliasesName);
735  cfgView = config->getViewP();
736  cfgView->setVersion(ConfigurationVersion(flatVersion));
737  theInterface_->saveActiveVersion(config);
738 
739  memberMap[versionAliasesName] = flatVersion; //change version in the member map
740 
741  std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
742  versionAliasesName << ":v" << memberMap[versionAliasesName] << std::endl;
743 
744  //memberMap should now consist of members with new flat version, so save
745  theInterface_->saveConfigurationGroup(memberMap,
746  ConfigurationGroupKey::getFullGroupString(
747  activeBackboneTableName,
748  ConfigurationGroupKey(flatVersion)));
749 
750  activeGroupKeys[activeBackboneTableName].second =
751  ConfigurationGroupKey(flatVersion);
752 
753  std::cout << __COUT_HDR_FL__ << "New to-be-active backbone group " << activeBackboneTableName <<
754  ":v" << activeGroupKeys[activeBackboneTableName].second << std::endl;
755  }
756 
757 
758 
759  //backup the file ConfigurationManager::ACTIVE_GROUP_FILENAME with time
760  // and change the ConfigurationManager::ACTIVE_GROUP_FILENAME
761  // to reflect new group names/keys
762 
763  {
764  std::cout << "\n\n" << __COUT_HDR_FL__ << "Manipulating the Active Groups file..." << std::endl;
765 
766  //check if original active file exists
767  FILE *fp = fopen(ConfigurationManager::ACTIVE_GROUP_FILENAME.c_str(),"r");
768  if(!fp)
769  {
770  __SS__ << "Original active groups file '" << ConfigurationManager::ACTIVE_GROUP_FILENAME
771  << "' not found." << std::endl;
772  goto CLEAN_UP;
773  }
774 
775  std::cout << __COUT_HDR_FL__ << "Backing up file: " << ConfigurationManager::ACTIVE_GROUP_FILENAME
776  << std::endl;
777 
778  fclose(fp);
779 
780  std::string renameFile = ConfigurationManager::ACTIVE_GROUP_FILENAME + "." + nowTime;
781  rename(ConfigurationManager::ACTIVE_GROUP_FILENAME.c_str(),
782  renameFile.c_str());
783 
784  std::cout << __COUT_HDR_FL__ << "Backup file name: " << renameFile
785  << std::endl;
786 
787  ConfigurationGroupKey *theConfigurationGroupKey_, *theContextGroupKey_, *theBackboneGroupKey_;
788  std::string theConfigurationGroup_, theContextGroup_, theBackboneGroup_;
789 
790  theConfigurationGroup_ = activeConfigTableName;
791  theConfigurationGroupKey_ = &(activeGroupKeys[activeConfigTableName].second);
792 
793  theContextGroup_ = activeContextTableName;
794  theContextGroupKey_ = &(activeGroupKeys[activeContextTableName].second);
795 
796  theBackboneGroup_ = activeBackboneTableName;
797  theBackboneGroupKey_ = &(activeGroupKeys[activeBackboneTableName].second);
798 
799 
800  //the following is copied from ConfigurationManagerRW::activateConfigurationGroup
801  {
802  std::cout << __COUT_HDR_FL__ << "Updating persistent active groups to " <<
803  ConfigurationManager::ACTIVE_GROUP_FILENAME << " ..." << std::endl;
804 
805  std::string fn = ConfigurationManager::ACTIVE_GROUP_FILENAME;
806  FILE *fp = fopen(fn.c_str(),"w");
807  if(!fp) return;
808 
809  fprintf(fp,"%s\n",theContextGroup_.c_str());
810  fprintf(fp,"%s\n",theContextGroupKey_?theContextGroupKey_->toString().c_str():"-1");
811  fprintf(fp,"%s\n",theBackboneGroup_.c_str());
812  fprintf(fp,"%s\n",theBackboneGroupKey_?theBackboneGroupKey_->toString().c_str():"-1");
813  fprintf(fp,"%s\n",theConfigurationGroup_.c_str());
814  fprintf(fp,"%s\n",theConfigurationGroupKey_?theConfigurationGroupKey_->toString().c_str():"-1");
815  fclose(fp);
816  }
817  }
818 
819  //print resulting all groups
820 
821  std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups:" << std::endl;
822  for(const auto &group: groupSet)
823  std::cout << __COUT_HDR_FL__ << "\t" << group.first.first << ": " <<
824  group.first.second << " => " << group.second << std::endl;
825  std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups end." << std::endl;
826 
827 
828  //print resulting active groups
829 
830  std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Active Groups:" << std::endl;
831  for(const auto &activeGroup: activeGroupKeys)
832  std::cout << __COUT_HDR_FL__ << "\t" << activeGroup.first << ": " <<
833  activeGroup.second.first << " => " << activeGroup.second.second << std::endl;
834 
835  std::cout << __COUT_HDR_FL__ << activeBackboneTableName << " is the " <<
836  ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE << "." << std::endl;
837  std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Active Groups end." << std::endl;
838 
839 
840 CLEAN_UP:
841  //==============================================================================
842  std::cout << "\n\n" << __COUT_HDR_FL__ << "End of Flattening Active Configuration Groups!\n\n\n" << std::endl;
843 
844 
845 
846  std::cout << __COUT_HDR_FL__ << "****************************" << std::endl;
847  std::cout << __COUT_HDR_FL__ << "There were " << groupSet.size() <<
848  " groups considered, and there were errors handling " << groupErrors.size() <<
849  " of those groups." << std::endl;
850  std::cout << __COUT_HDR_FL__ << "The following errors were found handling the groups:" << std::endl;
851  for(auto& groupErr:groupErrors)
852  std::cout << __COUT_HDR_FL__ << "\t" << groupErr.first.first << " " << groupErr.first.second <<
853  ": \t" << groupErr.second << std::endl;
854  std::cout << __COUT_HDR_FL__ << "End of errors.\n\n" << std::endl;
855 
856 
857  std::cout << __COUT_HDR_FL__ << "Run the following to return to your previous database structure:" <<
858  std::endl;
859  std::cout << __COUT_HDR_FL__ << "\t otsdaq_flatten_system_aliases -1 " << moveToDir <<
860  "\n\n" << std::endl;
861 
862 
863  return;
864 }
865 
866 int main(int argc, char* argv[])
867 {
868  FlattenActiveSystemAliasConfigurationGroups(argc,argv);
869  return 0;
870 }
871 //BOOST_AUTO_TEST_SUITE_END()
872 
873