RBadStationSQLManager.cc
Go to the documentation of this file.
2 
3 #include <boost/lambda/lambda.hpp>
4 
5 #include <utl/ErrorLogger.h>
6 #include <rdet/RDetector.h>
7 #include <rdet/RManagerRegister.h>
8 #include <det/VManager.h>
9 #include <utl/UTCDateTime.h>
10 #include <utl/TimeStamp.h>
11 #include <utl/LeapSeconds.h>
12 #include <utl/TabulatedFunctionComplexLgAmpPhase.h>
13 #include <utl/MathConstants.h>
14 
15 using namespace std;
16 
17 
18 REGISTER_R_MANAGER("RBadStationSQLManager", rdet::RBadStationSQLManager);
19 
20 
21 namespace rdet {
22 
23 void
24 RBadStationSQLManager::Init(const string& configLink)
25 {
26  VSQLManager::Init(configLink);
27  AddAvailability<unsigned long long int>("BadStation");
28 }
29 
30 
32 RBadStationSQLManager::InternalGetData(std::vector<int>& returnData, const std::string& componentProperty,
33  const std::string& componentName, const IndexMap& /*componentIndex*/)
34  const
35 {
36  if (componentProperty != "BadStation")
37  return eNotFound;
38 
39  if (fBadPeriodList.empty()) {
40  std::cout << "Buffer BadStation Database" << std::endl;
41  BufferBadPeriodTable();
42  }
43 
44  union I2LL {
45  int fI[2];
46  unsigned int long long fL;
47  };
48 
49  I2LL llReturn;
50  llReturn.fL = GetBadPeriodFromBuffer(boost::lexical_cast<int>(componentName));
51  //returnData = GetBadPeriod(componentName); // direct query to database
52  returnData.resize(2);
53  for (unsigned int i = 0; i < 2; ++i)
54  returnData[i] = llReturn.fI[i];
55  return eFound;
56 }
57 
58 
60 RBadStationSQLManager::GenericGetData(Handle& returnData,
61  const std::string& component,
62  const std::string& property,
63  const IndexMap& /*index*/)
64  const
65 {
66  if (!(returnData.Is<unsigned long long int>() && component == "BadStation"))
67  return eNotFound;
68 
69  if (fBadPeriodList.empty()) {
70  std::cout << "Buffer BadStation Database" << std::endl;
71  BufferBadPeriodTable();
72  }
73 
74  returnData.Get<unsigned long long int>() = GetBadPeriodFromBuffer(boost::lexical_cast<int>(property));
75  return eFound;
76 }
77 
78 
79 int
80 RBadStationSQLManager::GetBadPeriod(const int stationId)
81  const
82 {
83  std::cout << "\nRBadStationSQLManager::GetBadPeriod()" << std::endl;
84  const utl::TimeStamp& currentTime = det::Detector::GetInstance().GetTime();
85  const utl::UTCDateTime currentDateTime(currentTime);
86  const string mySQLTime = currentDateTime.GetInMySQLFormat();
87  ostringstream query;
88  query << "select Reason from RBadStationList where" << " StartTime<\"" << mySQLTime << "\" "
89  << " and StopTime>\"" << mySQLTime << "\" and StationName = \"" << stationId << "\";";
90  std::cout << query.str() << endl;
91 
92  const int queryResult =
93  Query(query.str(), QueryInfoMessage("Station", boost::lexical_cast<string>(stationId)));
94  cout << "Query Result = " << queryResult << endl;
95  if (queryResult == eNotFound) {
96  std::cout << "RdBadStationSQLManager: No bad period for station " << stationId << " at time "
97  << utl::UTCDateTime(currentTime) << std::endl;
98  return 0; // return 0 in case of no bad period
99  }
100 
101  int result = 0;
102  vector<string> sqlResult;
103  while (FetchRow(sqlResult, false) == eFound) {
104  cout << "while " << endl;
105  result = result | boost::lexical_cast<int>(sqlResult[0]); // combine different BadStationReasons with a logic or
106  }
107  if (!result) {
108  std::cout << "RdBadStationSQLManager: No bad period for station " << stationId << " at time "
109  << utl::UTCDateTime(currentTime) << std::endl;
110  return 0; // return 0 in case of no bad period
111  }
112  std::cout << "RdBadStationSQLManager: Found bad period for station " << stationId << " at time "
113  << utl::UTCDateTime(currentTime) << " with reason = " << result << std::endl;
114  return result;
115 }
116 
117 
118 unsigned long long int
119 RBadStationSQLManager::GetBadPeriodFromBuffer(const int stationId)
120  const
121 {
122  //std::cout << "\nRBadStationSQLManager::GetBadPeriodFromBuffer()" << std::endl;
123 
124  // get current time from detector
125  const utl::TimeStamp& currentTime = det::Detector::GetInstance().GetTime();
126  const utl::UTCDateTime currentDateTime = utl::UTCDateTime(currentTime);
127  const unsigned int currentGPSSecond = currentDateTime.GetTimeStamp().GetGPSSecond();
128 
129  const std::vector<BadPeriod>& vBadPeriod = fBadPeriodList[stationId];
130 
131  unsigned long long int reason = 0;
132  for (std::vector<BadPeriod>::const_iterator itBadPeriod = vBadPeriod.begin();
133  itBadPeriod != vBadPeriod.end(); ++itBadPeriod) {
134 
135  if ((itBadPeriod->startGPSSecond <= currentGPSSecond) &&
136  (itBadPeriod->stopGPSSecond >= currentGPSSecond)) {
137  reason = reason | itBadPeriod->reason;
138  //std::cout << "RdBadStationSQLManager: Found bad period for station " << stationId
139  // << " at time " << utl::UTCDateTime(currentTime) << " = " << currentGPSSecond
140  // << " GPSSecond with reason = " << reason << std::endl;
141  }
142  }
143 
144  return reason;
145 }
146 
147 
148 // bool RBadStationSQLManager::BufferBadPeriodsForCurrentDetector() const
149 // {
150 // std::cout << "\nRBadStationSQLManager::BufferBadPeriodTable()" << std::endl;
151 //
152 // // load full station list from radio detector
153 // const rdet::RDetector& rDetector = det::Detector::GetInstance().GetRDetector();
154 // const std::vector<int>& fullStationList = rDetector.GetFullStationList();
155 //
156 // // loop over all stations
157 // for(int i = 0; i < fullStationList.size(); ++i) {
158 // ostringstream query;
159 //
160 // string stationName = rDetector.GetStation(fullStationList[i]).GetName();
161 //
162 // // select all entries for this station in DB
163 // query << "select Reason, StartTime, StopTime, Comments, Reporter from RBadStationList where StationName = \""
164 // << stationName << "\";";
165 //
166 // std::vector<BadPeriod> vBadPeriod; // vector to store all bad periods of station i
167 //
168 // vector<string> sqlResult;
169 // while(FetchRow(sqlResult, false) == eFound) {
170 // BadPeriod sBadPeriod;
171 // sBadPeriod.reason = boost::lexical_cast<unsigned int>(sqlResult[0]);
172 // sBadPeriod.startGPSSecond = boost::lexical_cast<unsigned int>(sqlResult[1]);
173 // sBadPeriod.stopGPSSecond = boost::lexical_cast<unsigned int>(sqlResult[2]);
174 // sBadPeriod.comment = boost::lexical_cast<std::string>(sqlResult[3]);
175 // sBadPeriod.reporter = boost::lexical_cast<std::string>(sqlResult[4]);
176 //
177 // vBadPeriod.push_back(sBadPeriod);
178 // }
179 // fBadPeriodList.insert(std::pair<std::string,std::vector<BadPeriod> >(stationName,vBadPeriod));
180 // }
181 //
182 // return true;
183 // }
184 
185 
186 bool
187 RBadStationSQLManager::BufferBadPeriodTable()
188  const
189 {
190  utl::LeapSeconds leapSeconds;
191  //INFO("RBadStationSQLManager::BufferBadPeriodTable()");
192 
193  std::vector<BadPeriod> vBadPeriod; // vector to store all bad periods of station i
194 
195  ostringstream query;
196  // select all entries for all stations in DB
197  // query << "select RStationId, Reason, UNIX_TIMESTAMP(StartTime), UNIX_TIMESTAMP(StopTime), Comments, ReporterId from RBadStationList where ReviewStatus>0 order by RStationId asc";
198  query << "SELECT DISTINCT PartName, ReasonPattern, GPSStart, GPSStop FROM `BadPeriod` "
199  "LEFT JOIN PartsRel using (BadPeriod_id) "
200  "LEFT JOIN Parts on PartsRel.ComponentId = Parts.Parts_id "
201  "WHERE Status_id = 3 AND Projects_id = 3 AND SiteId = 140 "
202  "order by PartName asc, GPSStart asc";
203  // Projects_id 3 is RD
204  // SiteId 140 is AERA
205  // Status_id 3 is "confirmed"
206 
207  std::string error;
208  if (Query(query.str(), error) == eNotFound) {
209  ostringstream error_msg;
210  error_msg << "BadPeriod has no entries. " << error << std::endl;
211  ERROR(error_msg);
212  return false;
213  }
214 
215  ostringstream info;
216  vector<string> sqlResult;
217  int counter = 0;
218  int stationId = 0;
219  while (FetchRow(sqlResult, false) == eFound) {
220  ++counter;
221 
222  const int gotId = boost::lexical_cast<int>(sqlResult[0]);
223  // save bad periods for station i-1 and clear vector
224  if (stationId != gotId) {
225 
226  //cout << "station id has changed from " << stationId << " to " << gotId << endl;
227  // save bad periods of station i
228  if (!vBadPeriod.empty()) {
229  info << "\n\tBuffer bad periods for station " << stationId << ": " << vBadPeriod.size() << " bad periods in total";
230  fBadPeriodList.insert(std::pair<int, std::vector<BadPeriod> >(stationId, vBadPeriod));
231  }
232  vBadPeriod.clear(); // clear bad period vector
233  }
234 
235  stationId = gotId;
236 
237  BadPeriod sBadPeriod;
238  sBadPeriod.reason = boost::lexical_cast<unsigned long long int>(sqlResult[1]);
239  sBadPeriod.startGPSSecond = boost::lexical_cast<unsigned int>(sqlResult[2]);
240  sBadPeriod.stopGPSSecond = boost::lexical_cast<unsigned int>(sqlResult[3]);
241  //cout << counter << ": station" << stationId << ", "
242  // << sBadPeriod.startGPSSecond << " - " << sBadPeriod.stopGPSSecond << ", " << sBadPeriod.reason << endl;
243 
244  //leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned int>(sqlResult[2]), sBadPeriod.startGPSSecond);
245  //leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned int>(sqlResult[3]), sBadPeriod.stopGPSSecond);
246 
247  //sBadPeriod.comment = boost::lexical_cast<std::string>(sqlResult[4]);
248  //sBadPeriod.reporter = boost::lexical_cast<std::string>(sqlResult[5]);
249 
250  vBadPeriod.push_back(sBadPeriod);
251  }
252 
253  // save last station
254  if (!vBadPeriod.empty()) {
255  info << "\n\tSaving bad periods for last station (station " << stationId << ", " << vBadPeriod.size() << " bad periods)" << endl;
256  fBadPeriodList.insert(std::pair<int, std::vector<BadPeriod> >(stationId, vBadPeriod));
257  }
258  INFO(info);
259 
260  return true;
261 }
262 
263 }
bool Is() const
Definition: VManager.h:162
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
std::string GetInMySQLFormat() const
Definition: UTCDateTime.cc:107
void Init()
Initialise the registry.
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
#define REGISTER_R_MANAGER(_name_, _Type_)
unsigned long long int reason
Manager for RD Bad Stations.
const Data result[]
unsigned long startGPSSecond
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
boost::add_reference< typename boost::remove_const< T >::type >::type Get()
Definition: VManager.h:150
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
unsigned long stopGPSSecond

, generated on Tue Sep 26 2023.