ACloudSQLManager.cc
Go to the documentation of this file.
1 
9 #include <sstream>
10 
11 #include <atm/ACloudSQLManager.h>
12 #include <det/Detector.h>
13 #include <utl/ErrorLogger.h>
14 #include <utl/TimeStamp.h>
15 #include <utl/StringCompare.h>
16 #include <utl/MoonCycle.h>
17 
18 using namespace std;
19 using namespace atm;
20 using namespace det;
21 
22 
23 REGISTER_A_MANAGER("ACloudSQLManager", ACloudSQLManager);
24 
25 
26 ACloudSQLManager::ACloudSQLManager() :
27  VSQLManager()
28 {
29  const char* const tables[] = {
30  "authors", "building_camera", "cloud_pixel",
31  "db_header", "software"
32  };
33  const int n = sizeof(tables) / sizeof(tables[0]);
34  fAvailableComponents.insert(tables, tables + n);
35 }
36 
38 ACloudSQLManager::GetData(vector<vector<int> >& returnData,
39  const string& tableName,
40  const string& componentName,
41  const IndexMap& componentIndex)
42  const
43 {
44  if (tableName != "cloud_pixel") {
45  ERROR(string("Cannot handle call for ") +
46  QueryInfoMessage(tableName, componentName));
47  return eNotFound;
48  }
49 
50  ostringstream query;
51  query << "SELECT pixel_id, cloud_coverage, start_time, end_time ";
52 
53  // Check for a software version specification...
55  query << "FROM cloud_pixel, building_camera, software "
56  << "WHERE cloud_pixel.building_camera_id = building_camera.building_camera_id "
57  << "AND cloud_pixel.software_id = software.software_id "
58  << "AND software.software_version = \"" << fDatabaseSoftwareVersion
59  << "\" ";
60  // ...or don't
61  else
62  query << "FROM cloud_pixel, building_camera "
63  << "WHERE cloud_pixel.building_camera_id = building_camera.building_camera_id ";
64 
65  const utl::TimeStamp detTime = Detector::GetInstance().GetTime();
66 
67  query << "AND building_camera.building_number = "
68  << componentIndex.find("eyeId")->second << " "
69  << "AND building_camera.camera_number = "
70  << componentIndex.find("telescopeId")->second << " "
71  << "AND moon_cycle = "
72  << utl::MoonCycle(detTime).GetFullMoonCycle() << " "
73  << "AND start_time <= " << detTime.GetGPSSecond() << " "
74  << "AND (end_time > " << detTime.GetGPSSecond() << " "
75  << "OR end_time IS NULL) "
76  << "ORDER BY pixel_id";
77 
78  if (Query(query) == eNotFound)
79  return eNotFound;
80 
81  boost::tuple<int, int, int, int> pixelId_coverage_gpsStart_gpsEnd;
82  vector<int> rowData;
83  rowData.reserve(4);
84  returnData.clear();
85 
86  while (FetchRowMany(pixelId_coverage_gpsStart_gpsEnd, false) == eFound) {
87  rowData.clear();
88  rowData.push_back(pixelId_coverage_gpsStart_gpsEnd.get<0>());
89  rowData.push_back(pixelId_coverage_gpsStart_gpsEnd.get<1>());
90  rowData.push_back(pixelId_coverage_gpsStart_gpsEnd.get<2>());
91  rowData.push_back(pixelId_coverage_gpsStart_gpsEnd.get<3>());
92  returnData.push_back(rowData);
93  }
94 
95  FreeResult();
96 
97  return eFound;
98 }
99 
101 ACloudSQLManager::GetDBResFundamental(const std::string& tableName,
102  const std::string& columnName,
103  const IndexMap& componentIndex)
104  const
105 {
106  const utl::TimeStamp detTime = Detector::GetInstance().GetTime();
107 
108  ostringstream query;
109 
110  // cloud_pixel table is special, as it requires checking software version
111  if (tableName == "cloud_pixel") {
112 
113  query << "SELECT " << columnName << " "
114  << "FROM cloud_pixel, building_camera "
115  << "WHERE cloud_pixel.building_camera_id = building_camera.building_camera_id ";
116 
117  // If software version is not "default," check for a particular version.
118  if (!utl::StringEquivalent(fDatabaseSoftwareVersion, "default"))
119  query << "AND software.software_version = \""
120  << fDatabaseSoftwareVersion << "\" ";
121 
122  query << "AND start_time <= \"" << detTime.GetGPSSecond() << "\" "
123  << "AND (end_time > \"" << detTime.GetGPSSecond() << "\" "
124  << "OR end_time IS NULL) "
125  << "AND moon_cycle = \"" << utl::MoonCycle(detTime).GetFullMoonCycle() << "\" "
126  << "AND building_camera.building_number = "
127  << componentIndex.find("eyeId")->second << " "
128  << "AND building_camera.camera_number = "
129  << componentIndex.find("telescopeId")->second << " "
130  << "AND cloud_pixel.pixel_id = "
131  << componentIndex.find("pixelId")->second << " "
132  << "ORDER BY cloud_pixel.start_time, cloud_pixel.last_modified "
133  << "DESC LIMIT 1";
134 
135  } else {
136 
137  if (fAvailableComponents.find(tableName) == fAvailableComponents.end())
138  return eNotFound;
139 
140  // issue queries in form :
141  // SELECT columnName FROM tableName WHERE
142  // componentIndex[0].first = componentIndex[0].second AND
143  // componentIndex[1].first = componentIndex[1].second
144  // (etc)
145  //
146  query << "SELECT " << columnName << " "
147  "FROM " << tableName << " "
148  "WHERE " << MergeIndexMap(componentIndex);
149  }
150 
151  return Query(query);
152 }
153 
154 
155 // Configure (x)emacs for this file ...
156 // Local Variables:
157 // mode: c++
158 // End:
Status FetchRowMany(boost::tuples::cons< TH, TT > &tuple, const bool freeResult=true) const
Get a tuple of values from the first row of an SQLite query result.
virtual Status GetDBResFundamental(const std::string &tableName, const std::string &columnName, const IndexMap &componentIndex) const
overload this in all derived managers that need it
std::string fDatabaseSoftwareVersion
bool StringEquivalent(const std::string &a, const std::string &b, Predicate p)
Utility to compare strings for equivalence. It takes a predicate to determine the equivalence of indi...
Definition: StringCompare.h:38
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
std::set< std::string > fAvailableComponents
Definition: VManager.h:362
Interface for detector managers that use MySQL.
virtual Status GetData(double &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const =0
#define REGISTER_A_MANAGER(_name_, _Type_)
static std::string QueryInfoMessage(const Handle &returnData, const std::string &component)
Definition: VManager.cc:108
long GetFullMoonCycle(const LunationEpoch epoch=eJan2004) const
Integer number of full moons since a start date (default = Jan 2004).
Definition: MoonCycle.h:40
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
Manager for cloudy portion of atmospheric monitoring database.
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
Status Query(const std::ostringstream &os, const std::string &what="") const
query MySQL
static std::string MergeIndexMap(const IndexMap &componentIndex)
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.