HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
horaslowreader.pc
Go to the documentation of this file.
1 //*-- AUTHOR : Ilse Koenig
2 //*-- Created : 16/08/2004 by Ilse Koenig
3 //*-- Modified : 09/02/2011 by Ilse Koenig
4 
5 //_HADES_CLASS_DESCRIPTION
6 //////////////////////////////////////////////////////////////////////////////
7 //
8 // HOraSlowReader
9 //
10 // Base class to read slowcontrol data from Oracle
11 //
12 //////////////////////////////////////////////////////////////////////////////
13 
14 #include "horaslowreader.h"
15 #include "horaslowmanager.h"
16 #include "hdbconn.h"
17 #include "horaslowpartition.h"
18 #include "horaslowperiod.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #define SQLCA_STORAGE_CLASS extern
23 #define ORACA_STORAGE_CLASS extern
24 
25 // Oracle communication area
26 #include <oraca.h>
27 // SQL Communications Area
28 #include <sqlca.h>
29 
30 ClassImp(HOraSlowReader)
31 
32 HOraSlowReader::HOraSlowReader() {
33  // Constructor
34  pPartition=0;
35  pConn=new HDbConn();
36 }
37 
38 HOraSlowReader::~HOraSlowReader() {
39  // Destructor
40  if (pPartition) {
41  delete pPartition;
42  pPartition=0;
43  }
44  if (pConn) {
45  delete pConn;
46  pConn=0;
47  }
48 }
49 
50 Bool_t HOraSlowReader::open() {
51  // Opens an Oracle connection
52  if (pConn->isOpen()) return kTRUE;
53  return pConn->connectDb(gHOraSlowManager->getOraUser(),gHOraSlowManager->getDbName());
54 }
55 
56 void HOraSlowReader::close() {
57  // Disconnects from Oracle
58  pConn->closeDbConn();
59 }
60 
61 Bool_t HOraSlowReader::isOpen() {
62  return pConn->isOpen();
63 }
64 
65 void HOraSlowReader::print() {
66  // Prints information about the database connection
67  if (pConn->isOpen())
68  cout<<"Connected to Oracle-Database db-hades\n";
69  else cout<<"*** no connection to Oracle established ***\n";
70 }
71 
72 Bool_t HOraSlowReader::readPartition() {
73  // Reads the start time and end time of the data partion
74  if (!pConn->isOpen()||!pPartition) return kFALSE;
75  TString startTime(pPartition->getStartTime());
76  TString endTime(pPartition->getEndTime());
77  if (startTime.Length()!=0 && endTime.Length()!=0) return kTRUE;
78  EXEC SQL BEGIN DECLARE SECTION;
79  char* pname;
80  char ts[20];
81  char te[20];
82  EXEC SQL END DECLARE SECTION;
83  pname=(Char_t*)pPartition->GetName();
84  EXEC SQL WHENEVER SQLERROR GOTO errorfound;
85  EXEC SQL WHENEVER NOT FOUND GOTO notfound;
86  EXEC SQL SELECT TO_CHAR(starttime,'yyyy-mm-dd hh24:mi:ss'),
87  TO_CHAR(NVL(endtime,SYSDATE),'yyyy-mm-dd hh24:mi:ss')
88  INTO :ts, :te
89  FROM hades_slow2.archive_partitionmgnt
90  WHERE partitionname = :pname;
91  ts[19]='\0';
92  te[19]='\0';
93  if (startTime.Length()==0) startTime=ts;
94  if (endTime.Length()==0) endTime=te;
95  pPartition->setTimeRange(startTime,endTime);
96  return kTRUE;
97 notfound:
98  Error("readPartition","Partition %s not found",pPartition->GetName());
99  return kFALSE;
100 errorfound:
101  pConn->showSqlError("readPartition");
102  return kFALSE;
103 }
104 
105 Bool_t HOraSlowReader::readRunPeriods() {
106  // Reads the run periods in the partition range
107  if (!pConn->isOpen()||!pPartition) return kFALSE;
108  TString start=pPartition->getStartTime();
109  TString end=pPartition->getEndTime();
110  if (start.IsNull()||end.IsNull()) {
111  readPartition();
112  start=pPartition->getStartTime();
113  end=pPartition->getEndTime();
114  }
115  if (start.IsNull()||end.IsNull()) return kFALSE;
116  EXEC SQL BEGIN DECLARE SECTION;
117  char* pname;
118  char* pstart;
119  char* pend;
120  int nruns;
121  int minid;
122  int maxid;
123  short nruns_Ind;
124  short minid_Ind;
125  short maxid_Ind;
126  EXEC SQL END DECLARE SECTION;
127  pname=(Char_t*)pPartition->GetName();
128  pstart=(Char_t*)start.Data();
129  pend=(Char_t*)end.Data();
130  EXEC SQL WHENEVER SQLERROR GOTO errorfound;
131  EXEC SQL WHENEVER NOT FOUND GOTO notfound;
132  EXEC SQL SELECT count(1), min(id), max(id)
133  INTO :nruns:nruns_Ind, :minid:minid_Ind, :maxid:maxid_Ind
134  FROM hades_slow2.hscs_run_period
135  WHERE partition_name = :pname
136  AND starttime BETWEEN TO_DATE(:pstart,'yyyy-mm-dd hh24:mi:ss')
137  AND TO_DATE(:pend,'yyyy-mm-dd hh24:mi:ss');
138  if (nruns_Ind!=-1 && nruns>0) {
139  TObjArray* data=pPartition->setNumPeriods(nruns);
140  return readPeriods(pname,data,nruns,minid,maxid);
141  }
142 notfound:
143  Error("readRunPeriods","No run summary for partition %s or specified time range",
144  pPartition->GetName());
145  return kFALSE;
146 errorfound:
147  pConn->showSqlError("readRunPeriods");
148  return kFALSE;
149 }
150 
151 Bool_t HOraSlowReader::readPeriods(Char_t* partitionName,TObjArray* data,Int_t nData,
152  Int_t minPeriodId,Int_t maxPeriodId) {
153  // Private function to read the run periods in the partition range
154  if (!data) return kFALSE;
155  EXEC SQL BEGIN DECLARE SECTION;
156  char* pname;
157  int minid;
158  int maxid;
159  struct {
160  int id[NMAX_SCS];
161  char ts[NMAX_SCS][20];
162  char te[NMAX_SCS][20];
163  int rid[NMAX_SCS];
164  varchar rname[NMAX_SCS][81];
165  } periods;
166  struct {
167  short id_Ind[NMAX_SCS];
168  short ts_Ind[NMAX_SCS];
169  short te_Ind[NMAX_SCS];
170  short rid_Ind[NMAX_SCS];
171  short rname_Ind[NMAX_SCS];
172  } periods_Ind;
173  EXEC SQL END DECLARE SECTION;
174  pname=partitionName;
175  minid=minPeriodId;
176  maxid=maxPeriodId;
177  Int_t nTot=0;
178  Int_t nLast=0;
179  EXEC SQL WHENEVER SQLERROR GOTO errorfound;
180  EXEC SQL WHENEVER NOT FOUND continue;
181  EXEC SQL DECLARE run_cursor CURSOR FOR
182  SELECT period_id,
183  TO_CHAR(period_begin,'yyyy-mm-dd hh24:mi:ss'),
184  TO_CHAR(period_end,'yyyy-mm-dd hh24:mi:ss'),
185  run_id, filename
186  FROM hades_slow2.hscs_periods
187  WHERE partition = :pname AND period_id BETWEEN :minid AND :maxid
188  ORDER BY period_id;
189  EXEC SQL OPEN run_cursor;
190  do {
191  EXEC SQL FETCH run_cursor INTO :periods INDICATOR :periods_Ind;
192  nLast=sqlca.sqlerrd[2]-nTot;
193  for (Int_t i=0;i<nLast;i++) {
194  HOraSlowPeriod* p=new HOraSlowPeriod();
195  periods.ts[i][19]='\0';
196  periods.te[i][19]='\0';
197  p->setPeriodId(periods.id[i]);
198  p->setStartTime((Char_t*)(periods.ts[i]));
199  p->setEndTime((Char_t*)(periods.te[i]));
200  if (periods_Ind.rid_Ind[i]!=-1) p->setRunId(periods.rid[i]);
201  if (periods_Ind.rname_Ind[i]!=-1) {
202  periods.rname[i].arr[periods.rname[i].len]='\0';
203  p->setFilename((Char_t*)(periods.rname[i].arr));
204  }
205  data->AddAt(p,nTot);
206  nTot++;
207  }
208  } while (nLast==NMAX_SCS&&nTot<nData);
209  EXEC SQL CLOSE run_cursor;
210  cout<<"*** Run periods read: "<<nTot<<endl;
211  if (nTot==nData) return kTRUE;
212  else {
213  Error("readPeriods","Too few data read");
214  return kFALSE;
215  }
216 errorfound:
217  pConn->showSqlError("readPeriods");
218  return kFALSE;
219 }