-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDerivedMetrics.cxx
More file actions
79 lines (70 loc) · 2.24 KB
/
DerivedMetrics.cxx
File metadata and controls
79 lines (70 loc) · 2.24 KB
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
///
/// \file DerivedMetrics.cxx
/// \author Adam Wegrzynek <adam.wegrzynek@cern.ch>
///
#include "Monitoring/DerivedMetrics.h"
#include "Exceptions/MonitoringInternalException.h"
#include <boost/lexical_cast.hpp>
#include <boost/variant/variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <chrono>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "MonLogger.h"
#include "VariantVisitorRate.h"
#include "VariantVisitorAdd.h"
namespace o2
{
/// ALICE O2 Monitoring system
namespace monitoring
{
Metric DerivedMetrics::rate(Metric& metric)
{
// disallow string
std::string name = metric.getName();
if (metric.getType() == MetricType::STRING) {
throw MonitoringInternalException("DerivedMetrics/ProcessMetric", "Not able to process string values");
}
// search for previous value
auto search = mStorage.find(name);
if (search == mStorage.end()) {
mStorage.insert(std::make_pair(name, metric));
return Metric{(double) 0.0, name + "Rate"};
}
auto timestampDifference = std::chrono::duration_cast<std::chrono::milliseconds>(
metric.getTimestamp()
- search->second.getTimestamp()
);
int timestampCount = timestampDifference.count();
// disallow dividing by 0
if (timestampCount == 0) {
throw MonitoringInternalException("DerivedMetrics/Calculate rate", "Division by 0");
}
auto current = metric.getValue();
auto previous = search->second.getValue();
auto rate = boost::apply_visitor(VariantVisitorRate(timestampCount), current, previous);
// swap metrics
mStorage.erase(name);
mStorage.insert(std::make_pair(name, metric));
return Metric{rate, name + "Rate"};
}
Metric DerivedMetrics::increment(Metric& metric) {
std::string name = metric.getName();
auto search = mStorage.find(name);
if (search != mStorage.end()) {
auto currentValue = metric.getValue();
auto storedValue = search->second.getValue();
auto value = boost::apply_visitor(VariantVisitorAdd(), currentValue, storedValue);
mStorage.erase(search);
Metric result = Metric{value, name};
mStorage.insert(std::make_pair(name, result));
return result;
}
mStorage.insert(std::make_pair(name, metric));
return metric;
}
} // namespace monitoring
} // namespace o2