LCOV - code coverage report
Current view: top level - otsdaq-utilities/tools - ots_table_upload_csv.cc (source / functions) Coverage Total Hit
Test: mu2edaq.info.cleaned Lines: 0.0 % 84 0
Test Date: 2026-07-30 01:56:44 Functions: 0.0 % 18 0

            Line data    Source code
       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              : 
      12              : // Shared test utilities
      13              : #include "otsdaq/Macros/TestUtilities.h"
      14              : 
      15              : /// Uploads a table name as CSV file (compatible with Excel, Google Docs, etc.) and creates a new version of the table on the configuration database,
      16              : ///     Same format as downloaded/uploaded by the web app Configuration GUI.
      17              : ///
      18              : /// usage:
      19              : ///     ots_table_upload_csv <table name> <file path to CSV>
      20              : ///
      21              : /// Note: CSV file format is same as created by ots_table_download_csv
      22              : 
      23              : using namespace ots;
      24              : 
      25            0 : void UploadTableCSV(int argc, char* argv[])
      26              : {
      27            0 :         std::cout << "=================================================\n";
      28            0 :         std::cout << "=================================================\n";
      29            0 :         std::cout << "=================================================\n";
      30            0 :         __COUT_INFO__ << "Uploading Table CSV!" << std::endl;
      31              : 
      32            0 :         std::cout << "\n\nusage: 2 arguments:\n\t <table name> <file path to CSV> \n\n"
      33            0 :                   << std::endl;
      34              : 
      35            0 :         std::cout << "argc = " << argc << std::endl;
      36            0 :         for(int i = 0; i < argc; i++)
      37            0 :                 std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
      38              : 
      39            0 :         if(argc != 3)
      40              :         {
      41            0 :                 std::cout << "Error! Must provide two parameters.\n\n" << std::endl;
      42            0 :                 return;
      43              :         }
      44              : 
      45            0 :         std::string tableName = argv[1];
      46            0 :         __COUTV__(tableName);
      47            0 :         auto tablePos = tableName.find("Table");
      48            0 :         if(tablePos == std::string::npos ||  //avoid case when tableName is length 4
      49            0 :            tablePos != tableName.size() - strlen("Table"))
      50            0 :                 tableName += "Table";
      51            0 :         __COUTV__(tableName);
      52              : 
      53            0 :         std::string uploadPath = argv[2];
      54            0 :         __COUTV__(uploadPath);
      55              : 
      56            0 :         std::string author = "";
      57              :         try
      58              :         {
      59            0 :                 author = __ENV__("OTS_KCACHE_USER");
      60              :         }
      61            0 :         catch(...)
      62              :         {
      63            0 :                 __SS__ << "No valid ots kerberos user found, please kinit and source "
      64            0 :                           "setup_ots.sh or kint_setup.sh."
      65            0 :                        << __E__;
      66            0 :                 __SS_THROW__;
      67            0 :         }
      68            0 :         __COUTV__(author);
      69              : 
      70            0 :         std::FILE* fp = std::fopen(uploadPath.c_str(), "rb");
      71            0 :         if(!fp)
      72              :         {
      73            0 :                 __SS__ << "Could not open file at " << uploadPath << ". Error: " << errno << " - "
      74            0 :                        << strerror(errno) << __E__;
      75            0 :                 __SS_THROW__;
      76            0 :         }
      77              : 
      78            0 :         std::string csv;
      79            0 :         std::fseek(fp, 0, SEEK_END);
      80            0 :         csv.resize(std::ftell(fp));
      81            0 :         std::rewind(fp);
      82            0 :         std::fread(&csv[0], 1, csv.size(), fp);
      83            0 :         std::fclose(fp);
      84              : 
      85            0 :         __COUTV__(csv);
      86              : 
      87              :         // return;
      88              : 
      89              :         //==============================================================================
      90              :         // get prepared with initial source db
      91              : 
      92              :         // ConfigurationManager instance immediately loads active groups
      93            0 :         __COUT_INFO__ << "Initializing..." << std::endl;
      94              : 
      95            0 :         std::string ARTDAQ_DATABASE_URI = __ENV__("ARTDAQ_DATABASE_URI");
      96            0 :         __COUTV__(ARTDAQ_DATABASE_URI);
      97              : 
      98              :         // return;
      99              : 
     100            0 :         ConfigurationManagerRW  cfgMgrInst("export_admin");
     101            0 :         ConfigurationManagerRW* cfgMgr = &cfgMgrInst;
     102              : 
     103              :         //load all table info to fill nameToTableMap_
     104              :         {
     105            0 :                 std::string                             accumulatedWarnings;
     106              :                 const std::map<std::string, TableInfo>& allTableInfo =
     107            0 :                     cfgMgr->getAllTableInfo(true /* refresh */,
     108              :                                             &accumulatedWarnings,
     109              :                                             "" /* errorFilterName */,
     110              :                                             false /* getGroupKeys*/,
     111              :                                             false /* getGroupInfo */,
     112              :                                             false /* initializeActiveGroups */);
     113            0 :                 __COUTV__(allTableInfo.size());
     114            0 :         }
     115              : 
     116              :         //Note: similar to 'void ConfigurationSupervisorBase::handleCreateTableXML'
     117              : 
     118            0 :         TableVersion version;  //point to mockup
     119            0 :         TableBase*   table = cfgMgr->getTableByName(tableName);
     120              :         // create a temporary version from the source version
     121            0 :         TableVersion temporaryVersion = table->createTemporaryView(version);
     122              : 
     123            0 :         __COUT__ << "\t\ttemporaryVersion: " << temporaryVersion << __E__;
     124              : 
     125            0 :         TableView* cfgView = table->getTemporaryView(temporaryVersion);
     126              : 
     127              :         try
     128              :         {
     129              :                 // returns -1 on error that data was unchanged, calls init to check table
     130            0 :                 cfgView->fillFromCSV(csv, 0 /* dataOffset */, author);
     131              : 
     132            0 :                 std::stringstream comment;
     133            0 :                 comment << "Table CSV uploaded with 'ots_table_upload_csv.'" << __E__;
     134            0 :                 cfgView->setComment(comment.str());
     135            0 :                 __COUT__ << "Table comment was set to:\n\t" << cfgView->getComment() << __E__;
     136            0 :         }
     137            0 :         catch(...)  // erase temporary view before re-throwing error
     138              :         {
     139            0 :                 __COUT__ << "Caught error while editing. Erasing temporary version." << __E__;
     140            0 :                 table->eraseView(temporaryVersion);
     141            0 :                 throw;
     142            0 :         }
     143              : 
     144            0 :         cfgView->print();
     145              : 
     146              :         // return;
     147              :         // note: if sourceTableAsIs, accept equivalent versions
     148              :         bool         foundEquivalent;
     149              :         TableVersion newVersion = cfgMgr->saveModifiedVersion(tableName,
     150              :                                                               version,
     151              :                                                               false /* makeTemporary */,
     152              :                                                               table,
     153              :                                                               temporaryVersion,
     154              :                                                               false /* ignoreDuplicates */,
     155              :                                                               true /* lookForEquivalent */,
     156            0 :                                                               &foundEquivalent);
     157              : 
     158            0 :         if(foundEquivalent)
     159            0 :                 __COUT_WARN__ << "Found equivalent version of CSV upload as Table " << tableName
     160            0 :                               << "-v" << newVersion << "... no need to create new version from "
     161            0 :                               << uploadPath << __E__;
     162              :         else
     163            0 :                 __COUT_INFO__ << "Table " << tableName << "-v" << newVersion
     164            0 :                               << " was successfully uploaded as CSV from " << uploadPath << "!"
     165            0 :                               << __E__;
     166              : 
     167            0 : }  //end UploadTableCSV()
     168              : 
     169            0 : int main(int argc, char* argv[])
     170              : {
     171              :         //==============================================================================
     172              :         // Define environment variables
     173              :         //      Note: normally these environment variables are set by ots script
     174              : 
     175            0 :         test::util::check_and_make_envs();
     176              : 
     177              :         ////////////////////////////////////////////////////
     178              : 
     179            0 :         INIT_MF("UploadTableCSV");
     180            0 :         UploadTableCSV(argc, argv);
     181            0 :         return 0;
     182              : }
     183              : // BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1