CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OMSAccess.h
Go to the documentation of this file.
1 #ifndef CondTools_RunInfo_OMSAccess_h
2 #define CondTools_RunInfo_OMSAccess_h
3 
4 #include <string>
5 #include <stdexcept>
6 #include <boost/property_tree/ptree.hpp>
7 #include <boost/property_tree/json_parser.hpp>
8 #include <boost/date_time/posix_time/posix_time.hpp>
9 #include <memory>
10 
11 namespace cond {
12 
13  // implementation details for data extraction from json/ data conversion to build query urls
14  namespace impl {
15 
16  static constexpr const char* const OMS_TIME_FMT = "%Y-%m-%dT%H:%M:%SZ";
17 
18  template <typename T, T fun(const std::string&)>
19  inline T from_string_impl(const std::string& attributeValue, T zero) {
20  T ret = zero;
21  if (not attributeValue.empty() && attributeValue != "null") {
22  ret = fun(attributeValue);
23  }
24  return ret;
25  }
26 
27  template <typename T>
28  inline T from_string(const std::string& attributeValue) {
29  throw std::invalid_argument("");
30  }
31 
32  template <>
33  inline std::string from_string(const std::string& attributeValue) {
34  return std::string(attributeValue);
35  }
36 
37  inline float s_to_f(const std::string& val) { return std::stof(val); }
38  template <>
39  inline float from_string(const std::string& attributeValue) {
40  return from_string_impl<float, &s_to_f>(attributeValue, 0.);
41  }
42 
43  inline int s_to_i(const std::string& val) { return std::stoi(val); }
44  template <>
45  inline int from_string(const std::string& attributeValue) {
46  return from_string_impl<int, &s_to_i>(attributeValue, 0);
47  }
48 
49  inline unsigned long s_to_ul(const std::string& val) { return std::stoul(val); }
50  template <>
51  inline unsigned short from_string(const std::string& attributeValue) {
52  unsigned long int_val = from_string_impl<unsigned long, &s_to_ul>(attributeValue, 0);
53  return (unsigned short)int_val;
54  }
55 
56  inline boost::posix_time::ptime s_to_time(const std::string& val) {
57  boost::posix_time::time_input_facet* facet = new boost::posix_time::time_input_facet(OMS_TIME_FMT);
58  std::stringstream ss;
59  ss.imbue(std::locale(std::locale(), facet));
60  ss << val;
61  boost::posix_time::ptime time;
62  ss >> time;
63  return time;
64  }
65  template <>
66  inline boost::posix_time::ptime from_string(const std::string& attributeValue) {
67  return from_string_impl<boost::posix_time::ptime, &s_to_time>(attributeValue, boost::posix_time::ptime());
68  }
69 
70  template <typename V>
71  inline std::string to_string(const V& value) {
72  return std::to_string(value);
73  }
74  template <typename V>
75  inline std::string to_string(const V* value) {
76  return std::to_string(*value);
77  }
78  template <>
80  return std::string(value);
81  }
82  template <>
83  inline std::string to_string(const char* value) {
84  return std::string(value);
85  }
86  template <>
87  inline std::string to_string(const boost::posix_time::ptime& value) {
88  boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
89  facet->format(OMS_TIME_FMT);
90  std::stringstream stream;
91  stream.imbue(std::locale(std::locale::classic(), facet));
92  stream << value;
93  return stream.str();
94  }
95 
96  } // namespace impl
97 
98  // reference of a result set row. it does not own/hold data.
100  public:
101  OMSServiceResultRef() = delete;
102  OMSServiceResultRef(const boost::property_tree::ptree* row);
103 
104  // return true if no attribute is available
105  bool empty();
106  // typed getter for single param
107  template <typename T>
108  inline T get(const std::string& attributeName) {
109  return impl::from_string<T>(getAttribute(attributeName));
110  }
111  // getter for arrays
112  template <typename primitive>
113  std::vector<primitive> getArray(const std::string& attributeName) {
114  std::vector<primitive> ret;
115  for (auto& item : m_row->get_child(attributeName)) {
116  ret.push_back(item.second.get_value<primitive>());
117  }
118  return ret;
119  }
120 
121  private:
122  std::string getAttribute(const std::string& attributeName);
123  const boost::property_tree::ptree* m_row = nullptr;
124  };
125 
126  // iterator object for result
128  public:
129  OMSServiceResultIterator() = delete;
130  OMSServiceResultIterator(boost::property_tree::ptree::const_iterator iter);
131 
134 
135  bool operator==(const OMSServiceResultIterator& rhs);
136  bool operator!=(const OMSServiceResultIterator& rhs);
137 
138  private:
139  boost::property_tree::ptree::const_iterator m_iter;
140  };
141 
142  // container wrapping the query result, based on boost property tree
144  public:
146  // basic iterators, to enable the C++11 for loop semantics
149 
150  // parse json returned from curl, filling the property tree
151  size_t parseData(const std::string& data);
152 
153  // returns the number of top level elements of the tree ( result set "rows" )
154  size_t size() const;
155 
156  // returns size()==0
157  bool empty() const;
158 
159  private:
160  boost::property_tree::ptree m_root;
161  boost::property_tree::ptree* m_data;
162  };
163 
164  // Query object
166  public:
167  // comparison operator label, used in query urls
168  static constexpr const char* const NEQ = "NEQ";
169  static constexpr const char* const EQ = "EQ";
170  static constexpr const char* const LT = "LT";
171  static constexpr const char* const GT = "GT";
172  static constexpr const char* const SNULL = "null";
173 
174  public:
175  OMSServiceQuery() = delete;
176  OMSServiceQuery(const std::string& baseUrl, const std::string& function);
177 
178  // functions to restring query output to specific variables
179  OMSServiceQuery& addOutputVar(const std::string& varName);
180  OMSServiceQuery& addOutputVars(const std::initializer_list<const char*>& varNames);
181 
182  // generic query filter
183  template <typename T>
184  inline OMSServiceQuery& filter(const char* cmp, const std::string& varName, const T& value) {
185  std::stringstream filter;
186  if (m_filter.empty()) {
187  filter << "?";
188  } else {
189  filter << m_filter << "&";
190  }
191  filter << "filter[" << varName << "][" << cmp << "]=" << impl::to_string(value);
192  m_filter = filter.str();
193  return *this;
194  }
195  // filters with specific comparison operators
196  template <typename T>
197  inline OMSServiceQuery& filterEQ(const std::string& varName, const T& value) {
198  return filter<T>(EQ, varName, value);
199  }
200  template <typename T>
201  inline OMSServiceQuery& filterNEQ(const std::string& varName, const T& value) {
202  return filter<T>(NEQ, varName, value);
203  }
204  template <typename T>
205  inline OMSServiceQuery& filterGT(const std::string& varName, const T& value) {
206  return filter<T>(GT, varName, value);
207  }
208  template <typename T>
209  inline OMSServiceQuery& filterLT(const std::string& varName, const T& value) {
210  return filter<T>(LT, varName, value);
211  }
212  // not null filter
213  inline OMSServiceQuery& filterNotNull(const std::string& varName) { return filterNEQ(varName, SNULL); }
214 
215  // triggers the execution of the query ( calling curl functions )
216  bool execute();
217 
218  // return code from curl
219  unsigned long status();
220 
221  // result from the query. memory allocated for data is owned by the query object itself
223 
224  // the url constructed and used for the query
225  std::string url();
226 
227  private:
228  void addVar(const std::string& varName);
229 
230  private:
234  std::unique_ptr<OMSServiceResult> m_result;
235  unsigned long m_status = 0;
236  };
237 
238  // provides query access to OMS Web services
239  class OMSService {
240  public:
241  OMSService();
242 
243  void connect(const std::string& baseUrl);
244  std::unique_ptr<OMSServiceQuery> query(const std::string& function) const;
245 
246  private:
248  };
249 } // namespace cond
250 
251 #endif
OMSServiceQuery & addOutputVars(const std::initializer_list< const char * > &varNames)
Definition: OMSAccess.cc:80
tuple ret
prodAgent to be discontinued
OMSServiceQuery & filterNEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:201
OMSServiceQuery & filterLT(const std::string &varName, const T &value)
Definition: OMSAccess.h:209
unsigned long status()
Definition: OMSAccess.cc:98
boost::property_tree::ptree * m_data
Definition: OMSAccess.h:161
std::string getAttribute(const std::string &attributeName)
Definition: OMSAccess.cc:11
OMSServiceQuery & addOutputVar(const std::string &varName)
Definition: OMSAccess.cc:76
static constexpr const char *const NEQ
Definition: OMSAccess.h:168
OMSServiceResultRef operator*()
Definition: OMSAccess.cc:17
static constexpr const char *const GT
Definition: OMSAccess.h:171
std::string m_url
Definition: OMSAccess.h:231
T from_string(const std::string &attributeValue)
Definition: OMSAccess.h:28
OMSServiceQuery & filterGT(const std::string &varName, const T &value)
Definition: OMSAccess.h:205
std::string m_filter
Definition: OMSAccess.h:232
OMSServiceQuery & filter(const char *cmp, const std::string &varName, const T &value)
Definition: OMSAccess.h:184
std::string to_string(const V &value)
Definition: OMSAccess.h:71
OMSServiceQuery & filterEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:197
float s_to_f(const std::string &val)
Definition: OMSAccess.h:37
std::vector< primitive > getArray(const std::string &attributeName)
Definition: OMSAccess.h:113
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
constexpr char const * varNames[]
unsigned long m_status
Definition: OMSAccess.h:235
static constexpr const char *const EQ
Definition: OMSAccess.h:169
Primitive< F, X >::type primitive(const F &f)
Definition: Primitive.h:41
boost::posix_time::ptime s_to_time(const std::string &val)
Definition: OMSAccess.h:56
OMSServiceResultIterator begin() const
Definition: OMSAccess.cc:32
int s_to_i(const std::string &val)
Definition: OMSAccess.h:43
static constexpr const char *const OMS_TIME_FMT
Definition: OMSAccess.h:16
bool operator!=(const OMSServiceResultIterator &rhs)
Definition: OMSAccess.cc:28
size_t size() const
Definition: OMSAccess.cc:51
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
OMSServiceResult & result()
Definition: OMSAccess.cc:100
boost::property_tree::ptree::const_iterator m_iter
Definition: OMSAccess.h:139
std::string url()
Definition: OMSAccess.cc:102
bool empty() const
Definition: OMSAccess.cc:59
const boost::property_tree::ptree * m_row
Definition: OMSAccess.h:123
bool operator==(const OMSServiceResultIterator &rhs)
Definition: OMSAccess.cc:27
T from_string_impl(const std::string &attributeValue, T zero)
Definition: OMSAccess.h:19
OMSServiceResultIterator & operator++()
Definition: OMSAccess.cc:22
static constexpr const char *const LT
Definition: OMSAccess.h:170
void addVar(const std::string &varName)
Definition: OMSAccess.cc:61
unsigned long s_to_ul(const std::string &val)
Definition: OMSAccess.h:49
std::unique_ptr< OMSServiceResult > m_result
Definition: OMSAccess.h:234
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
size_t parseData(const std::string &data)
Definition: OMSAccess.cc:36
OMSServiceQuery & filterNotNull(const std::string &varName)
Definition: OMSAccess.h:213
void connect(const std::string &baseUrl)
Definition: OMSAccess.cc:106
boost::property_tree::ptree m_root
Definition: OMSAccess.h:160
OMSServiceResultIterator end() const
Definition: OMSAccess.cc:34
std::string m_varList
Definition: OMSAccess.h:233
long double T
static constexpr const char *const SNULL
Definition: OMSAccess.h:172
std::string m_baseUrl
Definition: OMSAccess.h:247
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
Definition: OMSAccess.cc:107