Skip to content

Commit 5b35618

Browse files
authored
Reset rate to 0 when new run starts (#159)
1 parent e11f3d2 commit 5b35618

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/DerivedMetrics.cxx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
#include <map>
2020
#include <memory>
2121
#include <string>
22-
#include <variant>
2322
#include <vector>
2423
#include "VariantVisitorAdd.h"
2524
#include "VariantVisitorRate.h"
2625

26+
template <class... Ts>
27+
struct overloaded : Ts... {
28+
using Ts::operator()...;
29+
};
30+
template <class... Ts>
31+
overloaded(Ts...)->overloaded<Ts...>;
32+
2733
namespace o2
2834
{
2935
/// ALICE O2 Monitoring system
@@ -82,6 +88,15 @@ Metric DerivedMetrics::process(Metric& metric, DerivedMetricMode mode)
8288
auto previous = search->second.getValue();
8389
auto rate = std::visit(VariantVisitorRate(timestampCount), current, previous);
8490

91+
// handle situation when a new run starts
92+
auto isZero = std::visit(overloaded{
93+
[](auto arg) { return arg == 0; },
94+
[](const std::string& arg) { return arg == ""; }},
95+
current);
96+
if (rate < 0 && isZero) {
97+
rate = 0;
98+
}
99+
85100
// swap metrics
86101
mStorage.erase(key);
87102
mStorage.insert(std::make_pair(key, metric));

test/testDerived.cxx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ BOOST_AUTO_TEST_CASE(derivedRateInt)
5151
}
5252
}
5353

54+
BOOST_AUTO_TEST_CASE(derivedRateInt_newRun)
55+
{
56+
struct RateResults {
57+
int value;
58+
int rate;
59+
};
60+
std::vector<RateResults> results = {{10, 0}, {20, 100}, {30, 100}, {50, 200}, {0, 0}, {10, 100}, {20, 100}, {30, 100}, {50, 200}};
61+
o2::monitoring::DerivedMetrics derivedHandler;
62+
std::string name("metricInt");
63+
64+
for (auto const result : results) {
65+
try {
66+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
67+
o2::monitoring::Metric metric(result.value, name);
68+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::RATE);
69+
BOOST_CHECK_EQUAL(derived.getName(), "metricIntRate");
70+
BOOST_WARN_CLOSE(std::get<double>(derived.getValue()), result.rate, 1.0);
71+
} catch (MonitoringException& e) {
72+
BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values"));
73+
}
74+
}
75+
}
76+
5477
BOOST_AUTO_TEST_CASE(derivedRateDouble)
5578
{
5679
struct RateResults {
@@ -70,7 +93,7 @@ BOOST_AUTO_TEST_CASE(derivedRateDouble)
7093
o2::monitoring::Metric derivedTagged = derivedHandler.process(metricTagged, DerivedMetricMode::RATE);
7194
BOOST_CHECK_EQUAL(derived.getName(), "metricDoubleRate");
7295
BOOST_WARN_CLOSE(std::get<double>(derived.getValue()), results[i].rate, 1.0);
73-
BOOST_WARN_CLOSE(std::get<double>(derivedTagged.getValue()), resultsTagged[i].rate, 5.0);
96+
BOOST_WARN_CLOSE(std::get<double>(derivedTagged.getValue()), resultsTagged[i].rate, 1.0);
7497
} catch (MonitoringException& e) {
7598
BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values"));
7699
}

0 commit comments

Comments
 (0)