BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
Coverage.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/calibUtil/src/dbIntegrity/Coverage.cxx,v 1.1.1.1 2005/10/17 06:12:26 maqm Exp $
2/**
3 @file Coverage.cxx
4
5 Implementation of Coverage class
6
7 Also define and implement tiny data-only helper class
8*/
9#include <iostream>
10#include <cstdio>
11#include "Coverage.h"
12#include "facilities/Util.h"
13#include "calibUtil/Metadata.h"
14#include "rdbModel/Management/Manager.h"
15#include "rdbModel/Management/XercesBuilder.h"
16
17#include "rdbModel/Db/MysqlConnection.h"
18#include "rdbModel/Db/MysqlResults.h"
19#include "rdbModel/Rdb.h"
20#include "rdbModel/RdbException.h"
21#include "rdbModel/Tables/Assertion.h"
22#include "rdbModel/Tables/Table.h"
23#include "rdbModel/Tables/Column.h"
24
26public:
27 SelectInfo(int ser, const facilities::Timestamp& start,
28 const facilities::Timestamp& end) : m_ser(ser)
29 {
31 m_vstart = Timestamp(start.getClibTime());
32 m_vend = Timestamp(end.getClibTime());
33 }
34 int m_ser;
37};
38
39std::ostream& operator<<(std::ostream& out, const SelectInfo& info) {
40 out << "Serial #" << info.m_ser << " validitiy interval ["
41 << info.m_vstart.getString() << ", " << info.m_vend.getString() << "]"
42 << std::endl;
43 return out;
44}
45
47 const std::string& instr, const std::string& flavor,
48 const std::string& level, const facilities::Timestamp& ts)
49 : m_instr(instr), m_flavor(flavor), m_level(level), m_overlap(300) {
51 m_selects.reserve(3);
52 m_selects.push_back("ser_no");
53 m_selects.push_back("vstart");
54 m_selects.push_back("vend");
55
56 m_orderBy.reserve(1);
57 m_orderBy.push_back("vstart asc");
58
59 m_rdb = meta->getRdb();
60 m_conn = meta->getReadConnection();
61 m_table = meta->getTable();
62}
63
64
65
66// Implement wildcards later
67bool Coverage::expandTypes(std::string& nickname,
68 std::vector<std::string>& types) {
69 if ((nickname == "CAL") ||
70 (nickname == "TKR") ||
71 (nickname == "*") ) {
72 if (!m_rdb) {
73 std::cerr << "Unable to expand wildcard argument " << nickname
74 << " without schema for metadata dbs " << std::endl;
75 }
76 else {
77 std::cerr <<
78 "Wildcard expansion of calib_type argument not yet supported";
79 std::cerr << std::endl << "Have a nice day" << std::endl;
80 }
81 std::cerr.flush();
82 exit(1);
83 }
84 types.push_back(nickname);
85 return true;
86}
87
88 unsigned Coverage::checkType(std::string calibtype) {
89 using namespace rdbModel;
91 // Make a query to find all rows matching criteria, ordered ascending
92 // by vstart
93 // match calib_type, flavor, instrument, proc_level
94 // completion = "OK"
95 // vstart >= ts
96 // Ask for return of ser_no, vstart, vend
97
98 std::vector<Assertion::Operator *> conditions;
99 conditions.reserve(6);
100
101 Assertion::Operator completeOp(OPTYPEequal, "completion", "OK",
102 FIELDTYPEold, FIELDTYPElit);
103 // false, true);
104 Assertion::Operator instOp(OPTYPEequal, "instrument", m_instr,
105 FIELDTYPEold, FIELDTYPElit);
106 // false, true);
107 Assertion::Operator calibTypeOp(OPTYPEequal, "calib_type", calibtype,
108 FIELDTYPEold, FIELDTYPElit);
109 // false, true);
110 Assertion::Operator flavorOp(OPTYPEequal, "flavor", m_flavor,
111 FIELDTYPEold, FIELDTYPElit);
112 // false, true);
113 Assertion::Operator levelOp(OPTYPEequal, "proc_level", m_level,
114 FIELDTYPEold, FIELDTYPElit);
115 // false, true);
116 Assertion::Operator vstartOp(OPTYPElessThan, m_ts.getString(),
117 "vstart",
118 FIELDTYPElit, FIELDTYPEold);
119 // true, false);
120 conditions.push_back(&calibTypeOp);
121 conditions.push_back(&instOp);
122 conditions.push_back(&flavorOp);
123 conditions.push_back(&levelOp);
124 conditions.push_back(&vstartOp);
125 conditions.push_back(&completeOp);
126 Assertion::Operator* andOp = new Assertion::Operator(OPTYPEand, conditions);
127 Assertion* whereClause = new Assertion(andOp);
128
129 ResultHandle* results = 0;
130
131 try { // make the query
132 results = m_conn->select(m_table, m_selects, m_orderBy, whereClause);
133 }
134 catch (RdbException ex) {
135 std::cerr << ex.getMsg() << std::endl;
136 std::cerr.flush(); // exit(1);
137 return false;
138 }
139
140 if (!results) { // Error. Should have ResultHandle even if 0 rows.
141 std::cerr << "Error making query " << std::endl;
142 std::cerr.flush();
143 exit(1);
144 }
145
146 // Save returned values
147 std::vector<SelectInfo> info;
148 unsigned nFound = results->getNRows();
149
150 if (!nFound) {
151 std::cout << "No calibrations found for calib_type '" << calibtype
152 << "'" << std::endl;
153 return true;
154 }
155
156 info.reserve(results->getNRows() );
157
158 unsigned iRow = 0;
159 std::vector<std::string> fields;
160 fields.reserve(3);
161
162 bool ok = true;
163 unsigned retCode = 0;
164
165 // 1. For each row store columnes; check if vstart < vend
166 std::cout << std::endl
167 << "Checking for valid timestamps, vstart < vend .. "
168 << std::endl;
169 for (iRow = 0; iRow < nFound; iRow++) {
170 results->getRow(fields, iRow);
171 int ser = facilities::Util::stringToInt(fields[0]);
172 try {
173 Timestamp vstart = Timestamp(fields[1]);
174 Timestamp vend = Timestamp(fields[2]);
175 info.push_back(SelectInfo(ser, vstart, vend));
176
177 if (vend.getClibTime() < vstart.getClibTime() ) {
178 std::cerr << "vend < vstart for " << info.back();
179 ok = false; // or could discard the row and try to continue
180 retCode = 1;
181 }
182 }
183 catch (facilities::BadTimeInput ex) {
184 std::cerr << "Bad vstart or vend in row " << ser << std::endl;
185 ok = false;
186 retCode = 1;
187 }
188
189 }
190 if (!ok) return retCode;
191
192 // 2. Check if vend's also increase monotonically
193 std::cout << std::endl << "Checking for vends monotonic.. " << std::endl;
194 for (iRow = 0; iRow < nFound - 1; iRow++) {
195 if (info[iRow].m_vend.getClibTime() > info[iRow+1].m_vend.getClibTime()) {
196 std::cerr << "Validity interval for row with serial #"
197 << info[iRow+1].m_ser
198 << " completely contained in interval for row with serial #"
199 << info[iRow].m_ser << std::endl;
200 std::cerr << info[iRow];
201 std::cerr << info[iRow+1] << std::endl;
202 ok = false;
203 retCode = 2;
204 }
205 }
206 if (!ok) return retCode;
207
208 // 3. Look for gaps
209 std::cout << std::endl << "Checking for gaps.. " << std::endl;
210 for (iRow = 0; iRow < nFound - 1; iRow++) {
211 if (info[iRow].m_vend < info[iRow+1].m_vstart) {
212 std::cerr << "Validity interval gap between calibrations with serial #"
213 << info[iRow].m_ser << " and #" << info[iRow+1].m_ser
214 << std::endl;
215 std::cerr << info[iRow];
216 std::cerr << info[iRow+1] << std::endl;
217 ok = false;
218 retCode = 3;
219 }
220 }
221 // 4. Look for overlaps
222 std::cout << std::endl << "Checking for overlaps.. " << std::endl;
223 for (iRow = 0; iRow < nFound - 1; iRow++) {
224 if ((info[iRow].m_vend).getClibTime() >
225 (info[iRow+1].m_vstart).getClibTime() + m_overlap) {
226 std::cerr << "Unacceptable overlap between serial #" << info[iRow].m_ser
227 << " and #" << info[iRow+1].m_ser << std::endl;
228 std::cerr << info[iRow];
229 std::cerr << info[iRow+1] << std::endl;
230 ok = false;
231 if (!retCode) retCode = 4;
232 }
233 }
234 return retCode;
235
236 }
std::ostream & operator<<(std::ostream &out, const SelectInfo &info)
Definition: Coverage.cxx:39
unsigned checkType(std::string calibtype)
Definition: Coverage.cxx:88
bool expandTypes(std::string &nickname, std::vector< std::string > &types)
Definition: Coverage.cxx:67
Coverage(calibUtil::Metadata *meta, const std::string &instr, const std::string &flavor, const std::string &level, const facilities::Timestamp &ts)
Definition: Coverage.cxx:46
facilities::Timestamp m_vstart
Definition: Coverage.cxx:35
SelectInfo(int ser, const facilities::Timestamp &start, const facilities::Timestamp &end)
Definition: Coverage.cxx:27
facilities::Timestamp m_vend
Definition: Coverage.cxx:36
std::string getString() const
Return string representation of time, not including nanoseconds;.
Definition: Timestamp.cxx:92
static int stringToInt(const std::string &InStr)
virtual ResultHandle * select(const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)=0
virtual bool getRow(std::vector< std::string > &fields, unsigned int i=0, bool clear=true)=0
virtual unsigned int getNRows() const =0
Return number of rows in results.