Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:11

0001 #include "SphenixClient.h"
0002 
0003 #include <nopayloadclient/exception.hpp>  // for DataBaseException
0004 #include <nopayloadclient/nopayloadclient.hpp>
0005 
0006 #include <nlohmann/json.hpp>
0007 
0008 #include <iostream>
0009 #include <stdexcept>
0010 
0011 SphenixClient::SphenixClient(const std::string& gt_name)
0012   : nopayloadclient::NoPayloadClient(gt_name)
0013   , m_CachedGlobalTag(gt_name)
0014 {
0015 }
0016 
0017 nlohmann::json SphenixClient::getPayloadIOVs(long long iov)
0018 {
0019   return nopayloadclient::NoPayloadClient::getPayloadIOVs(0, iov);
0020 }
0021 
0022 nlohmann::json SphenixClient::getUrl(const std::string& pl_type, long long iov)
0023 {
0024   nlohmann::json resp = getPayloadIOVs(iov);
0025   if (resp["code"] != 0)
0026   {
0027     return resp;
0028   }
0029   nlohmann::json payload_iovs = resp["msg"];
0030   if (!payload_iovs.contains(pl_type))
0031   {
0032     return nopayloadclient::DataBaseException("No valid payload with type " + pl_type).jsonify();
0033   }
0034   nlohmann::json payload_iov = payload_iovs[pl_type];
0035   if (m_Verbosity > 0)
0036   {
0037     std::cout << "pl_type: " << pl_type
0038               << ", iov: " << iov
0039               << ", minor_iov_start: " << payload_iov["minor_iov_start"]
0040               << ", minor_iov_end: " << payload_iov["minor_iov_end"]
0041               << std::endl;
0042   }
0043   if (payload_iov["minor_iov_end"] <= iov)
0044   {
0045     return nopayloadclient::DataBaseException("No valid payload with type " + pl_type).jsonify();
0046   }
0047   std::string payloadurl = payload_iov["payload_url"];
0048   //  std::cout << "payload url: " << payloadurl << std::endl;
0049   // the makeResp(T msg)  creates always problems when just doing
0050   // makeResp(payload_iov["payload_url"] ) we get unresolved externals in non optimized code
0051   return {{"code", 0}, {"msg", payloadurl}};
0052 }
0053 
0054 nlohmann::json SphenixClient::getUrlDict(long long iov)
0055 {
0056   nlohmann::json resp = getPayloadIOVs(iov);
0057   if (resp["code"] != 0)
0058   {
0059     return resp;
0060   }
0061   for (auto it = resp["msg"].begin(); it != resp["msg"].end();)
0062   {
0063     if (it.value()["minor_iov_end"] < iov)
0064     {
0065       it = resp["msg"].erase(it);
0066     }
0067     else
0068     {
0069       ++it;
0070     }
0071   }
0072   for (auto& piov : resp["msg"].items())
0073   {
0074     piov.value() = piov.value()["payload_url"];
0075   }
0076   return resp;
0077 }
0078 
0079 nlohmann::json SphenixClient::deletePayloadIOV(const std::string& pl_type, long long iov_start)
0080 {
0081   return nopayloadclient::NoPayloadClient::deletePayloadIOV(pl_type, 0, iov_start);
0082 }
0083 
0084 nlohmann::json SphenixClient::deletePayloadIOV(const std::string& pl_type, long long iov_start, long long iov_end)
0085 {
0086   return nopayloadclient::NoPayloadClient::deletePayloadIOV(pl_type, 0, iov_start, 0, iov_end);
0087 }
0088 
0089 std::string SphenixClient::getCalibration(const std::string& pl_type, long long iov)
0090 {
0091   nlohmann::json resp = getUrl(pl_type, iov);
0092   if (resp["code"] != 0)
0093   {
0094     if (m_Verbosity > 0)
0095     {
0096       std::cout << resp << std::endl;
0097     }
0098     return "";
0099   }
0100   return resp["msg"];
0101 }
0102 
0103 nlohmann::json SphenixClient::unlockGlobalTag(const std::string& gt_name)
0104 {
0105   if (existGlobalTag(gt_name))
0106   {
0107     return nopayloadclient::NoPayloadClient::unlockGlobalTag(gt_name);
0108   }
0109   std::string message = "global tag " + gt_name + " does not exist";
0110   return {{"code", -1}, {"msg", message}};
0111 }
0112 
0113 nlohmann::json SphenixClient::lockGlobalTag(const std::string& gt_name)
0114 {
0115   if (existGlobalTag(gt_name))
0116   {
0117     return nopayloadclient::NoPayloadClient::lockGlobalTag(gt_name);
0118   }
0119   std::string message = "global tag " + gt_name + " does not exist";
0120   return {{"code", -1}, {"msg", message}};
0121 }
0122 
0123 nlohmann::json SphenixClient::insertPayload(const std::string& pl_type, const std::string& file_url,
0124                                             long long iov_start)
0125 {
0126   return nopayloadclient::NoPayloadClient::insertPayload(pl_type, file_url, 0, iov_start);
0127 }
0128 
0129 nlohmann::json SphenixClient::insertPayload(const std::string& pl_type, const std::string& file_url,
0130                                             long long iov_start, long long iov_end)
0131 {
0132   return nopayloadclient::NoPayloadClient::insertPayload(pl_type, file_url, 0, iov_start, 0, iov_end);
0133 }
0134 
0135 nlohmann::json SphenixClient::setGlobalTag(const std::string& gt_name)
0136 {
0137   if (m_CachedGlobalTag == gt_name)
0138   {
0139     std::string message = "global tag already set to " + gt_name;
0140     return {{"code", 0}, {"msg", message}};
0141   }
0142   // check if the global tag exists before switching
0143   if (existGlobalTag(gt_name))
0144   {
0145     m_CachedGlobalTag = gt_name;
0146     return nopayloadclient::NoPayloadClient::setGlobalTag(gt_name);
0147   }
0148 
0149   std::string message = "global tag " + gt_name + " does not exist";
0150   return {{"code", -1}, {"msg", message}};
0151 }
0152 
0153 int SphenixClient::cache_set_GlobalTag(const std::string& tagname)
0154 {
0155   int iret = 0;
0156   if (tagname == m_CachedGlobalTag)  // global tag already set
0157   {
0158     return iret;
0159   }
0160   m_CachedGlobalTag = tagname;
0161   nopayloadclient::NoPayloadClient::setGlobalTag(tagname);
0162   bool found_gt = false;
0163   nlohmann::json resp = nopayloadclient::NoPayloadClient::getGlobalTags();
0164   nlohmann::json msgcont = resp["msg"];
0165   for (auto& it : msgcont.items())
0166   {
0167     std::string exist_gt = it.value().at("name");
0168     std::cout << "global tag: " << exist_gt << std::endl;
0169     if (exist_gt == tagname)
0170     {
0171       found_gt = true;
0172       break;
0173     }
0174   }
0175   if (!found_gt)
0176   {
0177     resp = nopayloadclient::NoPayloadClient::createGlobalTag();
0178     iret = resp["code"];
0179     if (iret != 0)
0180     {
0181       std::cout << "Error creating global tag, msg: " << resp["msg"] << std::endl;
0182     }
0183     else
0184     {
0185       std::cout << "SphenixClient: Created new global tag " << tagname << std::endl;
0186     }
0187   }
0188   return iret;
0189 }
0190 
0191 int SphenixClient::createDomain(const std::string& domain)
0192 {
0193   int iret = -1;
0194   nlohmann::json resp;
0195   if (m_DomainCache.empty())
0196   {
0197     resp = nopayloadclient::NoPayloadClient::getPayloadTypes();
0198     nlohmann::json msgcont = resp["msg"];
0199     for (auto& it : msgcont.items())
0200     {
0201       std::string existent_domain = it.value().at("name");
0202       m_DomainCache.insert(existent_domain);
0203     }
0204   }
0205   if (m_DomainCache.find(domain) == m_DomainCache.end())
0206   {
0207     resp = nopayloadclient::NoPayloadClient::createPayloadType(domain);
0208     iret = resp["code"];
0209     if (iret == 0)
0210     {
0211       m_DomainCache.insert(domain);
0212     }
0213   }
0214   else
0215   {
0216     iret = 0;
0217   }
0218   return iret;
0219 }
0220 
0221 bool SphenixClient::isGlobalTagSet()
0222 {
0223   if (m_CachedGlobalTag.empty())
0224   {
0225     return false;
0226   }
0227   return true;
0228 }
0229 
0230 bool SphenixClient::existGlobalTag(const std::string& tagname)
0231 {
0232   if (m_GlobalTagCache.find(tagname) != m_GlobalTagCache.end())
0233   {
0234     return true;
0235   }
0236   nlohmann::json resp = nopayloadclient::NoPayloadClient::getGlobalTags();
0237   nlohmann::json msgcont = resp["msg"];
0238   for (auto& it : msgcont.items())
0239   {
0240     std::string exist_gt = it.value().at("name");
0241     m_GlobalTagCache.insert(tagname);
0242     if (exist_gt == tagname)
0243     {
0244       return true;
0245     }
0246   }
0247   return false;
0248 }