Belle II Software  release-08-01-10
CentralMetadataProvider.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/database/CentralMetadataProvider.h>
10 #include <framework/logging/Logger.h>
11 
12 using json = nlohmann::json;
13 
14 namespace Belle2::Conditions {
15  CentralMetadataProvider::CentralMetadataProvider(std::string baseUrl, const std::set<std::string>& usableTagStates):
16  MetadataProvider(usableTagStates), m_baseUrl(std::move(baseUrl))
17  {
18  // We want to be sure on construction that the server is working. So let's
19  // just check the list of valid states
20  const auto result = get("/v2/globalTagStatus");
21  // check list of valid states
22  auto validStates = getUsableTagStates();
23  std::string invalidStates = "";
24  for (const auto& info : result) {
25  const std::string status = info.at("name");
26  if (not validStates.erase(status)) {
27  if (!invalidStates.empty()) invalidStates += ", ";
28  invalidStates += status;
29  }
30  }
32  B2DEBUG(31, "Conditions Database: unusable globaltag states: " << invalidStates);
33  for (const auto& status : validStates) {
34  B2WARNING("Conditions Database: status marked as usable for global tags is not known to the database"
35  << LogVar("status", status));
36 
37  }
38  }
39 
40  json CentralMetadataProvider::get(const std::string& url)
41  {
42  std::stringstream stream;
43  const auto fullUrl = m_downloader.joinWithSlash(m_baseUrl, url);
44  m_downloader.download(fullUrl, stream);
45  stream.clear();
46  stream.seekg(0, std::ios::beg);
47  return json::parse(stream);
48  }
49 
50  std::string CentralMetadataProvider::getGlobaltagStatus(const std::string& name)
51  {
52  auto escaped = m_downloader.escapeString(name);
53  const std::string url = "/v2/globalTag/" + escaped;
54  try {
55  const auto gtinfo = get(url);
56  return gtinfo.at("globalTagStatus").at("name");
57  } catch (std::runtime_error& e) {
58  B2ERROR("Conditions Database: Cannot download information on global tag. Usually this means it "
59  "doesn't exist and you misspelled the name"
60  << LogVar("server url", m_baseUrl) << LogVar("globaltag", name));
61  } catch (std::exception& e) {
62  B2ERROR("Conditions Database: Problem determining global tag status"
63  << LogVar("server url", m_baseUrl) << LogVar("globaltag", name) << LogVar("error", e.what()));
64  }
65  return "";
66  }
67 
68  bool CentralMetadataProvider::updatePayloads(const std::string& globaltag, int exp, int run)
69  {
70  auto escaped = m_downloader.escapeString(globaltag);
71  const std::string url = "v2/iovPayloads/?gtName=" + escaped +
72  "&expNumber=" + std::to_string(exp) +
73  "&runNumber=" + std::to_string(run);
74  try {
75  const auto payloads = get(url);
76  if (!payloads.is_array()) throw std::runtime_error("expected array");
77  for (const auto& info : payloads) {
78  if (!info.is_object()) throw std::runtime_error("excpected payload object");
79  const auto& payload = info.at("payload");
80  const auto& iov = info.at("payloadIov");
82  payload.at("basf2Module").at("name"),
83  globaltag,
84  payload.at("payloadUrl"),
85  payload.at("baseUrl"),
86  payload.at("checksum"),
87  iov.at("expStart"), iov.at("runStart"), iov.at("expEnd"), iov.at("runEnd"),
88  payload.at("revision")
89  ));
90  }
91  } catch (std::exception& e) {
92  B2ERROR("Conditions Database: Problem parsing payload information."
93  << LogVar("globaltag", globaltag) << LogVar("server url", m_baseUrl) << LogVar("error", e.what()));
94  return false;
95  }
96  return true;
97  }
98 } // Belle2::Conditions namespace
CentralMetadataProvider(std::string baseUrl, const std::set< std::string > &usableTagStates)
Create using a base rest url to find the server endpoints.
std::string getGlobaltagStatus(const std::string &name) override
Check the status of a given globaltag .
bool updatePayloads(const std::string &globaltag, int exp, int run) override
Update the list of known payloads for the given globaltag/exp/run.
Downloader & m_downloader
Reference to the downloader instance for convenience.
std::string m_baseUrl
base url of the server
nlohmann::json get(const std::string &url)
Downlad a given relative url (the baseUrl will be prependend) and return the json description.
bool download(const std::string &url, std::ostream &stream, bool silentOnMissing=false)
get an url and save the content to stream This function raises exceptions when there are any problems
Definition: Downloader.cc:258
std::string joinWithSlash(const std::string &base, const std::string &second)
Join two strings and make sure that there is exactly one '/' between them.
Definition: Downloader.cc:155
std::string escapeString(const std::string &text)
Escape a string to make it safe to be used in web requests.
Definition: Downloader.cc:141
Base class for a payload metadata provider.
std::set< std::string > getUsableTagStates()
Get the valid tag states when checking globaltag status.
void printInfoMessage(const std::string &provider)
Print an INFO message about the used metadata provider.
void addPayload(PayloadMetadata &&payload, const std::string &messagePrefix="")
Add a payload information to the internal list.
Class to store variables with their name which were sent to the logging service.
Simple struct to group all information necessary for a single payload.