Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-19 05:08:17

0001 #include "CondTools/RunInfo/interface/OMSAccess.h"
0002 #include "CondCore/CondDB/interface/WebUtils.h"
0003 #include "CondCore/CondDB/interface/Exception.h"
0004 
0005 namespace cond {
0006 
0007   OMSServiceResultRef::OMSServiceResultRef(const boost::property_tree::ptree* row) : m_row(row) {}
0008 
0009   bool OMSServiceResultRef::empty() { return m_row == nullptr; }
0010 
0011   std::string OMSServiceResultRef::getAttribute(const std::string& attributeName) {
0012     return m_row->get<std::string>(attributeName);
0013   }
0014 
0015   OMSServiceResultIterator::OMSServiceResultIterator(boost::property_tree::ptree::const_iterator iter) : m_iter(iter) {}
0016 
0017   OMSServiceResultRef OMSServiceResultIterator::operator*() {
0018     auto& attributeList = m_iter->second.get_child("attributes");
0019     return OMSServiceResultRef(&attributeList);
0020   }
0021 
0022   OMSServiceResultIterator& OMSServiceResultIterator::operator++() {
0023     m_iter++;
0024     return *this;
0025   }
0026 
0027   bool OMSServiceResultIterator::operator==(const OMSServiceResultIterator& rhs) { return m_iter == rhs.m_iter; }
0028   bool OMSServiceResultIterator::operator!=(const OMSServiceResultIterator& rhs) { return m_iter != rhs.m_iter; }
0029 
0030   OMSServiceResult::OMSServiceResult() {}
0031 
0032   OMSServiceResultIterator OMSServiceResult::begin() const { return OMSServiceResultIterator(m_data->begin()); }
0033 
0034   OMSServiceResultIterator OMSServiceResult::end() const { return OMSServiceResultIterator(m_data->end()); }
0035 
0036   size_t OMSServiceResult::parseData(const std::string& data) {
0037     m_data = nullptr;
0038     std::stringstream sout;
0039     sout << data;
0040     try {
0041       boost::property_tree::read_json(sout, m_root);
0042     } catch (boost::property_tree::json_parser_error const& ex) {
0043       throw cond::Exception(ex.what(), "OMSServiceResult::parseData");
0044     }
0045     if (!m_root.empty()) {
0046       m_data = &m_root.get_child("data");
0047     }
0048     return m_root.size();
0049   }
0050 
0051   size_t OMSServiceResult::size() const {
0052     size_t ret = 0;
0053     if (m_data) {
0054       ret = m_data->size();
0055     }
0056     return ret;
0057   }
0058 
0059   bool OMSServiceResult::empty() const { return size() == 0; }
0060 
0061   void OMSServiceQuery::addVar(const std::string& varName) {
0062     std::stringstream varList;
0063     if (m_varList.empty()) {
0064       varList << "&fields=";
0065     } else {
0066       varList << m_varList << ",";
0067     }
0068     varList << varName;
0069     m_varList = varList.str();
0070   }
0071 
0072   OMSServiceQuery::OMSServiceQuery(const std::string& baseUrl, const std::string& function) {
0073     m_url = baseUrl + "/" + function;
0074   }
0075 
0076   OMSServiceQuery& OMSServiceQuery::addOutputVar(const std::string& varName) {
0077     addVar(varName);
0078     return *this;
0079   }
0080   OMSServiceQuery& OMSServiceQuery::addOutputVars(const std::initializer_list<const char*>& varNames) {
0081     for (auto v : varNames)
0082       addVar(v);
0083     return *this;
0084   }
0085 
0086   bool OMSServiceQuery::execute() {
0087     bool ret = false;
0088     std::string out;
0089     m_status = cond::httpGet(url(), out);
0090     if (m_status == 200 || m_status == 201) {
0091       m_result = std::make_unique<OMSServiceResult>();
0092       m_result->parseData(out);
0093       ret = true;
0094     }
0095     return ret;
0096   }
0097 
0098   unsigned long OMSServiceQuery::status() { return m_status; }
0099 
0100   OMSServiceResult& OMSServiceQuery::result() { return *m_result; }
0101 
0102   std::string OMSServiceQuery::url() { return m_url + m_filter + m_varList; }
0103 
0104   OMSService::OMSService() : m_baseUrl() {}
0105 
0106   void OMSService::connect(const std::string& baseUrl) { m_baseUrl = baseUrl; }
0107   std::unique_ptr<OMSServiceQuery> OMSService::query(const std::string& function) const {
0108     return std::make_unique<OMSServiceQuery>(m_baseUrl, function);
0109   }
0110 }  // namespace cond