forked from AliceO2Group/QualityControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck.cxx
299 lines (265 loc) · 10.1 KB
/
Check.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "QualityControl/Check.h"
#include <memory>
#include <algorithm>
// boost
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <utility>
// O2
#include <Common/Exceptions.h>
// QC
#include "QualityControl/ActivityHelpers.h"
#include "QualityControl/CheckInterface.h"
#include "QualityControl/CheckSpec.h"
#include "QualityControl/CommonSpec.h"
#include "QualityControl/InputUtils.h"
#include "QualityControl/MonitorObject.h"
#include "QualityControl/RootClassFactory.h"
#include "QualityControl/QcInfoLogger.h"
#include "QualityControl/Quality.h"
#include "QualityControl/HashDataDescription.h"
#include <QualityControl/AggregatorRunner.h>
using namespace AliceO2::Common;
using namespace AliceO2::InfoLogger;
using namespace o2::quality_control::checker;
using namespace o2::quality_control::core;
using namespace std;
namespace o2::quality_control::checker
{
/// Static functions
o2::header::DataDescription Check::createCheckDataDescription(const std::string& checkName)
{
if (checkName.empty()) {
BOOST_THROW_EXCEPTION(FatalException() << errinfo_details("Empty checkName for check's data description"));
}
return quality_control::core::createDataDescription(checkName, Check::descriptionHashLength);
}
o2::header::DataOrigin Check::createCheckDataOrigin(const std::string& detector)
{
using Origin = o2::header::DataOrigin;
Origin header;
header.runtimeInit(std::string{ "C" }.append(detector.substr(0, Origin::size - 1)).c_str());
return header;
}
/// Members
Check::Check(CheckConfig config)
: mCheckConfig(std::move(config))
{
}
void Check::init()
{
try {
mCheckInterface = root_class_factory::create<CheckInterface>(mCheckConfig.moduleName, mCheckConfig.className);
mCheckInterface->setName(mCheckConfig.name);
mCheckInterface->setCustomParameters(mCheckConfig.customParameters);
mCheckInterface->setCcdbUrl(mCheckConfig.conditionUrl);
} catch (...) {
std::string diagnostic = boost::current_exception_diagnostic_information();
ILOG(Fatal, Ops) << "Unexpected exception, diagnostic information follows: "
<< diagnostic << ENDM;
throw;
}
// Print setting
ILOG(Info, Devel) << "Check config: ";
ILOG(Info, Devel) << "Module " << mCheckConfig.moduleName;
ILOG(Info, Devel) << "; Name " << mCheckConfig.name;
ILOG(Info, Devel) << "; Class " << mCheckConfig.className;
ILOG(Info, Devel) << "; Detector " << mCheckConfig.detectorName;
ILOG(Info, Devel) << "; Policy " << UpdatePolicyTypeUtils::ToString(mCheckConfig.policyType);
ILOG(Info, Devel) << "; MonitorObjects : ";
for (const auto& moname : mCheckConfig.objectNames) {
ILOG(Info, Devel) << " / " << moname;
}
ILOG(Info, Devel) << ENDM;
}
void Check::reset()
{
mCheckInterface->reset();
}
QualityObjectsType Check::check(std::map<std::string, std::shared_ptr<MonitorObject>>& moMap)
{
if (mCheckInterface == nullptr) {
BOOST_THROW_EXCEPTION(FatalException() << errinfo_details("Attempting to check, but no CheckInterface is loaded"));
}
std::map<std::string, std::shared_ptr<MonitorObject>> shadowMap;
// Take only the MOs which are needed to be checked
if (mCheckConfig.allObjects) {
/*
* User didn't specify the MOs.
* All MOs are passed, no shadowing needed.
*/
shadowMap = moMap;
} else {
/*
* Shadow MOs.
* Don't pass MOs that weren't specified by user.
* The user might safely rely on getting only required MOs inside the map.
*
* Implementation: Copy to different map only required MOs.
*/
for (auto& key : mCheckConfig.objectNames) {
// don't create empty shared_ptr
if (moMap.count(key)) {
shadowMap.insert({ key, moMap[key] });
}
}
}
// Prepare a vector of MO maps to be checked, each one will receive a separate Quality.
std::vector<std::map<std::string, std::shared_ptr<MonitorObject>>> moMapsToCheck;
if (mCheckConfig.policyType == UpdatePolicyType::OnEachSeparately) {
// In this case we want to check all MOs separately and we get separate QOs for them.
for (auto mo : shadowMap) {
moMapsToCheck.push_back({ std::move(mo) });
}
} else {
moMapsToCheck.emplace_back(shadowMap);
}
QualityObjectsType qualityObjects;
for (auto& moMapToCheck : moMapsToCheck) {
std::vector<std::string> monitorObjectsNames;
boost::copy(moMapToCheck | boost::adaptors::map_keys, std::back_inserter(monitorObjectsNames));
if (std::any_of(moMapToCheck.begin(), moMapToCheck.end(), [](const std::pair<std::string, std::shared_ptr<MonitorObject>>& item) {
return item.second == nullptr || item.second->getObject() == nullptr;
})) {
ILOG(Warning, Devel) << "Some MOs in the map to check are nullptr, skipping check '" << mCheckInterface->getName() << "'" << ENDM;
continue;
}
Quality quality;
try {
quality = mCheckInterface->check(&moMapToCheck);
} catch (...) {
std::string diagnostic = boost::current_exception_diagnostic_information();
ILOG(Error, Ops) << "Unexpected exception in user code (check):"
<< diagnostic << ENDM;
continue;
}
auto commonActivity = activity_helpers::strictestMatchingActivity(
moMapToCheck.begin(),
moMapToCheck.end(),
[](const std::pair<std::string, std::shared_ptr<MonitorObject>>& item) -> const Activity& {
return item.second->getActivity();
});
ILOG(Debug, Devel) << "Check '" << mCheckConfig.name << "', quality '" << quality << "'" << ENDM;
// todo: take metadata from somewhere
qualityObjects.emplace_back(std::make_shared<QualityObject>(
quality,
mCheckConfig.name,
mCheckConfig.detectorName,
UpdatePolicyTypeUtils::ToString(mCheckConfig.policyType),
stringifyInput(mCheckConfig.inputSpecs),
monitorObjectsNames));
qualityObjects.back()->setActivity(commonActivity);
beautify(moMapToCheck, quality);
}
return qualityObjects;
}
void Check::beautify(std::map<std::string, std::shared_ptr<MonitorObject>>& moMap, const Quality& quality)
{
if (!mCheckConfig.allowBeautify) {
return;
}
for (auto const& item : moMap) {
try {
mCheckInterface->beautify(item.second /*mo*/, quality);
} catch (...) {
std::string diagnostic = boost::current_exception_diagnostic_information();
ILOG(Error, Ops) << "Unexpected exception in user code (beautify):"
<< diagnostic << ENDM;
continue;
}
}
}
UpdatePolicyType Check::getUpdatePolicyType() const
{
return mCheckConfig.policyType;
}
std::vector<std::string> Check::getObjectsNames() const
{
return mCheckConfig.objectNames;
}
bool Check::getAllObjectsOption() const
{
return mCheckConfig.allObjects;
}
CheckConfig Check::extractConfig(const CommonSpec& commonSpec, const CheckSpec& checkSpec)
{
framework::Inputs inputs;
std::vector<std::string> objectNames;
UpdatePolicyType updatePolicy = checkSpec.updatePolicy;
bool checkAllObjects = false;
for (const auto& dataSource : checkSpec.dataSources) {
if (!dataSource.isOneOf(DataSourceType::Task, DataSourceType::TaskMovingWindow, DataSourceType::ExternalTask, DataSourceType::PostProcessingTask)) {
throw std::runtime_error(
"Unsupported dataSource '" + dataSource.name + "' for a Check '" + checkSpec.checkName + "'");
}
inputs.insert(inputs.end(), dataSource.inputs.begin(), dataSource.inputs.end());
// Subscribe on predefined MOs.
// If "MOs" are not set, the check function will be triggered whenever a new MO appears.
if (dataSource.subInputs.empty()) {
// fixme: this is a dirty fix. Policies should be refactored, so this check won't be needed.
if (checkSpec.updatePolicy != UpdatePolicyType::OnEachSeparately) {
updatePolicy = UpdatePolicyType::OnGlobalAny;
}
checkAllObjects = true;
} else {
// todo consider moving this to spec reader
for (const auto& moName : dataSource.subInputs) {
auto name = dataSource.name + "/" + moName;
// todo: consider making objectNames an std::set
if (std::find(objectNames.begin(), objectNames.end(), name) == objectNames.end()) {
objectNames.push_back(name);
}
}
}
}
bool allowBeautify = checkSpec.dataSources.size() <= 1;
if (!allowBeautify) {
// See QC-299 for details
ILOG(Warning, Devel) << "Beautification disabled because more than one source is used in this Check (" << checkSpec.checkName << ")" << ENDM;
}
return {
checkSpec.checkName,
checkSpec.moduleName,
checkSpec.className,
checkSpec.detectorName,
checkSpec.customParameters,
updatePolicy,
std::move(objectNames),
checkAllObjects,
allowBeautify,
std::move(inputs),
createOutputSpec(checkSpec.detectorName, checkSpec.checkName),
commonSpec.conditionDBUrl
};
}
framework::OutputSpec Check::createOutputSpec(const std::string& detector, const std::string& checkName)
{
return { createCheckDataOrigin(detector), createCheckDataDescription(checkName), 0, framework::Lifetime::Sporadic };
}
void Check::startOfActivity(const core::Activity& activity)
{
if (mCheckInterface) {
mCheckInterface->startOfActivity(activity);
} else {
throw std::runtime_error("Trying to start an Activity on an empty CheckInterface '" + mCheckConfig.name + "'");
}
}
void Check::endOfActivity(const core::Activity& activity)
{
if (mCheckInterface) {
mCheckInterface->endOfActivity(activity);
} else {
throw std::runtime_error("Trying to stop an Activity on an empty CheckInterface '" + mCheckConfig.name + "'");
}
}
} // namespace o2::quality_control::checker