File indexing completed on 2024-04-06 12:03:20
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 OMSServiceResultRef OMSServiceResult::front() const {
0037 auto& attributeList = m_data->front().second.get_child("attributes");
0038 return OMSServiceResultRef(&attributeList);
0039 }
0040
0041 OMSServiceResultRef OMSServiceResult::back() const {
0042 auto& attributeList = m_data->back().second.get_child("attributes");
0043 return OMSServiceResultRef(&attributeList);
0044 }
0045
0046 size_t OMSServiceResult::parseData(const std::string& data) {
0047 m_data = nullptr;
0048 std::stringstream sout;
0049 sout << data;
0050 try {
0051 boost::property_tree::read_json(sout, m_root);
0052 } catch (boost::property_tree::json_parser_error const& ex) {
0053 throw cond::Exception(ex.what(), "OMSServiceResult::parseData");
0054 }
0055 if (!m_root.empty()) {
0056 m_data = &m_root.get_child("data");
0057 }
0058 return m_root.size();
0059 }
0060
0061 size_t OMSServiceResult::size() const {
0062 size_t ret = 0;
0063 if (m_data) {
0064 ret = m_data->size();
0065 }
0066 return ret;
0067 }
0068
0069 bool OMSServiceResult::empty() const { return size() == 0; }
0070
0071 void OMSServiceQuery::addVar(const std::string& varName) {
0072 std::stringstream varList;
0073 if (m_varList.empty()) {
0074 varList << "&fields=";
0075 } else {
0076 varList << m_varList << ",";
0077 }
0078 varList << varName;
0079 m_varList = varList.str();
0080 }
0081
0082 OMSServiceQuery::OMSServiceQuery(const std::string& baseUrl, const std::string& function) {
0083 m_url = baseUrl + "/" + function;
0084 }
0085
0086 OMSServiceQuery& OMSServiceQuery::addOutputVar(const std::string& varName) {
0087 addVar(varName);
0088 return *this;
0089 }
0090 OMSServiceQuery& OMSServiceQuery::addOutputVars(const std::initializer_list<const char*>& varNames) {
0091 for (auto v : varNames)
0092 addVar(v);
0093 return *this;
0094 }
0095
0096 OMSServiceQuery& OMSServiceQuery::limit(int value) {
0097 std::stringstream pageLimit;
0098 if (m_filter.empty()) {
0099 pageLimit << "?";
0100 } else {
0101 pageLimit << "&";
0102 }
0103 pageLimit << "page[limit]=" << value;
0104 m_limit = pageLimit.str();
0105 return *this;
0106 }
0107
0108 bool OMSServiceQuery::execute() {
0109 bool ret = false;
0110 std::string out;
0111 m_status = cond::httpGet(url(), out);
0112 if (m_status == 200 || m_status == 201) {
0113 m_result = std::make_unique<OMSServiceResult>();
0114 m_result->parseData(out);
0115 ret = true;
0116 }
0117 return ret;
0118 }
0119
0120 unsigned long OMSServiceQuery::status() { return m_status; }
0121
0122 OMSServiceResult& OMSServiceQuery::result() { return *m_result; }
0123
0124 std::string OMSServiceQuery::url() { return m_url + m_filter + m_limit + m_varList; }
0125
0126 OMSService::OMSService() : m_baseUrl() {}
0127
0128 void OMSService::connect(const std::string& baseUrl) { m_baseUrl = baseUrl; }
0129 std::unique_ptr<OMSServiceQuery> OMSService::query(const std::string& function) const {
0130 return std::make_unique<OMSServiceQuery>(m_baseUrl, function);
0131 }
0132 }