tdaq-develop-2025-02-12
otsdaq_save_json_document.cc
1 #include "otsdaq/MessageFacility/MessageFacility.h"
2 
3 #include <dirent.h>
4 #include <cassert>
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 
9 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
10 #include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h"
11 // #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
12 // #include "artdaq-database/JsonDocument/JSONDocument.h"
13 
18 #define TRACE_NAME "SaveJSON_Document"
19 
20 // #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
21 // #define __MF_SUBJECT__ __FILENAME__
22 // #define __MF_DECOR__ (__MF_SUBJECT__)
23 // #define __SHORTFILE__ (__builtin_strstr(&__FILE__[0], "/srcs/") ? __builtin_strstr(&__FILE__[0], "/srcs/") + 6 : __FILE__)
24 // #define __COUT_HDR_L__ "[" << std::dec << __LINE__ << "]\t"
25 // #define __COUT_HDR_FL__ __SHORTFILE__ << " " << __COUT_HDR_L__
26 // #define __COUT_ERR__ TLOG(TLVL_ERROR)
27 // #define __COUT_INFO__ TLOG(TLVL_INFO)
28 #undef __COUT__
29 #define __COUT__ \
30  std::cout \
31  << __MF_DECOR__ \
32  << __COUT_HDR_FL__ //TLOG(TLVL_DEBUG) //std::cout << __MF_DECOR__ << __COUT_HDR_FL__
33 
34 using namespace ots;
35 
36 void SaveJSON_Document(int argc, char* argv[])
37 {
38  // The configuration uses __ENV__("SERVICE_DATA_PATH") in init() so define it if it is not defined
39  if(getenv("SERVICE_DATA_PATH") == NULL)
40  setenv("SERVICE_DATA_PATH",
41  (std::string(__ENV__("USER_DATA")) + "/ServiceData").c_str(),
42  1);
43 
44  __COUT__ << "=================================================\n";
45  __COUT__ << "=================================================\n";
46  __COUT__ << "=================================================\n";
47  __COUT__ << "\nSaving Trigger Document!" << std::endl;
48 
49  __COUT__
50  << "\n\nusage: Two arguments:\n\t <path_to_source_JSON> <document_name_to_save>"
51  << std::endl
52  << std::endl;
53 
54  __COUT__ << "argc = " << argc << std::endl;
55  for(int i = 0; i < argc; i++)
56  __COUT__ << "argv[" << i << "] = " << argv[i] << std::endl;
57 
58  if(argc != 3)
59  {
60  __COUT__ << "Error! Must provide 2 parameters.\n\n" << std::endl;
61  return;
62  }
63 
64  //==============================================================================
65  // Define environment variables
66  // Note: normally these environment variables are set by StartOTS.sh
67 
68  // These are needed by
69  // otsdaq/otsdaq/ConfigurationDataFormats/ConfigurationInfoReader.cc [207]
70  // setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest
71  // setenv("CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 1);
72  // setenv("TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1);
74 
75  // Some configuration plug-ins use __ENV__("OTSDAQ_LIB") and
76  // __ENV__("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok
77  // setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1);
78  // setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1);
79 
80  // // Some configuration plug-ins use __ENV__("OTS_MAIN_PORT") in init() so define it
81  // setenv("OTS_MAIN_PORT", "2015", 1);
82 
83  // // also xdaq envs for XDAQContextTable
84  // setenv("XDAQ_CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 1);
85  // setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1);
87 
88  //==============================================================================
89  // get prepared with initial source db
90 
91  // ConfigurationManager instance immediately loads active groups
92  __COUT__ << "Saving document..." << std::endl;
93  ConfigurationManagerRW cfgMgrInst("doc_admin");
94  ConfigurationManagerRW* cfgMgr = &cfgMgrInst;
95 
96  std::FILE* fp = std::fopen(argv[1], "rb");
97  if(!fp)
98  {
99  __COUT__ << "\n\nERROR! Could not open file at " << argv[1]
100  << ". Error: " << errno << " - " << strerror(errno) << __E__;
101  return;
102  }
103  std::string json;
104  std::fseek(fp, 0, SEEK_END);
105  json.resize(std::ftell(fp));
106  std::rewind(fp);
107  std::fread(&json[0], 1, json.size(), fp);
108  std::fclose(fp);
109 
110  std::string artad_db_uri = __ENV__("ARTDAQ_DATABASE_URI");
111  __COUT__ << artad_db_uri << __E__;
112  if(!artad_db_uri.size())
113  {
114  __COUT__ << "ERROR! ARTDAQ_DATABASE_URI not set." << __E__;
115  return;
116  }
117  ConfigurationInterface* theInterface_ = cfgMgr->getConfigurationInterface();
118  std::pair<std::string, TableVersion> savedDoc =
119  theInterface_->saveCustomJSON(json, argv[2]);
120  __COUT__ << "Done with JSON doc save as '" << savedDoc.first << "-v"
121  << savedDoc.second << "'" << __E__;
122  return;
123 } //end SaveJSON_Document()
124 
125 int main(int argc, char* argv[])
126 {
127  if(getenv("OTSDAQ_LOG_FHICL") == NULL)
128  setenv("OTSDAQ_LOG_FHICL",
129  (std::string(__ENV__("USER_DATA")) +
130  "/MessageFacilityConfigurations/MessageFacilityWithCout.fcl")
131  .c_str(),
132  1);
133 
134  if(getenv("OTSDAQ_LOG_ROOT") == NULL)
135  setenv(
136  "OTSDAQ_LOG_ROOT", (std::string(__ENV__("USER_DATA")) + "/Logs").c_str(), 1);
137 
138  // INIT_MF("SaveJSON_Document");
139  SaveJSON_Document(argc, argv);
140  return 0;
141 }
142 // BOOST_AUTO_TEST_SUITE_END()