Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CDBUtils.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CDBUtils.cc
1 #include "CDBUtils.h"
2 
3 #include "SphenixClient.h"
4 
5 #include <nlohmann/json.hpp>
6 
7 #include <iostream>
8 #include <stdexcept> // for out_of_range
9 
11  : cdbclient(new SphenixClient())
12 {
13 }
14 
16  : cdbclient(new SphenixClient(globaltag))
17 {
18 }
19 
21 {
22  nlohmann::json resp = cdbclient->createGlobalTag(tagname);
23  int iret = resp["code"];
24  nlohmann::json msgcont = resp["msg"];
25  std::cout << msgcont << std::endl;
26  return iret;
27 }
28 
30 {
31  nlohmann::json resp = cdbclient->deleteGlobalTag(tagname);
32  int iret = resp["code"];
33  nlohmann::json msgcont = resp["msg"];
34  std::cout << msgcont << std::endl;
35  return iret;
36 }
37 
39 {
40  nlohmann::json resp = cdbclient->lockGlobalTag(tagname);
41  int iret = resp["code"];
42  nlohmann::json msgcont = resp["msg"];
43  std::cout << msgcont << std::endl;
44  return iret;
45 }
46 
48 {
49  nlohmann::json resp = cdbclient->unlockGlobalTag(tagname);
50  int iret = resp["code"];
51  nlohmann::json msgcont = resp["msg"];
52  std::cout << "message: " << msgcont << std::endl;
53  return iret;
54 }
55 
57 {
58  nlohmann::json resp = cdbclient->clearCache();
59  std::cout << resp["msg"] << std::endl;
60  return;
61 }
62 
64 {
65  nlohmann::json resp = cdbclient->getUrl(type, iov);
66  return resp["msg"];
67 }
68 
70 {
71  return cdbclient->createDomain(pt);
72 }
73 
74 void CDBUtils::listPayloadIOVs(uint64_t iov)
75 {
76  nlohmann::json resp = cdbclient->getPayloadIOVs(iov);
77  if (resp["code"] != 0)
78  {
79  std::cout << resp["msg"] << std::endl;
80  return;
81  }
82  nlohmann::json payload_iovs = resp["msg"];
83  for (auto &[pt, val] : payload_iovs.items())
84  {
85  std::cout << pt << ": " << val["payload_url"]
86  << ", begin ts: " << val["minor_iov_start"]
87  << ", end ts: " << val["minor_iov_end"]
88  << std::endl;
89  }
90  return;
91 }
92 
94 {
95  nlohmann::json resp = cdbclient->getGlobalTags();
96  nlohmann::json msgcont = resp["msg"];
97  std::set<std::string> gtset;
98  for (auto &it : msgcont.items())
99  {
100  std::string exist_gt = it.value().at("name");
101  gtset.insert(exist_gt);
102  }
103  if (gtset.find(source) == gtset.end())
104  {
105  std::cout << "source tag " << source << " does not exist" << std::endl;
106  return -1;
107  }
108  if (gtset.find(target) != gtset.end())
109  {
110  std::cout << "Target tag " << target << " exists, delete it first" << std::endl;
111  return -1;
112  }
113  resp = cdbclient->cloneGlobalTag(source, target);
114  int iret = resp["code"];
115  std::cout << resp["msg"] << std::endl;
116  return iret;
117 }
118 
120 {
121  nlohmann::json resp = cdbclient->getGlobalTags();
122  nlohmann::json msgcont = resp["msg"];
123  for (auto &it : msgcont.items())
124  {
125  std::string exist_gt = it.value().at("name");
126  std::cout << "global tag: " << exist_gt << std::endl;
127  }
128  return;
129 }
130 
132 {
133  nlohmann::json resp = cdbclient->getPayloadTypes();
134  nlohmann::json msgcont = resp["msg"];
135  for (auto &it : msgcont.items())
136  {
137  std::string exist_pl = it.value().at("name");
138  std::cout << "payload type: " << exist_pl << std::endl;
139  }
140  return;
141 }
142 
143 int CDBUtils::insertPayload(const std::string &pl_type, const std::string &file_url, uint64_t iov_start)
144 {
145  if (!isGlobalTagSet())
146  {
147  std::cout << "No Global Tag set" << std::endl;
148  return -1;
149  }
150  nlohmann::json resp = cdbclient->insertPayload(pl_type, file_url, iov_start);
151  int iret = resp["code"];
152  if (iret != 0)
153  {
154  std::cout << "Error inserting payload " << file_url << ", msg: " << resp["msg"] << std::endl;
155  }
156  else
157  {
158  std::cout << resp["msg"] << std::endl;
159  }
160  return iret;
161 }
162 
163 int CDBUtils::insertPayload(const std::string &pl_type, const std::string &file_url, uint64_t iov_start, uint64_t iov_end)
164 {
165  if (!isGlobalTagSet())
166  {
167  std::cout << "No Global Tag set" << std::endl;
168  return -1;
169  }
170  nlohmann::json resp = cdbclient->insertPayload(pl_type, file_url, iov_start, iov_end);
171  int iret = resp["code"];
172  if (iret != 0)
173  {
174  std::cout << "Error inserting payload " << file_url << ", msg: " << resp["msg"] << std::endl;
175  }
176  else
177  {
178  std::cout << resp["msg"] << std::endl;
179  }
180  return iret;
181 }
182 
184 {
185  nlohmann::json resp = cdbclient->setGlobalTag(tagname);
186  int iret = resp["code"];
187  std::cout << "message: " << resp["msg"] << std::endl;
188  return iret;
189 }
190 
192 {
193  return cdbclient->isGlobalTagSet();
194 }
195 
197 {
198  if (cdbclient)
199  {
200  cdbclient->Verbosity(i);
201  }
202  m_Verbosity = i;
203 }
204 
205 int CDBUtils::deletePayloadIOV(const std::string &pl_type, uint64_t iov_start)
206 {
207  nlohmann::json resp = cdbclient->deletePayloadIOV(pl_type, iov_start);
208  int iret = resp["code"];
209  if (iret != 0)
210  {
211  std::cout << "Error deleting payload iov, type " << pl_type
212  << ", iov_start: " << iov_start
213  << ", msg: " << resp["msg"] << std::endl;
214  }
215  else
216  {
217  std::cout << resp["msg"] << std::endl;
218  }
219  return iret;
220 }
221 
222 int CDBUtils::deletePayloadIOV(const std::string &pl_type, uint64_t iov_start, uint64_t iov_end)
223 {
224  nlohmann::json resp = cdbclient->deletePayloadIOV(pl_type, iov_start, iov_end);
225  int iret = resp["code"];
226  if (iret != 0)
227  {
228  std::cout << "Error deleting payload iov, type " << pl_type
229  << ", iov_start: " << iov_start
230  << ", iov_end: " << iov_end
231  << ", msg: " << resp["msg"] << std::endl;
232  }
233  else
234  {
235  std::cout << resp["msg"] << std::endl;
236  }
237  return iret;
238 }