File indexing completed on 2024-04-06 12:23:06
0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004
0005 #include "OnlineDB/EcalCondDB/interface/MonDelaysTTDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
0007 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0008
0009 using namespace std;
0010 using namespace oracle::occi;
0011
0012 MonDelaysTTDat::MonDelaysTTDat() {
0013 m_env = nullptr;
0014 m_conn = nullptr;
0015 m_writeStmt = nullptr;
0016 m_readStmt = nullptr;
0017
0018 m_delayMean = 0;
0019 m_delayRMS = 0;
0020 m_taskStatus = false;
0021 }
0022
0023 MonDelaysTTDat::~MonDelaysTTDat() {}
0024
0025 void MonDelaysTTDat::prepareWrite() noexcept(false) {
0026 this->checkConnection();
0027
0028 try {
0029 m_writeStmt = m_conn->createStatement();
0030 m_writeStmt->setSQL(
0031 "INSERT INTO mon_delays_tt_dat (iov_id, logic_id, "
0032 "delay_mean, delay_rms, task_status) "
0033 "VALUES (:iov_id, :logic_id, "
0034 ":delay_mean, :delay_rms, :task_status)");
0035 } catch (SQLException& e) {
0036 throw(std::runtime_error("MonDelaysTTDat::prepareWrite(): " + e.getMessage()));
0037 }
0038 }
0039
0040 void MonDelaysTTDat::writeDB(const EcalLogicID* ecid, const MonDelaysTTDat* item, MonRunIOV* iov) noexcept(false) {
0041 this->checkConnection();
0042 this->checkPrepare();
0043
0044 int iovID = iov->fetchID();
0045 if (!iovID) {
0046 throw(std::runtime_error("MonDelaysTTDat::writeDB: IOV not in DB"));
0047 }
0048
0049 int logicID = ecid->getLogicID();
0050 if (!logicID) {
0051 throw(std::runtime_error("MonDelaysTTDat::writeDB: Bad EcalLogicID"));
0052 }
0053
0054 try {
0055 m_writeStmt->setInt(1, iovID);
0056 m_writeStmt->setInt(2, logicID);
0057
0058 m_writeStmt->setFloat(3, item->getDelayMean());
0059 m_writeStmt->setFloat(4, item->getDelayRMS());
0060 m_writeStmt->setInt(5, item->getTaskStatus());
0061
0062 m_writeStmt->executeUpdate();
0063 } catch (SQLException& e) {
0064 throw(std::runtime_error("MonDelaysTTDat::writeDB(): " + e.getMessage()));
0065 }
0066 }
0067
0068 void MonDelaysTTDat::fetchData(std::map<EcalLogicID, MonDelaysTTDat>* fillMap, MonRunIOV* iov) noexcept(false) {
0069 this->checkConnection();
0070 fillMap->clear();
0071
0072 iov->setConnection(m_env, m_conn);
0073 int iovID = iov->fetchID();
0074 if (!iovID) {
0075
0076 return;
0077 }
0078
0079 try {
0080 m_readStmt->setSQL(
0081 "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0082 "d.delay_mean, d.delay_rms, d.task_status "
0083 "FROM channelview cv JOIN mon_delays_tt_dat d "
0084 "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0085 "WHERE d.iov_id = :iov_id");
0086 m_readStmt->setInt(1, iovID);
0087 ResultSet* rset = m_readStmt->executeQuery();
0088
0089 std::pair<EcalLogicID, MonDelaysTTDat> p;
0090 MonDelaysTTDat dat;
0091 while (rset->next()) {
0092 p.first = EcalLogicID(rset->getString(1),
0093 rset->getInt(2),
0094 rset->getInt(3),
0095 rset->getInt(4),
0096 rset->getInt(5),
0097 rset->getString(6));
0098
0099 dat.setDelayMean(rset->getFloat(7));
0100 dat.setDelayRMS(rset->getFloat(8));
0101 dat.setTaskStatus(rset->getInt(9));
0102
0103 p.second = dat;
0104 fillMap->insert(p);
0105 }
0106 } catch (SQLException& e) {
0107 throw(std::runtime_error("MonDelaysTTDat::fetchData(): " + e.getMessage()));
0108 }
0109 }
0110
0111 void MonDelaysTTDat::writeArrayDB(const std::map<EcalLogicID, MonDelaysTTDat>* data, MonRunIOV* iov) noexcept(false) {
0112 this->checkConnection();
0113 this->checkPrepare();
0114
0115 int iovID = iov->fetchID();
0116 if (!iovID) {
0117 throw(std::runtime_error("MonDelaysTTDat::writeArrayDB: IOV not in DB"));
0118 }
0119
0120 int nrows = data->size();
0121 int* ids = new int[nrows];
0122 int* iovid_vec = new int[nrows];
0123 float* xx = new float[nrows];
0124 float* yy = new float[nrows];
0125 int* st = new int[nrows];
0126
0127 ub2* ids_len = new ub2[nrows];
0128 ub2* iov_len = new ub2[nrows];
0129 ub2* x_len = new ub2[nrows];
0130 ub2* y_len = new ub2[nrows];
0131 ub2* st_len = new ub2[nrows];
0132
0133 const EcalLogicID* channel;
0134 const MonDelaysTTDat* dataitem;
0135 int count = 0;
0136 typedef map<EcalLogicID, MonDelaysTTDat>::const_iterator CI;
0137 for (CI p = data->begin(); p != data->end(); ++p) {
0138 channel = &(p->first);
0139 int logicID = channel->getLogicID();
0140 if (!logicID) {
0141 throw(std::runtime_error("MonDelaysTTDat::writeArrayDB: Bad EcalLogicID"));
0142 }
0143 ids[count] = logicID;
0144 iovid_vec[count] = iovID;
0145
0146 dataitem = &(p->second);
0147
0148 float x = dataitem->getDelayMean();
0149 float y = dataitem->getDelayRMS();
0150 int statu = dataitem->getTaskStatus();
0151
0152 xx[count] = x;
0153 yy[count] = y;
0154 st[count] = statu;
0155
0156 ids_len[count] = sizeof(ids[count]);
0157 iov_len[count] = sizeof(iovid_vec[count]);
0158
0159 x_len[count] = sizeof(xx[count]);
0160 y_len[count] = sizeof(yy[count]);
0161 st_len[count] = sizeof(st[count]);
0162
0163 count++;
0164 }
0165
0166 try {
0167 m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0168 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0169 m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0170 m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0171 m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0172
0173 m_writeStmt->executeArrayUpdate(nrows);
0174
0175 delete[] ids;
0176 delete[] iovid_vec;
0177 delete[] xx;
0178 delete[] yy;
0179 delete[] st;
0180
0181 delete[] ids_len;
0182 delete[] iov_len;
0183 delete[] x_len;
0184 delete[] y_len;
0185 delete[] st_len;
0186
0187 } catch (SQLException& e) {
0188 throw(std::runtime_error("MonDelaysTTDat::writeArrayDB(): " + e.getMessage()));
0189 }
0190 }