CMS 3D CMS Logo

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  inline unsigned long long s_to_ull(const std::string& val) { return std::stoull(val); }
56  template <>
57  inline unsigned long long from_string(const std::string& attributeValue) {
58  unsigned long long int_val = from_string_impl<unsigned long long, &s_to_ull>(attributeValue, 0);
59  return int_val;
60  }
61 
62  inline boost::posix_time::ptime s_to_time(const std::string& val) {
63  boost::posix_time::time_input_facet* facet = new boost::posix_time::time_input_facet(OMS_TIME_FMT);
64  std::stringstream ss;
65  ss.imbue(std::locale(std::locale(), facet));
66  ss << val;
67  boost::posix_time::ptime time;
68  ss >> time;
69  return time;
70  }
71  template <>
72  inline boost::posix_time::ptime from_string(const std::string& attributeValue) {
73  return from_string_impl<boost::posix_time::ptime, &s_to_time>(attributeValue, boost::posix_time::ptime());
74  }
75 
76  template <typename V>
77  inline std::string to_string(const V& value) {
78  return std::to_string(value);
79  }
80  template <typename V>
81  inline std::string to_string(const V* value) {
82  return std::to_string(*value);
83  }
84  template <>
86  return std::string(value);
87  }
88  template <>
89  inline std::string to_string(const char* value) {
90  return std::string(value);
91  }
92  template <>
93  inline std::string to_string(const boost::posix_time::ptime& value) {
94  boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
95  facet->format(OMS_TIME_FMT);
96  std::stringstream stream;
97  stream.imbue(std::locale(std::locale::classic(), facet));
98  stream << value;
99  return stream.str();
100  }
101 
102  } // namespace impl
103 
104  // reference of a result set row. it does not own/hold data.
106  public:
107  OMSServiceResultRef() = delete;
108  OMSServiceResultRef(const boost::property_tree::ptree* row);
109 
110  // return true if no attribute is available
111  bool empty();
112  // typed getter for single param
113  template <typename T>
114  inline T get(const std::string& attributeName) {
115  return impl::from_string<T>(getAttribute(attributeName));
116  }
117  // getter for arrays
118  template <typename primitive>
119  std::vector<primitive> getArray(const std::string& attributeName) {
120  std::vector<primitive> ret;
121  for (auto& item : m_row->get_child(attributeName)) {
122  ret.push_back(item.second.get_value<primitive>());
123  }
124  return ret;
125  }
126 
127  private:
128  std::string getAttribute(const std::string& attributeName);
129  const boost::property_tree::ptree* m_row = nullptr;
130  };
131 
132  // iterator object for result
134  public:
135  OMSServiceResultIterator() = delete;
136  OMSServiceResultIterator(boost::property_tree::ptree::const_iterator iter);
137 
140 
141  bool operator==(const OMSServiceResultIterator& rhs);
142  bool operator!=(const OMSServiceResultIterator& rhs);
143 
144  private:
145  boost::property_tree::ptree::const_iterator m_iter;
146  };
147 
148  // container wrapping the query result, based on boost property tree
150  public:
152  // basic iterators, to enable the C++11 for loop semantics
155 
156  OMSServiceResultRef front() const;
157  OMSServiceResultRef back() const;
158 
159  // parse json returned from curl, filling the property tree
160  size_t parseData(const std::string& data);
161 
162  // returns the number of top level elements of the tree ( result set "rows" )
163  size_t size() const;
164 
165  // returns size()==0
166  bool empty() const;
167 
168  private:
169  boost::property_tree::ptree m_root;
170  boost::property_tree::ptree* m_data;
171  };
172 
173  // Query object
175  public:
176  // comparison operator label, used in query urls
177  static constexpr const char* const NEQ = "NEQ";
178  static constexpr const char* const EQ = "EQ";
179  static constexpr const char* const LT = "LT";
180  static constexpr const char* const LE = "LE";
181  static constexpr const char* const GT = "GT";
182  static constexpr const char* const GE = "GE";
183  static constexpr const char* const SNULL = "null";
184 
185  public:
186  OMSServiceQuery() = delete;
187  OMSServiceQuery(const std::string& baseUrl, const std::string& function);
188 
189  // functions to restring query output to specific variables
190  OMSServiceQuery& addOutputVar(const std::string& varName);
191  OMSServiceQuery& addOutputVars(const std::initializer_list<const char*>& varNames);
192 
193  // generic query filter
194  template <typename T>
195  inline OMSServiceQuery& filter(const char* cmp, const std::string& varName, const T& value) {
196  std::stringstream filter;
197  if (m_filter.empty()) {
198  filter << "?";
199  if (!m_limit.empty()) {
200  m_limit.front() = '&';
201  }
202  } else {
203  filter << m_filter << "&";
204  }
205  filter << "filter[" << varName << "][" << cmp << "]=" << impl::to_string(value);
206  m_filter = filter.str();
207  return *this;
208  }
209  // filters with specific comparison operators
210  template <typename T>
211  inline OMSServiceQuery& filterEQ(const std::string& varName, const T& value) {
212  return filter<T>(EQ, varName, value);
213  }
214  template <typename T>
215  inline OMSServiceQuery& filterNEQ(const std::string& varName, const T& value) {
216  return filter<T>(NEQ, varName, value);
217  }
218  template <typename T>
219  inline OMSServiceQuery& filterGT(const std::string& varName, const T& value) {
220  return filter<T>(GT, varName, value);
221  }
222  template <typename T>
223  inline OMSServiceQuery& filterGE(const std::string& varName, const T& value) {
224  return filter<T>(GE, varName, value);
225  }
226  template <typename T>
227  inline OMSServiceQuery& filterLT(const std::string& varName, const T& value) {
228  return filter<T>(LT, varName, value);
229  }
230  template <typename T>
231  inline OMSServiceQuery& filterLE(const std::string& varName, const T& value) {
232  return filter<T>(LE, varName, value);
233  }
234  // not null filter
235  inline OMSServiceQuery& filterNotNull(const std::string& varName) { return filterNEQ(varName, SNULL); }
236 
237  // limit for the page size, when unspecified OMS's default limit is 100
239 
240  // triggers the execution of the query ( calling curl functions )
241  bool execute();
242 
243  // return code from curl
244  unsigned long status();
245 
246  // result from the query. memory allocated for data is owned by the query object itself
248 
249  // the url constructed and used for the query
250  std::string url();
251 
252  private:
253  void addVar(const std::string& varName);
254 
255  private:
260  std::unique_ptr<OMSServiceResult> m_result;
261  unsigned long m_status = 0;
262  };
263 
264  // provides query access to OMS Web services
265  class OMSService {
266  public:
267  OMSService();
268 
269  void connect(const std::string& baseUrl);
270  std::unique_ptr<OMSServiceQuery> query(const std::string& function) const;
271 
272  private:
274  };
275 } // namespace cond
276 
277 #endif
OMSServiceQuery & addOutputVars(const std::initializer_list< const char *> &varNames)
Definition: OMSAccess.cc:90
OMSServiceQuery & filterNEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:215
OMSServiceQuery & filterLT(const std::string &varName, const T &value)
Definition: OMSAccess.h:227
unsigned long status()
Definition: OMSAccess.cc:120
boost::property_tree::ptree * m_data
Definition: OMSAccess.h:170
std::string getAttribute(const std::string &attributeName)
Definition: OMSAccess.cc:11
OMSServiceQuery & addOutputVar(const std::string &varName)
Definition: OMSAccess.cc:86
static constexpr const char *const NEQ
Definition: OMSAccess.h:177
OMSServiceResultRef operator*()
Definition: OMSAccess.cc:17
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
Definition: OMSAccess.cc:129
static constexpr const char *const GT
Definition: OMSAccess.h:181
std::string m_url
Definition: OMSAccess.h:256
ret
prodAgent to be discontinued
T from_string(const std::string &attributeValue)
Definition: OMSAccess.h:28
bool empty() const
Definition: OMSAccess.cc:69
OMSServiceQuery & filterGT(const std::string &varName, const T &value)
Definition: OMSAccess.h:219
std::string m_filter
Definition: OMSAccess.h:257
OMSServiceQuery & filter(const char *cmp, const std::string &varName, const T &value)
Definition: OMSAccess.h:195
std::string to_string(const V &value)
Definition: OMSAccess.h:77
OMSServiceQuery & filterEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:211
float s_to_f(const std::string &val)
Definition: OMSAccess.h:37
OMSServiceResultRef front() const
Definition: OMSAccess.cc:36
std::vector< primitive > getArray(const std::string &attributeName)
Definition: OMSAccess.h:119
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
constexpr char const * varNames[]
OMSServiceResultRef back() const
Definition: OMSAccess.cc:41
unsigned long m_status
Definition: OMSAccess.h:261
static constexpr const char *const EQ
Definition: OMSAccess.h:178
static std::string to_string(const XMLCh *ch)
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:62
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
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
OMSServiceQuery & filterGE(const std::string &varName, const T &value)
Definition: OMSAccess.h:223
unsigned long long s_to_ull(const std::string &val)
Definition: OMSAccess.h:55
OMSServiceResult & result()
Definition: OMSAccess.cc:122
boost::property_tree::ptree::const_iterator m_iter
Definition: OMSAccess.h:145
std::string url()
Definition: OMSAccess.cc:124
size_t size() const
Definition: OMSAccess.cc:61
Definition: value.py:1
const boost::property_tree::ptree * m_row
Definition: OMSAccess.h:129
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 LE
Definition: OMSAccess.h:180
std::string m_limit
Definition: OMSAccess.h:258
OMSServiceResultIterator end() const
Definition: OMSAccess.cc:34
static constexpr const char *const LT
Definition: OMSAccess.h:179
void addVar(const std::string &varName)
Definition: OMSAccess.cc:71
unsigned long s_to_ul(const std::string &val)
Definition: OMSAccess.h:49
std::unique_ptr< OMSServiceResult > m_result
Definition: OMSAccess.h:260
Definition: plugin.cc:23
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
OMSServiceQuery & filterLE(const std::string &varName, const T &value)
Definition: OMSAccess.h:231
size_t parseData(const std::string &data)
Definition: OMSAccess.cc:46
OMSServiceQuery & filterNotNull(const std::string &varName)
Definition: OMSAccess.h:235
void connect(const std::string &baseUrl)
Definition: OMSAccess.cc:128
OMSServiceResultIterator begin() const
Definition: OMSAccess.cc:32
boost::property_tree::ptree m_root
Definition: OMSAccess.h:169
std::string m_varList
Definition: OMSAccess.h:259
long double T
static constexpr const char *const SNULL
Definition: OMSAccess.h:183
std::string m_baseUrl
Definition: OMSAccess.h:273
OMSServiceQuery & limit(int value)
Definition: OMSAccess.cc:96
static constexpr const char *const GE
Definition: OMSAccess.h:182