Skip to content

Commit 0dd3dc3

Browse files
authored
Allow to retrieve the latest MO or QO with DatabaseInterface (#2089)
1 parent 72a97e2 commit 0dd3dc3

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

Framework/include/QualityControl/CcdbDatabase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ class CcdbDatabase : public DatabaseInterface
7272
std::string const& detectorName, std::string const& taskName, long from = -1, long to = -1) override;
7373

7474
void* retrieveAny(std::type_info const& tinfo, std::string const& path,
75-
std::map<std::string, std::string> const& metadata, long timestamp = -1,
75+
std::map<std::string, std::string> const& metadata, long timestamp = Timestamp::Current,
7676
std::map<std::string, std::string>* headers = nullptr,
7777
const std::string& createdNotAfter = "", const std::string& createdNotBefore = "") override;
7878

7979
// retrieval - MO - deprecated
80-
std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = -1, const core::Activity& activity = {}) override;
80+
std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = Timestamp::Current, const core::Activity& activity = {}) override;
8181
// retrieval - QO - deprecated
82-
std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = -1, const core::Activity& activity = {}) override;
82+
std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = Timestamp::Current, const core::Activity& activity = {}) override;
8383
std::shared_ptr<o2::quality_control::TimeRangeFlagCollection> retrieveTRFC(const std::string& name, const std::string& detector, int runNumber = 0,
8484
const std::string& passName = "", const std::string& periodName = "",
8585
const std::string& provenance = "", long timestamp = -1) override;
8686

8787
// retrieval - general
8888
std::string retrieveJson(std::string path, long timestamp, const std::map<std::string, std::string>& metadata) override;
89-
TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = -1, std::map<std::string, std::string>* headers = nullptr) override;
89+
TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = Timestamp::Current, std::map<std::string, std::string>* headers = nullptr) override;
9090

9191
void disconnect() override;
9292
void prepareTaskDataContainer(std::string taskName) override;

Framework/include/QualityControl/DatabaseInterface.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class DatabaseInterface
4444
public:
4545
constexpr static framework::ServiceKind service_kind = framework::ServiceKind::Global;
4646

47+
enum Timestamp : long {
48+
Current = -1,
49+
Latest = 0
50+
};
51+
4752
/// Default constructor
4853
DatabaseInterface() = default;
4954
/// Destructor
@@ -140,7 +145,7 @@ class DatabaseInterface
140145
* @param activity Activity of the object
141146
* @deprecated
142147
*/
143-
virtual std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = -1, const core::Activity& activity = {}) = 0;
148+
virtual std::shared_ptr<o2::quality_control::core::MonitorObject> retrieveMO(std::string objectPath, std::string objectName, long timestamp = Timestamp::Current, const core::Activity& activity = {}) = 0;
144149
/**
145150
* \brief Look up a quality object and return it.
146151
* Look up a quality object and return it if found or nullptr if not.
@@ -149,14 +154,14 @@ class DatabaseInterface
149154
* @param activity Activity of the object
150155
* @deprecated
151156
*/
152-
virtual std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = -1, const core::Activity& activity = {}) = 0;
157+
virtual std::shared_ptr<o2::quality_control::core::QualityObject> retrieveQO(std::string qoPath, long timestamp = Timestamp::Current, const core::Activity& activity = {}) = 0;
153158
/**
154159
* \brief Look up a TimeRangeFlagCollection object and return it.
155160
* Look up a TimeRangeFlagCollection and return it if found or nullptr if not.
156161
*/
157162
virtual std::shared_ptr<o2::quality_control::TimeRangeFlagCollection> retrieveTRFC(const std::string& name, const std::string& detector, int runNumber = 0,
158163
const std::string& passName = "", const std::string& periodName = "",
159-
const std::string& provenance = "", long timestamp = -1) = 0;
164+
const std::string& provenance = "", long timestamp = Timestamp::Current) = 0;
160165
/**
161166
* \brief Look up an object and return it.
162167
* Look up an object and return it if found or nullptr if not. It is a raw pointer because we might need it to build a MO.
@@ -165,7 +170,7 @@ class DatabaseInterface
165170
* \param headers Map to be populated with the headers we received, if it is not null.
166171
* \param metadata filters under the form of key-value pairs to select data
167172
*/
168-
virtual TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = -1, std::map<std::string, std::string>* headers = nullptr) = 0;
173+
virtual TObject* retrieveTObject(std::string path, const std::map<std::string, std::string>& metadata, long timestamp = Timestamp::Current, std::map<std::string, std::string>* headers = nullptr) = 0;
169174

170175
/**
171176
* \brief Look up an object and return it in JSON format.

Framework/src/CcdbDatabase.cxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ void CcdbDatabase::storeTRFC(std::shared_ptr<const o2::quality_control::TimeRang
336336

337337
TObject* CcdbDatabase::retrieveTObject(std::string path, std::map<std::string, std::string> const& metadata, long timestamp, std::map<std::string, std::string>* headers)
338338
{
339+
if (timestamp == Timestamp::Latest) {
340+
auto latestValidity = getLatestObjectValidity(path, metadata);
341+
if (latestValidity.isInvalid()) {
342+
return nullptr;
343+
}
344+
timestamp = latestValidity.getMin();
345+
}
339346
// we try first to load a TFile
340347
auto* object = ccdbApi->retrieveFromTFileAny<TObject>(path, metadata, timestamp, headers);
341348
if (object == nullptr) {
@@ -348,6 +355,13 @@ TObject* CcdbDatabase::retrieveTObject(std::string path, std::map<std::string, s
348355

349356
void* CcdbDatabase::retrieveAny(const type_info& tinfo, const string& path, const map<std::string, std::string>& metadata, long timestamp, std::map<std::string, std::string>* headers, const string& createdNotAfter, const string& createdNotBefore)
350357
{
358+
if (timestamp == Timestamp::Latest) {
359+
auto latestValidity = getLatestObjectValidity(path, metadata);
360+
if (latestValidity.isInvalid()) {
361+
return nullptr;
362+
}
363+
timestamp = latestValidity.getMin();
364+
}
351365
auto* object = ccdbApi->retrieveFromTFile(tinfo, path, metadata, timestamp, headers, "", createdNotAfter, createdNotBefore);
352366
if (object == nullptr) {
353367
ILOG(Warning, Support) << "We could NOT retrieve the object " << path << " with timestamp " << timestamp << "." << ENDM;

Framework/test/testCcdbDatabase.cxx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <TH1F.h>
2828
#include "QualityControl/RepoPathUtils.h"
2929
#include "QualityControl/ObjectMetadataKeys.h"
30+
#include "QualityControl/ActivityHelpers.h"
3031
#include <DataFormatsQualityControl/TimeRangeFlagCollection.h>
3132
#include <TROOT.h>
3233
#include <CCDB/CcdbApi.h>
@@ -387,6 +388,34 @@ BOOST_AUTO_TEST_CASE(ccdb_store_retrieve_any)
387388
BOOST_CHECK(h1_back->GetEntries() > 0);
388389
}
389390

391+
BOOST_AUTO_TEST_CASE(ccdb_store_retrieve_latest)
392+
{
393+
test_fixture f;
394+
395+
TH1F* h1 = new TH1F("latest_test", "latest_test", 100, 0, 99);
396+
h1->FillRandom("gaus", 10000);
397+
shared_ptr<MonitorObject> mo1 = make_shared<MonitorObject>(h1, f.taskName, "TestClass", "TST");
398+
mo1->updateActivity(1234, "LHC66", "passName1", "qc");
399+
mo1->setValidity({ 30, 50 });
400+
f.backend->storeMO(mo1);
401+
402+
h1->FillRandom("gaus", 10000);
403+
mo1->updateActivity(1234, "LHC66", "passName1", "qc");
404+
mo1->setValidity({ 10, 30 });
405+
f.backend->storeMO(mo1); // this is going to be the latest version matching the provided Activity
406+
407+
h1->FillRandom("gaus", 10000);
408+
mo1->updateActivity(1234, "LHC66", "differentPassName", "qc");
409+
mo1->setValidity({ 10, 30 });
410+
f.backend->storeMO(mo1);
411+
412+
std::shared_ptr<MonitorObject> moBack = f.backend->retrieveMO(f.getMoFolder("latest_test"), "latest_test", DatabaseInterface::Timestamp::Latest, { 1234, 0, "LHC66", "passName1", "qc" });
413+
BOOST_REQUIRE(moBack != nullptr);
414+
auto h1Back = dynamic_cast<TH1F*>(moBack->getObject());
415+
BOOST_REQUIRE(h1Back != nullptr);
416+
BOOST_CHECK_EQUAL(h1Back->GetEntries(), 20000);
417+
}
418+
390419
BOOST_AUTO_TEST_CASE(ccdb_trfc)
391420
{
392421
test_fixture f;

0 commit comments

Comments
 (0)