File indexing completed on 2022-09-08 03:21:38
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 OMSServiceQuery& OMSServiceQuery::limit(int value) {
0087 std::stringstream pageLimit;
0088 if (m_filter.empty()) {
0089 pageLimit << "?";
0090 } else {
0091 pageLimit << "&";
0092 }
0093 pageLimit << "page[limit]=" << value;
0094 m_limit = pageLimit.str();
0095 return *this;
0096 }
0097
0098 bool OMSServiceQuery::execute() {
0099 bool ret = false;
0100 std::string out;
0101 m_status = cond::httpGet(url(), out);
0102 if (m_status == 200 || m_status == 201) {
0103 m_result = std::make_unique<OMSServiceResult>();
0104 m_result->parseData(out);
0105 ret = true;
0106 }
0107 return ret;
0108 }
0109
0110 unsigned long OMSServiceQuery::status() { return m_status; }
0111
0112 OMSServiceResult& OMSServiceQuery::result() { return *m_result; }
0113
0114 std::string OMSServiceQuery::url() { return m_url + m_filter + m_limit + m_varList; }
0115
0116 OMSService::OMSService() : m_baseUrl() {}
0117
0118 void OMSService::connect(const std::string& baseUrl) { m_baseUrl = baseUrl; }
0119 std::unique_ptr<OMSServiceQuery> OMSService::query(const std::string& function) const {
0120 return std::make_unique<OMSServiceQuery>(m_baseUrl, function);
0121 }
0122 }