Skip to content

Commit 80b28f0

Browse files
authored
[OMON-503] Add possibility to increase metric value (#269)
1 parent 0a82155 commit 80b28f0

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

include/Monitoring/Metric.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class Metric
9797
/// \param name
9898
Metric&& addValue(const std::variant<int, std::string, double, uint64_t>& value, const std::string& name);
9999

100+
/// Increases metric value by provided value
101+
/// \param value value that metric will be increased
102+
/// \param name value name
103+
Metric&& increaseValue(const std::variant<int, std::string, double, uint64_t>& value, const std::string& name = "value");
104+
100105
/// Default destructor
101106
~Metric() = default;
102107

src/Metric.cxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
///
1616

1717
#include "Monitoring/Metric.h"
18+
#include "VariantVisitorAdd.h"
1819

1920
#include <iostream>
2021
#include <chrono>
@@ -76,6 +77,20 @@ Metric&& Metric::addValue(uint64_t value, const std::string& name)
7677
return std::move(*this);
7778
}
7879

80+
Metric&& Metric::increaseValue(const std::variant<int, std::string, double, uint64_t>& value, const std::string& name)
81+
{
82+
auto find = std::find_if(
83+
mValues.begin(), mValues.end(),
84+
[&](std::pair<std::string, std::variant<int, std::string, double, uint64_t>>& el) { return el.first == name; }
85+
);
86+
if (find != mValues.end()) {
87+
find->second = std::visit(VariantVisitorAdd{}, find->second, value);
88+
} else {
89+
addValue(value, name);
90+
}
91+
return std::move(*this);
92+
}
93+
7994
Metric&& Metric::addValue(std::string value, const std::string& name)
8095
{
8196
mValues.push_back({name, value});

src/VariantVisitorAdd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// or submit itself to any jurisdiction.
1111

1212
#include <variant>
13+
#include "Exceptions/MonitoringException.h"
1314

1415
namespace o2
1516
{

test/testMetric.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ BOOST_AUTO_TEST_CASE(retrieveUnsignedLongLongType)
151151
BOOST_CHECK_EQUAL(metricInstance.getFirstValueType(), 3);
152152
}
153153

154+
BOOST_AUTO_TEST_CASE(increaseMetric)
155+
{
156+
Metric metric = Metric{10, "myMetric"};
157+
metric.increaseValue(20);
158+
BOOST_CHECK_EQUAL(std::get<int>(metric.getFirstValue().second), 30);
159+
160+
Metric metric2 = Metric{"myValue"};
161+
metric2.addValue(3, "sum");
162+
metric2.increaseValue(10, "sum");
163+
BOOST_CHECK_EQUAL(std::get<int>(metric2.getFirstValue().second), 13);
164+
165+
Metric metric3 = Metric{"summed"};
166+
for (int i = 0; i < 5; i++) {
167+
metric3.increaseValue(i);
168+
}
169+
BOOST_CHECK_EQUAL(std::get<int>(metric3.getFirstValue().second), 10);
170+
}
171+
154172
BOOST_AUTO_TEST_CASE(regexVerbosityPolicy)
155173
{
156174
Metric::setVerbosityPolicy(Verbosity::Prod, std::regex("myMetric", std::regex::optimize));

0 commit comments

Comments
 (0)