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 
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 LE = "LE";
172  static constexpr const char* const GT = "GT";
173  static constexpr const char* const GE = "GE";
174  static constexpr const char* const SNULL = "null";
175 
176  public:
177  OMSServiceQuery() = delete;
178  OMSServiceQuery(const std::string& baseUrl, const std::string& function);
179 
180  // functions to restring query output to specific variables
181  OMSServiceQuery& addOutputVar(const std::string& varName);
182  OMSServiceQuery& addOutputVars(const std::initializer_list<const char*>& varNames);
183 
184  // generic query filter
185  template <typename T>
186  inline OMSServiceQuery& filter(const char* cmp, const std::string& varName, const T& value) {
187  std::stringstream filter;
188  if (m_filter.empty()) {
189  filter << "?";
190  if (!m_limit.empty()) {
191  m_limit.front() = '&';
192  }
193  } else {
194  filter << m_filter << "&";
195  }
196  filter << "filter[" << varName << "][" << cmp << "]=" << impl::to_string(value);
197  m_filter = filter.str();
198  return *this;
199  }
200  // filters with specific comparison operators
201  template <typename T>
202  inline OMSServiceQuery& filterEQ(const std::string& varName, const T& value) {
203  return filter<T>(EQ, varName, value);
204  }
205  template <typename T>
206  inline OMSServiceQuery& filterNEQ(const std::string& varName, const T& value) {
207  return filter<T>(NEQ, varName, value);
208  }
209  template <typename T>
210  inline OMSServiceQuery& filterGT(const std::string& varName, const T& value) {
211  return filter<T>(GT, varName, value);
212  }
213  template <typename T>
214  inline OMSServiceQuery& filterGE(const std::string& varName, const T& value) {
215  return filter<T>(GE, varName, value);
216  }
217  template <typename T>
218  inline OMSServiceQuery& filterLT(const std::string& varName, const T& value) {
219  return filter<T>(LT, varName, value);
220  }
221  template <typename T>
222  inline OMSServiceQuery& filterLE(const std::string& varName, const T& value) {
223  return filter<T>(LE, varName, value);
224  }
225  // not null filter
226  inline OMSServiceQuery& filterNotNull(const std::string& varName) { return filterNEQ(varName, SNULL); }
227 
228  // limit for the page size, when unspecified OMS's default limit is 100
230 
231  // triggers the execution of the query ( calling curl functions )
232  bool execute();
233 
234  // return code from curl
235  unsigned long status();
236 
237  // result from the query. memory allocated for data is owned by the query object itself
239 
240  // the url constructed and used for the query
241  std::string url();
242 
243  private:
244  void addVar(const std::string& varName);
245 
246  private:
251  std::unique_ptr<OMSServiceResult> m_result;
252  unsigned long m_status = 0;
253  };
254 
255  // provides query access to OMS Web services
256  class OMSService {
257  public:
258  OMSService();
259 
260  void connect(const std::string& baseUrl);
261  std::unique_ptr<OMSServiceQuery> query(const std::string& function) const;
262 
263  private:
265  };
266 } // namespace cond
267 
268 #endif
OMSServiceQuery & addOutputVars(const std::initializer_list< const char *> &varNames)
Definition: OMSAccess.cc:80
OMSServiceQuery & filterNEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:206
OMSServiceQuery & filterLT(const std::string &varName, const T &value)
Definition: OMSAccess.h:218
unsigned long status()
Definition: OMSAccess.cc:110
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
std::unique_ptr< OMSServiceQuery > query(const std::string &function) const
Definition: OMSAccess.cc:119
static constexpr const char *const GT
Definition: OMSAccess.h:172
std::string m_url
Definition: OMSAccess.h:247
ret
prodAgent to be discontinued
T from_string(const std::string &attributeValue)
Definition: OMSAccess.h:28
bool empty() const
Definition: OMSAccess.cc:59
OMSServiceQuery & filterGT(const std::string &varName, const T &value)
Definition: OMSAccess.h:210
std::string m_filter
Definition: OMSAccess.h:248
OMSServiceQuery & filter(const char *cmp, const std::string &varName, const T &value)
Definition: OMSAccess.h:186
std::string to_string(const V &value)
Definition: OMSAccess.h:71
OMSServiceQuery & filterEQ(const std::string &varName, const T &value)
Definition: OMSAccess.h:202
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:252
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
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:214
OMSServiceResult & result()
Definition: OMSAccess.cc:112
boost::property_tree::ptree::const_iterator m_iter
Definition: OMSAccess.h:139
std::string url()
Definition: OMSAccess.cc:114
size_t size() const
Definition: OMSAccess.cc:51
Definition: value.py:1
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 LE
Definition: OMSAccess.h:171
std::string m_limit
Definition: OMSAccess.h:249
OMSServiceResultIterator end() const
Definition: OMSAccess.cc:34
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:251
Definition: plugin.cc:23
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
OMSServiceQuery & filterLE(const std::string &varName, const T &value)
Definition: OMSAccess.h:222
size_t parseData(const std::string &data)
Definition: OMSAccess.cc:36
OMSServiceQuery & filterNotNull(const std::string &varName)
Definition: OMSAccess.h:226
void connect(const std::string &baseUrl)
Definition: OMSAccess.cc:118
OMSServiceResultIterator begin() const
Definition: OMSAccess.cc:32
boost::property_tree::ptree m_root
Definition: OMSAccess.h:160
std::string m_varList
Definition: OMSAccess.h:250
long double T
static constexpr const char *const SNULL
Definition: OMSAccess.h:174
std::string m_baseUrl
Definition: OMSAccess.h:264
OMSServiceQuery & limit(int value)
Definition: OMSAccess.cc:86
std::string to_string(const boost::posix_time::ptime &value)
Definition: OMSAccess.h:87
static constexpr const char *const GE
Definition: OMSAccess.h:173