tdaq-develop-2025-02-12
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
31  << std::endl;
32 
33  if(__ENV__("USER_DATA") == NULL)
34  __COUT__ << "Missing env variable: USER_DATA. It must be set!" << std::endl;
35 
36  std::vector<std::string> configTables; // list of tables to migrate
37  std::vector<std::string>
38  failedConfigVersions; // list of tables/versions that failed to migrate
39 
40  // normally CONFIGURATION_TYPE is set by StartOTS.sh
41  setenv("CONFIGURATION_DATA_PATH",
42  (std::string(__ENV__("USER_DATA")) + "/ConfigurationDataExamples").c_str(),
43  1);
44  std::string configDir = std::string(__ENV__("CONFIGURATION_DATA_PATH")) + '/';
45 
46  // CONFIGURATION_TYPE needed by
47  // otsdaq/otsdaq/ConfigurationDataFormats/ConfigurationInfoReader.cc [187] Can
48  // be File, Database, DatabaseTest
49  setenv("CONFIGURATION_TYPE", "File", 1);
50 
51  // add configurations to vector list from directory
52  {
53  __COUT__ << "ConfigurationDir: " << configDir << std::endl;
54  DIR* dp;
55 
56  struct dirent* dirp;
57 
58  if((dp = opendir(configDir.c_str())) == 0)
59  {
60  __COUT__ << "ERROR:(" << errno << "). Can't open directory: " << configDir
61  << std::endl;
62  exit(0);
63  }
64 
65  const unsigned char isDir = 0x4;
66  while((dirp = readdir(dp)) != 0)
67  if(dirp->d_type == isDir && dirp->d_name[0] != '.')
68  {
69  __COUT__ << dirp->d_name << std::endl;
70  configTables.push_back(dirp->d_name);
71  }
72 
73  closedir(dp);
74  }
75 
76  unsigned int configurationsCount = 0, skippedConfigurations = 0, skippedVersions = 0,
77  versionsCount = 0;
78 
79  ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(true);
80 
81  for(unsigned int i = 0; i < configTables.size(); ++i)
82  {
83  TableBase* base = 0;
84  __COUT__ << std::endl;
85  __COUT__ << std::endl;
86  __COUT__ << (i + 1) << " of " << configTables.size() << ": " << configTables[i]
87  << std::endl;
88 
89  try
90  {
91  theInterface_->get(
92  base,
93  configTables[i],
94  0,
95  0,
96  true); // load an empty instance, just to get all available version
97  }
98  catch(cet::exception e)
99  {
100  __COUT__ << std::endl << e.what() << std::endl;
101  __COUT__ << "Caught exception, so skip. (likely not a defined configuration "
102  "class) "
103  << std::endl;
104 
105  ++skippedConfigurations;
106  failedConfigVersions.push_back(configTables[i] + ":*");
107  continue;
108  }
109  ++configurationsCount;
110 
111  auto version = theInterface_->getVersions(base);
112 
113  for(auto currVersion : version)
114  {
115  __COUT__ << "loading " << configTables[i] << " version " << currVersion
116  << std::endl;
117 
118  try
119  {
120  // reset configurationView and load current version
121  theInterface_->get(base,
122  configTables[i],
123  0,
124  0,
125  false,
126  currVersion,
127  true); // load version 0 for all, first
128  }
129  catch(std::runtime_error e)
130  {
131  __COUT__ << std::endl << e.what() << std::endl;
132  __COUT__ << "Caught exception for version, so skip. (likely invalid "
133  "column names) "
134  << std::endl;
135 
136  ++skippedVersions;
137  failedConfigVersions.push_back(configTables[i] + ":" +
138  currVersion.toString());
139  continue;
140  }
141  ++versionsCount;
142 
143  __COUT__ << "loaded " << configTables[i] << std::endl;
144 
145  // save the active version
146  __COUT__ << "Current version: " << base->getViewVersion() << std::endl;
147  __COUT__ << "Current version: " << base->getView().getVersion() << std::endl;
148 
149  //
150  // **** switch to db style interface?!!?!? **** //
151  //
152  theInterface_ = ConfigurationInterface::getInstance(
153  false); // true for File interface, false for artdaq database
154  //
155  //*****************************************
156  //*****************************************
157 
158  // =========== Save as Current Version Number ========== //
159  // uses same version number in migration database
160  //
161  theInterface_->saveActiveVersion(base);
162  //
163  // =========== END Save as Current Version Number ========== //
164 
165  // =========== Save as New Version Number ========== //
166  // if wanted to create a new version number based on this version
167  //
168  // int tmpView = base->createTemporaryView(currVersion);
169  // theInterface_->saveNewVersion(base,tmpView);
170  //
171  // =========== END Save as Current Version Number ========== //
172 
173  __COUT__ << "Version saved " << std::endl;
174 
175  //*****************************************
176  //*****************************************
177  //
178  // **** switch back db style interface?!!?!? **** //
179  //
180  theInterface_ = ConfigurationInterface::getInstance(
181  true); // true for File interface, false for artdaq database
182  //
183  //
184 
185  // break; //uncomment to just do the one version (for debugging)
186  }
187  delete base; // cleanup config instance
188  // break; //uncomment to just do the one config table (for
189  // debugging)
190  }
191 
192  __COUT__ << "End of migrating Configuration!" << std::endl;
193 
194  __COUT__ << "\n\nList of failed configs:versions (size="
195  << failedConfigVersions.size() << std::endl;
196  for(auto& f : failedConfigVersions)
197  __COUT__ << f << std::endl;
198 
199  __COUT__ << "\n\nEND List of failed configs:versions" << std::endl;
200 
201  __COUT__ << "\n\n\tStats:" << std::endl;
202  __COUT__ << "\t\tconfigurationsCount: " << configurationsCount << std::endl;
203  __COUT__ << "\t\tskippedConfigurations: " << skippedConfigurations << std::endl;
204  __COUT__ << "\t\tversionsCount: " << versionsCount << std::endl;
205  __COUT__ << "\t\tskippedVersions: " << skippedVersions << std::endl;
206 
207  __COUT__ << "\nEnd of migrating Configuration!" << std::endl;
208 
209  return;
210 }
211 
212 int main(int, char**)
213 {
214  readxml_writedb_configurations();
215  return 0;
216 }
217 // BOOST_AUTO_TEST_SUITE_END()
const TableVersion & getViewVersion(void) const
always the active one
Definition: TableBase.cc:690