HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dbharpo.cxx
Go to the documentation of this file.
1 //
2 // harpo database manipulation utility
3 // Write access to harpo database was required !!!
4 // Can be run on harpo servers ONLY !!!
5 //
6 
7 #include "HarpoDB.h"
8 #include "HarpoDBrunconfig.h"
9 #include "TXMLFile.h"
10 #include "TString.h"
11 #include "TSystem.h"
12 #include <iostream>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <getopt.h>
16 #include <libgen.h>
17 
18 typedef enum DBACTIONS_
20 
21 static const char* const short_options = "hvDnc:s:e:rlg::Q:";
22 
23 static const struct option long_options[] = {
24  { "help", 0, NULL, 'h' },
25  { "verbose", 0, NULL, 'v' },
26  { "debug", 0, NULL, 'D' },
27  { "dry-run", 0, NULL, 'n' },
28  { "startrun", 1, NULL, 's' },
29  { "endrun", 1, NULL, 'e' },
30  { "running", 0, NULL, 'r' },
31  { "lastrun", 0, NULL, 'l' },
32  { "config", 1, NULL, 'c' },
33  { "generate",2,NULL,'g' },
34  { "runquality",1,NULL,'Q' },
35  { NULL, 0, NULL, 0 }
36 };
37 
38 static TString *ProgramName;
39 static TString *CfgFileName;
40 static int Verbose = 0;
41 static int Debug = 0;
42 
44 void help()
45 {
46  printf("Usage: %s -c cfgfile [OPTIONS] [ACTION] ...\n",ProgramName->Data());
47  printf(" Where options :\n\n"
48  " -h, --help\t print this text and exit\n"
49  " -v, --verbose\t increase verbosity level, can be repeated several times\n"
50  " -D, --debug\t increase debug level can repeat several times\n"
51  " -n, --dry-run\t perform a trial run with no changes made\n"
52  " -c file , --config\t use file us config file (defult harpodbrc.xml)\n"
53  "actions:\n"
54  " -sNNNN , --startrun\t create new run record in DB and mark the run us running\n"
55  " -eNNNN , --endrun\t mark run us finished and update run duration\n"
56  " -r, --running\t show runs marked us running in DB\n"
57  " -l, --lastrun\t show the last run number in DB\n"
58  " -g[NNNN] , --generate\t crete configuration file using information from run NNNN\n"
59  " -QNNNN str(12) , --runquality\t set RunQuality for run NNNN to str\n"
60  "\n");
61  exit(1);
62 }
63 
64 typedef dbResults::size_type dbResSize;
65 
68 {
69  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
70  HarpoDB *db;
71 
72  db = new HarpoDB;
73  db->Open();
74 
75  if (db->ExecQuery("SELECT `run` FROM `runconfig` WHERE `RunQuality` LIKE 'Running%';",&run_list) ) {
76  if ( Debug > 0)std::cout << "Ok, size of result " << run_list.size() << std::endl;
77  } else
78  std::cout << "Bad SELET QUERY" << std::endl;
79 
80  db->Close();
81  delete db;
82  return run_list.size();
83 }
84 
85 // Read run template from file
86 void ReadConfig(HarpoDBRow &rinfo)
87 {
88  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
89  if ( gSystem->AccessPathName(CfgFileName->Data(),kReadPermission) ) {
90  std::cout << "File not exist " << *CfgFileName<< std::endl;
91  } else {
92  HarpoDBrunconfig *entry = nullptr;
93 
94  TFile * rcfile = TXMLFile::Open(CfgFileName->Data());
95  if (Debug > 0 ) rcfile->ls();
96  rcfile->GetObject("HarpoDBrunconfig",entry);
97  if (entry != 0 ) {
98  entry->Export(rinfo);
99  delete entry;
100  }
101  delete rcfile; // close file
102  }
103  if (Debug > 0 ) rinfo.list();
104 }
105 
106 
108 void StartRun(long int r)
109 {
110  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
111  dbResults run_list;
112  dbResSize nrun = RunningList(run_list);
113  if ( nrun > 0 ) {
114  std::cout << "Warning : " << nrun << " runs marked as running Harpo DB" << std::endl;
115  }
116 
117  HarpoDB *db = new HarpoDB;
118  //db->SetUser("harpo","harpo");
119  if ( ! db->Open() ) return;
120 
121  HarpoDBTable t0;
122  db->GetTable("runconfig",t0);
123  if (Verbose > 1 ) t0.list();
124 
125  // check if run in DB
126  HarpoDBRow runinfo;
127  Bool_t ok = t0.GetRunRow(r,runinfo);
128  if (ok) {
129  std::cout << "Error : Run " << r << " exist in Harpo DB" << std::endl;
130  return;
131  }
132  runinfo.SetFields(t0.GetFiledsTypes());
133  ReadConfig(runinfo);
134  runinfo.SetVal("RunQuality",TString("Running"));
135  t0.TypeExpression("date");
136  runinfo.SetVal("date",TString("NOW()"));
137  runinfo.SetVal("Duration",TString());
138  runinfo.SetVal("nevt",0L);
139  runinfo.SetRun(r);
140  if (Debug > 0 ) {
141  std::cout << "----------------------------------------" << std::endl;
142  runinfo.list();
143  }
144  TString opt = ((Debug > 0) ? "verbose" : "" );
145  t0.InsertRunRow(runinfo,opt);
146  if ( Verbose > 1 ) std::cout << "Run " << r << " Started." << std::endl;
147  return;
148 
149 }
150 
153 void RunDone(long int r)
154 {
155  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
156  HarpoDB *db;
157  //HarpoDBrunconfig * entry;
158 
159  db = new HarpoDB;
160  db->Open();
161 
162  HarpoDBTable t0;
163  db->GetTable("runconfig",t0);
164  // Narrow query to "intresting" fileds
165  dbFields fields = t0.GetFiledsTypes();
166  for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
167  if (!( it->first.Contains("run") || it->first.Contains("RunQuality")
168  || it->first.Contains("Duration") ))
169  { // Other, we not need
170  t0.Erase(it->first);
171  }
172  }
173  if (Verbose > 1 ) t0.list();
174 
175  Bool_t ok;
176  HarpoDBRow runinfo;
177  ok = t0.GetRunRow(r,runinfo);
178  if (ok) {
179  runinfo.SetVal("RunQuality",TString("Done"));
180  t0.TypeExpression("Duration");
181  runinfo.SetVal("Duration",TString("TIMEDIFF(NOW(),`date`)"));
182  if ( Verbose > 0 ) runinfo.list();
183  TString opt = ((Debug > 0) ? "verbose" : "" );
184  t0.UpdateRunRow(runinfo,opt);
185  } else {
186  std::cout << "Error : Run " << r << " not in Harpo DB" << std::endl;
187 
188  }
189 
190  db->Close();
191  delete db;
192 }
194 void EndRun(long int r)
195 {
196  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
197  dbResults run_list;
198 
199  if (RunningList(run_list) > 0) {
200  for(dbResults::iterator il=run_list.begin();il !=run_list.end();++il){
201  for(dbFields::iterator im=il->begin();im != il->end();++im){
202  if (im->first.EqualTo("run") && ( im->second.Atoll() == r)) {
203  std::cout << "Ending run " << im->second << std::endl;
204  RunDone(r);
205  return;
206  }
207  }
208  }
209  } else {
210  std::cout << "No Runs running" << std::endl;
211  }
212  std::cout << "Run " << r << " not running" << std::endl;
213 }
214 
217 {
218  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
219  dbResults run_list;
220 
221  if (RunningList(run_list) > 0) {
222  for(dbResults::iterator il=run_list.begin();il !=run_list.end();++il){
223  if (Verbose > 0) std::cout << " ======================" << std::endl;
224  for(dbFields::iterator im=il->begin();im != il->end();++im){
225  if (Verbose > 0) {
226  std::cout << im->first << " = " << im->second << std::endl;
227  } else {
228  std::cout << im->second << std::endl;
229  }
230  }
231  }
232  } else {
233  std::cout << "No Runs running" << std::endl;
234  }
235 }
236 
239 {
240  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
241  HarpoDB *db;
242 
243  db = new HarpoDB;
244  db->Open();
245 
246  dbResults run_list;
247 
248  if (db->ExecQuery("SELECT MAX(`run`) FROM `runconfig`;",&run_list) ) {
249  if ( Debug > 0)std::cout << "Ok, size of result " << run_list.size() << std::endl;
250  } else
251  std::cout << "Bad SELET QUERY" << std::endl;
252 
253  for(dbResults::iterator il=run_list.begin();il !=run_list.end();++il){
254  if (Verbose > 0) std::cout << " ======================" << std::endl;
255  for(dbFields::iterator im=il->begin();im != il->end();++im){
256  if (Verbose > 0) {
257  std::cout << im->first << " = " << im->second << std::endl;
258  } else {
259  std::cout << im->second << std::endl;
260  }
261  }
262  }
263  db->Close();
264  delete db;
265 }
266 
268 void SetConfig(const char *file)
269 {
270  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
271  delete CfgFileName;
272  CfgFileName = new TString(file);
273 }
274 
276 void GenConfig(long int r)
277 {
278  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
279  HarpoDB *db;
280  HarpoDBrunconfig * entry;
281  TFile *rcfile;
282 
283  db = new HarpoDB;
284  db->Open();
285 
286  HarpoDBTable t0;
287  db->GetTable("runconfig",t0);
288  if (Verbose > 1 ) t0.list();
289 
290  // runconfig raw in "ROOT" format
291  entry = new HarpoDBrunconfig();
292  entry->Reset(); // full wit zero values
293 
294  Bool_t ok;
295  HarpoDBRow runinfo;
296  ok = t0.GetRunRow(r,runinfo);
297  if (ok) {
298  if ( Verbose > 0 ) runinfo.list();
299  entry->Import(runinfo);
300  // clan up some field
301  entry->nevt = 0;
302  entry->Duration = ""; // time
303  entry->RunQuality = ""; // varchar(12)
304  }
305 
306  rcfile = TXMLFile::Open(CfgFileName->Data(),"RECREATE","Harpo DB rc file",0);
307 
308  entry->Write();
309  rcfile->Close();
310  delete entry;
311  delete rcfile;
312  db->Close();
313  delete db;
314 }
315 
317 void SetRunQuality(long int r,const char *qstr)
318 {
319  if (Debug > 0 ) std::cout << __FUNCTION__ << std::endl;
320  HarpoDB *db;
321  // HarpoDBrunconfig * entry;
322 
323  db = new HarpoDB;
324  db->Open();
325 
326  HarpoDBTable t0;
327  db->GetTable("runconfig",t0);
328  // Narrow query to "intresting" fileds
329  dbFields fields = t0.GetFiledsTypes();
330  for (dbFields::iterator it=fields.begin(); it!=fields.end(); ++it) {
331  if (!( it->first.Contains("run") || it->first.Contains("RunQuality")))
332  { // Other, we not need
333  t0.Erase(it->first);
334  }
335  }
336  if (Verbose > 1 ) t0.list();
337 
338  Bool_t ok;
339  HarpoDBRow runinfo;
340  ok = t0.GetRunRow(r,runinfo);
341  if (ok) {
342  runinfo.SetVal("RunQuality",TString(qstr));
343  if ( Verbose > 0 ) runinfo.list();
344  TString opt = ((Debug > 0) ? "verbose" : "" );
345  t0.UpdateRunRow(runinfo,opt);
346  } else {
347  std::cout << "Error : Run " << r << " not in Harpo DB" << std::endl;
348 
349  }
350 
351  db->Close();
352  delete db;
353 }
354 
356 int main(int argc, char **argv)
357 {
358  int c;
359  long int run;
360  DBACTIONS action = RNONE;
361  Bool_t doUpdates = true;
362 
363  ProgramName = new TString(basename(argv[0]));
364  CfgFileName = new TString("dbharporc.xml");
365  while (1) {
366  // int this_option_optind = optind ? optind : 1;
367  int option_index = 0;
368 
369  c = getopt_long(argc, argv, short_options,
370  long_options, &option_index);
371 
372  if (c == -1) break;
373 
374  switch (c) {
375  case 0:
376  printf("option %s", long_options[option_index].name);
377  if (optarg)
378  printf(" with arg %s", optarg);
379  printf("\n");
380  break;
381 
382  case 'h':
383  help();
384  break;
385 
386  case 'v':
387  Verbose++;
388  break;
389 
390  case 'D':
391  Debug++;
392  break;
393 
394  case 'n':
395  doUpdates = false;
396  break;
397 
398  case 's':
399  //printf("option s\n");
400  run = strtol(optarg,NULL,0);
401  action = RSTART;
402  break;
403 
404  case 'e':
405  if ( optarg != NULL ) {
406  run = strtol(optarg,NULL,0);
407  } else {
408  run = 0;
409  if (Debug>0) std::cout << "'e' without arg" << std::endl;
410  }
411  action = REND;
412  break;
413 
414  case 'r':
415  action = RRUN;
416  break;
417 
418  case 'l':
419  action = RLAST;
420  break;
421 
422  case 'c':
423  SetConfig(optarg);
424  break;
425 
426  case 'g':
427  if ( optarg != NULL ) {
428  run = strtol(optarg,NULL,0);
429  } else {
430  run = 0;
431  if (Debug>0) std::cout << "'g' without arg" << std::endl;
432  }
433  action = RGEN;
434  break;
435 
436  case 'Q':
437  run = strtol(optarg,NULL,0);
438  action = RQUALITY;
439  break;
440 
441  case '?':
442  printf("UNKNOWN option\n");
443  help();
444  break;
445 
446  default:
447  printf("?? getopt returned character code 0%o ??\n", c);
448  }
449  }
450 
451  // user with update rights
452  if(doUpdates)
453  HarpoDB::SetUser("harpo","harpo");
454 
455  switch (action) {
456  case RSTART:
457  StartRun(run);
458  break;
459 
460  case REND:
461  EndRun(run);
462  break;
463 
464  case RRUN:
465  GetRunning();
466  break;
467 
468  case RLAST:
469  GetLastRun();
470  break;
471 
472  case RGEN:
473  GenConfig(run);
474  break;
475 
476  case RQUALITY:
477  if ( optind < argc ) {
478  SetRunQuality(run,argv[optind]);
479  } else {
480  SetRunQuality(run,"");
481  }
482  break;
483 
484  default:
485  help();
486  break;
487  }
488  return 0;
489 }
490 
491 
492 
void help()
Print Usage and exit.
Definition: dbharpo.cxx:44
static TString * ProgramName
Definition: dbharpo.cxx:38
Bool_t UpdateRunRow(HarpoDBRow row, const TString option="")
Definition: HarpoDB.cxx:408
static int Verbose
Definition: dbharpo.cxx:40
Bool_t GetRunRow(Long_t nrun, HarpoDBRow &row)
Query Table for given run information.
Definition: HarpoDB.cxx:206
void EndRun(long int r)
Mark run us finished and update Duration.
Definition: dbharpo.cxx:194
void GetRunning()
Get list of runs marked us Running in DB.
Definition: dbharpo.cxx:216
void Export(HarpoDBRow &row)
static void SetUser(TString u, TString p)
Definition: HarpoDB.cxx:60
dbResSize RunningList(dbResults &run_list)
Get list of runs marked us Running in DB.
Definition: dbharpo.cxx:67
void SetRunQuality(long int r, const char *qstr)
Set Run Quqlity String.
Definition: dbharpo.cxx:317
Bool_t Open(const TString *DbUrl=NULL)
Definition: HarpoDB.cxx:77
Definition: dbharpo.cxx:19
std::list< dbFields > dbResults
Definition: HarpoDB.h:21
void list()
Definition: HarpoDB.cxx:286
Bool_t GetTable(const TString name, HarpoDBTable *table)
Definition: HarpoDB.h:122
Bool_t InsertRunRow(HarpoDBRow row, const TString option="")
Definition: HarpoDB.cxx:477
void ReadConfig(HarpoDBRow &rinfo)
Definition: dbharpo.cxx:86
Bool_t SetVal(const TString field, TString val)
Definition: HarpoDB.cxx:322
void StartRun(long int r)
Mark Run us running @ date and Fill other fiels from configuration files.
Definition: dbharpo.cxx:108
static const char *const short_options
Definition: dbharpo.cxx:21
Definition: dbharpo.cxx:19
void Import(HarpoDBRow &row)
Bool_t TypeExpression(const TString field)
Mark filed as expression for insert and update.
Definition: HarpoDB.cxx:371
dbResults::size_type dbResSize
Definition: dbharpo.cxx:64
void GetLastRun()
Query Max Run Number in DB table.
Definition: dbharpo.cxx:238
dbFields & GetFiledsTypes()
Definition: HarpoDB.h:94
Harpo Database access class. The class query MySQL database. Deal with (single !) Harpo Database and ...
Definition: HarpoDB.h:112
void SetRun(Long_t r)
Definition: HarpoDB.h:54
Bool_t ExecQuery(const TString sql, dbResults *list=NULL)
Definition: HarpoDB.cxx:170
void Erase(const TString &field)
Definition: HarpoDB.h:97
void RunDone(long int r)
Definition: dbharpo.cxx:153
DBACTIONS_
Definition: dbharpo.cxx:18
enum DBACTIONS_ DBACTIONS
static TString * CfgFileName
Definition: dbharpo.cxx:39
int main(int argc, char **argv)
Program start.
Definition: dbharpo.cxx:356
static const struct option long_options[]
Definition: dbharpo.cxx:23
static int Debug
Definition: dbharpo.cxx:41
void SetConfig(const char *file)
Set Configuration File Name.
Definition: dbharpo.cxx:268
Definition: dbharpo.cxx:19
void GenConfig(long int r)
Genarate Configuration File ( run template )
Definition: dbharpo.cxx:276
void list()
list all fields with values
Definition: HarpoDB.cxx:274
std::map< TString, TString > dbFields
Definition: HarpoDB.h:16
Bool_t Close()
Definition: HarpoDB.cxx:111
void SetFields(dbFields f)
Definition: HarpoDB.cxx:345