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 : /// Downloads a table name/version as CSV file (compatible with Excel, Google Docs, etc.),
16 : /// Same format as downloaded by the web app Configuration GUI.
17 : /// If not path is given, the CSV file will be saved as <tableName>_v<tableVersion>_<linuxTime>.csv
18 : ///
19 : /// usage:
20 : /// ots_table_download_csv <table name> <table version> <optional: file path to save CSV>
21 : ///
22 : /// Note: CSV file format is compatible with ots_table_upload_csv
23 :
24 : using namespace ots;
25 :
26 0 : void DownloadTableCSV(int argc, char* argv[])
27 : {
28 0 : std::cout << "=================================================\n";
29 0 : std::cout << "=================================================\n";
30 0 : std::cout << "=================================================\n";
31 0 : __COUT_INFO__ << "Downloading Table CSV!" << std::endl;
32 :
33 : std::cout << "\n\nusage: 2 or 3 arguments:\n\t <table name> <table version> "
34 0 : "<optional: file path to save CSV> \n\n"
35 0 : << std::endl;
36 :
37 0 : std::cout << "argc = " << argc << std::endl;
38 0 : for(int i = 0; i < argc; i++)
39 0 : std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
40 :
41 0 : if(argc < 3)
42 : {
43 0 : std::cout << "Error! Must provide two parameters.\n\n" << std::endl;
44 0 : return;
45 : }
46 :
47 0 : std::string tableName = argv[1];
48 0 : __COUTV__(tableName);
49 0 : auto tablePos = tableName.find("Table");
50 0 : if(tablePos == std::string::npos || //avoid case when tableName is length 4
51 0 : tablePos != tableName.size() - strlen("Table"))
52 0 : tableName += "Table";
53 0 : __COUTV__(tableName);
54 0 : TableVersion tableVersion(atoi(argv[2]));
55 0 : __COUTV__(tableVersion);
56 0 : std::string downloadPath = "";
57 0 : if(argc > 3)
58 0 : downloadPath = argv[3];
59 : else
60 : {
61 0 : std::stringstream ss;
62 0 : ss << tableName << "_v" << tableVersion << "_" << time(0) << ".csv";
63 0 : downloadPath = ss.str();
64 0 : }
65 0 : __COUTV__(downloadPath);
66 :
67 : // return;
68 :
69 : //==============================================================================
70 : // Define environment variables
71 : // Note: normally these environment variables are set by ots script
72 :
73 0 : test::util::check_and_make_envs();
74 : ////////////////////////////////////////////////////
75 :
76 : //==============================================================================
77 : // get prepared with initial source db
78 :
79 : // ConfigurationManager instance immediately loads active groups
80 0 : __COUT_INFO__ << "Initializing..." << std::endl;
81 :
82 0 : std::string ARTDAQ_DATABASE_URI = __ENV__("ARTDAQ_DATABASE_URI");
83 0 : __COUTV__(ARTDAQ_DATABASE_URI);
84 :
85 : // return;
86 :
87 0 : ConfigurationManagerRW cfgMgrInst("export_admin");
88 0 : ConfigurationManagerRW* cfgMgr = &cfgMgrInst;
89 :
90 : //load all table info to fill nameToTableMap_
91 : {
92 0 : std::string accumulatedWarnings;
93 : const std::map<std::string, TableInfo>& allTableInfo =
94 0 : cfgMgr->getAllTableInfo(true /* refresh */,
95 : &accumulatedWarnings,
96 : "" /* errorFilterName */,
97 : false /* getGroupKeys*/,
98 : false /* getGroupInfo */,
99 : false /* initializeActiveGroups */);
100 0 : __COUTV__(allTableInfo.size());
101 0 : }
102 :
103 0 : std::stringstream csv;
104 0 : cfgMgr->getVersionedTableByName(tableName, tableVersion)->getViewP()->printCSV(csv);
105 :
106 : //Uploade should use similar to fillFromCSV
107 :
108 0 : FILE* fp = std::fopen(downloadPath.c_str(), "w");
109 0 : if(!fp)
110 : {
111 0 : __COUT_ERR__ << "\n\nERROR! Could not open file at " << downloadPath
112 0 : << ". Error: " << errno << " - " << strerror(errno) << __E__;
113 0 : return;
114 : }
115 0 : fputs(csv.str().c_str(), fp);
116 0 : fclose(fp);
117 :
118 0 : __COUT_INFO__ << "Table " << tableName << "-v" << tableVersion
119 0 : << "was successfully downloaded as CSV to " << downloadPath << __E__;
120 :
121 0 : } //end DownloadTableCSV()
122 :
123 0 : int main(int argc, char* argv[])
124 : {
125 : //==============================================================================
126 : // Define environment variables
127 : // Note: normally these environment variables are set by ots script
128 :
129 0 : test::util::check_and_make_envs();
130 :
131 : ////////////////////////////////////////////////////
132 :
133 0 : INIT_MF("DownloadTableCSV");
134 0 : DownloadTableCSV(argc, argv);
135 0 : return 0;
136 : }
137 : // BOOST_AUTO_TEST_SUITE_END()
|