otsdaq  v1_01_03
 All Classes Namespaces Functions
otsdaq_database_migrate.cc
1 //#define BOOST_TEST_MODULE ( databaseconfiguration test)
2 
3 //#include "boost/test/auto_unit_test.hpp"
4 
5 #include <string>
6 #include <iostream>
7 #include <memory>
8 #include <cassert>
9 #include <dirent.h>
10 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
11 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
12 //#include "otsdaq-core/ConfigurationPluginDataFormats/Configurations.h"
13 //#include "otsdaq-core/ConfigurationPluginDataFormats/ConfigurationAliases.h"
14 //#include "otsdaq-core/ConfigurationPluginDataFormats/FEConfiguration.h"
15 //#include "otsdaq-core/PluginMakers/MakeInterfaceConfiguration.h"
16 //#include "otsdaq-core/PluginMakers/MakeInterface.h"
17 #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
18 #include "artdaq-database/JsonDocument/JSONDocument.h"
19 
20 
21 using namespace ots;
22 
23 //BOOST_AUTO_TEST_SUITE( databaseconfiguration_test )
24 
25 void readxml_writedb_configurations()
26 {
27 
28  //artdaq::database::filesystem::index::debug::enable();
29  //artdaq::database::jsonutils::debug::enableJSONDocument();
30 
31 
32 
33  std::string dbDir = std::string(getenv("ARTDAQ_DATABASE_DATADIR"));
34  std::cout << __COUT_HDR_FL__ << "Destination DB Directory ARTDAQ_DATABASE_DATADIR: " << dbDir << std::endl;
35 
36  if(getenv("USER_DATA") == NULL) std::cout << __COUT_HDR_FL__ << "Missing env variable: USER_DATA. It must be set!" << std::endl;
37 
38  std::vector<std::string> configTables; //list of tables to migrate
39  std::vector<std::string> failedConfigVersions; //list of tables/versions that failed to migrate
40 
41 
42  //normally CONFIGURATION_TYPE is set by StartOTS.sh
43  setenv("CONFIGURATION_DATA_PATH",(std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(),1);
44  std::string configDir = std::string(getenv("CONFIGURATION_DATA_PATH")) + '/';
45 
46  //CONFIGURATION_TYPE needed by otsdaq/otsdaq-core/ConfigurationDataFormats/ConfigurationInfoReader.cc [187]
47  //Can be File, Database, DatabaseTest
48  setenv("CONFIGURATION_TYPE","File",1);
49 
50  //add configurations to vector list from directory
51  {
52  std::cout << __COUT_HDR_FL__ << "ConfigurationDir: " << configDir << std::endl;
53  DIR *dp;
54 
55  struct dirent *dirp;
56 
57  if((dp = opendir(configDir.c_str())) == 0)
58  {
59  std::cout << __COUT_HDR_FL__<< "ERROR:(" << errno << "). Can't open directory: " << configDir << std::endl;
60  exit(0);
61  }
62 
63  const unsigned char isDir = 0x4;
64  while ((dirp = readdir(dp)) != 0)
65  if(dirp->d_type == isDir && dirp->d_name[0] != '.' )
66  {
67  std::cout << __COUT_HDR_FL__<< dirp->d_name << std::endl;
68  configTables.push_back(dirp->d_name);
69  }
70 
71  closedir(dp);
72  }
73 
74  unsigned int configurationsCount=0, skippedConfigurations=0,
75  skippedVersions=0, versionsCount=0;
76 
77  ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(true);
78 
79  for(unsigned int i = 0; i < configTables.size(); ++i)
80  {
81  ConfigurationBase* base = 0;
82  std::cout << __COUT_HDR_FL__ << std::endl;
83  std::cout << __COUT_HDR_FL__ << std::endl;
84  std::cout << __COUT_HDR_FL__ << (i+1) << " of " << configTables.size() << ": " << configTables[i] << std::endl;
85 
86  try
87  {
88  theInterface_->get(base,configTables[i], 0, 0, true); //load an empty instance, just to get all available version
89  }
90  catch(cet::exception e)
91  {
92  std::cout << __COUT_HDR_FL__ << std::endl << e.what() << std::endl;
93  std::cout << __COUT_HDR_FL__ <<
94  "Caught exception, so skip. (likely not a defined configuration class) " << std::endl;
95 
96  ++skippedConfigurations;
97  failedConfigVersions.push_back(configTables[i] + ":*");
98  continue;
99  }
100  ++configurationsCount;
101 
102 
103  auto version = theInterface_->getVersions(base);
104 
105  for(auto currVersion:version)
106  {
107  std::cout << __COUT_HDR_FL__ << "loading " << configTables[i] << " version " << currVersion << std::endl;
108 
109  try
110  {
111  //reset configurationView and load current version
112  theInterface_->get(base,configTables[i], 0, 0, false, currVersion, true); //load version 0 for all, first
113  }
114  catch(std::runtime_error e)
115  {
116  std::cout << __COUT_HDR_FL__ << std::endl << e.what() << std::endl;
117  std::cout << __COUT_HDR_FL__ << "Caught exception for version, so skip. (likely invalid column names) " << std::endl;
118 
119  ++skippedVersions;
120  failedConfigVersions.push_back(configTables[i] + ":" + currVersion.toString());
121  continue;
122  }
123  ++versionsCount;
124 
125  std::cout << __COUT_HDR_FL__ << "loaded " << configTables[i] << std::endl;
126 
127  //save the active version
128  std::cout << __COUT_HDR_FL__ << "Current version: " << base->getViewVersion() << std::endl;
129  std::cout << __COUT_HDR_FL__ << "Current version: " << base->getView().getVersion() << std::endl;
130 
131 
132  //
133  // **** switch to db style interface?!!?!? **** //
134  //
135  theInterface_ = ConfigurationInterface::getInstance(false); //true for File interface, false for artdaq database
136  //
137  //*****************************************
138  //*****************************************
139 
140 
141 
142 
143  // =========== Save as Current Version Number ========== //
144  //uses same version number in migration database
145  //
146  theInterface_->saveActiveVersion(base);
147  //
148  // =========== END Save as Current Version Number ========== //
149 
150 
151 
152 
153  // =========== Save as New Version Number ========== //
154  //if wanted to create a new version number based on this version
155  //
156  //int tmpView = base->createTemporaryView(currVersion);
157  //theInterface_->saveNewVersion(base,tmpView);
158  //
159  // =========== END Save as Current Version Number ========== //
160 
161  std::cout << __COUT_HDR_FL__ << "Version saved " << std::endl;
162 
163 
164 
165 
166 
167 
168  //*****************************************
169  //*****************************************
170  //
171  // **** switch back db style interface?!!?!? **** //
172  //
173  theInterface_ = ConfigurationInterface::getInstance(true); //true for File interface, false for artdaq database
174  //
175  //
176 
177  //break; //uncomment to just do the one version (for debugging)
178  }
179  delete base; //cleanup config instance
180  //break; //uncomment to just do the one config table (for debugging)
181  }
182 
183  std::cout << __COUT_HDR_FL__ << "End of migrating Configuration!" << std::endl;
184 
185  std::cout << __COUT_HDR_FL__ << "\n\nList of failed configs:versions (size=" <<
186  failedConfigVersions.size() << std::endl;
187  for(auto &f : failedConfigVersions)
188  std::cout << __COUT_HDR_FL__ << f << std::endl;
189 
190  std::cout << __COUT_HDR_FL__ << "\n\nEND List of failed configs:versions" << std::endl;
191 
192 
193  std::cout << __COUT_HDR_FL__ << "\n\n\tStats:" << std::endl;
194  std::cout << __COUT_HDR_FL__ << "\t\tconfigurationsCount: " << configurationsCount << std::endl;
195  std::cout << __COUT_HDR_FL__ << "\t\tskippedConfigurations: " << skippedConfigurations << std::endl;
196  std::cout << __COUT_HDR_FL__ << "\t\tversionsCount: " << versionsCount << std::endl;
197  std::cout << __COUT_HDR_FL__ << "\t\tskippedVersions: " << skippedVersions << std::endl;
198 
199  std::cout << __COUT_HDR_FL__ << "\nEnd of migrating Configuration!" << std::endl;
200 
201 
202  return;
203 }
204 
205 int main(int ,char **)
206 {
207  readxml_writedb_configurations();
208  return 0;
209 }
210 //BOOST_AUTO_TEST_SUITE_END()
211