otsdaq_utilities  v2_05_02_indev
ECLSupervisor.cc
1 #include "otsdaq-utilities/ECLWriter/ECLSupervisor.h"
2 #include "otsdaq/CgiDataUtilities/CgiDataUtilities.h"
3 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
6 #include "otsdaq/SOAPUtilities/SOAPCommand.h"
7 #include "otsdaq/SOAPUtilities/SOAPParameters.h"
8 #include "otsdaq/SOAPUtilities/SOAPUtilities.h"
9 #include "otsdaq/TablePlugins/XDAQContextTable.h"
10 #include "otsdaq/XmlUtilities/HttpXmlDocument.h"
11 
12 #include "otsdaq-utilities/ECLWriter/ECLConnection.h"
13 
14 #include <dirent.h> /*DIR and dirent*/
15 #include <sys/stat.h> /*mkdir*/
16 
17 #include <xdaq/NamespaceURI.h>
18 
19 #include <iomanip>
20 #include <iostream>
21 #include "otsdaq/TableCore/TableGroupKey.h"
22 
23 using namespace ots;
24 
25 #undef __MF_SUBJECT__
26 #define __MF_SUBJECT__ "ECL"
27 
28 XDAQ_INSTANTIATOR_IMPL(ECLSupervisor)
29 
30 //==============================================================================
31 ECLSupervisor::ECLSupervisor(xdaq::ApplicationStub* s)
32  : CoreSupervisorBase(s)
33  , theConfigurationManager_(
34  new ConfigurationManager) //(Singleton<ConfigurationManager>::getInstance())
36  // load a partial configuration (new
37  // ConfigurationManager)
38  , supervisorContextUID_(
39  theConfigurationManager_->__GET_CONFIG__(XDAQContextTable)
40  ->getContextUID(getApplicationContext()->getContextDescriptor()->getURL()))
41  , supervisorApplicationUID_(
42  theConfigurationManager_->__GET_CONFIG__(XDAQContextTable)
43  ->getApplicationUID(
44  getApplicationContext()->getContextDescriptor()->getURL(),
45  getApplicationDescriptor()->getLocalId()))
46  , supervisorConfigurationPath_("/" + supervisorContextUID_ +
47  "/LinkToApplicationTable/" +
48  supervisorApplicationUID_ + "/LinkToSupervisorTable")
49 {
50  INIT_MF("." /*directory used is USER_DATA/LOG/.*/);
51  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
52  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
53  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
54  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
55  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
56  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
57  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
58  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
59  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
60  __COUT__ << __PRETTY_FUNCTION__ << std::endl;
61 
62  __COUT__ << __PRETTY_FUNCTION__ << "done data manager" << std::endl;
63 
64  init();
65 }
66 
67 //==============================================================================
68 ECLSupervisor::~ECLSupervisor(void) { destroy(); }
69 //==============================================================================
70 void ECLSupervisor::init(void)
71 {
72  // called by constructor
73  // allSupervisorInfo_.init(getApplicationContext());
74 }
75 
76 //==============================================================================
77 void ECLSupervisor::destroy(void)
78 {
79  // called by destructor
80  delete theConfigurationManager_;
81 }
82 
83 //==============================================================================
84 void ECLSupervisor::defaultPage(xgi::Input* /*in*/, xgi::Output* out)
85 
86 {
87  //__COUT__ << this->getApplicationContext()->getURL() << __E__;
88 
89  *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame "
90  "src='/WebPath/html/ECL.html?urn="
91  << this->getApplicationDescriptor()->getLocalId() << "'></frameset></html>";
92 }
93 
94 //==============================================================================
95 void ECLSupervisor::transitionConfiguring(toolbox::Event::Reference /*e*/)
96 
97 {
98  // try
99  {
100  // theConfigurationTableGroupKey_ =
101  // theConfigurationManager_->makeTheTableGroupKey(atoi(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("TableGroupKey").c_str()));
102  // theConfigurationManager_->activateTableGroupKey(theConfigurationTableGroupKey_,0);
103 
104  std::pair<std::string /*group name*/, TableGroupKey> theGroup(
105  SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
106  .getParameters()
107  .getValue("ConfigurationTableGroupName"),
108  TableGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
109  .getParameters()
110  .getValue("ConfigurationTableGroupKey")));
111 
112  __COUT__ << "Configuration table group name: " << theGroup.first
113  << " key: " << theGroup.second << std::endl;
114 
115  theConfigurationManager_->loadTableGroup(theGroup.first, theGroup.second, true);
116 
117  ConfigurationTree configLinkNode =
118  theConfigurationManager_->getSupervisorTableNode(supervisorContextUID_,
119  supervisorApplicationUID_);
120 
121  ECLUser = configLinkNode.getNode("ECLUserName").getValue<std::string>();
122  ECLHost = configLinkNode.getNode("ECLInstanceURL").getValue<std::string>();
123  ECLPwd = configLinkNode.getNode("ECLPassword").getValue<std::string>();
124  ExperimentName = configLinkNode.getNode("ExperimentName").getValue<std::string>();
125  }
126  // catch(...)
127  //{
128  // {__SS__;__THROW__(ss.str()+"Error configuring the visual supervisor most likely a
129  // plugin name is wrong or your configuration table is outdated and doesn't match the
130  // new plugin definition!");}
131  //}
132 }
133 
134 //==============================================================================
135 void ECLSupervisor::transitionStarting(toolbox::Event::Reference /*e*/)
136 
137 {
138  try
139  {
140  __COUT_INFO__ << "ECLSupervisor sending Start Run log message to ECL"
141  << std::endl;
142  run = SOAPUtilities::translate(theStateMachine_.getCurrentMessage())
143  .getParameters()
144  .getValue("RunNumber");
145  run_start = std::chrono::steady_clock::now();
146  duration_ms = 0;
147  Write(WriteState::kStart);
148  }
149  catch(...)
150  {
151  __COUT_INFO__ << "ERROR! Couldn't Start the ECLSupervisor" << std::endl;
152  }
153 }
154 
155 //==============================================================================
156 void ECLSupervisor::transitionStopping(toolbox::Event::Reference /*e*/)
157 
158 {
159  try
160  {
161  __COUT_INFO__ << "ECLSupervisor sending Stop Run log message to ECL" << std::endl;
162  Write(WriteState::kStop);
163  }
164  catch(...)
165  {
166  __COUT_INFO__ << "ERROR! Couldn't Stop the ECLSupervisor" << std::endl;
167  }
168 }
169 
170 //==============================================================================
171 void ECLSupervisor::transitionPausing(toolbox::Event::Reference /*e*/)
172 
173 {
174  try
175  {
176  __COUT_INFO__ << "ECLSupervisor sending Pause Run log message to ECL"
177  << std::endl;
178  Write(WriteState::kPause);
179  duration_ms += std::chrono::duration_cast<std::chrono::milliseconds>(
180  std::chrono::steady_clock::now() - run_start)
181  .count();
182  }
183  catch(...)
184  {
185  __COUT_INFO__ << "ERROR! Couldn't Pause the ECLSupervisor" << std::endl;
186  }
187 }
188 
189 //==============================================================================
190 void ECLSupervisor::transitionResuming(toolbox::Event::Reference /*e*/)
191 
192 {
193  try
194  {
195  __COUT_INFO__ << "ECLSupervisor sending Resume Run log message to ECL"
196  << std::endl;
197  run_start = std::chrono::steady_clock::now();
198  Write(WriteState::kResume);
199  }
200  catch(...)
201  {
202  __COUT_INFO__ << "ERROR! Couldn't Resume the ECLSupervisor" << std::endl;
203  }
204 }
205 
206 void ECLSupervisor::enteringError(toolbox::Event::Reference /*e*/)
207 {
208  try
209  {
210  __COUT_INFO__ << "ECLSupervisor sending Error log message to ECL" << std::endl;
211  Write(WriteState::kError);
212  }
213  catch(...)
214  {
215  __COUT_INFO__ << "ERROR! Couldn't Error the ECLSupervisor" << std::endl;
216  }
217 }
218 
219 //==============================================================================
220 // xoap::MakeSystemLogbookEntry
221 // make a system logbook entry into active experiment's logbook from Supervisor only
222 // TODO: (how to enforce?)
223 xoap::MessageReference ECLSupervisor::MakeSystemLogbookEntry(xoap::MessageReference msg)
224 
225 {
226  SOAPParameters parameters("EntryText");
227  // SOAPParametersV parameters(1);
228  // parameters[0].setName("EntryText");
229  SOAPUtilities::receive(msg, parameters);
230  std::string EntryText = parameters.getValue("EntryText");
231 
232  __COUT__ << "Received External Supervisor System Entry " << EntryText << std::endl;
233 
234  std::string retStr = "Success";
235 
236  ECLEntry_t eclEntry;
237  eclEntry.author(ECLUser);
238  eclEntry.category("Facility/DAQ");
239  Form_t form;
240  Field_t field;
241  Form_t::field_sequence fields;
242  std::string users =
243  theRemoteWebUsers_.getActiveUserList();
244 
245  form.name("OTSDAQ System Logbook Entry");
246 
247  field = Field_t(EscapeECLString(ExperimentName), "Experiment");
248  fields.push_back(field);
249 
250  field = Field_t(EscapeECLString(run), "RunNumber");
251  fields.push_back(field);
252 
253  field = Field_t(EscapeECLString(users), "ActiveUsers");
254  fields.push_back(field);
255 
256  field = Field_t(EscapeECLString(EntryText), "Entry");
257  fields.push_back(field);
258 
259  ECLConnection eclConn(ECLUser, ECLPwd, ECLHost);
260  if(!eclConn.Post(eclEntry))
261  {
262  retStr = "Failure";
263  }
264 
265  // fill return parameters
266  SOAPParameters retParameters("Status", retStr);
267 
268  return SOAPUtilities::makeSOAPMessageReference("LogbookEntryStatusResponse",
269  retParameters);
270 }
271 
272 int ECLSupervisor::Write(WriteState state)
273 {
274  ECLEntry_t eclEntry;
275  eclEntry.author(ECLUser);
276  eclEntry.category("Facility/DAQ");
277  Form_t form;
278  Field_t field;
279  Form_t::field_sequence fields;
280  std::string users =
281  theRemoteWebUsers_.getActiveUserList();
282 
283  switch(state)
284  {
285  case WriteState::kStart:
286  form.name("OTSDAQ Start Run");
287  break;
288  case WriteState::kStop:
289  form.name("OTSDAQ Stop Run");
290  break;
291  case WriteState::kResume:
292  form.name("OTSDAQ Resume Run");
293  break;
294  case WriteState::kPause:
295  form.name("OTSDAQ Pause Run");
296  break;
297  case WriteState::kError:
298  form.name("OTSDAQ Run Error");
299  break;
300  }
301 
302  field = Field_t(EscapeECLString(ExperimentName), "Experiment");
303  fields.push_back(field);
304 
305  field = Field_t(EscapeECLString(run), "RunNumber");
306  fields.push_back(field);
307 
308  field = Field_t(EscapeECLString(users), "ActiveUsers");
309  fields.push_back(field);
310 
311  if(state != WriteState::kStart && state != WriteState::kResume)
312  {
313  int dur = std::chrono::duration_cast<std::chrono::milliseconds>(
314  std::chrono::steady_clock::now() - run_start)
315  .count() +
316  duration_ms;
317  int dur_s = dur / 1000;
318  dur = dur % 1000;
319  int dur_m = dur_s / 60;
320  dur_s = dur_s % 60;
321  int dur_h = dur_m / 60;
322  dur_m = dur_m % 60;
323 
324  std::ostringstream dur_ss;
325  dur_ss << std::setw(2) << std::setfill('0') << dur_h << ":" << std::setw(2)
326  << std::setfill('0') << dur_m << ":" << std::setw(2) << std::setfill('0')
327  << dur_s << "." << dur;
328 
329  field = Field_t(EscapeECLString(dur_ss.str()), "Duration");
330  fields.push_back(field);
331  }
332 
333  form.field(fields);
334 
335  eclEntry.form(form);
336 
337  ECLConnection eclConn(ECLUser, ECLPwd, ECLHost);
338  if(!eclConn.Post(eclEntry))
339  {
340  return -1;
341  }
342 
343  return 0;
344 }
345 
346 //************************************************************
347 
348 std::string ECLSupervisor::EscapeECLString(std::string input)
349 {
350  std::string output = input;
351  size_t pos = output.find('&');
352  while(pos != std::string::npos)
353  {
354  output = output.replace(pos, 1, "&amp;");
355  pos = output.find('&', pos + 2);
356  }
357 
358  pos = output.find('"');
359  while(pos != std::string::npos)
360  {
361  output = output.replace(pos, 1, "&quot;");
362  pos = output.find('"', pos + 1);
363  }
364 
365  pos = output.find('\'');
366  while(pos != std::string::npos)
367  {
368  output = output.replace(pos, 1, "&apos;");
369  pos = output.find('\'', pos + 1);
370  }
371 
372  pos = output.find('<');
373  while(pos != std::string::npos)
374  {
375  output = output.replace(pos, 1, "&lt;");
376  pos = output.find('<', pos + 1);
377  }
378 
379  pos = output.find('>');
380  while(pos != std::string::npos)
381  {
382  output = output.replace(pos, 1, "&gt;");
383  pos = output.find('>', pos + 1);
384  }
385 
386  return output;
387 }
Definition: ECL.hxx:547
Definition: ECL.hxx:410
Definition: ECL.hxx:474