Skip to content

Commit 6d462c9

Browse files
committed
1. extract isOnlyDigits to stringUtils
2. handle the case when the run type is a number and convert it to the string representation 3. tests
1 parent faeb035 commit 6d462c9

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

Framework/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ set(TEST_SRCS
279279
test/testQualitiesToTRFCollectionConverter.cxx
280280
test/testUserCodeInterface.cxx
281281
test/testCustomParameters.cxx
282-
)
282+
test/testStringUtils.cxx
283+
)
283284

284285
set(TEST_ARGS
285286
""
@@ -301,6 +302,7 @@ set(TEST_ARGS
301302
""
302303
""
303304
""
305+
""
304306
)
305307

306308
list(LENGTH TEST_SRCS count)

Framework/include/QualityControl/stringUtils.h

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ bool decodeBool(const std::string& value);
4040
/// @throw std::runtime_error the value is not a bool
4141
bool parseBoolParam(const CustomParameters& customParameters, const std::string& name, const std::string& runType = "default", const std::string& beamType = "default");
4242

43+
/**
44+
* Check if the string contains only digits.
45+
*/
46+
bool isOnlyDigits(std::string s);
47+
4348
} // namespace o2::quality_control::core
4449

4550
#endif // QC_STRING_UTILS_H

Framework/src/ActivityHelpers.cxx

+17-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <CCDB/BasicCCDBManager.h>
2121
#include "QualityControl/ObjectMetadataKeys.h"
2222

23+
#include <DataFormatsParameters/ECSDataAdapters.h>
24+
#include <QualityControl/stringUtils.h>
25+
2326
using namespace o2::quality_control::repository;
2427

2528
namespace o2::quality_control::core::activity_helpers
@@ -47,7 +50,13 @@ core::Activity asActivity(const std::map<std::string, std::string>& metadata, co
4750
{
4851
core::Activity activity;
4952
if (auto runType = metadata.find(metadata_keys::runType); runType != metadata.end()) {
50-
activity.mType = runType->second;
53+
if(isOnlyDigits(runType->second)) {
54+
// we probably got the former representation of run types, i.e. an integer. We convert it as best
55+
// as we can using O2's ECSDataAdapter
56+
activity.mType = parameters::GRPECS::RunTypeNames[std::stoi(runType->second)];
57+
} else {
58+
activity.mType = runType->second;
59+
}
5160
}
5261
if (auto runNumber = metadata.find(metadata_keys::runNumber); runNumber != metadata.end()) {
5362
activity.mId = std::strtol(runNumber->second.c_str(), nullptr, 10);
@@ -72,7 +81,13 @@ core::Activity asActivity(const boost::property_tree::ptree& tree, const std::st
7281
{
7382
core::Activity activity;
7483
if (auto runType = tree.get_optional<std::string>(metadata_keys::runType); runType.has_value()) {
75-
activity.mType = runType.value();
84+
if(isOnlyDigits(runType.value())) {
85+
// we probably got the former representation of run types, i.e. an integer. We convert it as best
86+
// as we can using O2's ECSDataAdapter
87+
activity.mType = parameters::GRPECS::RunTypeNames[std::stoi(runType.value())];
88+
} else {
89+
activity.mType = runType.value();
90+
}
7691
}
7792
if (auto runNumber = tree.get_optional<int>(metadata_keys::runNumber); runNumber.has_value()) {
7893
activity.mId = runNumber.value();

Framework/src/stringUtils.cxx

+6
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,10 @@ bool parseBoolParam(const CustomParameters& customParameters, const std::string&
8080
}
8181
}
8282

83+
bool isOnlyDigits(std::string s)
84+
{
85+
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
86+
}
87+
88+
8389
} // namespace o2::quality_control::core

Framework/test/testActivityHelpers.cxx

+25
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,28 @@ TEST_CASE("test_overlappingActivity")
212212
CHECK(result == expectation);
213213
}
214214
}
215+
216+
TEST_CASE("test_backward_compatible_activity_type")
217+
{
218+
SECTION("metadata map")
219+
{
220+
std::map<std::string, std::string> metadata = {
221+
{ metadata_keys::runType, "2" }, // technical
222+
};
223+
224+
Activity activity = activity_helpers::asActivity(metadata, "test_provenance");
225+
226+
CHECK(activity.mType == "TECHNICAL");
227+
std::cout << "type " << activity << std::endl;
228+
}
229+
230+
SECTION("property tree")
231+
{
232+
boost::property_tree::ptree tree;
233+
tree.put(metadata_keys::runType, "2");
234+
235+
Activity activity = activity_helpers::asActivity(tree, "test_provenance");
236+
237+
CHECK(activity.mType == "TECHNICAL");
238+
}
239+
}

Framework/test/testStringUtils.cxx

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
///
13+
/// \file testActivity.cxx
14+
/// \author Piotr Konopka
15+
///
16+
17+
#include "QualityControl/stringUtils.h"
18+
19+
#define BOOST_TEST_MODULE Triggers test
20+
#define BOOST_TEST_MAIN
21+
#define BOOST_TEST_DYN_LINK
22+
23+
#include <boost/test/unit_test.hpp>
24+
25+
using namespace o2::quality_control::core;
26+
27+
BOOST_AUTO_TEST_CASE(test_is_number)
28+
{
29+
BOOST_CHECK_EQUAL(isOnlyDigits("1"), true);
30+
BOOST_CHECK_EQUAL(isOnlyDigits("-1"), false);
31+
BOOST_CHECK_EQUAL(isOnlyDigits("1000000"), true);
32+
BOOST_CHECK_EQUAL(isOnlyDigits("0.1"), false);
33+
BOOST_CHECK_EQUAL(isOnlyDigits(".2"), false);
34+
BOOST_CHECK_EQUAL(isOnlyDigits("x"), false);
35+
BOOST_CHECK_EQUAL(isOnlyDigits("1x"), false);
36+
BOOST_CHECK_EQUAL(isOnlyDigits("......"), false);
37+
}

0 commit comments

Comments
 (0)