ConfiguredProcessList.cpp
Go to the documentation of this file.
1 #include <DatabaseUtils/DAQAppMgr/ConfiguredProcessList.h>
2 #include <DatabaseUtils/DAQAppMgr/ApplicationType.h>
3 #include <DatabaseUtils/DAQConfig/NamedConfigUtils.h>
4 #include <DatabaseUtils/DAQConfig/DBColumnUtils.h>
5 #include <NovaDatabase/Table.h>
6 #include <NovaDatabase/Util.h>
7 #include <NovaDAQUtilities/EnvVarCache.h>
8 #include <boost/shared_ptr.hpp>
9 #include <boost/algorithm/string.hpp>
10 
11 namespace NOVADB = nova::database;
12 namespace DAQCFG = dbutils::daqconfig;
13 
14 namespace dbutils {
15 namespace daqappmgr {
16 
17 /**
18  * Creates a new instance based on the specified detector name
19  * (NDOS, NearDet, FarDet).
20  */
22  int partitionNumber,
23  const bool& useDBData)
24 {
25  _hasValidData = false;
26 
27  if (useDBData) {
29 
30  fname = "NovaDatabase/tables/DAQAppMgr/BNEVBApplicationMap.xml";
31  if (! _fetchProcessData(fname, detectorName,
33 
34  fname = "NovaDatabase/tables/DAQAppMgr/ControlApplicationMap.xml";
35  if (! _fetchProcessData(fname, detectorName, "")) {return;}
36 
37  fname = "NovaDatabase/tables/DAQAppMgr/DataLoggerApplicationMap.xml";
38  if (! _fetchProcessData(fname, detectorName,
39  ApplicationType::DL_APPTYPE)) {return;}
40 
41  fname = "NovaDatabase/tables/DAQAppMgr/DCMApplicationMap.xml";
42  if (! _fetchProcessData(fname, detectorName,
44  }
45 }
46 
47 /**
48  * Fetches the list of applications with the specified type.
49  */
52  std::vector<ApplicationInstance>& resultingAppList) const
53 {
54  if (! _hasValidData) {return false;}
55 
56  for (int idx = 0; idx < (int) _fullProcessList.size(); ++idx) {
57  if (_fullProcessList[idx].getTypeName() == appType) {
58  resultingAppList.push_back(_fullProcessList[idx]);
59  }
60  }
61 
62  return true;
63 }
64 
65 /**
66  * Fetches the list of applications that do not have one of the
67  * specified types.
68  */
70 getProcessListByExcludedType(const std::vector<std::string>& appTypeList,
71  std::vector<ApplicationInstance>&
72  resultingAppList) const
73 {
74  if (! _hasValidData) {return false;}
75 
76  for (int idx = 0; idx < (int) _fullProcessList.size(); ++idx) {
77  bool found = false;
78  for (int jdx = 0; jdx < (int) appTypeList.size(); ++jdx) {
79  if (_fullProcessList[idx].getTypeName() == appTypeList[jdx]) {
80  found = true;
81  break;
82  }
83  }
84  if (! found) {
85  resultingAppList.push_back(_fullProcessList[idx]);
86  }
87  }
88 
89  return true;
90 }
91 
92 /**
93  * Fetches the specified application, by type and name.
94  */
95 boost::shared_ptr<ApplicationInstance> ConfiguredProcessList::
96 getProcess(const std::string& appType, const std::string& appName) const
97 {
98  boost::shared_ptr<ApplicationInstance> appInstancePtr;
99  if (! _hasValidData) {return appInstancePtr;}
100 
101  for (int idx = 0; idx < (int) _fullProcessList.size(); ++idx) {
102  if (_fullProcessList[idx].getTypeName() == appType &&
103  _fullProcessList[idx].getAppName() == appName) {
104  appInstancePtr.reset(new ApplicationInstance(_fullProcessList[idx]));
105  break;
106  }
107  }
108 
109  return appInstancePtr;
110 }
111 
112 /**
113  * Fetches the process data from the specified database table.
114  */
116 _fetchProcessData(const std::string& tableDefinitionFilename,
117  const std::string& detectorName,
118  const std::string& inputAppType)
119 {
120  boost::shared_ptr<NOVADB::Table>
121  dbt(new NOVADB::Table(tableDefinitionFilename.c_str()));
122 
124  novadaq::EnvVarCache::getInstance().getEnvVar("NOVADAQ_ENVIRONMENT");
125  dbt->SetDetector(detName);
126  std::string dbUser =
127  novadaq::EnvVarCache::getInstance().getEnvVar("CFGMGR_DB_USER");
128  if (dbUser.length() > 0) {
129  dbt->SetUser(dbUser);
130  }
131 
132  dbt->Clear();
133  dbt->SetValidityRange("detectorName", detectorName);
134  dbt->SetValidityRange("tstart", NOVADB::Util::GetFarPastTimeAsString(),
136  dbt->SetValidityRange("tstop", NOVADB::Util::GetCurrentTimeAsString(),
138  //dbt->SetVerbosity(1);
139  if (! dbt->LoadFromDB()) {return false;}
140 
141  std::vector<std::string> colNames = dbt->GetColNames();
142  for (int idx = 0; idx < dbt->NRow(); ++idx) {
143  NOVADB::Row* rowPtr = dbt->GetRow(idx);
144  NOVADB::Column* colPtr;
145 
146  std::string appName;
147  std::string appType = inputAppType;
148  std::string appHost;
149  bool isEnabled;
150  boost::shared_ptr<SimpleParameterSet>
151  paramSet(new SimpleParameterSet());
152 
153  for (int colIdx = 0; colIdx < (int) colNames.size(); ++colIdx) {
154  std::string colName = boost::to_lower_copy(colNames[colIdx]);
156  continue;
157  }
158 
159  std::string valueString;
160  colPtr = rowPtr->Col(colName);
161  if (! colPtr->Get(valueString)) {
162  return false;
163  }
164 
165  if (colName == "appname") {
166  appName = valueString;
167  }
168  else if (colName == "apptype") {
169  appType = valueString;
170  }
171  else if (colName == "host") {
172  appHost = valueString;
173  }
174  else if (colName == "enabled") {
175  if (valueString.find("t") != std::string::npos ||
176  valueString.find("T") != std::string::npos ||
177  valueString.find("1") != std::string::npos) {
178  isEnabled = true;
179  }
180  else {
181  isEnabled = false;
182  }
183  }
184 
185  paramSet->put(colName, valueString);
186  }
187 
188  boost::shared_ptr<ApplicationType> typePtr;
189  boost::shared_ptr<Host> hostPtr;
190  ApplicationInstance appInstance(appName, appType, appHost,
191  typePtr, hostPtr, isEnabled);
192  appInstance.setParameterSet(paramSet);
193  _addApplicationInstance(appInstance);
194  }
195 
196  return true;
197 }
198 
199 /**
200  * Stores the information about the specified application instance.
201  */
204 {
205  _hasValidData = true;
206 
207  _fullProcessList.push_back(appInstance);
208 
209  if (std::find(_uniqueAppTypes.begin(), _uniqueAppTypes.end(),
210  appInstance.getTypeName()) == _uniqueAppTypes.end()) {
211  _uniqueAppTypes.push_back(appInstance.getTypeName());
212  }
213 
214  if (std::find(_uniqueHosts.begin(), _uniqueHosts.end(),
215  appInstance.getHostName()) == _uniqueHosts.end()) {
216  _uniqueHosts.push_back(appInstance.getHostName());
217  }
218 }
219 
220 } // end of namespace daqappmgr
221 } // end of namespace dbutils
static const std::string DL_APPTYPE
std::vector< ApplicationInstance > _fullProcessList
static const std::string BNEVB_APPTYPE
bool getProcessListByExcludedType(const std::vector< std::string > &appTypeList, std::vector< ApplicationInstance > &resultingAppList) const
static std::string GetCurrentTimeAsString()
Definition: Util.cpp:34
static EnvVarCache & getInstance()
Definition: EnvVarCache.cpp:12
static const std::string DCMAPP_APPTYPE
static std::string GetFarFutureTimeAsString()
Definition: Util.cpp:65
ConfiguredProcessList(const std::string &detectorName, int partitionNumber=0, const bool &useDBData=false)
bool getProcessListByType(const std::string &appType, std::vector< ApplicationInstance > &resultingAppList) const
bool Get(T &val) const
Definition: Column.h:53
void _addApplicationInstance(const ApplicationInstance &appInstance)
bool _fetchProcessData(const std::string &tableDefinitionFilename, const std::string &detectorName, const std::string &inputAppType)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string getEnvVar(std::string name)
Definition: EnvVarCache.cpp:33
static std::string GetFarPastTimeAsString()
Definition: Util.cpp:60
static bool isInfrastructureColumn(const std::string &columnName)
boost::shared_ptr< ApplicationInstance > getProcess(const std::string &appType, const std::string &appName) const
void setParameterSet(boost::shared_ptr< dbutils::SimpleParameterSet > additionalParams)
detName
Definition: mkDefs.py:106
Column & Col(int i)
Definition: Row.h:51