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