From a18c9cd913210ff84e009374d3079b7ce9636486 Mon Sep 17 00:00:00 2001 From: gianipez Date: Wed, 6 Dec 2023 16:24:44 -0600 Subject: [PATCH] added feature for custom JSON doc save/load; also fixed mongodb bug in ots script. --- .../ConfigurationInterface.h | 3 + .../Database_configInterface.cc | 93 ++++++++++++- .../Database_configInterface.h | 5 + otsdaq/TableCore/TableBase.cc | 8 +- otsdaq/TableCore/TableBase.h | 1 + otsdaq/TableCore/TableView.cc | 23 +++- tools/CMakeLists.txt | 20 ++- tools/ots | 37 ++--- tools/otsdaq_load_json_document.cc | 112 +++++++++++++++ tools/otsdaq_save_json_document.cc | 130 ++++++++++++++++++ 10 files changed, 407 insertions(+), 25 deletions(-) create mode 100644 tools/otsdaq_load_json_document.cc create mode 100644 tools/otsdaq_save_json_document.cc diff --git a/otsdaq/ConfigurationInterface/ConfigurationInterface.h b/otsdaq/ConfigurationInterface/ConfigurationInterface.h index 90f5d591..286acdfd 100755 --- a/otsdaq/ConfigurationInterface/ConfigurationInterface.h +++ b/otsdaq/ConfigurationInterface/ConfigurationInterface.h @@ -54,6 +54,9 @@ class ConfigurationInterface TableVersion /*version*/> getTableGroupMembers (std::string const& /*groupName*/, bool /*includeMetaDataTable*/ = false) const { __SS__; __THROW__(ss.str() + "ConfigurationInterface::... Must only call getTableGroupMembers in a mode with this functionality implemented (e.g. DatabaseConfigurationInterface)."); } virtual void saveTableGroup (std::map const& /*tableToVersionMap*/, std::string const& /*groupName*/) const { __SS__; __THROW__(ss.str() + "ConfigurationInterface::... Must only call saveTableGroup in a mode with this functionality implemented (e.g. DatabaseConfigurationInterface)."); } + virtual std::pair saveCustomJSON (const std::string& JSON, const std::string& documentNameToSave) const { __COUTV__(JSON); __COUTV__(documentNameToSave); return std::make_pair("",TableVersion());} + virtual std::string loadCustomJSON (const std::string& documentNameToLoad, TableVersion documentVersionToLoad) const {__COUTV__(documentNameToLoad); __COUTV__(documentVersionToLoad); return "{}";}; + protected: virtual void fill (TableBase* configuration, TableVersion version) const = 0; diff --git a/otsdaq/ConfigurationInterface/Database_configInterface.cc b/otsdaq/ConfigurationInterface/Database_configInterface.cc index d2c3869e..ae06c4c8 100644 --- a/otsdaq/ConfigurationInterface/Database_configInterface.cc +++ b/otsdaq/ConfigurationInterface/Database_configInterface.cc @@ -411,7 +411,7 @@ try __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Getting cache for " << groupName << "(" << groupKey << ")" << __E__; - TableBase localGroupMemberCacheSaver("GroupCache_" + groupName); + TableBase localGroupMemberCacheSaver(TableBase::GROUP_CACHE_PREPEND + groupName); TableVersion localVersion(atoi(groupKey.c_str())); localGroupMemberCacheSaver.changeVersionAndActivateView( localGroupMemberCacheSaver.createTemporaryView(), @@ -464,7 +464,7 @@ try std::string groupKey = tableGroup.substr(vi+2); __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Saving cache for " << groupName << "(" << groupKey << ")" << __E__; - TableBase localGroupMemberCacheSaver("GroupCache_" + groupName); + TableBase localGroupMemberCacheSaver(TableBase::GROUP_CACHE_PREPEND + groupName); localGroupMemberCacheSaver.changeVersionAndActivateView( localGroupMemberCacheSaver.createTemporaryView(), TableVersion(atoi(groupKey.c_str()))); @@ -560,4 +560,93 @@ catch(...) __SS_THROW__; } //end saveTableGroup() catch + + + +//============================================================================== +std::pair DatabaseConfigurationInterface::saveCustomJSON(const std::string& json, const std::string& documentNameToSave) const +try +{ + __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Saving doc '" << documentNameToSave << "'" << __E__; + + TableBase localDocSaver(TableBase::JSON_DOC_PREPEND + documentNameToSave); + + std::set versions = getVersions(&localDocSaver); + TableVersion version; + if(versions.size()) + version = TableVersion::getNextVersion(*versions.rbegin()); + else + version = TableVersion::DEFAULT; + __COUTV__(version); + + localDocSaver.changeVersionAndActivateView( + localDocSaver.createTemporaryView(), + version); + + localDocSaver.getViewP()->setCustomStorageData(json); + + __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Saving JSON string: " << + localDocSaver.getViewP()->getCustomStorageData() << __E__; + + __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Saving JSON doc as " << + localDocSaver.getView().getTableName() << "(" << + localDocSaver.getView().getVersion().toString() << ")" << __E__; + + + // save to db, and do not allow overwrite + saveActiveVersion(&localDocSaver, false /* overwrite */); + + return std::make_pair(localDocSaver.getTableName(), + localDocSaver.getView().getVersion()); +} //end saveCustomJSON() +catch(std::exception const& e) +{ + __SS__ << "DBI Exception saveCustomJSON for '" << documentNameToSave << "':\n\n" << e.what() << "\n"; + __COUT_ERR__ << ss.str(); + __SS_THROW__; +} +catch(...) +{ + __SS__ << "DBI Unknown exception saveCustomJSON for '" << documentNameToSave << ".'\n"; + __COUT_ERR__ << ss.str(); + __SS_THROW__; +} //end saveCustomJSON() catch + + +//============================================================================== +std::string DatabaseConfigurationInterface::loadCustomJSON(const std::string& documentNameToLoad, TableVersion documentVersionToLoad) const +try +{ + + __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Loading doc '" << documentNameToLoad << "-v" << + documentVersionToLoad << "'" << __E__; + + TableBase localDocLoader(TableBase::JSON_DOC_PREPEND + documentNameToLoad); + + localDocLoader.changeVersionAndActivateView( + localDocLoader.createTemporaryView(), + documentVersionToLoad); + + fill(&localDocLoader,documentVersionToLoad); + + __COUT_TYPE__(TLVL_DEBUG+20) << __COUT_HDR__ << "Loaded JSON doc string " << + localDocLoader.getViewP()->getCustomStorageData() << __E__; + + return localDocLoader.getViewP()->getCustomStorageData(); +} //end loadCustomJSON() +catch(std::exception const& e) +{ + __SS__ << "DBI Exception saveCustomJSON for '" << documentNameToLoad << "-v" << + documentVersionToLoad << "':\n\n" << e.what() << "\n"; + __COUT_ERR__ << ss.str(); + __SS_THROW__; +} +catch(...) +{ + __SS__ << "DBI Unknown exception saveCustomJSON for '" << documentNameToLoad << "-v" << + documentVersionToLoad << ".'\n"; + __COUT_ERR__ << ss.str(); + __SS_THROW__; +} //end loadCustomJSON() catch + DEFINE_OTS_CONFIGURATION_INTERFACE(DatabaseConfigurationInterface) diff --git a/otsdaq/ConfigurationInterface/Database_configInterface.h b/otsdaq/ConfigurationInterface/Database_configInterface.h index ea301af6..93692c77 100644 --- a/otsdaq/ConfigurationInterface/Database_configInterface.h +++ b/otsdaq/ConfigurationInterface/Database_configInterface.h @@ -48,6 +48,11 @@ class DatabaseConfigurationInterface : public ConfigurationInterface // create a new table group from the contents map void saveTableGroup (table_version_map_t const& memberMap, std::string const& tableGroup) const; + + + std::pair saveCustomJSON (const std::string& JSON, const std::string& documentNameToSave) const override; + std::string loadCustomJSON (const std::string& documentNameToLoad, TableVersion documentVersionToLoad) const override; + private: table_version_map_t getCachedTableGroupMembers (std::string const& tableGroup) const; void saveTableGroupMemberCache (table_version_map_t const& memberMap, std::string const& tableGroup) const; diff --git a/otsdaq/TableCore/TableBase.cc b/otsdaq/TableCore/TableBase.cc index a929978a..32712ede 100644 --- a/otsdaq/TableCore/TableBase.cc +++ b/otsdaq/TableCore/TableBase.cc @@ -13,6 +13,7 @@ using namespace ots; #define __COUT_HDR__ ("TableBase-" + getTableName() + "\t<> ") const std::string TableBase::GROUP_CACHE_PREPEND = "GroupCache_"; +const std::string TableBase::JSON_DOC_PREPEND = "JSONDoc_"; //============================================================================== // TableBase @@ -50,7 +51,8 @@ TableBase::TableBase(const std::string& tableName, } //if special GROUP CACHE table, handle construction in a special way - if(tableName.substr(0,TableBase::GROUP_CACHE_PREPEND.length()) == TableBase::GROUP_CACHE_PREPEND) + if(tableName.substr(0,TableBase::GROUP_CACHE_PREPEND.length()) == TableBase::GROUP_CACHE_PREPEND || + tableName.substr(0,TableBase::JSON_DOC_PREPEND.length()) == TableBase::JSON_DOC_PREPEND) { __COUT__ << "TableBase for '" << tableName << "' constructed." << __E__; return; @@ -1531,7 +1533,9 @@ std::string TableBase::convertToCaps(std::string& str, bool isTableName) else // error! non-alpha { //allow underscores for group cache document name - if(str.substr(0,TableBase::GROUP_CACHE_PREPEND.length()) == TableBase::GROUP_CACHE_PREPEND && str[c] == '_') + if((str.substr(0,TableBase::GROUP_CACHE_PREPEND.length()) == TableBase::GROUP_CACHE_PREPEND || + str.substr(0,TableBase::JSON_DOC_PREPEND.length()) == TableBase::JSON_DOC_PREPEND) && + str[c] == '_') { capsStr += '-'; continue; diff --git a/otsdaq/TableCore/TableBase.h b/otsdaq/TableCore/TableBase.h index 880006c7..230dde81 100644 --- a/otsdaq/TableCore/TableBase.h +++ b/otsdaq/TableCore/TableBase.h @@ -98,6 +98,7 @@ class TableBase public: static const std::string GROUP_CACHE_PREPEND; + static const std::string JSON_DOC_PREPEND; protected: std::string tableName_; diff --git a/otsdaq/TableCore/TableView.cc b/otsdaq/TableCore/TableView.cc index 61553494..99cc4a7c 100644 --- a/otsdaq/TableCore/TableView.cc +++ b/otsdaq/TableCore/TableView.cc @@ -86,9 +86,12 @@ TableView& TableView::copy(const TableView& src, TableVersion destinationVersion std::string tmpCachePrepend = TableBase::GROUP_CACHE_PREPEND; tmpCachePrepend = TableBase::convertToCaps(tmpCachePrepend); + std::string tmpJsonDocPrepend = TableBase::JSON_DOC_PREPEND; + tmpJsonDocPrepend = TableBase::convertToCaps(tmpJsonDocPrepend); //if special GROUP CACHE table, handle construction in a special way - if(tableName_.substr(0,tmpCachePrepend.length()) == tmpCachePrepend) + if(tableName_.substr(0,tmpCachePrepend.length()) == tmpCachePrepend || + tableName_.substr(0,tmpJsonDocPrepend.length()) == tmpJsonDocPrepend) { __COUT__ << "TableView copy for '" << tableName_ << "' done." << __E__; return *this; @@ -1930,9 +1933,13 @@ void TableView::printJSON(std::ostream& out) const { //handle special GROUP CACHE table std::string tmpCachePrepend = TableBase::GROUP_CACHE_PREPEND; tmpCachePrepend = TableBase::convertToCaps(tmpCachePrepend); - __COUT__ << " '" << tableName_ << "' vs " << tmpCachePrepend << __E__; + std::string tmpJsonDocPrepend = TableBase::JSON_DOC_PREPEND; + tmpJsonDocPrepend = TableBase::convertToCaps(tmpJsonDocPrepend); + __COUT__ << " '" << tableName_ << "' vs " << tmpCachePrepend << + " or " << tmpJsonDocPrepend << __E__; //if special GROUP CACHE table, handle construction in a special way - if(tableName_.substr(0,tmpCachePrepend.length()) == tmpCachePrepend) + if(tableName_.substr(0,tmpCachePrepend.length()) == tmpCachePrepend || + tableName_.substr(0,tmpJsonDocPrepend.length()) == tmpJsonDocPrepend) { out << getCustomStorageData(); return; @@ -2073,7 +2080,17 @@ int TableView::fillFromJSON(const std::string& json) //handle special GROUP CACHE table std::string tmpCachePrepend = TableBase::GROUP_CACHE_PREPEND; tmpCachePrepend = TableBase::convertToCaps(tmpCachePrepend); + std::string tmpJsonDocPrepend = TableBase::JSON_DOC_PREPEND; + tmpJsonDocPrepend = TableBase::convertToCaps(tmpJsonDocPrepend); + //if special JSON DOC table, handle construction in a special way + if(tableName_.substr(0,tmpJsonDocPrepend.length()) == tmpJsonDocPrepend) + { + __COUT__ << "Special JSON doc: " << json << __E__; + setCustomStorageData(json); + return 0; //success + } //end special JSON DOC table construction + //if special GROUP CACHE table, handle construction in a special way if(tableName_.substr(0,tmpCachePrepend.length()) == tmpCachePrepend) { diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 418cec48..2f3b2b64 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -3,7 +3,25 @@ cet_make_exec(NAME otsdaq_fix_new_table_fields_tool LIBRARIES otsdaq::Configurat cet_make_exec(NAME otsdaq_flatten_system_aliases LIBRARIES otsdaq::ConfigurationInterface) cet_make_exec(NAME otsdaq_import_system_aliases LIBRARIES otsdaq::ConfigurationInterface) -cet_script(ALWAYS_COPY common.sh EclipseBuild.sh ots otsdaq_add_artdaq_tables otsdaq_beautify_artdaq_tables otsdaq_convert_artdaq_daq_tables otsdaq_convert_config_to_table otsweb_login.sh ots_remote_start redmine_login.sh reset_ots_snapshot.sh StartOTS.sh vless_ots.sh vtail_ots.sh) +cet_make_exec(NAME otsdaq_save_json_document LIBRARIES otsdaq::ConfigurationInterface) +cet_make_exec(NAME otsdaq_load_json_document LIBRARIES otsdaq::ConfigurationInterface) + + +cet_script(ALWAYS_COPY + common.sh + EclipseBuild.sh + ots + otsdaq_add_artdaq_tables + otsdaq_beautify_artdaq_tables + otsdaq_convert_artdaq_daq_tables + otsdaq_convert_config_to_table + otsweb_login.sh + ots_remote_start + redmine_login.sh + reset_ots_snapshot.sh + StartOTS.sh + vless_ots.sh + vtail_ots.sh) install_fhicl(SUBDIRS fcl) install_headers() diff --git a/tools/ots b/tools/ots index dda99d3a..a51309e0 100644 --- a/tools/ots +++ b/tools/ots @@ -652,11 +652,30 @@ if [ "x$ARTDAQ_DATABASE_TYPE" == "xfilesystemdb" ]; then # As of 27-Oct-2023, no longer... check for somewhat-antiquated(pre-configuration-to-table) artdaq database + + #check for somewhat-antiquated artdaq 'DAQ' tables and auto-fix + if [ ! -e ${ARTDAQ_DATABASE_FILESYSTEM_PATH}/artdaqDaqTableConversion ]; then + out "Converting artdaq 'daq' tables in databases..." + + #backup because we are scared + cp -r ${ARTDAQ_DATABASE_FILESYSTEM_PATH} ${ARTDAQ_DATABASE_FILESYSTEM_PATH}bk12346 + cp -r ${USER_DATA} ${USER_DATA}bk12346 + + otsdaq_convert_artdaq_daq_tables + echo "converted" > ${ARTDAQ_DATABASE_FILESYSTEM_PATH}/artdaqDaqTableConversion + out "Conversion complete. You must run wiz mode once now to activate the configuration changes." + + if [ $ISCONFIG == 0 ]; then + exit + fi + fi +elif [ "x$ARTDAQ_DATABASE_TYPE" == "xmongodb" ]; then + out "mongodb identified." else out "${Red}${Bold}${Blink}Fatal Error${RstClr}." out "ARTDAQ_DATABASE_URI=$ARTDAQ_DATABASE_URI" - out "Environment variable ${Cyan}${Bold}ARTDAQ_DATABASE_URI${RstClr} does not have a value!" + out "Environment variable ${Cyan}${Bold}ARTDAQ_DATABASE_URI${RstClr} does not have a valid type: ${ARTDAQ_DATABASE_TYPE}!" out "To setup, use 'export ARTDAQ_DATABASE_URI='" @@ -669,22 +688,6 @@ else # mkdir $ARTDAQ_DATABASE_FILESYSTEM_PATH &>/dev/null 2>&1 #hide output fi -#check for somewhat-antiquated artdaq 'DAQ' tables and auto-fix -if [ ! -e ${ARTDAQ_DATABASE_FILESYSTEM_PATH}/artdaqDaqTableConversion ]; then - out "Converting artdaq 'daq' tables in databases..." - - #backup because we are scared - cp -r ${ARTDAQ_DATABASE_FILESYSTEM_PATH} ${ARTDAQ_DATABASE_FILESYSTEM_PATH}bk12346 - cp -r ${USER_DATA} ${USER_DATA}bk12346 - - otsdaq_convert_artdaq_daq_tables - echo "converted" > ${ARTDAQ_DATABASE_FILESYSTEM_PATH}/artdaqDaqTableConversion - out "Conversion complete. You must run wiz mode once now to activate the configuration changes." - - if [ $ISCONFIG == 0 ]; then - exit - fi -fi export TABLE_INFO_PATH=${USER_DATA}/TableInfo export SERVICE_DATA_PATH=${USER_DATA}/ServiceData #it is not recommended that the SERVICE_DATA_PATH environment variable be changed by the user, it may break some features. diff --git a/tools/otsdaq_load_json_document.cc b/tools/otsdaq_load_json_document.cc new file mode 100644 index 00000000..771aeb3e --- /dev/null +++ b/tools/otsdaq_load_json_document.cc @@ -0,0 +1,112 @@ +#include "otsdaq/MessageFacility/MessageFacility.h" + +#include +#include +#include +#include +#include + +#include "otsdaq/ConfigurationInterface/ConfigurationInterface.h" +#include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h" +// #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h" +// #include "artdaq-database/JsonDocument/JSONDocument.h" + +// usage: +// otsdaq_load_json_document + + +#define TRACE_NAME "LoadJSON_Document" + +#undef __COUT__ +#define __COUT__ std::cout << __MF_DECOR__ << __COUT_HDR_FL__ //TLOG(TLVL_DEBUG) //std::cout << __MF_DECOR__ << __COUT_HDR_FL__ + + +using namespace ots; + +void LoadJSON_Document(int argc, char* argv[]) +{ + // The configuration uses __ENV__("SERVICE_DATA_PATH") in init() so define it if it is not defined + if(getenv("SERVICE_DATA_PATH") == NULL) + setenv("SERVICE_DATA_PATH", (std::string(__ENV__("USER_DATA")) + "/ServiceData").c_str(), 1); + + __COUT__ << "=================================================\n"; + __COUT__ << "=================================================\n"; + __COUT__ << "=================================================\n"; + __COUT__ << "\nLoading Trigger Document!" << std::endl; + + __COUT__ << "\n\nusage: Two arguments:\n\t " + << std::endl << std::endl; + + + __COUT__ << "argc = " << argc << std::endl; + for(int i = 0; i < argc; i++) + __COUT__ << "argv[" << i << "] = " << argv[i] << std::endl; + + if(argc != 4) + { + __COUT__ << "\n\nError! Must provide 3 parameters.\n\n" << std::endl; + return; + } + + + //============================================================================== + // Define environment variables + // Note: normally these environment variables are set by StartOTS.sh + + // These are needed by + // otsdaq/otsdaq/ConfigurationDataFormats/ConfigurationInfoReader.cc [207] + // setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest + // setenv("CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 1); + // setenv("TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1); + // //////////////////////////////////////////////////// + + // // Some configuration plug-ins use __ENV__("OTSDAQ_LIB") and + // // __ENV__("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok + // setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); + // setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); + + // // Some configuration plug-ins use __ENV__("OTS_MAIN_PORT") in init() so define it + // setenv("OTS_MAIN_PORT", "2015", 1); + + // // also xdaq envs for XDAQContextTable + // setenv("XDAQ_CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 1); + // setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1); + //////////////////////////////////////////////////// + + //============================================================================== + // get prepared with initial source db + + // ConfigurationManager instance immediately loads active groups + __COUT__ << "Loading JSON Document..." << std::endl; + ConfigurationManagerRW cfgMgrInst("load_admin"); + ConfigurationManagerRW* cfgMgr = &cfgMgrInst; + + + ConfigurationInterface* theInterface_ = cfgMgr->getConfigurationInterface(); + std::string json = theInterface_->loadCustomJSON(argv[1],TableVersion(atoi(argv[2]))); + __COUTV__(json); + FILE* fp = std::fopen(argv[3], "w"); + if(!fp) + { + __COUT__ << "\n\nERROR! Could not open file at " << argv[1] << + ". Error: " << errno << " - " << strerror(errno) << __E__; + return; + } + fputs(json.c_str(),fp); + fclose(fp); + return; +} //end LoadJSON_Document() + +int main(int argc, char* argv[]) +{ + if(getenv("OTSDAQ_LOG_FHICL") == NULL) + setenv("OTSDAQ_LOG_FHICL", (std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/MessageFacilityWithCout.fcl").c_str(), 1); + + if(getenv("OTSDAQ_LOG_ROOT") == NULL) + setenv("OTSDAQ_LOG_ROOT", (std::string(__ENV__("USER_DATA")) + "/Logs").c_str(), 1); + + // INIT_MF("LoadJSON_Document"); + LoadJSON_Document(argc, argv); + return 0; +} +// BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/otsdaq_save_json_document.cc b/tools/otsdaq_save_json_document.cc new file mode 100644 index 00000000..93369cad --- /dev/null +++ b/tools/otsdaq_save_json_document.cc @@ -0,0 +1,130 @@ +#include "otsdaq/MessageFacility/MessageFacility.h" + +#include +#include +#include +#include +#include + +#include "otsdaq/ConfigurationInterface/ConfigurationInterface.h" +#include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h" +// #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h" +// #include "artdaq-database/JsonDocument/JSONDocument.h" + +// usage: +// otsdaq_save_json_document +// + +#define TRACE_NAME "SaveJSON_Document" + +// #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +// #define __MF_SUBJECT__ __FILENAME__ +// #define __MF_DECOR__ (__MF_SUBJECT__) +// #define __SHORTFILE__ (__builtin_strstr(&__FILE__[0], "/srcs/") ? __builtin_strstr(&__FILE__[0], "/srcs/") + 6 : __FILE__) +// #define __COUT_HDR_L__ "[" << std::dec << __LINE__ << "]\t" +// #define __COUT_HDR_FL__ __SHORTFILE__ << " " << __COUT_HDR_L__ +// #define __COUT_ERR__ TLOG(TLVL_ERROR) +// #define __COUT_INFO__ TLOG(TLVL_INFO) +#undef __COUT__ +#define __COUT__ std::cout << __MF_DECOR__ << __COUT_HDR_FL__ //TLOG(TLVL_DEBUG) //std::cout << __MF_DECOR__ << __COUT_HDR_FL__ + +using namespace ots; + +void SaveJSON_Document(int argc, char* argv[]) +{ + // The configuration uses __ENV__("SERVICE_DATA_PATH") in init() so define it if it is not defined + if(getenv("SERVICE_DATA_PATH") == NULL) + setenv("SERVICE_DATA_PATH", (std::string(__ENV__("USER_DATA")) + "/ServiceData").c_str(), 1); + + __COUT__ << "=================================================\n"; + __COUT__ << "=================================================\n"; + __COUT__ << "=================================================\n"; + __COUT__ << "\nSaving Trigger Document!" << std::endl; + + __COUT__ << "\n\nusage: Two arguments:\n\t " + << std::endl << std::endl; + + + __COUT__ << "argc = " << argc << std::endl; + for(int i = 0; i < argc; i++) + __COUT__ << "argv[" << i << "] = " << argv[i] << std::endl; + + if(argc != 3) + { + __COUT__ << "Error! Must provide 2 parameters.\n\n" << std::endl; + return; + } + + //============================================================================== + // Define environment variables + // Note: normally these environment variables are set by StartOTS.sh + + // These are needed by + // otsdaq/otsdaq/ConfigurationDataFormats/ConfigurationInfoReader.cc [207] + // setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest + // setenv("CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 1); + // setenv("TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1); + //////////////////////////////////////////////////// + + // Some configuration plug-ins use __ENV__("OTSDAQ_LIB") and + // __ENV__("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok + // setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); + // setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); + + // // Some configuration plug-ins use __ENV__("OTS_MAIN_PORT") in init() so define it + // setenv("OTS_MAIN_PORT", "2015", 1); + + // // also xdaq envs for XDAQContextTable + // setenv("XDAQ_CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 1); + // setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1); + //////////////////////////////////////////////////// + + //============================================================================== + // get prepared with initial source db + + // ConfigurationManager instance immediately loads active groups + __COUT__ << "Saving document..." << std::endl; + ConfigurationManagerRW cfgMgrInst("doc_admin"); + ConfigurationManagerRW* cfgMgr = &cfgMgrInst; + + + std::FILE* fp = std::fopen(argv[1], "rb"); + if(!fp) + { + __COUT__ << "\n\nERROR! Could not open file at " << argv[1] << + ". Error: " << errno << " - " << strerror(errno) << __E__; + return; + } + std::string json; + std::fseek(fp, 0, SEEK_END); + json.resize(std::ftell(fp)); + std::rewind(fp); + std::fread(&json[0], 1, json.size(), fp); + std::fclose(fp); + + std::string artad_db_uri = __ENV__("ARTDAQ_DATABASE_URI"); + __COUT__ << artad_db_uri << __E__; + if(!artad_db_uri.size()) + { + __COUT__ << "ERROR! ARTDAQ_DATABASE_URI not set." << __E__; + return; + } + ConfigurationInterface* theInterface_ = cfgMgr->getConfigurationInterface(); + std::pair savedDoc = theInterface_->saveCustomJSON(json,argv[2]); + __COUT__ << "Done with JSON doc save as '" << savedDoc.first << "-v" << savedDoc.second << "'" << __E__; + return; +} //end SaveJSON_Document() + +int main(int argc, char* argv[]) +{ + if(getenv("OTSDAQ_LOG_FHICL") == NULL) + setenv("OTSDAQ_LOG_FHICL", (std::string(__ENV__("USER_DATA")) + "/MessageFacilityConfigurations/MessageFacilityWithCout.fcl").c_str(), 1); + + if(getenv("OTSDAQ_LOG_ROOT") == NULL) + setenv("OTSDAQ_LOG_ROOT", (std::string(__ENV__("USER_DATA")) + "/Logs").c_str(), 1); + + // INIT_MF("SaveJSON_Document"); + SaveJSON_Document(argc, argv); + return 0; +} +// BOOST_AUTO_TEST_SUITE_END()